ExtAudioFileRead is too slow. How to make it faster?

252 views Asked by At

I've written my own audio library. (Silly, I know but I enjoy it.) Right now, I'm using it to play stereo files from the iPod library on iOS. It works well except that sometimes the call to ExtAudioFileRead() takes longer than 23ms. Since my AudioUnits output setup is playing audio at 44.1KHz, and it's asking for 1024 frames per callback. My code + ExtAudioFileRead() must take no longer than 23ms to respond.

I'm quite surprised that ExtAudioFileRead() is so slow. What I'm doing seems quite the normal thing to do. I'm thinking there must be some undocumented configuration magic that I'm not doing. Here is my relevant configuration code:

ExtAudioFileRef _extAudioFileRef;
OSStatus        result;

result = ::ExtAudioFileOpenURL((__bridge CFURLRef)_url, &_extAudioFileRef);

AudioStreamBasicDescription     format;

format.mBitsPerChannel = 8 * sizeof(float);
format.mBytesPerFrame = sizeof(float);
format.mBytesPerPacket = sizeof(float);
format.mChannelsPerFrame = channelCount;
format.mFormatFlags = kAudioFormatFlagIsFloat|kAudioFormatFlagIsNonInterleaved;
format.mFormatID = kAudioFormatLinearPCM;
format.mFramesPerPacket = 1;
format.mSampleRate = framesPerSecond;
result = ::ExtAudioFileSetProperty(_extAudioFileRef,
                                    kExtAudioFileProperty_ClientDataFormat,
                                    sizeof(format),
                                    &format);

I'm not setting any other properties of _extAudioFileRef.

I've traced the heck out of this. I've put time measurement around just my call to ExtAudioFileRead() so I know it's not my code slowing this process down. It's got to be a configuration issue, right?

Thanks so much for any help or even guesses!

Cheers, Christopher

1

There are 1 answers

2
Gordon Childs On BEST ANSWER

You shouldn't be reading from the audio file in your audio callback - you should be buffering in another thread and passing the samples across.

You're breaking cardinal audio rule #4:

  1. Don’t do file or network IO on the audio thread. Like read, write or sendto.