对format=fltp格式的PCM进行音量计算

在作音频合并的过程中,发现,如果合并的音频路数太多,合并的结果问题不能让人满意。根据实际情况分析,很多情况下有些音频数据并不需要合并,比如静音的情况,此时就需要实现音量大小的计算了。算法也是从网上找的,精髓一致,只不过音频数据的格式不一致。我参考的是这篇文章。下面是测试的算法实现,结果还可以吧(fltp的格式每个采样点占四个字节,所以算法中我把声道的数据转换成float)。

static int pcm_fltp_db_count(AVFrame *frame, int channels)
{
    int i = 0, ch = 0;
    int ndb = 0;
    float value = 0.;
    float *ch_left = (float *)frame->data[0];
    //float *ch_right = (float *)frame->data[1];
    for(i=0; i < frame->nb_samples; i++)
    {
        value += fabs(ch_left[i]);
    }

    value = value / frame->nb_samples;
    if(0 != value)
    {
        ndb = (int)(20.0*log10((value / 1.0)));
    }
    else
        ndb = -100;

    printf("ndb:%d\n",ndb);
    return ndb;
}

猜你喜欢

转载自blog.csdn.net/topsluo/article/details/77320830