iOS核心音频录制缓冲区问题

时间:2012-10-31 07:58:31

标签: core-audio recording circular-buffer

我正在尝试将一堆样本写入TPCircularBuffer,由迈克尔·泰森提供http://atastypixel.com/blog/a-simple-fast-circular-buffer-implementation-for-audio-processing/comment-page-1/#comment-4988

我成功地实时回放这些记录的样本。像监视器的东西。

但是,我希望将样本保存在TPCircularBuffer中以便以后播放,因此我实现了2个标志,rio->录制和rio->播放。 我的想法是,我会使用按钮激活rio->录制为YES。录制一段时间,然后通过将标志设置为NO来停止录制。从理论上讲,TPCircularBuffer会保存我的音频信息。

但是,当我在播放回叫中激活rio->播放为YES时,我只是听到一些与我录制的内容没有相似之处的抖动声。

我正确使用缓冲区吗?或者这通常是另一种方式吗?

感谢。

码头。

static OSStatus recordingCallback(void *inRefCon, 
                              AudioUnitRenderActionFlags *ioActionFlags, 
                              const AudioTimeStamp *inTimeStamp, 
                              UInt32 inBusNumber, 
                              UInt32 inNumberFrames, 
                              AudioBufferList *ioData) {

RIO *rio = (RIO*)inRefCon;
AudioUnit rioUnit = rio->theAudioUnit;
//ExtAudioFileRef eaf = rio->outEAF;
AudioBufferList abl = rio->audioBufferList;

SInt32 samples[NUMBER_OF_SAMPLES]; // A large enough size to not have to worry about buffer overrun
abl.mNumberBuffers = 1;
abl.mBuffers[0].mData = &samples;
abl.mBuffers[0].mNumberChannels = 1;
abl.mBuffers[0].mDataByteSize = inNumberFrames  * sizeof(SInt16);

OSStatus result;
result = AudioUnitRender(rioUnit, 
                         ioActionFlags, 
                         inTimeStamp,
                         inBusNumber, 
                         inNumberFrames, 
                         &abl);

if (noErr != result) { NSLog(@"Obtain recorded samples error"); }

// React to a recording flag, if recording, save the abl into own buffer, else ignore
if (rio->recording)
{   
    TPCircularBufferProduceBytes(&rio->buffer, abl.mBuffers[0].mData, inNumberFrames  * sizeof(SInt16));
    NSLog(@"Recording!"); 
}
else
{
    NSLog(@"Not Recording!");
}
// once stop recording save the circular buffer to a temp circular buffer

    return noErr;
}

static OSStatus playbackCallback(void *inRefCon, 
                             AudioUnitRenderActionFlags *ioActionFlags, 
                             const AudioTimeStamp *inTimeStamp, 
                             UInt32 inBusNumber, 
                             UInt32 inNumberFrames, 
                             AudioBufferList *ioData) {    

RIO *rio = (RIO*)inRefCon;

int bytesToCopy = ioData->mBuffers[0].mDataByteSize;
SInt16 *targetBuffer = (SInt16*)ioData->mBuffers[0].mData;

// Pull audio from playthrough buffer
int32_t availableBytes;

if (rio->playing)
{
    SInt16 * tempbuffer = TPCircularBufferTail(&rio->buffer, &availableBytes);
    memcpy(targetBuffer, tempbuffer, MIN(bytesToCopy, availableBytes));
    TPCircularBufferConsume(&rio->buffer, MIN(bytesToCopy, availableBytes));
    NSLog(@"Playing!");
}
else
{
   NSLog(@"Playing silence!");

   for (int i = 0 ; i < ioData->mNumberBuffers; i++){
    //get the buffer to be filled
    AudioBuffer buffer = ioData->mBuffers[i];
    UInt32 *frameBuffer = buffer.mData;

    //loop through the buffer and fill the frames
       for (int j = 0; j < inNumberFrames; j++){
           frameBuffer[j] = 0;
       }
   }
}

   return noErr;
}

1 个答案:

答案 0 :(得分:2)

我会自己回答这个问题。 基本上垃圾声音是由于TPCircularBuffer不足以容纳声音。播放回调只是播放垃圾,因为缓冲区不再包含有效的音频数据。

基本上,使TPCircularBuffer更大可以解决我的问题。 (杜!)

码头。