def make_dataset_5(output_dir): """ Dataset 1 consists of 6*5 hours of synthetic noisy speech. About the same length as the noise, however it has been sampled with It has been sampled with replacemnt. The Clean files are from switchboard and the noise is anotated noise only segments from the lre17_dev set. Each sample is 5 seconds long. SNR 10 % is reserved for the validation set """ # int(119.2 *10**9 /2/8000/5)/int(6.6 * .9 * 3600 / 5 * 5) # output_dir a = Path('.') / 'data' / 'processed' / 'dataset_1' # lre17_train is 119.2 GB which is 119.2 *10**9 /2/8000 seconds # almost 70 times larger # train_len = int(6.6 * .9 * 3600 / 5 * 5) # synthesise 1 times the train noise train_len = int(119.2 * 10**9 / 2 / 8000 / 5) # synthesise equel to lre17_train # test_len = int(6.6 * .1 * 3600 / 5 * 5) # synthesise 5 times the test noise train_set = Data_synthesis_1( length=train_len, speech_list='lre_train', noise_version=2) training_data_loader = DataLoader(train_set, batch_size=1, num_workers=2) t_path_str_x = os.path.join(output_dir, 'train', 'x', 'sample_{}.wav') t_path_str_y = os.path.join(output_dir, 'train', 'y', 'sample_{}.wav') list_ = ((t_path_str_x, t_path_str_y, training_data_loader), ) for path_str_x, path_str_y, data_loader in list_: makedir(os.path.dirname(path_str_x)) makedir(os.path.dirname(path_str_y)) for i, (x, y) in enumerate(data_loader): x, y = x.numpy()[0], y.numpy()[0] save_audio_(x, path_str_x.format(i)) save_audio_(y, path_str_y.format(i))
def make_dataset_1(output_dir): """ Dataset 1 consists of 6 hours of synthetic noisy speech. About the same length as the noise, however it has been sampled with It has been sampled with replacemnt. The Clean files are from switchboard and the noise is anotated noise only segments from the lre17_dev set. Each sample is 5 seconds long. SNR 10 % is reserved for the validation set """ train_len = int(6.6 * .9 * 3600 / 5) # synthesise 1 times the train noise test_len = int(6.6 * .1 * 3600 / 5) # synthesise 1 times the test noise train_set = Data_synthesis_1(length=train_len) training_data_loader = DataLoader(train_set, batch_size=1, num_workers=2) t_path_str_x = os.path.join(output_dir, 'train', 'x', 'sample_{}.wav') t_path_str_y = os.path.join(output_dir, 'train', 'y', 'sample_{}.wav') validation_set = Data_synthesis_1(length=test_len, test=True) validation_data_loader = DataLoader(validation_set, batch_size=1, num_workers=2) v_path_str_x = os.path.join(output_dir, 'val', 'x', 'sample_{}.wav') v_path_str_y = os.path.join(output_dir, 'val', 'y', 'sample_{}.wav') list_ = ((t_path_str_x, t_path_str_y, training_data_loader), (v_path_str_x, v_path_str_y, validation_data_loader) ) for path_str_x, path_str_y, data_loader in list_: makedir(os.path.dirname(path_str_x)) makedir(os.path.dirname(path_str_y)) for i, (x, y) in enumerate(data_loader): x, y = x.numpy()[0], y.numpy()[0] save_audio_(x, path_str_x.format(i)) save_audio_(y, path_str_y.format(i))
def make_noise_files(overlap=False): # project path annot_dir = os.path.join('data', 'raw', 'lre17_dev_annot') soundfiledir = os.path.join('data', 'processed', 'lre17_dev') noisefiledir = os.path.join('data', 'processed', 'noise') if overlap: noisefiledir = os.path.join('data', 'processed', 'noise2') makedir(noisefiledir) for (dirpath, dirnames, filenames) in os.walk(annot_dir): break filenames = sorted(filenames) for name in filenames: with open(os.path.join(dirpath, name), 'r') as markfile: lines = markfile.readlines() fs, x = wavfile.read(os.path.join(soundfiledir, name[:-4] + 'wav')) I = np.zeros((len(lines) + 1, 2), dtype=np.int) for k, s in enumerate(lines): i, j = s.replace('\n', '').split()[1:] i, j = float(i), float(j) i, j = int(i * 8000), int((i + j) * 8000) I[k, 1], I[k + 1, 0] = i, j I[-1, 1] = len(x) """perhaps trow away some """ I = I[(np.diff(I) > 1000).flatten()] if len(I) == 0: continue n = np.zeros(np.sum(np.diff(I))) w1, w2 = np.split(np.hanning(256), 2) k = 0 for i, j in I: k2 = k + j - i n[k:k2] = x[i:j] n[k:k + 128] *= w1 n[k2 - 128:k2] *= w2 k = k2 if overlap is False else k2 - 128 if overlap: n = n[:k2 + 128] n = n.astype(np.int16) wavfile.write(os.path.join(noisefiledir, name[:-4] + 'wav'), fs, n)
def eval_PESQ(reference_dir, degraded_dir, output_path): makedir(os.path.dirname(output_path)) print('Warning: PESQ will save a file in the current working directory') fs = "+8000" PESQ_path = os.path.join('src', 'external', 'PESQ') for (dirpath, dirnames, reference_files) in os.walk(reference_dir): break for (dirpath, dirnames, degraded_files) in os.walk(degraded_dir): break assert len(reference_files) == len(degraded_files) def try_to_run_PESQ(filename): reference = os.path.join(reference_dir, filename) degraded = os.path.join(degraded_dir, filename) PESQ = [PESQ_path, fs, reference, degraded] try: c = subprocess.run(PESQ, stdout=subprocess.PIPE) mos, mos_lqo = c.stdout[-100:].decode().split('\n')[-2].split( '=')[-1].split('\t') mos, mos_lqo = float(mos), float(mos_lqo) return mos, mos_lqo except: return None, None mos_list, mos_lqo_list = np.zeros((2, len(reference_files))) # #single process # res = list(map(try_to_run_PESQ, reference_files)) # mos_list, mos_lqo_list = list(zip(*res)) from multiprocessing.dummy import Pool # threading p = Pool(4) res = p.map(try_to_run_PESQ, reference_files) p.close() mos_list, mos_lqo_list = list(zip(*res)) io.savemat( output_path, { 'data_mos': np.array(mos_list, dtype=np.float), 'data_mos_lqo': np.array(mos_lqo_list, dtype=np.float), 'names': reference_files })
def PESQ_evalfolder(reference_dir, degraded_dir, output_path, fs): """Compute the PESQ scores for all wavefiles in a folder. Walks though a directory of degraded wavefiles and computes all the scores with similar named refrence wavefiles in the refrence directory. Saves a csv file with the filenames, mos, and mos_lqo scores. Parameters ---------- reference_dir : str The path to the refrence wavfile. degraded_dir : str The path to the degraded wavfile. fs : int The sample frequency should be 8000 or 16000. """ reference_paths = walk_dir(reference_dir) degraded_paths = format_paths(reference_paths, reference_dir, degraded_dir) assert set(degraded_paths) == set(walk_dir(degraded_dir)) print('Warning: PESQ will save a file in the current working directory') mos_list, mos_lqo_list = PESQ_evalpaths(reference_paths, degraded_paths, output_path, fs=fs) print('PESQ failed for {} samples'.format(np.isnan(mos_list).sum())) makedir(os.path.dirname(output_path)) io.savemat( output_path, { 'data_mos': mos_list, 'data_mos_lqo': mos_lqo_list, 'names': list(os.path.relpath(s, reference_dir) for s in reference_paths), 'reference_dir': reference_dir, 'degraded_dir': degraded_dir })