I am using AudioQueue in iOS to implement streaming recording on the Internet.
The problem is that it usually works quite well, but sometimes (~ 20% of my attempts) the sound is terribly distorted - it sounds robotic.
Edit: I can easily play it on the ios6 and ios6.1 simulator, but I could not play it on a real phone (ios6.1.3).
Trying to debug it. I save the PCM data to a file. The same distortion appears in the PCM file, so this is not a problem in the encoding or loading code. I also tried to play with the number of buffers and the size of the buffers - nothing helped.
The problem is that I donβt know how to debug it further - it seems that the buffer is distorted as an input for a callback - before my code is activated (except for the configuration of the audio queue).
- Do you have any idea what might be the problem?
- or how to debug it further?
Queue Setting Code:
audioFormat.mFormatID = kAudioFormatLinearPCM; audioFormat.mSampleRate = SAMPLE_RATE; //16000.0; audioFormat.mChannelsPerFrame = CHANNELS; //1; audioFormat.mBitsPerChannel = 16; audioFormat.mFramesPerPacket = 1; audioFormat.mBytesPerFrame = audioFormat.mChannelsPerFrame * sizeof(SInt16); audioFormat.mBytesPerPacket = audioFormat.mBytesPerFrame * audioFormat.mFramesPerPacket; audioFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; AudioQueueNewInput( &audioFormat, recordCallback, self, // userData CFRunLoopGetMain(), // run loop NULL, // run loop mode 0, // flags &recordQueue); UInt32 trueValue = true; AudioQueueSetProperty(recordQueue,kAudioQueueProperty_EnableLevelMetering,&trueValue,sizeof (UInt32)); for (int t = 0; t < NUMBER_AUDIO_DATA_BUFFERS; ++t) { AudioQueueAllocateBuffer( recordQueue, bufferByteSize, &recordQueueBuffers[t]); } for (int t = 0; t < NUMBER_AUDIO_DATA_BUFFERS; ++t) { AudioQueueEnqueueBuffer( recordQueue, recordQueueBuffers[t], 0, NULL); }
Start recording:
pcmFile = [[NSOutputStream alloc] initToFileAtPath:pcmFilePath append:YES]; [pcmFile scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [pcmFile open]; setupQueue();
Callback Code:
static void recordCallback( void* inUserData, AudioQueueRef inAudioQueue, AudioQueueBufferRef inBuffer, const AudioTimeStamp* inStartTime, UInt32 inNumPackets, const AudioStreamPacketDescription* inPacketDesc) Recorder* recorder = (Recorder*) inUserData; if (!recorder.recording) return; [recorder.pcmFile write:inBuffer->mAudioData maxLength:inBuffer->mAudioDataByteSize]; AudioQueueEnqueueBuffer(inAudioQueue, inBuffer, 0, NULL); }