c - 采样率转换函数无法产生可听声音,但只能产生小段音频

标签 c audio ffmpeg signal-processing

使用 libmpg123 的 playmp3()

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 个样本。

最佳答案

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

关于c - 采样率转换函数无法产生可听声音,但只能产生小段音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24537427/

相关文章:

c - 如何确保以下函数没有过度分配?

javascript - 跨多个页面保持一致的音量 (HTML/Javascript)

python - Docker Compose 和 Postgres 连接被拒绝

ruby - 使用 Rmagick 和 FFmpeg 实现图片缩放效果

javascript - 在页面上连续播放随机声音

video - ffmpeg在特定时间在mp4上覆盖png

c - 为什么程序输出x1 = Nan,x2 = Nan?

c - 为什么同样的程序在 linux 2.6.32 中比 2.6.18 慢?

c - 混合 fdopen() 和 open() -> 错误的文件描述符

audio - 如何使用 ffmpeg 从 Linux USB 捕获设备同时捕获音频和视频