Python 读取音频数组,计算音频的电平rms和能量

在处理音视频数据时,我们可以先将音视频数据读取成数组,这样进行剪切、截取、合并等等操作就非常的方便。
封装了一个方读取音频数组数据:
支持从wav、pcm、raw中读取音频数组和采样率等信息
依赖库:os、array、numpy、scipy.io.wavfile

def read_audio_data(audio_file, pcm_rate=16000, chs=1, bit_depth='int16'):
	# 读取音频数组
    if not os.path.exists(audio_file):
        raise IOError
    if audio_file.endswith('.wav'):
        wave_rate, audio_data = wavfile.read(audio_file)
        return audio_data.T, wave_rate
    elif audio_file.endswith('.pcm') or audio_file.endswith('.raw'):
        data_array = array.array('h')
        if bit_depth == 'float32':
            data_array = array.array('f')
        elif bit_depth == 'int16':
            data_array = array.array('h')
        with open(audio_file, 'rb') as f:
            data_array.frombytes(f.read())
            if chs > 1:
                mono_length = len(data_array[0::int(chs)])
                data_array = np.array(data_array).reshape(mono_length, int(chs))
                return data_array.T, pcm_rate
            elif chs == 1:
                return np.array(data_array), pcm_rate
            else:
                raise ValueError('please check the file chs')

归一化,转化数据类型,将int数组转化为浮点float32

def rescale_to_float32(audio_data, signal_dtype=np.float32):
    if not np.issubdtype(signal_dtype, np.floating):
        raise ValueError('signal_dtype not support')
    if np.issubdtype(audio_data.dtype, np.floating):
        max_num_audio_signal = np.max(abs(audio_data))
        if max_num_audio_signal > 1:
            if len(audio_data.shape) > 1:
                return audio_data.astype(signal_dtype) / np.max(np.absolute(audio_data))
            elif len(audio_data.shape) ==1:
                return audio_data.astype(signal_dtype) / max(abs(audio_data))
        else:
            return audio_data.astype(signal_dtype)
    elif np.issubdtype(audio_data.dtype, np.integer):
        return (audio_data / np.iinfo(audio_data.dtype).max).astype(signal_dtype)
    else:
        raise ValueError('signal_dtype not support')```

计算音频的电平rms值


def eva_rms(audio_data, chs=1, mics=1):
	# audio_data:音频数组
	# chs:通道数
	# mics:目标通道
    new_audio_data = rescale_to_float32(audio_data)
    if len(audio_data.shape) == 1:
        total_rms = np.sqrt(np.mean(np.square(new_audio_data)))
        if total_rms == 0 or math.isinf(total_rms):
            total_rms = 1e-10
        total_rms_db = 20 * np.log10(total_rms) + 3
        return total_rms_db
    elif len(audio_data.shape) > 1:
        if audio_data.shape[0] == int(chs):
            if int(mics - 1) >= 0:
                total_rms = np.sqrt(np.mean(np.square(new_audio_data), axis=1))
                total_rms_db = 20 * np.log10(total_rms) + 3
                total_rms_db_list = [i for i in total_rms_db if i > -90]
                return np.mean(total_rms_db_list)
            else:
                return ValueError('mics error')
        elif audio_data.shape[1] == int(chs):
            if int(mics - 1) >= 0:
                total_rms = np.sqrt(np.mean(np.square(new_audio_data.T), axis=1))
                total_rms_db = 20 * np.log10(total_rms) + 3
                total_rms_db_list = [i for i in total_rms_db if i > -90]
                return np.mean(total_rms_db_list)
        else:
            raise ValueError('mics error')
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值