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

Posted

技术标签:

【中文标题】采样率转换功能无法产生可听见的声音,但只能产生一小段音频【英文标题】:samplerate conversion function fails to produce an audible sound but only small pieces of audio 【发布时间】:2014-07-02 17:29:43 【问题描述】:

使用 libmpg123 播放mp3()

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个样本从store计算出来的。

【问题讨论】:

【参考方案1】:

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

【讨论】:

这怎么可能?理论上,程序很小,这是唯一改变声音的功能。加上我的电脑是 i7 quadcoreda 3 GHz,目标是在这个树莓派上运行我的程序,所以我认为这个解决方案不适用:/

以上是关于采样率转换功能无法产生可听见的声音,但只能产生一小段音频的主要内容,如果未能解决你的问题,请参考以下文章

[随笔] 音频, 采样, 采样率, 样本大小, 样本编码

音频中采样位数,采样率,比特率的名词解释(转)

多媒体文件格式:PCM / WAV 格式

音频 属性详解(涉及采样率通道数位数比特率帧等)

麦克风采集与播放 (源码)

声音就是二维码 阿里云IoT发布智能声码服务