我的应用程序使用 LAME 将原始 pcm 数据编码为 mp3。但我有一个问题 - 输出 mp3 在持续时间内包含“点击”。像这样的东西:
声音...“点击”声音..“点击”声音..“点击”等...
我尝试了不同版本的 LAME 并尝试更改许多 LAME 设置但没有成功。我的应用程序还可以将 pcm 转换为 wav 和 ogg,但此转换器不会导致“点击”。
有一个编码代码(pcm输入包含一个通道数据,pcm已经重采样到44100频率):
初始化:
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;