Hi everyone. I have a small LAME + JUCE question. I've integrated LAME (loading it through a DLL currently) to my JUCE project but I am having problem with my encoded MP3 files, they all end up with garbage/white noise audio. I have a quick and dirty function just to test LAME functionality in JUCE that I am using at the moment, I am wondering if I am doing something wrong reading the input wavefile or when writing the file?
(Quick and dirty function to create an MP3 from wave file)
AudioFormatManager formatManager;
formatManager.registerBasicFormats();
// open wave file
File sourceFile(filename);
ScopedPointer<AudioFormatReader> sourceFormatReader = formatManager.createReaderFor(sourceFile);
// create audio buffer
AudioSampleBuffer pcmBuffer(sourceFormatReader->numChannels, sourceFormatReader->lengthInSamples);
// read all the wave data to memory
sourceFormatReader->read(&pcmBuffer, 0, sourceFormatReader->lengthInSamples, 0, true, true);
// init LAME
lame_global_flags* lameInit = lame_init();
if (lameInit == nullptr)
{
return false;
}
lame_set_num_channels(lameInit, sourceFormatReader->numChannels);
lame_set_num_samples(lameInit, sourceFormatReader->lengthInSamples);
lame_set_in_samplerate(lameInit, sourceFormatReader->sampleRate);
lame_set_brate(lameInit,128);
lame_set_quality(lameInit, 2);
int lameInitResult = lame_init_params(lameInit);
if (lameInitResult < 0)
{
return false;
}
const int mp3buf_size = (int)(1.25f * (float)sourceFormatReader->lengthInSamples + 7200.0f);
unsigned char* mp3Buffer = new unsigned char[mp3buf_size];
int sizeEncoded = lame_encode_buffer(lameInit, (const short*)pcmBuffer.getReadPointer(0), (const short*)pcmBuffer.getReadPointer(1), sourceFormatReader->lengthInSamples, mp3Buffer, mp3buf_size);
int paddedSize = lame_encode_flush(lameInit, mp3Buffer, mp3buf_size);
lame_close(lameInit);
String targetFilename = sourceFile.getFullPathName().replace(".wav", ".mp3");
File targetFile(targetFilename);
targetFile.create();
targetFile.appendData(mp3Buffer, sizeEncoded + paddedSize);
delete [] mp3Buffer;
