How to record and Play back from Memory using Audio Queue Services
I've been trying lately to make a game that allows the user to talk and afterwards play what he talked 开发者_JAVA百科back in a funny voice.
The catch is that the recording is done only when somebody talks near the iphone (I've managed this part I have a recorder active all the time and get it's average and peak values).
The problem is when I want to play what the user said back. I've been searching around and the only way to do this seems to be Audio Queue Services due to the fact that it allows to save to memory. From my point of view saving the sound to a file and playing it back has issues due to the file allocation.
I haven't found any tutorial on how to save to memory so if anyone has an ideea how to do this or how to save to file and play back with no issues (now the sound is cut off the first 0.5 secs aren't heard and also the fps drops by 20).
My current code is:
void AudioInputCallback(
void *inUserData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp *inStartTime,
UInt32 inNumberPacketDescriptions,
const AudioStreamPacketDescription *inPacketDescs)
RecordState* recordState = (RecordState*)inUserData;
if(!recordState->recording)
{
printf("Not recording, returning\n");
}
printf("Writing buffer %d\n", (int)recordState->currentPacket);
//What to put here to save to memory?
recordState->currentPacket += inNumberPacketDescriptions;
AudioQueueEnqueueBuffer(recordState->queue, inBuffer, 0, NULL);
}
void AudioOutputCallback(
void* inUserData,
AudioQueueRef outAQ,
AudioQueueBufferRef outBuffer)
{
PlayState* playState = (PlayState*)inUserData;
if(!playState->playing)
{
printf("Not playing, returning\n");
return;
}
printf("Queuing buffer %d for playback\n",(int) playState->currentPacket);
AudioStreamPacketDescription* packetDescs;
UInt32 bytesRead;
UInt32 numPackets = 8000;
OSStatus status;
//What to put here to read from memory?
status = AudioQueueEnqueueBuffer(
playState->queue,
outBuffer,
0,
packetDescs);
playState->currentPacket += numPackets;
AudioQueueFreeBuffer(playState->queue, outBuffer);
}
精彩评论