def main_gen(params, filenum): '''Calls gen_audio() to generate the audio signals, verifies that they meet the requirements, and writes the files to storage''' print("Generating file #" + str(filenum)) clean_clipped_files = [] clean_low_activity_files = [] noise_clipped_files = [] noise_low_activity_files = [] while True: # generate clean speech clean, clean_source_files, clean_cf, clean_laf = \ gen_audio(True, params, filenum) # generate noise noise, noise_source_files, noise_cf, noise_laf = \ gen_audio(False, params, filenum, len(clean)) clean_clipped_files += clean_cf clean_low_activity_files += clean_laf noise_clipped_files += noise_cf noise_low_activity_files += noise_laf # mix clean speech and noise # if specified, use specified SNR value if not params['randomize_snr']: snr = params['snr'] # use a randomly sampled SNR value between the specified bounds else: snr = np.random.randint(params['snr_lower'], params['snr_upper']) clean_snr, noise_snr, noisy_snr, target_level = snr_mixer(params=params, clean=clean, noise=noise, snr=snr) # Uncomment the below lines if you need segmental SNR and comment the above lines using snr_mixer #clean_snr, noise_snr, noisy_snr, target_level = segmental_snr_mixer(params=params, # clean=clean, # noise=noise, # snr=snr) # unexpected clipping if is_clipped(clean_snr) or is_clipped(noise_snr) or is_clipped(noisy_snr): continue else: break # write resultant audio streams to files hyphen = '-' clean_source_filenamesonly = [i[:-4].split(os.path.sep)[-1] for i in clean_source_files] clean_files_joined = hyphen.join(clean_source_filenamesonly)[:MAXFILELEN] noise_source_filenamesonly = [i[:-4].split(os.path.sep)[-1] for i in noise_source_files] noise_files_joined = hyphen.join(noise_source_filenamesonly)[:MAXFILELEN] noisyfilename = clean_files_joined + '_' + noise_files_joined + '_snr' + \ str(snr) + '_fileid_' + str(filenum) + '.wav' cleanfilename = 'clean_fileid_'+str(filenum)+'.wav' noisefilename = 'noise_fileid_'+str(filenum)+'.wav' noisypath = os.path.join(params['noisyspeech_dir'], noisyfilename) cleanpath = os.path.join(params['clean_proc_dir'], cleanfilename) noisepath = os.path.join(params['noise_proc_dir'], noisefilename) audio_signals = [noisy_snr, clean_snr, noise_snr] file_paths = [noisypath, cleanpath, noisepath] for i in range(len(audio_signals)): try: audiowrite(file_paths[i], audio_signals[i], params['fs']) except Exception as e: print(str(e)) pass return clean_source_files, clean_clipped_files, clean_low_activity_files, \ noise_source_files, noise_clipped_files, noise_low_activity_files
def main(cfg): snr_lower = int(cfg["snr_lower"]) snr_upper = int(cfg["snr_upper"]) total_snrlevels = int(cfg["total_snrlevels"]) clean_dir = os.path.join(os.path.dirname(__file__), 'clean_train') if cfg["speech_dir"] != 'None': clean_dir = cfg["speech_dir"] if not os.path.exists(clean_dir): assert False, ("Clean speech data is required") try: noise_dir = os.path.join(os.path.dirname(__file__), 'noise_train_new') if cfg["noise_dir"] != 'None': noise_dir = cfg["noise_dir"] if not os.path.exists(noise_dir): raise Exception("path does not exist") if not len(os.listdir(noise_dir)): raise Exception("folder is empty") except: res = input( "Can't find noise data. Do you want to run the audio slicer?\nEnter [Y]es [N]o\n" ) if res.lower() in ["y", "yes", ""]: slicer() print("Sliced successfully.\n\n") else: input("Noise data is required.\nPress any key to exit.\n") sys.exit() fs = float(cfg["sampling_rate"]) audioformat = cfg["audioformat"] total_hours = float(cfg["total_hours"]) audio_length = float(cfg["audio_length"]) silence_length = float(cfg["silence_length"]) noisyspeech_dir = os.path.join(os.path.dirname(__file__), 'mix') if not os.path.exists(noisyspeech_dir): os.makedirs(noisyspeech_dir) clean_proc_dir = os.path.join(os.path.dirname(__file__), 's1') if not os.path.exists(clean_proc_dir): os.makedirs(clean_proc_dir) noise_proc_dir = os.path.join(os.path.dirname(__file__), 's2') if not os.path.exists(noise_proc_dir): os.makedirs(noise_proc_dir) total_secs = total_hours * 60 * 60 total_samples = int(total_secs * fs) audio_length = int(audio_length * fs) SNR = np.linspace(snr_lower, snr_upper, total_snrlevels) cleanfilenames = glob.glob(os.path.join(clean_dir, audioformat)) if cfg["noise_types_excluded"] == 'None': noisefilenames = glob.glob(os.path.join(noise_dir, audioformat)) else: filestoexclude = cfg["noise_types_excluded"].split(',') noisefilenames = glob.glob(os.path.join(noise_dir, audioformat)) for i in range(len(filestoexclude)): noisefilenames = [ fn for fn in noisefilenames if not os.path.basename(fn).startswith(filestoexclude[i]) ] filecounter = 0 num_samples = 0 while num_samples < total_samples: idx_s = np.random.randint(0, np.size(cleanfilenames)) clean, fs = audioread(cleanfilenames[idx_s]) if len(clean) == audio_length: clean = clean else: while len(clean) <= audio_length: idx_s = idx_s + 1 if idx_s >= np.size(cleanfilenames) - 1: idx_s = np.random.randint(0, np.size(cleanfilenames)) newclean, fs = audioread(cleanfilenames[idx_s]) cleanconcat = np.append(clean, np.zeros(int(fs * silence_length))) clean = np.append(cleanconcat, newclean) clean = clean[0:audio_length] idx_n = np.random.randint(0, np.size(noisefilenames)) noise, fs = audioread(noisefilenames[idx_n]) if len(noise) >= len(clean): noise = noise[0:len(clean)] else: while len(noise) <= len(clean): idx_n = idx_n + 1 if idx_n >= np.size(noisefilenames) - 1: idx_n = np.random.randint(0, np.size(noisefilenames)) newnoise, fs = audioread(noisefilenames[idx_n]) noiseconcat = np.append(noise, np.zeros(int(fs * silence_length))) noise = np.append(noiseconcat, newnoise) noise = noise[0:len(clean)] filecounter = filecounter + 1 for i in range(np.size(SNR)): clean_snr, noise_snr, noisy_snr = snr_mixer(clean=clean, noise=noise, snr=SNR[i]) noisyfilename = 'noisy' + str(filecounter) + '_SNRdb_' + str( SNR[i]) + noisefilenames[idx_n].split("\\")[-1].split( ".")[0] + '.wav' cleanfilename = 'clnsp' + str(filecounter) + '_.wav' noisefilename = 'noise' + str(filecounter) + '_SNRdb_' + str( SNR[i]) + noisefilenames[idx_n].split("\\")[-1].split( ".")[0] + '.wav' noisypath = os.path.join(noisyspeech_dir, noisyfilename) cleanpath = os.path.join(clean_proc_dir, cleanfilename) noisepath = os.path.join(noise_proc_dir, noisefilename) audiowrite(noisy_snr, fs, noisypath, norm=False) audiowrite(clean_snr, fs, cleanpath, norm=False) audiowrite(noise_snr, fs, noisepath, norm=False) num_samples = num_samples + len(noisy_snr)
def main(cfg): snr_lower = float(cfg["snr_lower"]) snr_upper = float(cfg["snr_upper"]) total_snrlevels = float(cfg["total_snrlevels"]) clean_dir = os.path.join(os.path.dirname(__file__), 'clean_train') if cfg["speech_dir"] != 'None': clean_dir = cfg["speech_dir"] if not os.path.exists(clean_dir): assert False, ("Clean speech data is required") noise_dir = os.path.join(os.path.dirname(__file__), 'noise_train') if cfg["noise_dir"] != 'None': noise_dir = cfg["noise_dir"] if not os.path.exists(noise_dir): assert False, ("Noise data is required") fs = float(cfg["sampling_rate"]) audioformat = cfg["audioformat"] total_hours = float(cfg["total_hours"]) audio_length = float(cfg["audio_length"]) silence_length = float(cfg["silence_length"]) noisyspeech_dir = os.path.join(os.path.dirname(__file__), 'NoisySpeech_training') if not os.path.exists(noisyspeech_dir): os.makedirs(noisyspeech_dir) clean_proc_dir = os.path.join(os.path.dirname(__file__), 'CleanSpeech_training') if not os.path.exists(clean_proc_dir): os.makedirs(clean_proc_dir) noise_proc_dir = os.path.join(os.path.dirname(__file__), 'Noise_training') if not os.path.exists(noise_proc_dir): os.makedirs(noise_proc_dir) total_secs = total_hours * 60 * 60 total_samples = int(total_secs * fs) audio_length = int(audio_length * fs) SNR = np.linspace(snr_lower, snr_upper, total_snrlevels) cleanfilenames = glob.glob(os.path.join(clean_dir, audioformat)) if cfg["noise_types_excluded"] == 'None': noisefilenames = glob.glob(os.path.join(noise_dir, audioformat)) else: filestoexclude = cfg["noise_types_excluded"].split(',') noisefilenames = glob.glob(os.path.join(noise_dir, audioformat)) for i in range(len(filestoexclude)): noisefilenames = [ fn for fn in noisefilenames if not os.path.basename(fn).startswith(filestoexclude[i]) ] filecounter = 0 num_samples = 0 while num_samples < total_samples: idx_s = np.random.randint(0, np.size(cleanfilenames)) clean, fs = audioread(cleanfilenames[idx_s]) if len(clean) > audio_length: clean = clean else: while len(clean) <= audio_length: idx_s = idx_s + 1 if idx_s >= np.size(cleanfilenames) - 1: idx_s = np.random.randint(0, np.size(cleanfilenames)) newclean, fs = audioread(cleanfilenames[idx_s]) cleanconcat = np.append(clean, np.zeros(int(fs * silence_length))) clean = np.append(cleanconcat, newclean) idx_n = np.random.randint(0, np.size(noisefilenames)) noise, fs = audioread(noisefilenames[idx_n]) if len(noise) >= len(clean): noise = noise[0:len(clean)] else: while len(noise) <= len(clean): idx_n = idx_n + 1 if idx_n >= np.size(noisefilenames) - 1: idx_n = np.random.randint(0, np.size(noisefilenames)) newnoise, fs = audioread(noisefilenames[idx_n]) noiseconcat = np.append(noise, np.zeros(int(fs * silence_length))) noise = np.append(noiseconcat, newnoise) noise = noise[0:len(clean)] filecounter = filecounter + 1 for i in range(np.size(SNR)): clean_snr, noise_snr, noisy_snr = snr_mixer(clean=clean, noise=noise, snr=SNR[i]) noisyfilename = 'noisy' + str(filecounter) + '_SNRdb_' + str( SNR[i]) + '_clnsp' + str(filecounter) + '.wav' cleanfilename = 'clnsp' + str(filecounter) + '.wav' noisefilename = 'noisy' + str(filecounter) + '_SNRdb_' + str( SNR[i]) + '.wav' noisypath = os.path.join(noisyspeech_dir, noisyfilename) cleanpath = os.path.join(clean_proc_dir, cleanfilename) noisepath = os.path.join(noise_proc_dir, noisefilename) audiowrite(noisy_snr, fs, noisypath, norm=False) audiowrite(clean_snr, fs, cleanpath, norm=False) audiowrite(noise_snr, fs, noisepath, norm=False) num_samples = num_samples + len(noisy_snr)
from audiolib import audioread, audiowrite, snr_mixer from os.path import isfile, join, basename import os import numpy as np """ Given a source folder, add white-noise in a range of different SNR levels to all files in the source folder. """ if __name__=="__main__": source_folder = "clips" source_files = [join(source_folder, f) for f in os.listdir(source_folder) if isfile(join(source_folder, f))] output_folder = source_folder+"_snr" snr_min = 30 snr_max = 50 for f in source_files: clean, fs = audioread(f) # white-noise noise = np.random.normal(0, 1, len(clean)) for i in range (snr_min,snr_max): clean_snr, noise_snr, noisy_snr = snr_mixer(clean=clean, noise=noise, snr=i) output_filename = join(output_folder, f'{i}S_{os.path.splitext(basename(f))[0]}.wav') audiowrite(noisy_snr, fs,output_filename, norm=False)