Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. static void OpenAudioFile(CFURLRef sourceURL) {
  2.  
  3. ExtAudioFileRef xafref = 0;
  4.  
  5. // open source file
  6. int result = ExtAudioFileOpenURL(sourceURL, &xafref);
  7. if (result || !xafref) {
  8. printf("ExtAudioFileOpenURL result %d %08X %4.4s\n", result, result,
  9. (char*)&result);
  10. return;
  11. }
  12.  
  13. // get the file data format, this represents the file's actual data format
  14. CAStreamBasicDescription clientFormat;
  15. UInt32 propSize = sizeof(clientFormat);
  16.  
  17. result = ExtAudioFileGetProperty(xafref,
  18. kExtAudioFileProperty_FileDataFormat, &propSize, &clientFormat);
  19. if (result) {
  20. printf("ExtAudioFileGetProperty kExtAudioFileProperty_FileDataFormat
  21. result %d %08X %4.4s\n", result, result, (char*)&result);
  22. return;
  23. }
  24.  
  25. // set the client format to be what we want back
  26. clientFormat.mSampleRate = 44100.0;
  27. clientFormat.SetAUCanonical(2, false);
  28.  
  29. propSize = sizeof(clientFormat);
  30. result = ExtAudioFileSetProperty(xafref,
  31. kExtAudioFileProperty_ClientDataFormat, propSize, &clientFormat);
  32. if (result) {
  33. printf("ExtAudioFileSetProperty
  34. kExtAudioFileProperty_ClientDataFormat %d %08X %4.4s\n", result,
  35. result, (char*)&result);
  36. return;
  37. }
  38.  
  39. // get the file's length in sample frames
  40. UInt64 numFrames = 0;
  41. propSize = sizeof(numFrames);
  42. result = ExtAudioFileGetProperty(xafref,
  43. kExtAudioFileProperty_FileLengthFrames, &propSize, &numFrames);
  44. if (result) {
  45. printf("ExtAudioFileGetProperty
  46. kExtAudioFileProperty_FileLengthFrames result %d %08X %4.4s\n",
  47. result, result, (char*)&result);
  48. return;
  49. }
  50.  
  51. UInt32 numFrames32 = (UInt32)(numFrames);
  52.  
  53. UInt32 samples = numFrames32 * clientFormat.mChannelsPerFrame;
  54.  
  55. AudioBufferList* bufList = (AudioBufferList*)calloc(1,
  56. sizeof(AudioBufferList) + 2 * sizeof(AudioBuffer));
  57. bufList->mNumberBuffers = 2;
  58. bufList->mBuffers[0].mNumberChannels = 1;
  59. bufList->mBuffers[0].mDataByteSize = samples * sizeof(AudioUnitSampleType);
  60. bufList->mBuffers[0].mData = (AudioUnitSampleType
  61. *)malloc(sizeof(AudioUnitSampleType) * 4096);
  62.  
  63. bufList->mBuffers[1].mNumberChannels = 1;
  64. bufList->mBuffers[1].mDataByteSize = samples * sizeof(AudioUnitSampleType);
  65. bufList->mBuffers[1].mData = (AudioUnitSampleType
  66. *)malloc(sizeof(AudioUnitSampleType) * 4096);
  67.  
  68. UInt32 numPacketsRead = 0;
  69.  
  70. //read up till the end of the file
  71. while (numPacketsRead + 4096 < numFrames32){
  72. UInt32 packetstoRead = 4096;
  73. result = ExtAudioFileRead(xafref, &packetstoRead, bufList);
  74. numPacketsRead += packetstoRead;
  75. }
  76.  
  77. UInt32 remainingPackets = numFrames32 - numPacketsRead;
  78.  
  79. //read the remaining packets
  80. result = ExtAudioFileRead(xafref, &remainingPackets, bufList);
  81.  
  82. //reset the file
  83. SInt64 position = 0;
  84. result = ExtAudioFileSeek(xafref, position);
  85.  
  86. //check the position
  87. SInt64 outFrameOffset = 0;
  88. result = ExtAudioFileTell (xafref, &outFrameOffset);
  89. printf("offset after reset %d\n", outFrameOffset);
  90.  
  91. //now start reading from the start of the file : THIS Should read
  92. 4096 packets, but it does not!
  93. UInt32 packetstoRead = 4096;
  94. result = ExtAudioFileRead(xafref, &packetstoRead, bufList);
  95.  
  96. result = ExtAudioFileTell (xafref, &outFrameOffset);
  97. printf("offset after read %d\n", outFrameOffset);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement