LAME + JUCE question


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;

    // 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_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);


    String targetFilename = sourceFile.getFullPathName().replace(".wav", ".mp3");

    File targetFile(targetFilename);
    targetFile.appendData(mp3Buffer, sizeEncoded + paddedSize);

    delete [] mp3Buffer;


Why don't you use the JUCE LAMEEncoderAudioFormat?


LAMEEncoderAudioFormat relies on external LAME executable which I want to avoid. Its a free open-source app so I rather make it convenient.


I did manage to solve my own problem, needed to use 'lame_encode_buffer_ieee_float' as AudioSampleBuffer must convert the wave data to +-1 scale.