当前位置:   article > 正文

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

pyaudio 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')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

归一化,转化数据类型,将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')```

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

计算音频的电平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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/106386
推荐阅读
相关标签
  

闽ICP备14008679号