def generate_new_no_bg_files(in_dir, out_dir, fg_path=None, bg_path=None): """ Generate the new JAMS and audio files with a different background SNR Args: new_snr: float, Sound to noise ratio (SNR) of the background from the reference in_dir: str, folder containing JAMS file with background SNR to be changed out_dir: str, folder where to save the new audio and JAMS Returns: """ logger = create_logger(__name__, "Desed.log") create_folder(out_dir) for jam_file in sorted(glob.glob(osp.join(in_dir, "*.jams"))): jams_obj = delete_bg(jam_file) out_jams = osp.join(out_dir, osp.basename(jam_file)) jams_obj.save(out_jams) audiofile = osp.join(out_dir, osp.splitext(osp.basename(jam_file))[0] + ".wav") logger.debug(audiofile) scaper.generate_from_jams(out_jams, audiofile, fg_path, bg_path, jams_outfile=out_jams)
def generate_audio(jam, anns, background_path, foreground_path, out_file_path): """The function generates an audio clip from a jam file given as input Args jam (JAMS): jam file anns (Annotations): annotations of the jam file background_path (str): path to background soundbank folder foreground_path (str): path to foreeground soundbank folder out_file_path (str): path to output file Returns: None """ jam.annotations.append(anns) jam.save(out_file_path) # generate the audio clip scaper.generate_from_jams( out_file_path, audio_outfile=out_file_path.replace(".jams", ".wav"), fg_path=foreground_path, bg_path=background_path, jams_outfile=out_file_path, save_isolated_events=False, txt_path=out_file_path.replace(".jams", ".txt"), )
def generate_files_from_jams(list_jams, out_folder, out_folder_jams=None, save_isolated_events=False, overwrite_exist_audio=False, **kwargs): """ Generate audio files from jams files generated by Scaper Args: list_jams: list, list of jams filepath generated by Scaper. out_folder: str, output path to save audio files. out_folder_jams: str, path to write the jams (could be modified by fg_path and bg_path for example), if None, jams not saved save_isolated_events: bool, whether or not to save isolated events in a separate folder overwrite_exist_audio: bool, whether to regenerate existing audio files or not kwargs: dict, scaper.generate_from_jams params (fg_path, bg_path, ...) Returns: None """ logger = create_logger(__name__ + "/" + inspect.currentframe().f_code.co_name) logger.info(f"generating audio files to {out_folder}") create_folder(out_folder) for n, jam_file in enumerate(list_jams): logger.debug(jam_file) audiofile = osp.join(out_folder, f"{osp.splitext(osp.basename(jam_file))[0]}.wav") if not osp.exists(audiofile) or overwrite_exist_audio: if out_folder_jams is not None: jams_outfile = osp.join(out_folder_jams, osp.basename(jam_file)) else: jams_outfile = None generate_from_jams(jam_file, audiofile, jams_outfile=jams_outfile, save_isolated_events=save_isolated_events, **kwargs) if n % 200 == 0: logger.info(f"generating {n} / {len(list_jams)} files (updated every 200)") logger.info("Done")
def generate_files(list_jams, outfolder, fg_path=None, bg_path=None, overwrite_jams=False): n = 0 for jam_file in list_jams: audiofile = osp.join(outfolder, f"{osp.splitext(osp.basename(jam_file))[0]}.wav") if overwrite_jams: jams_outfile = jam_file else: jams_outfile = None generate_from_jams(jam_file, audiofile, fg_path=fg_path, bg_path=bg_path, jams_outfile=jams_outfile) n += 1 if n % 500 == 0: print(f"generating {n} / {len(list_jams)} files") print("Done")
def generate_new_fg_onset_files(added_value, in_dir, out_dir): """ Generate the new JAMS and audio files adding a value to forground onsets Args: added_value: float, value in seconds, value to be added to previous onset in_dir: str, folder containing JAMS file with background SNR to be changed out_dir: str, folder where to save the new audio and JAMS Returns: """ for jam_file in sorted(glob.glob(os.path.join(in_dir, "*.jams"))): jams_obj = modify_fg_onset(added_value, jam_file) out_jams = osp.join(out_dir, os.path.basename(jam_file)) jams_obj.save(out_jams) audiofile = os.path.join(out_dir, osp.splitext(osp.basename(jam_file))[0] + ".wav") print(audiofile) scaper.generate_from_jams(out_jams, audiofile)
def generate_new_bg_snr_files(new_snr, in_dir, out_dir): """ Generate the new JAMS and audio files with a different background SNR Args: new_snr: float, Sound to noise ratio (SNR) of the background from the reference in_dir: str, folder containing JAMS file with background SNR to be changed out_dir: str, folder where to save the new audio and JAMS Returns: """ for jam_file in sorted(glob.glob(os.path.join(in_dir, "*.jams"))): jams_obj = modify_bg_snr(new_snr, jam_file) out_jams = osp.join(out_dir, os.path.basename(jam_file)) jams_obj.save(out_jams) audiofile = os.path.join( out_dir, osp.splitext(osp.basename(jam_file))[0] + ".wav") print(audiofile) scaper.generate_from_jams(out_jams, audiofile)
def synthesize_one_mixture(jams_file): wav_file = jams_file.replace('.jams', '.wav') generate_from_jams(jams_file, wav_file, save_sources=True)
def test_generate_from_jams(atol=1e-5, rtol=1e-8): # Test for invalid jams: no annotations tmpfiles = [] with _close_temp_files(tmpfiles): jam = jams.JAMS() jam.file_metadata.duration = 10 jam_file = tempfile.NamedTemporaryFile(suffix='.jams', delete=True) gen_file = tempfile.NamedTemporaryFile(suffix='.jams', delete=True) jam.save(jam_file.name) pytest.raises(ScaperError, scaper.generate_from_jams, jam_file.name, gen_file.name) # Test for valid jams files tmpfiles = [] with _close_temp_files(tmpfiles): # Create all necessary temp files orig_wav_file = tempfile.NamedTemporaryFile(suffix='.wav', delete=True) orig_jam_file = tempfile.NamedTemporaryFile(suffix='.jams', delete=True) gen_wav_file = tempfile.NamedTemporaryFile(suffix='.wav', delete=True) gen_jam_file = tempfile.NamedTemporaryFile(suffix='.jams', delete=True) tmpfiles.append(orig_wav_file) tmpfiles.append(orig_jam_file) tmpfiles.append(gen_wav_file) tmpfiles.append(gen_jam_file) # --- Define scaper --- * sc = scaper.Scaper(10, FG_PATH, BG_PATH) sc.protected_labels = [] sc.ref_db = -50 sc.add_background(label=('choose', []), source_file=('choose', []), source_time=('const', 0)) # Add 5 events for _ in range(5): sc.add_event(label=('choose', []), source_file=('choose', []), source_time=('const', 0), event_time=('uniform', 0, 9), event_duration=('choose', [1, 2, 3]), snr=('uniform', 10, 20), pitch_shift=('uniform', -1, 1), time_stretch=('uniform', 0.8, 1.2)) # generate, then generate from the jams and compare audio files # repeat 5 time for _ in range(5): sc.generate(orig_wav_file.name, orig_jam_file.name, disable_instantiation_warnings=True) scaper.generate_from_jams(orig_jam_file.name, gen_wav_file.name) # validate audio orig_wav, sr = soundfile.read(orig_wav_file.name) gen_wav, sr = soundfile.read(gen_wav_file.name) assert np.allclose(gen_wav, orig_wav, atol=atol, rtol=rtol) # Now add in trimming! for _ in range(5): sc.generate(orig_wav_file.name, orig_jam_file.name, disable_instantiation_warnings=True) scaper.trim(orig_wav_file.name, orig_jam_file.name, orig_wav_file.name, orig_jam_file.name, np.random.uniform(0, 5), np.random.uniform(5, 10)) scaper.generate_from_jams(orig_jam_file.name, gen_wav_file.name) # validate audio orig_wav, sr = soundfile.read(orig_wav_file.name) gen_wav, sr = soundfile.read(gen_wav_file.name) assert np.allclose(gen_wav, orig_wav, atol=atol, rtol=rtol) # Double trimming for _ in range(2): sc.generate(orig_wav_file.name, orig_jam_file.name, disable_instantiation_warnings=True) scaper.trim(orig_wav_file.name, orig_jam_file.name, orig_wav_file.name, orig_jam_file.name, np.random.uniform(0, 2), np.random.uniform(8, 10)) scaper.trim(orig_wav_file.name, orig_jam_file.name, orig_wav_file.name, orig_jam_file.name, np.random.uniform(0, 2), np.random.uniform(4, 6)) scaper.generate_from_jams(orig_jam_file.name, gen_wav_file.name) # Tripple trimming for _ in range(2): sc.generate(orig_wav_file.name, orig_jam_file.name, disable_instantiation_warnings=True) scaper.trim(orig_wav_file.name, orig_jam_file.name, orig_wav_file.name, orig_jam_file.name, np.random.uniform(0, 2), np.random.uniform(8, 10)) scaper.trim(orig_wav_file.name, orig_jam_file.name, orig_wav_file.name, orig_jam_file.name, np.random.uniform(0, 1), np.random.uniform(5, 6)) scaper.trim(orig_wav_file.name, orig_jam_file.name, orig_wav_file.name, orig_jam_file.name, np.random.uniform(0, 1), np.random.uniform(3, 4)) scaper.generate_from_jams(orig_jam_file.name, gen_wav_file.name) # validate audio orig_wav, sr = soundfile.read(orig_wav_file.name) gen_wav, sr = soundfile.read(gen_wav_file.name) assert np.allclose(gen_wav, orig_wav, atol=atol, rtol=rtol) # Test with new FG and BG paths for _ in range(5): sc.generate(orig_wav_file.name, orig_jam_file.name, disable_instantiation_warnings=True) scaper.generate_from_jams(orig_jam_file.name, gen_wav_file.name, fg_path=ALT_FG_PATH, bg_path=ALT_BG_PATH) # validate audio orig_wav, sr = soundfile.read(orig_wav_file.name) gen_wav, sr = soundfile.read(gen_wav_file.name) assert np.allclose(gen_wav, orig_wav, atol=atol, rtol=rtol) # Ensure jam file saved correctly scaper.generate_from_jams(orig_jam_file.name, gen_wav_file.name, jams_outfile=gen_jam_file.name) orig_jam = jams.load(orig_jam_file.name) gen_jam = jams.load(gen_jam_file.name) assert orig_jam == gen_jam