-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdownsampling.py
51 lines (35 loc) · 1.69 KB
/
downsampling.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
50
51
import json
import os
from os.path import join as opj
from tqdm import tqdm
import sys
import torchaudio
downsample_rate = 16000
clean_train_path = 'original clean train path'
noisy_train_path = 'original noisy train path'
clean_test_path = 'original clean test path'
noisy_test_path = 'original noisy test path'
resample_path = 'resample path'
def downsample_wave(clean_path, noisy_path, sample_rate=downsample_rate, phase='train'):
clean_list = os.listdir(clean_path)
noisy_list = os.listdir(noisy_path)
clean_list.sort(); noisy_list.sort()
clean_resample_path = opj(resample_path, 'clean_'+phase)
noisy_resample_path = opj(resample_path, 'noisy_'+phase)
if not os.path.isdir(clean_resample_path):
os.makedirs(clean_resample_path)
if not os.path.isdir(noisy_resample_path):
os.makedirs(noisy_resample_path)
tf = torchaudio.transforms.Resample(48000,sample_rate)
for i in tqdm(range(len(clean_list))):
clean, sr = torchaudio.load(opj(clean_path, clean_list[i]))
noisy, sr = torchaudio.load(opj(noisy_path, noisy_list[i]))
clean = tf(clean)
noisy = tf(noisy)
torchaudio.save(opj(clean_resample_path, clean_list[i]), clean, sample_rate)
torchaudio.save(opj(noisy_resample_path, noisy_list[i]), noisy, sample_rate)
if __name__ == "__main__":
print('--- Trainset Resampling ---')
downsample_wave(clean_train_path, noisy_train_path, sample_rate=downsample_rate, phase='train')
print('--- Testset Resampling ---')
downsample_wave(clean_test_path, noisy_test_path, sample_rate=downsample_rate, phase='test')