Re: ExtAudioFileRead
Re: ExtAudioFileRead
- Subject: Re: ExtAudioFileRead
- From: Aran Mulholland <email@hidden>
- Date: Thu, 9 Sep 2010 09:57:27 +1000
Here is sample code to illustrate what I have been experiencing. After
reading through the audio file in chunks of 4096 samples, if I read
less than this amount (up to the end of the file), after reseting the
read pointer I only get back the number of samples from the last read.
static void OpenAudioFile(CFURLRef sourceURL) {
ExtAudioFileRef xafref = 0;
// open source file
int result = ExtAudioFileOpenURL(sourceURL, &xafref);
if (result || !xafref) {
printf("ExtAudioFileOpenURL result %d X %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 X %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 X %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 X %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);
}
On Thu, Sep 9, 2010 at 2:57 AM, Aran Mulholland
<email@hidden> wrote:
> On subsequent calls to ExtAudioFileRead, after I have reset the read
> pointer to the start of the file I am requesting more packets i.e.
> 4096, It is just that after reading up to the end of file
> ExtAudioFileRead only ever lets me read the smaller number i.e. 544 no
> matter what numPackets I pass it .
>
>
> On Wed, Sep 8, 2010 at 11:45 PM, Ethan Funk <email@hidden> wrote:
>> I believe, in your code example, numPackets on entry to ExtAudioFileRead is the number of packets requested and on return it is set (thus the address being passed) to the number of packets actually read by the function.
>>
>> If you "reset" numPackets before each call to ExtAudioFileRead, you should be fine.
>>
>> Ethan...
>>
>> On Sep 8, 2010, at 4:09 AM, Aran Mulholland wrote:
>>
>>> Hello all,
>>>
>>> I am reading audio data from a wav file to populate a ring buffer and
>>> I have some issues when I get to the end of the file and want to start
>>> reading from back at the beginning.
>>>
>>> I am reading in 4096 frames at a time with the line
>>>
>>> result =
>>> ExtAudioFileRead(mSoundBuffer->fileReference, &numPackets, bufList); -
>>> where numPackets is 4096.
>>>
>>> Audio plays correctly.
>>>
>>> when i near the end of the file I cannot read 4096 frames as I have
>>> less than that left in the file. So I calculate the number to be read
>>> and read that number.
>>>
>>> result =
>>> ExtAudioFileRead(mSoundBuffer->fileReference, &numPackets, bufList); -
>>> where numPackets is say 544.
>>>
>>> Then i reset the read pointer using
>>>
>>> SInt64 position = 0;
>>> result = ExtAudioFileSeek(mSoundBuffer->fileReference, position);
>>>
>>> After this point, whenever I do an ExtAudioFileRead the numPackets is
>>> always 544 - the number it was to get me to the end of the file.
>>>
>>> Anyone have a similar experience, or know what i might be doing wrong?
>>>
>>> Thanks
>>>
>>> Aran
>>> _______________________________________________
>>> Do not post admin requests to the list. They will be ignored.
>>> Coreaudio-api mailing list (email@hidden)
>>> Help/Unsubscribe/Update your Subscription:
>>>
>>> This email sent to email@hidden
>>
>>
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden