关于wav的一些事

关于wav的一些事

在线转换文档,图像,视频,音频文件

基于C语言的 WAV 文件双声道转单声道的实现

Python将立体声转为单声道

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
30
31
32
33
34
35
36
37
38
39
40
41
42
import wave
import numpy as np
import struct
import 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.T
mono_wave = (wave_data[0])
# mono_wave = (wave_data[0]+wave_data[1])/2
print(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 file
wf_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()