File size: 6,820 Bytes
05b4fca |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
import os
from glob import glob
from librosa import load
from librosa.core import resample
import argparse
from argparse import ArgumentParser
from pathlib import Path
import numpy as np
from soundfile import write
from tqdm import tqdm
# Python script for generating noisy mixtures for training
#
# Mix WSJ0 with QUT noise with SNR sampled uniformly in [min_snr, max_snr]
min_snr = 0
max_snr = 15
sr = 16000
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("wsj0", type=str, help='path to WSJ0 directory')
parser.add_argument("qut", type=str, help='path to QUT directory')
parser.add_argument("target", type=str, help='target path for training files')
args = parser.parse_args()
# Clean speech for training
train_speech_files = sorted(glob(args.wsj0 + '**/si_tr_s/**/*.wav', recursive=True))
valid_speech_files = sorted(glob(args.wsj0 + '**/si_dt_05/**/*.wav', recursive=True))
test_speech_files = sorted(glob(args.wsj0 + '**/si_et_05/**/*.wav', recursive=True))
# Load QUT noise files
print('Loading QUT noise files')
cafe, sr_QUT = load(glob(args.qut + '**/CAFE-CAFE-1.wav', recursive=True)[0], sr=None)
car, sr_QUT = load(glob(args.qut + '**/CAR-WINDOWNB-1.wav', recursive=True)[0], sr=None)
home, sr_QUT = load(glob(args.qut + '**/HOME-KITCHEN-1.wav', recursive=True)[0], sr=None)
street, sr_QUT = load(glob(args.qut + '**/STREET-CITY-1.wav', recursive=True)[0], sr=None)
print('Resampling QUT noise files to 16kHz')
cafe = resample(cafe, sr_QUT, sr)
car = resample(car, sr_QUT, sr)
home = resample(home, sr_QUT, sr)
street = resample(street, sr_QUT, sr)
# ToDo: resampling with ffmpeg bacause librosa is soooo slow
# cafe, fs_QUT = load(os.path.join(args.qut, 'CAFE-CAFE-1_16k.wav'), sr=None)
# car, fs_QUT = load(os.path.join(args.qut, 'CAR-WINDOWNB-1_16k.wav'), sr=None)
# home, fs_QUT = load(os.path.join(args.qut, 'HOME-KITCHEN-1_16k.wav'), sr=None)
# street, fs_QUT = load(os.path.join(args.qut, 'STREET-CITY-1_16k.wav'), sr=None)
# Remove sweeps in the first and last 2 min in car noise file
car = car[120*sr:-120*sr]
# Create target dir
train_clean_path = Path(os.path.join(args.target, 'train/clean'))
train_noisy_path = Path(os.path.join(args.target, 'train/noisy'))
valid_clean_path = Path(os.path.join(args.target, 'valid/clean'))
valid_noisy_path = Path(os.path.join(args.target, 'valid/noisy'))
test_clean_path = Path(os.path.join(args.target, 'test/clean'))
test_noisy_path = Path(os.path.join(args.target, 'test/noisy'))
train_clean_path.mkdir(parents=True, exist_ok=True)
train_noisy_path.mkdir(parents=True, exist_ok=True)
valid_clean_path.mkdir(parents=True, exist_ok=True)
valid_noisy_path.mkdir(parents=True, exist_ok=True)
test_clean_path.mkdir(parents=True, exist_ok=True)
test_noisy_path.mkdir(parents=True, exist_ok=True)
# Initialize seed for reproducability
np.random.seed(0)
# Create files for training
print('Create training files')
for i, speech_file in enumerate(tqdm(train_speech_files)):
s, _ = load(speech_file, sr=sr)
snr_dB = np.random.uniform(min_snr, max_snr)
noise_type = np.random.randint(4)
speech_power = 1/len(s)*np.sum(s**2)
if noise_type == 0:
start = np.random.randint(len(cafe)-len(s))
n = cafe[start:start+len(s)]
elif noise_type == 1:
start = np.random.randint(len(home)-len(s))
n = home[start:start+len(s)]
elif noise_type == 2:
start = np.random.randint(len(street)-len(s))
n = street[start:start+len(s)]
elif noise_type == 3:
start = np.random.randint(len(car)-len(s))
n = car[start:start+len(s)]
else:
raise ValueError('Unexpected noise type index')
noise_power = 1/len(n)*np.sum(n**2)
noise_power_target = speech_power*np.power(10,-snr_dB/10)
k = noise_power_target / noise_power
n = n * np.sqrt(k)
x = s + n
file_name = speech_file.split('/')[-1]
write(os.path.join(train_clean_path, file_name), s, sr)
write(os.path.join(train_noisy_path, file_name), x, sr)
# Create files for validation
print('Create validation files')
for i, speech_file in enumerate(tqdm(valid_speech_files)):
s, _ = load(speech_file, sr=sr)
snr_dB = np.random.uniform(min_snr, max_snr)
noise_type = np.random.randint(4)
speech_power = 1/len(s)*np.sum(s**2)
if noise_type == 0:
start = np.random.randint(len(cafe)-len(s))
n = cafe[start:start+len(s)]
elif noise_type == 1:
start = np.random.randint(len(home)-len(s))
n = home[start:start+len(s)]
elif noise_type == 2:
start = np.random.randint(len(street)-len(s))
n = street[start:start+len(s)]
elif noise_type == 3:
start = np.random.randint(len(car)-len(s))
n = car[start:start+len(s)]
else:
raise ValueError('Unexpected noise type index')
noise_power = 1/len(n)*np.sum(n**2)
noise_power_target = speech_power*np.power(10,-snr_dB/10)
k = noise_power_target / noise_power
n = n * np.sqrt(k)
x = s + n
file_name = speech_file.split('/')[-1]
write(os.path.join(valid_clean_path, file_name), s, sr)
write(os.path.join(valid_noisy_path, file_name), x, sr)
# Create files for test
print('Create test files')
for i, speech_file in enumerate(tqdm(test_speech_files)):
s, _ = load(speech_file, sr=sr)
snr_dB = np.random.uniform(min_snr, max_snr)
noise_type = np.random.randint(4)
speech_power = 1/len(s)*np.sum(s**2)
if noise_type == 0:
start = np.random.randint(len(cafe)-len(s))
n = cafe[start:start+len(s)]
elif noise_type == 1:
start = np.random.randint(len(home)-len(s))
n = home[start:start+len(s)]
elif noise_type == 2:
start = np.random.randint(len(street)-len(s))
n = street[start:start+len(s)]
elif noise_type == 3:
start = np.random.randint(len(car)-len(s))
n = car[start:start+len(s)]
else:
raise ValueError('Unexpected noise type index')
noise_power = 1/len(n)*np.sum(n**2)
noise_power_target = speech_power*np.power(10,-snr_dB/10)
k = noise_power_target / noise_power
n = n * np.sqrt(k)
x = s + n
file_name = speech_file.split('/')[-1]
write(os.path.join(test_clean_path, file_name), s, sr)
write(os.path.join(test_noisy_path, file_name), x, sr) |