采样率转换功能无法产生可听见的声音,但只能产生一小段音频

时间:2014-07-02 17:29:43

标签: c audio ffmpeg signal-processing

playmp3()使用libmpg123

if (isPaused==0 && mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
{
    char * resBuffer=&buffer[0]; //22100=0,5s
    buffer = resample(resBuffer,22050,22050); // I think the result is 1/2 of audio speed
    if((ao_play(dev, (char*)buffer, done)==0)){
        return 1;
}

resample()使用ffmpeg的avcodec

#define LENGTH_MS 500       // how many milliseconds of speech to store 0,5s:x=1:44100 x=22050 sample to store
#define RATE 44100      // the sampling rate (input)
#define FORMAT PA_SAMPLE_S16NE  // sample size: 8 or 16 bits
#define CHANNELS 2      // 1 = mono 2 = stereo

struct AVResampleContext* audio_cntx = 0;
//(LENGTH_MS*RATE*16*CHANNELS)/8000

    void resample(char in_buffer[],int out_rate,int nsamples,char out_buffer[])
    {
        //char out_buffer[ sizeof( in_buffer ) * 4];
        audio_cntx = av_resample_init( out_rate, //out rate
            RATE, //in rate
            16, //filter length
            10, //phase count
            0, //linear FIR filter
            1.0 ); //cutoff frequency
        assert( audio_cntx && "Failed to create resampling context!");
        int samples_consumed;
        //*out_buffer = malloc(sizeof(in_buffer));
        int samples_output = av_resample( audio_cntx, //resample context
            (short*)out_buffer, //buffout
            (short*)in_buffer,  //buffin
            &samples_consumed,  //&consumed
            nsamples,       //nb_samples
            sizeof(out_buffer)/2,//lenout sizeof(out_buffer)/2
            0);//is_last
        assert( samples_output > 0 && "Error calling av_resample()!" );
        av_resample_close( audio_cntx );    
    }

当我运行此代码时,应用程序部分,问题是我听到声音不稳定,为什么? 我认为数组的大小是正确的,我考虑到下半年应该是来自商店的22050个样本。

1 个答案:

答案 0 :(得分:0)

只是一个猜测,但您可能无法满足实时要求。在内核中,内核可能无法为您的应用程序提供足够的CPU时间来进行所需的处理。如果您在linux中运行,请尝试降低应用程序的良好性,如果失败,请尝试将优先级设置为具有高优先级的SCHED_FIFO。您可能需要通过ulimit调整系统优先级限制才能使其正常工作。