gpt4 book ai didi

c - 使用lame编码音频: Converting 32bit float to mp3

转载 作者:行者123 更新时间:2023-11-30 17:26:15 25 4
gpt4 key购买 nike

我在使用 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;
}

最佳答案

我成功了,我只需要从使用 lame_encode_buffer_interleaved_ieee_float 更改为使用 lame_encode_buffer_ieee_float 函数

关于c - 使用lame编码音频: Converting 32bit float to mp3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26835891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com