def mix_with_scaper(num_mixtures, foreground_path, background_path, scene_duration, sample_rate, target_folder, event_parameters, num_sources=None, labels=None, coherent=False, allow_repeated_label=False, ref_db=-40, bitdepth=16, seed=0, num_workers=1): nussl.utils.seed(seed) os.makedirs(target_folder, exist_ok=True) scaper_seed = np.random.randint(100) logging.info('Starting mixing.') if num_sources is None and labels is None: raise ValueError("One of labels or num_sources must be set!") if coherent and labels is None: raise ValueError("Coherent mixing requires explicit labels!") generators = [] if background_path is None: background_path = foreground_path for i in range(num_mixtures): sc = Scaper( scene_duration, fg_path=foreground_path, bg_path=background_path, random_state=scaper_seed, ) sc.ref_db = ref_db sc.sr = sample_rate sc.bitdepth = bitdepth generators.append(sc) scaper_seed += 1 mix_func = make_one_mixture_coherent if coherent else make_one_mixture def arg_tuple(i): _args = (generators[i], os.path.join(target_folder, f'{i:08d}.wav'), labels if coherent else num_sources, event_parameters, allow_repeated_label) return _args args = [arg_tuple(i) for i in range(num_mixtures)] # do one by itself for testing mix_func(*args[0]) args = list(zip(*args[1:])) args = [list(a) for a in args] # now do the rest in parallel p_tqdm.p_map(mix_func, *args, num_cpus=num_workers)
def scaper_mix( mixture_parameters, sample_rate, event_parameters=None, coherent=False, ref_db=-40, bitdepth=16, seed=0, num_workers=1, ): np.random.seed(seed) logging.info('Making JAMS files') for key, params in mixture_parameters.items(): if 'event_parameters' in params: _event_parameters = params['event_parameters'] else: _event_parameters = event_parameters generators = [] logging.info('Making generators') bg_path = params['background_path'] if not bg_path: bg_path = params['foreground_path'] for i in tqdm(range(params['num_mixtures'])): sc = Scaper( params['scene_duration'], fg_path=params['foreground_path'], bg_path=bg_path, random_state=np.random.randint(params['num_mixtures']*10) ) sc.ref_db = ref_db sc.sr = sample_rate sc.bitdepth = bitdepth generators.append(sc) os.makedirs(params['target_path'], exist_ok=True) timings = {} silent_files = {'default': True} if coherent: args = [ { 'path_to_file': os.path.join(params['target_path'], f'{i:08d}.wav'), 'sc': generators[i], 'labels': params['labels'], 'event_parameters': _event_parameters, } for i in range(params['num_mixtures']) ] mix_func = make_one_mixture_coherent else: args = [ { 'path_to_file': os.path.join(params['target_path'], f'{i:08d}.wav'), 'sc': generators[i], 'num_sources': params['num_sources'], 'event_parameters': _event_parameters, } for i in range(params['num_mixtures']) ] mix_func = make_one_mixture parallel_process(args, mix_func, n_jobs=num_workers, front_num=1, use_kwargs=True) logging.info(f'Synthesizing mixtures in parallel across {num_workers} threads') for key, params in mixture_parameters.items(): synthesize_mixtures_in_parallel(params['target_path'], num_workers)