c++ - 管理 MP3 的播放速度和位置

标签 c++ c audio signal-processing alsa

我尝试了几个月来弄清楚它是如何工作的。我有一个我正在开发的程序,我有一个 mp3 文件进出我有 pcm 去“alsa”进行播放。使用主要代码如下的库 mpg123:

while (mpg123_read (mh, buffer, buffer_size, & done) == MPG123_OK)
    sendoutput (dev, buffer, done); 

现在,我的尝试是基于在缓冲区上使用库 avutil/avcodec 来减少/增加一秒钟内的样本数量。结果很糟糕,听不见。在之前的问题中,有人建议我提高 PC 性能,但如果像“VLC”这样的简单程序可以在旧计算机上做到这一点,为什么我不能?

对于音频文件中的位置问题,我该如何实现?

编辑 我添加了一些代码来尝试解释。

SampleConversion.c

#define LENGTH_MS 1000      // how many milliseconds of speech to store 0,5s:x=1:44100 x=22050 sample da memorizzare
#define RATE 44100      // the sampling rate (input)
struct AVResampleContext* audio_cntx = 0;
//(LENGTH_MS*RATE*16*CHANNELS)/8000
void inizializeResample(int inRate, int outRate)
{
    audio_cntx = av_resample_init( outRate, //out rate
        inRate, //in rate
        16, //filter length
        10, //phase count
        0, //linear FIR filter
        0.8 ); //cutoff frequency
    assert( audio_cntx && "Failed to create resampling context!");
}
void resample(char dataIn[],char dataOut[],int nsamples)
{
    int samples_consumed;
    int samples_output = av_resample( audio_cntx, //resample context
    (short*)dataOut,    //buffout
    (short*)dataIn,     //buffin
    &samples_consumed,  //&consumed
    nsamples,       //nb_samples
    sizeof(dataOut)/2,//lenout sizeof(out_buffer)/2 (Right?)
    0);//is_last
    assert( samples_output > 0 && "Error calling av_resample()!" );
}

void endResample()
{
    av_resample_close( audio_cntx );    
}

我编辑的播放函数(Mpg123.c)

if (isPaused==0 && mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
{   
  int i=0; char * resBuffer=malloc(sizeof(buffer));
  //resBuffer=&buffer[0];
  resample(buffer,resBuffer,44100);
  if((ao_play(dev, (char*)resBuffer, done)==0)){
    return 1;
  }
}

这两个代码都是我写的,所以我不能像上一个问题那样询问任何人提出的改进建议(虽然我不知道他们是否正确,唉) Edit2:更新了变化

最佳答案

在调用 av_resample 时,永远不会读取 samples_consumed,因此会跳过任何未使用的帧。 此外,nsamples 是常数值 44100,而不是实际读取的帧数(done 来自 mpg123_read)。 sizeof(dataOut) 错误;它是指针的大小。 is_last 在输入的末尾是错误的。

在play函数中,sizeof(buffer)很可能是错误的,这取决于buffer的定义。

关于c++ - 管理 MP3 的播放速度和位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25533963/

相关文章:

c++ - 如何聚合序列中的循环?

c - 将二维字符数组放入结构中,不知道从哪里开始

audio - WASAPI GetNextPacketSize 什么时候返回 0

安卓麦克风问题

IOS html 5 音频重复和跳过

c++ - 如何使用qt使用qwebview进行打印

c++ - 如何在 Windows 上修复 cmake find_package "Could NOT find SDL2"?

c - 更改返回位置时出现意外结果

c - 具有相同名称和类型的两个变量,在两个不同的 .c 文件中,使用 gcc 编译

c++ - iOS Voip 唤醒电话