Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void OpenAudioFile(CFURLRef sourceURL) {
- ExtAudioFileRef xafref = 0;
- // open source file
- int result = ExtAudioFileOpenURL(sourceURL, &xafref);
- if (result || !xafref) {
- printf("ExtAudioFileOpenURL result %d %08X %4.4s\n", result, result,
- (char*)&result);
- return;
- }
- // get the file data format, this represents the file's actual data format
- CAStreamBasicDescription clientFormat;
- UInt32 propSize = sizeof(clientFormat);
- result = ExtAudioFileGetProperty(xafref,
- kExtAudioFileProperty_FileDataFormat, &propSize, &clientFormat);
- if (result) {
- printf("ExtAudioFileGetProperty kExtAudioFileProperty_FileDataFormat
- result %d %08X %4.4s\n", result, result, (char*)&result);
- return;
- }
- // set the client format to be what we want back
- clientFormat.mSampleRate = 44100.0;
- clientFormat.SetAUCanonical(2, false);
- propSize = sizeof(clientFormat);
- result = ExtAudioFileSetProperty(xafref,
- kExtAudioFileProperty_ClientDataFormat, propSize, &clientFormat);
- if (result) {
- printf("ExtAudioFileSetProperty
- kExtAudioFileProperty_ClientDataFormat %d %08X %4.4s\n", result,
- result, (char*)&result);
- return;
- }
- // get the file's length in sample frames
- UInt64 numFrames = 0;
- propSize = sizeof(numFrames);
- result = ExtAudioFileGetProperty(xafref,
- kExtAudioFileProperty_FileLengthFrames, &propSize, &numFrames);
- if (result) {
- printf("ExtAudioFileGetProperty
- kExtAudioFileProperty_FileLengthFrames result %d %08X %4.4s\n",
- result, result, (char*)&result);
- return;
- }
- UInt32 numFrames32 = (UInt32)(numFrames);
- UInt32 samples = numFrames32 * clientFormat.mChannelsPerFrame;
- AudioBufferList* bufList = (AudioBufferList*)calloc(1,
- sizeof(AudioBufferList) + 2 * sizeof(AudioBuffer));
- bufList->mNumberBuffers = 2;
- bufList->mBuffers[0].mNumberChannels = 1;
- bufList->mBuffers[0].mDataByteSize = samples * sizeof(AudioUnitSampleType);
- bufList->mBuffers[0].mData = (AudioUnitSampleType
- *)malloc(sizeof(AudioUnitSampleType) * 4096);
- bufList->mBuffers[1].mNumberChannels = 1;
- bufList->mBuffers[1].mDataByteSize = samples * sizeof(AudioUnitSampleType);
- bufList->mBuffers[1].mData = (AudioUnitSampleType
- *)malloc(sizeof(AudioUnitSampleType) * 4096);
- UInt32 numPacketsRead = 0;
- //read up till the end of the file
- while (numPacketsRead + 4096 < numFrames32){
- UInt32 packetstoRead = 4096;
- result = ExtAudioFileRead(xafref, &packetstoRead, bufList);
- numPacketsRead += packetstoRead;
- }
- UInt32 remainingPackets = numFrames32 - numPacketsRead;
- //read the remaining packets
- result = ExtAudioFileRead(xafref, &remainingPackets, bufList);
- //reset the file
- SInt64 position = 0;
- result = ExtAudioFileSeek(xafref, position);
- //check the position
- SInt64 outFrameOffset = 0;
- result = ExtAudioFileTell (xafref, &outFrameOffset);
- printf("offset after reset %d\n", outFrameOffset);
- //now start reading from the start of the file : THIS Should read
- 4096 packets, but it does not!
- UInt32 packetstoRead = 4096;
- result = ExtAudioFileRead(xafref, &packetstoRead, bufList);
- result = ExtAudioFileTell (xafref, &outFrameOffset);
- printf("offset after read %d\n", outFrameOffset);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement