-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetmonochannel.py
57 lines (39 loc) · 2.01 KB
/
setmonochannel.py
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
43
44
45
46
47
48
49
import wave
import librosa
import numpy as np
import os
def run():
dir = '/Users/app/Desktop/IVRP/wavfile/'
file_names = []
channel = 0
for root_dir, subdir_list, file_list in os.walk(dir):
for file_name in file_list:
if os.path.splitext(file_name)[1].lower() == '.wav':
full_path = os.path.join(root_dir, file_name)
file_names.append(full_path)
for fname in file_names:
try:
wav = wave.open(fname)
fn = fname.split('.')[0] + '_mono.wav'
nch = wav.getnchannels()
depth = wav.getsampwidth()
wav.setpos(0)
sdata = wav.readframes(wav.getnframes())
typ = {1: np.uint8, 2: np.uint16, 4: np.uint32}.get(depth)
if not typ:
raise ValueError("sample width {} not supported".format(depth))
if channel >= nch:
raise ValueError("cannot extract channel {} out of {}".format(channel + 1, nch))
print("Extracting channel {} out of {} channels, {}-bit depth".format(channel + 1, nch,
depth * 8))
data = np.fromstring(sdata, dtype=typ)
ch_data = data[channel::nch]
outwav = wave.open(fn, 'w')
outwav.setparams(wav.getparams())
outwav.setnchannels(1)
outwav.writeframes(ch_data.tostring())
outwav.close()
print("done")
except:
print('Error occurred while reading "%s" wav file' % fname)
run()