我花了几个月的时间试图弄清楚它是如何工作的。我正在开发一个程序,我有一个mp3文件,我有pcm,它可以进入"alsa"进行播放。使用库mpg123,其中的主要代码是:
while (mpg123_read (mh, buffer, buffer_size, & done) == MPG123_OK)
sendoutput (dev, buffer, done);
现在,我的尝试是基于在缓冲区上使用库avutil/avcodec来减少/增加1秒内的样本数量。结果很糟糕,听不见。在之前的一个问题中,有人建议我提高电脑性能,但如果像"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;
}
}
这两个代码都是我自己编写的,所以我不能像前一个问题那样询问任何人提出改进建议(尽管我不知道它们是否正确,叹气)第2版:更新了更改
在对av_resample
的调用中,samples_consumed
从不被读取,因此任何未使用的帧都会被跳过。此外,nsamples
是常数值44100,而不是实际读取的帧数(来自mpg123_read
的done
)。sizeof(dataOut)
错误;这是一个指针的大小。is_last
在输入的末尾是错误的。
在播放函数中,sizeof(buffer)
可能是错误的,这取决于buffer
的定义。