My app using LAME to encode raw pcm data to mp3. But i have a problem - output mp3 contains "clicks" in persistent period of time. Something like that:
sound... "click" sound.. "click" sound.. "click" etc...
I have tried different versions of LAME and tried change many LAME settings but not succeed. My app also can convert pcm to wav and ogg, but this converters do not results that "clicks".
There is a encoding code (pcm input contains one channel data, pcm already resampled to 44100 freq):
Initialization:
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];
Writing pcm data
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);
}
};
}
}
Finalizing
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;