1

我正在尝试使用 AudioToolbox 框架和 Lame 将原始 PCM 数据从麦克风编码到 MP3。尽管一切似乎都运行良好,但编码流中存在“点击”和“失真”的问题。我不确定我是否正确设置了 AudioQueue 以及我是否在正确的 wat 中处理编码缓冲区...我设置音频录制的代码:

    AudioStreamBasicDescription streamFormat;
    memset(&streamFormat, 0, sizeof(AudioStreamBasicDescription));
    streamFormat.mSampleRate = 44100;
    streamFormat.mFormatID = kAudioFormatLinearPCM;
    streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger|kLinearPCMFormatFlagIsPacked;
    streamFormat.mBitsPerChannel = 16;
    streamFormat.mChannelsPerFrame = 1;
    streamFormat.mBytesPerPacket = 2;
    streamFormat.mBytesPerFrame = 2;
    streamFormat.mFramesPerPacket = 1;
    streamFormat.mReserved = 0;        

    AudioQueueNewInput(&streamFormat, InputBufferCallback, (__bridge void*)(self), nil, nil, 0, &mQueue);
    UInt32 bufferByteSize = 44100;
    memset(&mEncodedBuffer, 0, sizeof(mEncodedBuffer)); //mEncoded buffer is 
                                                        //unsigned char [72000]
    AudioQueueBufferRef buffer;
    for (int i=0; i<3; i++) {
         AudioQueueAllocateBuffer(mQueue, bufferByteSize, &buffer);      
         AudioQueueEnqueueBuffer(mQueue, buffer, 0, NULL);
    }
    AudioQueueStart(mQueue, nil);

然后AudioQueue回调函数调用lame_encode_buffer,然后将编码后的缓冲区写入文件:

void InputBufferCallback (void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer, const AudioTimeStamp *inStartTime, UInt32 inNumPackets, const AudioStreamPacketDescription* inPacketDesc) {
      memset(&mEncodedBuffer, 0, sizeof(mEncodedBuffer));
      int encodedBytes = lame_encode_buffer(glf, (short*)inBuffer->mAudioData, NULL, inBuffer->mAudioDataByteSize, mEncodedBuffer, 72000);
      //What I don't understand is that if I write the full 'encodedBytes' data,  then there are A LOT of distortions and original sound is seriously broken 
      NSData* data = [NSData dataWithBytes:mEncodedBuffer length:encodedBytes/2]; 
      [mOutputFile writeData:data];
}

当我之后尝试使用 AVAudioPlayer 播放包含 Lame 编码数据的文件时,我清楚地听到了原始声音,但周围有一些咔嗒声和失真。

有人可以建议这里有什么问题吗?

4

1 回答 1

2

您的代码似乎没有注意 inNumPackets,这是给定回调的实际音频数据量。

此外,在音频回调中执行长时间操作(例如运行编码器)可能不够快,因此可能违反响应要求。任何长函数调用都应该在回调之外完成。

于 2012-06-04T21:06:04.480 回答