iPhone audio units: Getting rid of an unnecessary read from file in favor of reading in from memory
iPhone audio units: Getting rid of an unnecessary read from file in favor of reading in from memory
- Subject: iPhone audio units: Getting rid of an unnecessary read from file in favor of reading in from memory
- From: Halle Winkler <email@hidden>
- Date: Thu, 17 Jun 2010 13:38:53 +0200
Hello,
I was inspired by Brian Willoughby's statement "You already have the audio data in your process space, as well as the converted audio data, so why would you want to read it back from a file?" to get some code into better shape since it does an unnecessary read/write from file at the same time that it has the data in memory. My current code uses AudioFileReadPackets() on an AudioFileID of a recorded file that is written out with ExtAudioFileWriteAsync using ExtAudioFileWrapAudioFileID().
I started out looking for a drop-in solution for replacing AudioFileReadPackets() -- The AudioFile header says "The AudioFile API is used for reading and writing audio files on disk or in memory" but so far I can't find specifics in it for reading packets or bytes from memory, or elsewhere. So, I've tried replacing the AudioFileReadPackets call with an attempt at passing the contents of the audio unit bufferlist into the buffer from readPackets, and also just setting a flag in readPackets to do an additional operation inside the render callback, but I hadn't quite gotten either working yet and before getting too far down that road I thought I'd check in here and see if I'm making it too complicated. Pointers on the simplest way to replace this file read with a memory read? For a Voice Processing audio unit on the iPhone which is 16-bit/16k/mono and WAVE format, this is my current render callback:
static OSStatus AudioUnitRenderCallback (void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
OSStatus audioUnitRenderError = AudioUnitRender(audioUnitWrapper->audioUnit,
ioActionFlags,
inTimeStamp,
1,
inNumberFrames,
ioData);
if(audioUnitRenderError != 0) NSLog(@"AudioUnitRender status is %d", audioUnitRenderError);
if (inNumberFrames > 0) {
OSStatus audioWriteError = ExtAudioFileWriteAsync(audioUnitWrapper->mExtRecordFile, inNumberFrames, ioData);
if(audioWriteError != 0) NSLog(@"ExtAudioFileWriteAsync status is %d", audioWriteError);
audioUnitWrapper->mRecordPacket += inNumberFrames;
}
audioUnitWrapper->bufferList = ioData;
return audioUnitRenderError;
}
This is the only function that currently relies on reading from the written-out file, which can't have its interface changed:
int32 readPackets(int16 * buffer, int32 maxToRead) {
UInt32 length = maxToRead;
UInt32 numberOfBytesRead;
OSStatus status = AudioFileReadPackets (audioUnitWrapper->audioFileID,
false,
&numberOfBytesRead,
NULL,
readPosition,
&length,
buffer);
if(status != 0 && status != -39) return -1;
switch (status) {
case -39:
if(length < 0) return -1;
else return length;
break;
case 0:
if(length < 0) {
return -1;
} else {
readPosition = readPosition + length;
return length;
}
break;
}
return -1;
}
Thank you,
Halle _______________________________________________
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