【发布时间】:2014-10-21 10:33:39
【问题描述】:
我尝试了几个月来弄清楚它是如何工作的。我有一个正在开发的程序,我有一个 mp3 文件进出我有 pcm 去“alsa”进行播放。使用库 mpg123 的主要代码是这样的:
while (mpg123_read (mh, buffer, buffer_size, & done) == MPG123_OK)
sendoutput (dev, buffer, done);
现在,我的尝试基于在缓冲区上使用库 avutil/avcodec 来减少/增加一秒内的样本数。结果很糟糕,听不见。在上一个问题中,有人建议我提高我的电脑性能,但如果像“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:更新了变化
【问题讨论】:
-
"如果像“VLC”这样的简单程序可以在旧电脑上做到这一点,为什么我不能呢?"你看过 VLC 的源代码吗?
-
如果您不解释您是如何使用
avutil/avcodec的,如何期望有人提供帮助?至于在音频文件中重新定位,这将取决于您正在使用的库的功能 - 如果无法做到,您将不得不将整个文件读入内存。 -
Avcodec 有一个特定的重采样函数,称为“av_resample”
-
您不能为每个块重新初始化
audio_cntx。而且我怀疑你所有的缓冲区大小都是错误的(它们是字节、样本还是帧?)。 -
基于再现开始的事实,没有重新采样,并考虑到函数 ao_play 的参数是这些:
(ao_device * device, **char * output_samples**, uint_32 num_bytes);我认为它们是样本。如果输入是样本,Out Buffer 的正确大小是多少?
标签: c++ c audio signal-processing alsa