关于wav的一些事 发表于 2022-09-01 | 更新于: 2022-09-01 | 分类于 日常知识 | 字数统计: 201 | 阅读时长 ≈ 1 关于wav的一些事 在线转换文档,图像,视频,音频文件 基于C语言的 WAV 文件双声道转单声道的实现 Python将立体声转为单声道 123456789101112131415161718192021222324252627282930313233343536373839404142import waveimport numpy as npimport structimport matplotlib.pyplot as plt# 只读方式打开WAV文件wf = wave.open(r'D:\project\py\123.wav', 'rb')nframes = wf.getnframes()framerate = wf.getframerate()str_data = wf.readframes(nframes)sample_width = wf.getsampwidth()wf.close()# 将波形数据转换成数组wave_data = np.fromstring(str_data, dtype=np.short)print(wave_data.shape)wave_data.shape = (-1, 2)wave_data = wave_data.Tmono_wave = (wave_data[0])# mono_wave = (wave_data[0]+wave_data[1])/2print(mono_wave)time = np.arange(0, nframes)*(1.0/framerate)plt.subplot(311)plt.plot(time, wave_data[0], c='r')plt.subplot(312)plt.plot(time, wave_data[1], c='r')plt.subplot(313)plt.plot(time, mono_wave, c='r')plt.show()#save wav filewf_mono = wave.open("mono.wav",'wb')wf_mono.setnchannels(1)wf_mono.setframerate(framerate)wf_mono.setsampwidth(sample_width)for i in mono_wave: data = struct.pack('<h', int(i)) wf_mono.writeframesraw( data )wf_mono.close()