将pcm转换为mp3(使用LAME)会导致mp3文件中出现“click”

时间:2012-04-27 05:11:21

标签: c++ pcm lame

我的应用使用LAME将原始pcm数据编码为mp3。但我有一个问题 - 输出mp3在持续的时间段内包含“点击”。这样的事情:

声音......“点击”声音..“点击”声音......“点击”等...

我尝试过不同版本的LAME,尝试更改了许多LAME设置,但没有成功。我的应用程序也可以将pcm转换为wav和ogg,但是此转换器不会导致“点击”。

有一个编码代码(pcm输入包含一个通道数据,pcm已经重新采样到44100 freq):

初​​始化:

lame = lame_init();
lame_set_in_samplerate(lame, 44100);
lame_set_VBR(lame, vbr_abr);//vbr_default
lame_init_params(lame);

//Samples count (do not have any effect on output)
dwSamples=1024;

//mp3 buffer size, if it do not equals dwSamples*2 than output sound getting scaled
dwMP3Buffer=dwSamples*2;

pMP3Buffer = new BYTE[dwMP3Buffer];

编写pcm数据

int Mp3Stream :: Write(short * _data, int _size)
{
    if (_size > 0)
    {
        for(int curPos = 0; curPos < _size; curPos += dwMP3Buffer)
        {
            //int size = dwMP3Buffer;

            //if (_size - curPos < dwMP3Buffer)
            //  size = _size - curPos;

            int bytes = lame_encode_buffer(lame, (short *)((char *)_data + curPos), (short *)((char *)_data + curPos), dwSamples, pMP3Buffer, 0);

            IPF_TRACE(1, "MP3 encoder wrote "<<bytes<<" bytes");

            if (bytes<0)
            {
                IPF_TRACE(1, "MP3 encoding failed with code "<<bytes);
                return bytes;
            }

            BOOL bResult = WriteFile(hFile, pMP3Buffer, bytes, &bw, NULL);

            if (!bResult || bytes != bw)
            {
                IPF_TRACE(1, "MP3 write to file failed with code "<<bytes);
            }
        };
    }
}

结束写入

int bytes = lame_encode_flush(lame, pMP3Buffer, 0);
    if (bytes<0)
{
    IPF_TRACE(1, "MP3 flush failed with code "<<bytes);
}
BOOL bResult = WriteFile(hFile, pMP3Buffer, bytes, &bw, NULL);
if (!bResult || bytes != bw)
{
    IPF_TRACE(1, "MP3 write to file failed with code "<<bytes);
}
int ret = lame_close(lame);
if (ret < 0) 
{
    IPF_TRACE(1, "MP3 lame close failed with code "<<ret);
}
delete []pMP3Buffer;

3 个答案:

答案 0 :(得分:1)

声音 - &gt;点击 - &gt;声音 - &gt;单击模式很可能意味着您一次只写入一半缓冲区,另一半对于Write调用中的每个循环都保持为零。

原因是你的循环增量器基于dwSamples * 2的dwMP3Buffer值。

 for(int curPos = 0; curPos < _size; curPos += dwMP3Buffer){

这意味着您实际上并没有对所有输入值进行编码,因为dwMP3Buffer是实际传递给lame_encode_buffer调用的样本数的两倍(dwSamples)。

lame_encode_buffer(lame, (short *)((char *)_data + curPos), 
                   (short *)((char *)_data + curPos), dwSamples, pMP3Buffer, 0);

尝试将循环增量器更改为以下内容:

 for(int curPos = 0; curPos < _size; curPos += dwSamples){

答案 1 :(得分:1)

试试这个,

void AQRecorder::MyInputBufferHandler(  void *                              inUserData,
                                AudioQueueRef                       inAQ,
                                AudioQueueBufferRef                 inBuffer,
                                const AudioTimeStamp *              inStartTime,
                                UInt32                              inNumPackets,
                                const AudioStreamPacketDescription* inPacketDesc)
{
AQRecorder *aqr = (AQRecorder *)inUserData;
//    NSLog(@"%f",inStartTime->mSampleTime);
try
{
    if (inNumPackets > 0)
    {
        AudioFileWritePackets(aqr->mRecordFile, FALSE, inBuffer->mAudioDataByteSize, inPacketDesc, aqr->mRecordPacket, &inNumPackets, inBuffer->mAudioData);

        aqr->mRecordPacket += inNumPackets;

        int MP3_SIZE =inBuffer->mAudioDataByteSize * 4;
        unsigned char mp3_buffer[MP3_SIZE];
        AppDelegate *delegate =[[UIApplication sharedApplication]delegate];
        lame_t lame = lame_init();
        lame_set_in_samplerate(lame, 44100);
        lame_set_VBR(lame, vbr_default);
        lame_init_params(lame);

  //                int encodedBytes=lame_encode_buffer_interleaved(lame, (short int *)inBuffer->mAudioData , inNumPackets, mp3_buffer, MP3_SIZE);


        int encodedBytes = lame_encode_buffer(lame, (short*)inBuffer->mAudioData,  (short*)inBuffer->mAudioData, inNumPackets, mp3_buffer, MP3_SIZE);

        [delegate.mp3AudioData appendBytes:mp3_buffer length:encodedBytes];

        if (inBuffer->mAudioDataByteSize != 0) {
        }
        else
        {
            int encode=lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
            [delegate.mp3AudioData appendBytes:mp3_buffer length:encode];
        }
        lame_close(lame);
    }

    if (aqr->IsRunning())
    {
        AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
    }
 } catch (CAXException e)
 {
 char buf[256];
 fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
}
}

答案 2 :(得分:0)

我会尝试设置

dwSamples = 1152

1152 - 是MPEG1Audio编码器的音频帧大小(样本中)。看起来LAME按帧多个块处理数据