背景
librosa读音频,librosa处理音频,librosa写音频,比较简单。
项目是wave模块读取音频,wave模块写入音频,但是wave模块处理音频数据方法太少,必须调用librosa模块实现音频处理,这个流程是:
音频文件—>wave读取—>得到字节码—>解码为整形数据—>librosa处理—>编码为字节码—>写入音频文件
代码如下
src = '1.wav'wave_read = wave.open(src, 'rb')length = wave_read.getnframes()channels = wave_read.getnchannels()sample_width = wave_read.getsampwidth()frame_rate = wave_read.getframerate()print(f'length: {length}')print(f'channels: {channels}')print(f'sample_width: {sample_width}')print(f'frame_rate: {frame_rate}')wava_data = wave_read.readframes(length)data = np.frombuffer(wava_data, dtype=np.int16) # 转data = np.reshape(data, (-1, channels))data = data.astype(np.float32).T # 转data = librosa.resample(data, frame_rate, 48000) # 重采样data = data.astype(np.int16).T # 转wave_data = data.tobytes()target = 'dst-1117.wav'wave_write = wave.open(target, 'wb')wave_write.setnframes(len(data))wave_write.setnchannels(channels)wave_write.setsampwidth(sample_width)wave_write.setframerate(48000)wave_write.writeframes(wave_data)wave_write.close()