使用lame编码音频:将32位浮点数转换为mp3

时间:2014-11-10 02:35:30

标签: c audio encoding mp3 lame

我很难使用lame将32位浮点音频转换为mp3。我目前能够将音频转换为mp3,但输出具有均匀间隔的间隙,看起来像是插入到正确的输出之间(没有预期的输出丢失)。

下面是Audacity中输出音频的时间线图像 Audio timeline

以下是我正在使用的代码:

#include <stdio.h>
#include <lame/lame.h>

int main(void)
{
    int read, write;

    FILE *pcm = fopen("file.pcm", "rb");
    FILE *mp3 = fopen("file.mp3", "wb");

    const int PCM_SIZE = 10000;
    const int MP3_SIZE = 10000;
    unsigned char mp3_buffer[MP3_SIZE];

    float pcm_buffer[PCM_SIZE*2];

    lame_t lame = lame_init();
    lame_set_in_samplerate(lame, (48000/2)); //The sampling rate of the input file is 48MHz
    //but the output sounds like its on fast-forward when input sample rate is set to 48MHz
    lame_set_VBR(lame, vbr_off); // also tried vbr_default
    lame_set_out_samplerate(lame, 16000);
    lame_init_params(lame);

    do {
        read = fread(pcm_buffer, sizeof(float), PCM_SIZE, pcm);
        printf("read = %d\n",read);
        if (read == 0)
            write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
        else
            write = lame_encode_buffer_interleaved_ieee_float(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
        fwrite(mp3_buffer, write, 1, mp3);
    } while (read != 0);

    lame_close(lame);
    fclose(mp3);
    fclose(pcm);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我开始工作,我只需要使用lame_encode_buffer_interleaved_ieee_float更改为使用lame_encode_buffer_ieee_float功能