获取音频文件能量大小或声音大小

获取PCM格式数据能量大小(格式为signed 16bit,单声道)
frm 包含着音频数据,以及音频数据buf的长度
user_threshold 为用户设置的临界值,超过这个值,返回1代表着这一段音频数据能量高于所设置的user_threshold (范围是0-32767),值越大,划分的越细

int IMP_AI_GetFramePower(IMPAudioFrame *frm, int user_threshold)
{
    short tmp = 0;
    int i = 0;
    int threshold = 0;
    int flag_power = 0;
    //int db = 0;

if ((user_threshold <= 0) || (user_threshold > 255)) {
    IMP_LOG_ERR(TAG, "%s %d threshold set error", __func__, __LINE__);
    return -1;
}

threshold = (pow(2, 15) * user_threshold) / 255;

short* addr = (short*)frm->virAddr;   //virAddr指向音频数据buffer,short类型是因为音频格式为signed 16bit单声道

for (i = 0 ; i < (frm->len/2) ; i += 1) {
    memcpy(&tmp, addr+i, sizeof(short));
    //printf(" current threshold = %d\n", abs(tmp));
    
    if (abs(tmp) > threshold) {
        if (++flag_power == 16) {     //连续16个采样点超过user_threshold ,就认为此段音频数据能量超过user_threshold 
            printf("18 current threshold(%d) exceed user_threshold(%d) and user_threshold = %d\n", abs(tmp), threshold, user_threshold);

            return 1;
        }
    }
}
return 0;

}

你可能感兴趣的:(音频)