def test_close_temp_files(): ''' Create a bunch of temp files and then make sure they've been closed and deleted. ''' # With delete=True tmpfiles = [] with _close_temp_files(tmpfiles): for _ in range(5): tmpfiles.append( tempfile.NamedTemporaryFile(suffix='.wav', delete=True)) for tf in tmpfiles: assert tf.file.closed assert not os.path.isfile(tf.name) # With delete=False tmpfiles = [] with _close_temp_files(tmpfiles): for _ in range(5): tmpfiles.append( tempfile.NamedTemporaryFile(suffix='.wav', delete=False)) for tf in tmpfiles: assert tf.file.closed assert not os.path.isfile(tf.name)
def test_match_sample_length(): durations_to_match = [1, 2, 5, 7, 22500, 44100, 88200, 100001] invalid_durations_to_match = [0, -1, .5, 1.0] tmpfiles = [] with _close_temp_files(tmpfiles): carhorn = tempfile.NamedTemporaryFile(suffix='.wav', delete=True) shutil.copyfile(CARHORN_FILE, carhorn.name) tmpfiles.append(carhorn) siren = tempfile.NamedTemporaryFile(suffix='.wav', delete=True) shutil.copyfile(SIREN_FILE, siren.name) tmpfiles.append(siren) for _duration in durations_to_match: match_sample_length(carhorn.name, _duration) carhorn_audio, _ = sf.read(carhorn.name) assert carhorn_audio.shape[0] == _duration match_sample_length(siren.name, _duration) siren_audio, _ = sf.read(siren.name) assert siren_audio.shape[0] == _duration # should be summable summed_events = sum([carhorn_audio, siren_audio]) assert summed_events.shape[0] == _duration for _duration in invalid_durations_to_match: pytest.raises(ScaperError, match_sample_length, carhorn.name, _duration) pytest.raises(ScaperError, match_sample_length, siren.name, _duration)
def test_close_temp_files(): ''' Create a bunch of temp files and then make sure they've been closed and deleted. ''' # With delete=True tmpfiles = [] with _close_temp_files(tmpfiles): for _ in range(5): tmpfiles.append( tempfile.NamedTemporaryFile(suffix='.wav', delete=True)) for tf in tmpfiles: assert tf.file.closed assert not os.path.isfile(tf.name) # With delete=False tmpfiles = [] with _close_temp_files(tmpfiles): for _ in range(5): tmpfiles.append( tempfile.NamedTemporaryFile(suffix='.wav', delete=False)) for tf in tmpfiles: assert tf.file.closed assert not os.path.isfile(tf.name) # with an exception before exiting try: tmpfiles = [] with _close_temp_files(tmpfiles): tmpfiles.append( tempfile.NamedTemporaryFile(suffix='.wav', delete=True)) raise ScaperError except ScaperError: for tf in tmpfiles: assert tf.file.closed assert not os.path.isfile(tf.name) else: assert False, 'Exception was not reraised.'
def test_generate_audio(atol=1e-4, rtol=1e-8): # Regression test: same spec, same audio (not this will fail if we update # any of the audio processing techniques used (e.g. change time stretching # algorithm. sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) sc.ref_db = -50 # background sc.add_background( label=('const', 'park'), source_file=('const', 'tests/data/audio/background/park/' '268903__yonts__city-park-tel-aviv-israel.wav'), source_time=('const', 0)) # foreground events sc.add_event(label=('const', 'siren'), source_file=('const', 'tests/data/audio/foreground/' 'siren/69-Siren-1.wav'), source_time=('const', 5), event_time=('const', 2), event_duration=('const', 5), snr=('const', 5), pitch_shift=None, time_stretch=None) sc.add_event(label=('const', 'car_horn'), source_file=('const', 'tests/data/audio/foreground/' 'car_horn/17-CAR-Rolls-Royce-Horn.wav'), source_time=('const', 0), event_time=('const', 5), event_duration=('const', 2), snr=('const', 20), pitch_shift=('const', 1), time_stretch=None) sc.add_event( label=('const', 'human_voice'), source_file=('const', 'tests/data/audio/foreground/' 'human_voice/42-Human-Vocal-Voice-taxi-2_edit.wav'), source_time=('const', 0), event_time=('const', 7), event_duration=('const', 2), snr=('const', 10), pitch_shift=None, time_stretch=('const', 1.2)) tmpfiles = [] with _close_temp_files(tmpfiles): wav_file = tempfile.NamedTemporaryFile(suffix='.wav', delete=True) tmpfiles.append(wav_file) jam = sc._instantiate(disable_instantiation_warnings=True) sc._generate_audio(wav_file.name, jam.annotations[0]) # validate audio wav, sr = soundfile.read(wav_file.name) regwav, sr = soundfile.read(REG_WAV_PATH) assert np.allclose(wav, regwav, atol=atol, rtol=rtol) # with reverb sc._generate_audio(wav_file.name, jam.annotations[0], reverb=0.2) # validate audio wav, sr = soundfile.read(wav_file.name) regwav, sr = soundfile.read(REG_REVERB_WAV_PATH) assert np.allclose(wav, regwav, atol=atol, rtol=rtol) # Don't disable sox warnings (just to cover line) sc._generate_audio(wav_file.name, jam.annotations[0], disable_sox_warnings=False) # validate audio wav, sr = soundfile.read(wav_file.name) regwav, sr = soundfile.read(REG_WAV_PATH) assert np.allclose(wav, regwav, atol=atol, rtol=rtol) # namespace must be scaper jam.annotations[0].namespace = 'tag_open' pytest.raises(ScaperError, sc._generate_audio, wav_file.name, jam.annotations[0]) # unsupported event role must raise error jam.annotations[0].namespace = 'scaper' jam.annotations[0].data[3].value['role'] = 'ewok' pytest.raises(ScaperError, sc._generate_audio, wav_file.name, jam.annotations[0]) # soundscape with no events will raise warning and won't generate audio sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) sc.ref_db = -50 jam = sc._instantiate(disable_instantiation_warnings=True) pytest.warns(ScaperWarning, sc._generate_audio, wav_file.name, jam.annotations[0]) # soundscape with only one event will use transformer (regression test) sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) sc.ref_db = -20 # background sc.add_background( label=('const', 'park'), source_file=('const', 'tests/data/audio/background/park/' '268903__yonts__city-park-tel-aviv-israel.wav'), source_time=('const', 0)) jam = sc._instantiate(disable_instantiation_warnings=True) sc._generate_audio(wav_file.name, jam.annotations[0], reverb=0.2) # validate audio wav, sr = soundfile.read(wav_file.name) regwav, sr = soundfile.read(REG_BGONLY_WAV_PATH) assert np.allclose(wav, regwav, atol=atol, rtol=rtol)
def test_trim(atol=1e-5, rtol=1e-8): # Things we want to test: # 1. Jam trimmed correctly (mainly handled by jams.slice) # 2. value dict updated correctly (event_time, event_duration, source_time) # 3. scaper sandbox updated correctly (n_events, poly, gini, duration) # 4. audio trimmed correctly 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) trim_wav_file = tempfile.NamedTemporaryFile(suffix='.wav', delete=True) trim_jam_file = tempfile.NamedTemporaryFile(suffix='.jams', delete=True) trimstrict_wav_file = tempfile.NamedTemporaryFile(suffix='.wav', delete=True) trimstrict_jam_file = tempfile.NamedTemporaryFile(suffix='.jams', delete=True) tmpfiles.append(orig_wav_file) tmpfiles.append(orig_jam_file) tmpfiles.append(trim_wav_file) tmpfiles.append(trim_jam_file) tmpfiles.append(trimstrict_wav_file) tmpfiles.append(trimstrict_jam_file) # --- Create soundscape and save to tempfiles --- # sc = scaper.Scaper(10, FG_PATH, BG_PATH) sc.protected_labels = [] sc.ref_db = -50 sc.add_background(label=('const', 'park'), source_file=('choose', []), source_time=('const', 0)) # Add 5 events start_times = [0.5, 2.5, 4.5, 6.5, 8.5] for event_time in start_times: sc.add_event(label=('const', 'siren'), source_file=('choose', []), source_time=('const', 5), event_time=('const', event_time), event_duration=('const', 1), snr=('const', 10), pitch_shift=None, time_stretch=None) sc.generate(orig_wav_file.name, orig_jam_file.name, disable_instantiation_warnings=True) # --- Trim soundscape using scaper.trim with strict=False --- # scaper.trim(orig_wav_file.name, orig_jam_file.name, trim_wav_file.name, trim_jam_file.name, 3, 7, no_audio=False) # --- Validate output --- # # validate JAMS trimjam = jams.load(trim_jam_file.name) trimann = trimjam.annotations.search(namespace='scaper')[0] # Time and duration of annotation observation must be changed, but # values in the value dict must remained unchanged! for event in trimann.data: if event.value['role'] == 'background': assert (event.time == 0 and event.duration == 4 and event.value['event_time'] == 0 and event.value['event_duration'] == 10 and event.value['source_time'] == 0) else: if event.time == 0: assert (event.duration == 0.5 and event.value['event_time'] == 2.5 and event.value['event_duration'] == 1 and event.value['source_time'] == 5) elif event.time == 1.5: assert (event.duration == 1 and event.value['event_time'] == 4.5 and event.value['event_duration'] == 1 and event.value['source_time'] == 5) elif event.time == 3.5: assert (event.duration == 0.5 and event.value['event_time'] == 6.5 and event.value['event_duration'] == 1 and event.value['source_time'] == 5) else: assert False # validate audio orig_wav, sr = soundfile.read(orig_wav_file.name) trim_wav, sr = soundfile.read(trim_wav_file.name) assert np.allclose(trim_wav, orig_wav[3 * sr:7 * sr], atol=atol, rtol=rtol)
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
def test_generate(atol=1e-4, rtol=1e-8): # Final regression test on all files sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) sc.ref_db = -50 # background sc.add_background( label=('const', 'park'), source_file=('const', 'tests/data/audio/background/park/' '268903__yonts__city-park-tel-aviv-israel.wav'), source_time=('const', 0)) # foreground events sc.add_event(label=('const', 'siren'), source_file=('const', 'tests/data/audio/foreground/' 'siren/69-Siren-1.wav'), source_time=('const', 5), event_time=('const', 2), event_duration=('const', 5), snr=('const', 5), pitch_shift=None, time_stretch=None) sc.add_event(label=('const', 'car_horn'), source_file=('const', 'tests/data/audio/foreground/' 'car_horn/17-CAR-Rolls-Royce-Horn.wav'), source_time=('const', 0), event_time=('const', 5), event_duration=('const', 2), snr=('const', 20), pitch_shift=('const', 1), time_stretch=None) sc.add_event( label=('const', 'human_voice'), source_file=('const', 'tests/data/audio/foreground/' 'human_voice/42-Human-Vocal-Voice-taxi-2_edit.wav'), source_time=('const', 0), event_time=('const', 7), event_duration=('const', 2), snr=('const', 10), pitch_shift=None, time_stretch=('const', 1.2)) tmpfiles = [] with _close_temp_files(tmpfiles): wav_file = tempfile.NamedTemporaryFile(suffix='.wav', delete=True) jam_file = tempfile.NamedTemporaryFile(suffix='.jams', delete=True) txt_file = tempfile.NamedTemporaryFile(suffix='.txt', delete=True) tmpfiles.append(wav_file) tmpfiles.append(jam_file) tmpfiles.append(txt_file) sc.generate(wav_file.name, jam_file.name, txt_path=txt_file.name, disable_instantiation_warnings=True) # validate audio wav, sr = soundfile.read(wav_file.name) regwav, sr = soundfile.read(REG_WAV_PATH) assert np.allclose(wav, regwav, atol=atol, rtol=rtol) # validate jams jam = jams.load(jam_file.name) regjam = jams.load(REG_JAM_PATH) _compare_scaper_jams(jam, regjam) # validate txt # read in both files txt_data = [] with open(txt_file.name) as file: reader = csv.reader(file, delimiter='\t') for row in reader: txt_data.append(row) txt_data = np.asarray(txt_data) regtxt_data = [] with open(REG_TXT_PATH) as file: reader = csv.reader(file, delimiter='\t') for row in reader: regtxt_data.append(row) regtxt_data = np.asarray(regtxt_data) # compare start and end times assert np.allclose([float(x) for x in txt_data[:, 0]], [float(x) for x in regtxt_data[:, 0]]) assert np.allclose([float(x) for x in txt_data[:, 1]], [float(x) for x in regtxt_data[:, 1]]) # compare labels assert (txt_data[:, 2] == regtxt_data[:, 2]).all() # reverb value must be in (0, 1) range for reverb in [-1, 2]: pytest.raises(ScaperError, sc.generate, wav_file.name, jam_file.name, reverb=reverb, disable_instantiation_warnings=True)
def _test_generate_audio(SR, REG_WAV_PATH, REG_BGONLY_WAV_PATH, REG_REVERB_WAV_PATH, atol=1e-4, rtol=1e-8): # Regression test: same spec, same audio (not this will fail if we update # any of the audio processing techniques used (e.g. change time stretching # algorithm. sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) sc.ref_db = -50 sc.sr = SR # background sc.add_background( label=('const', 'park'), source_file=( 'const', 'tests/data/audio/background/park/' '268903__yonts__city-park-tel-aviv-israel.wav'), source_time=('const', 0)) # foreground events sc.add_event( label=('const', 'siren'), source_file=('const', 'tests/data/audio/foreground/' 'siren/69-Siren-1.wav'), source_time=('const', 5), event_time=('const', 2), event_duration=('const', 5), snr=('const', 5), pitch_shift=None, time_stretch=None) sc.add_event( label=('const', 'car_horn'), source_file=('const', 'tests/data/audio/foreground/' 'car_horn/17-CAR-Rolls-Royce-Horn.wav'), source_time=('const', 0), event_time=('const', 5), event_duration=('const', 2), snr=('const', 20), pitch_shift=('const', 1), time_stretch=None) sc.add_event( label=('const', 'human_voice'), source_file=('const', 'tests/data/audio/foreground/' 'human_voice/42-Human-Vocal-Voice-taxi-2_edit.wav'), source_time=('const', 0), event_time=('const', 7), event_duration=('const', 2), snr=('const', 10), pitch_shift=None, time_stretch=('const', 1.2)) tmpfiles = [] with _close_temp_files(tmpfiles): wav_file = tempfile.NamedTemporaryFile(suffix='.wav', delete=True) tmpfiles.append(wav_file) jam = sc._instantiate(disable_instantiation_warnings=True) sc._generate_audio(wav_file.name, jam.annotations[0]) # validate audio wav, sr = soundfile.read(wav_file.name) regwav, sr = soundfile.read(REG_WAV_PATH) assert np.allclose(wav, regwav, atol=atol, rtol=rtol) # with reverb sc._generate_audio(wav_file.name, jam.annotations[0], reverb=0.2) # validate audio wav, sr = soundfile.read(wav_file.name) regwav, sr = soundfile.read(REG_REVERB_WAV_PATH) assert np.allclose(wav, regwav, atol=atol, rtol=rtol) # Don't disable sox warnings (just to cover line) sc._generate_audio(wav_file.name, jam.annotations[0], disable_sox_warnings=False) # validate audio wav, sr = soundfile.read(wav_file.name) regwav, sr = soundfile.read(REG_WAV_PATH) assert np.allclose(wav, regwav, atol=atol, rtol=rtol) # namespace must be scaper jam.annotations[0].namespace = 'tag_open' pytest.raises(ScaperError, sc._generate_audio, wav_file.name, jam.annotations[0]) # unsupported event role must raise error jam.annotations[0].namespace = 'scaper' jam.annotations[0].data[3].value['role'] = 'ewok' pytest.raises(ScaperError, sc._generate_audio, wav_file.name, jam.annotations[0]) # soundscape with no events will raise warning and won't generate audio sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) sc.ref_db = -50 jam = sc._instantiate(disable_instantiation_warnings=True) pytest.warns(ScaperWarning, sc._generate_audio, wav_file.name, jam.annotations[0]) # soundscape with only one event will use transformer (regression test) sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) sc.ref_db = -20 sc.sr = SR # background sc.add_background( label=('const', 'park'), source_file=('const', 'tests/data/audio/background/park/' '268903__yonts__city-park-tel-aviv-israel.wav'), source_time=('const', 0)) reverb = 0.2 jam = sc._instantiate(disable_instantiation_warnings=True, reverb=reverb) sc._generate_audio(wav_file.name, jam.annotations[0], reverb=reverb) # validate audio wav, sr = soundfile.read(wav_file.name) regwav, sr = soundfile.read(REG_BGONLY_WAV_PATH) assert np.allclose(wav, regwav, atol=atol, rtol=rtol)
def test_trim(atol=1e-5, rtol=1e-8): # Things we want to test: # 1. Jam trimmed correctly (mainly handled by jams.slice) # 2. value dict updated correctly (event_time, event_duration, source_time) # 3. scaper sandbox updated correctly (n_events, poly, gini, duration) # 4. audio trimmed correctly 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) trim_wav_file = tempfile.NamedTemporaryFile(suffix='.wav', delete=True) trim_jam_file = tempfile.NamedTemporaryFile(suffix='.jams', delete=True) trimstrict_wav_file = tempfile.NamedTemporaryFile( suffix='.wav', delete=True) trimstrict_jam_file = tempfile.NamedTemporaryFile( suffix='.jams', delete=True) tmpfiles.append(orig_wav_file) tmpfiles.append(orig_jam_file) tmpfiles.append(trim_wav_file) tmpfiles.append(trim_jam_file) tmpfiles.append(trimstrict_wav_file) tmpfiles.append(trimstrict_jam_file) # --- Create soundscape and save to tempfiles --- # sc = scaper.Scaper(10, FG_PATH, BG_PATH) sc.protected_labels = [] sc.ref_db = -50 sc.add_background(label=('const', 'park'), source_file=('choose', []), source_time=('const', 0)) # Add 5 events start_times = [0.5, 2.5, 4.5, 6.5, 8.5] for event_time in start_times: sc.add_event(label=('const', 'siren'), source_file=('choose', []), source_time=('const', 5), event_time=('const', event_time), event_duration=('const', 1), snr=('const', 10), pitch_shift=None, time_stretch=None) sc.generate(orig_wav_file.name, orig_jam_file.name, disable_instantiation_warnings=True) # --- Trim soundscape using scaper.trim with strict=False --- # scaper.trim(orig_wav_file.name, orig_jam_file.name, trim_wav_file.name, trim_jam_file.name, 3, 7, no_audio=False) # --- Validate output --- # # validate JAMS trimjam = jams.load(trim_jam_file.name) trimann = trimjam.annotations.search(namespace='scaper')[0] # Time and duration of annotation observation must be changed, but # values in the value dict must remained unchanged! for event in trimann.data: if event.value['role'] == 'background': assert (event.time == 0 and event.duration == 4 and event.value['event_time'] == 0 and event.value['event_duration'] == 10 and event.value['source_time'] == 0) else: if event.time == 0: assert (event.duration == 0.5 and event.value['event_time'] == 2.5 and event.value['event_duration'] == 1 and event.value['source_time'] == 5) elif event.time == 1.5: assert (event.duration == 1 and event.value['event_time'] == 4.5 and event.value['event_duration'] == 1 and event.value['source_time'] == 5) elif event.time == 3.5: assert (event.duration == 0.5 and event.value['event_time'] == 6.5 and event.value['event_duration'] == 1 and event.value['source_time'] == 5) else: assert False # validate audio orig_wav, sr = soundfile.read(orig_wav_file.name) trim_wav, sr = soundfile.read(trim_wav_file.name) assert np.allclose(trim_wav, orig_wav[3*sr:7*sr], atol=atol, rtol=rtol)
def _test_generate(SR, REG_WAV_PATH, REG_JAM_PATH, REG_TXT_PATH, atol=1e-4, rtol=1e-8): # Final regression test on all files sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) sc.ref_db = -50 sc.sr = SR # background sc.add_background( label=('const', 'park'), source_file=( 'const', 'tests/data/audio/background/park/' '268903__yonts__city-park-tel-aviv-israel.wav'), source_time=('const', 0)) # foreground events sc.add_event( label=('const', 'siren'), source_file=('const', 'tests/data/audio/foreground/' 'siren/69-Siren-1.wav'), source_time=('const', 5), event_time=('const', 2), event_duration=('const', 5), snr=('const', 5), pitch_shift=None, time_stretch=None) sc.add_event( label=('const', 'car_horn'), source_file=('const', 'tests/data/audio/foreground/' 'car_horn/17-CAR-Rolls-Royce-Horn.wav'), source_time=('const', 0), event_time=('const', 5), event_duration=('const', 2), snr=('const', 20), pitch_shift=('const', 1), time_stretch=None) sc.add_event( label=('const', 'human_voice'), source_file=('const', 'tests/data/audio/foreground/' 'human_voice/42-Human-Vocal-Voice-taxi-2_edit.wav'), source_time=('const', 0), event_time=('const', 7), event_duration=('const', 2), snr=('const', 10), pitch_shift=None, time_stretch=('const', 1.2)) tmpfiles = [] with _close_temp_files(tmpfiles): wav_file = tempfile.NamedTemporaryFile(suffix='.wav', delete=True) jam_file = tempfile.NamedTemporaryFile(suffix='.jams', delete=True) txt_file = tempfile.NamedTemporaryFile(suffix='.txt', delete=True) tmpfiles.append(wav_file) tmpfiles.append(jam_file) tmpfiles.append(txt_file) sc.generate(wav_file.name, jam_file.name, txt_path=txt_file.name, disable_instantiation_warnings=True) # validate audio wav, sr = soundfile.read(wav_file.name) regwav, sr = soundfile.read(REG_WAV_PATH) assert np.allclose(wav, regwav, atol=atol, rtol=rtol) # validate jams jam = jams.load(jam_file.name) regjam = jams.load(REG_JAM_PATH) _compare_scaper_jams(jam, regjam) # validate txt # read in both files txt_data = [] with open(txt_file.name) as file: reader = csv.reader(file, delimiter='\t') for row in reader: txt_data.append(row) txt_data = np.asarray(txt_data) regtxt_data = [] with open(REG_TXT_PATH) as file: reader = csv.reader(file, delimiter='\t') for row in reader: regtxt_data.append(row) regtxt_data = np.asarray(regtxt_data) # compare start and end times assert np.allclose([float(x) for x in txt_data[:, 0]], [float(x) for x in regtxt_data[:, 0]]) assert np.allclose([float(x) for x in txt_data[:, 1]], [float(x) for x in regtxt_data[:, 1]]) # compare labels assert (txt_data[:, 2] == regtxt_data[:, 2]).all() # reverb value must be in (0, 1) range for reverb in [-1, 2]: pytest.raises(ScaperError, sc.generate, wav_file.name, jam_file.name, reverb=reverb, disable_instantiation_warnings=True)
def test_generate(atol=1e-4, rtol=1e-8): # Final regression test on all files sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) sc.ref_db = -50 # background sc.add_background( label=('const', 'park'), source_file=('const', 'tests/data/audio/background/park/' '268903__yonts__city-park-tel-aviv-israel.wav'), source_time=('const', 0)) # foreground events sc.add_event(label=('const', 'siren'), source_file=('const', 'tests/data/audio/foreground/' 'siren/69-Siren-1.wav'), source_time=('const', 5), event_time=('const', 2), event_duration=('const', 5), snr=('const', 5), pitch_shift=None, time_stretch=None) sc.add_event(label=('const', 'car_horn'), source_file=('const', 'tests/data/audio/foreground/' 'car_horn/17-CAR-Rolls-Royce-Horn.wav'), source_time=('const', 0), event_time=('const', 5), event_duration=('const', 2), snr=('const', 20), pitch_shift=('const', 1), time_stretch=None) sc.add_event( label=('const', 'human_voice'), source_file=('const', 'tests/data/audio/foreground/' 'human_voice/42-Human-Vocal-Voice-taxi-2_edit.wav'), source_time=('const', 0), event_time=('const', 7), event_duration=('const', 2), snr=('const', 10), pitch_shift=None, time_stretch=('const', 1.2)) tmpfiles = [] with _close_temp_files(tmpfiles): wav_file = tempfile.NamedTemporaryFile(suffix='.wav', delete=True) jam_file = tempfile.NamedTemporaryFile(suffix='.jams', delete=True) txt_file = tempfile.NamedTemporaryFile(suffix='.txt', delete=True) tmpfiles.append(wav_file) tmpfiles.append(jam_file) tmpfiles.append(txt_file) sc.generate(wav_file.name, jam_file.name, txt_path=txt_file.name, disable_instantiation_warnings=True) # validate audio wav, sr = soundfile.read(wav_file.name) regwav, sr = soundfile.read(REG_WAV_PATH) assert np.allclose(wav, regwav, atol=atol, rtol=rtol) # validate jams jam = jams.load(jam_file.name) regjam = jams.load(REG_JAM_PATH) # version might change, rest should be the same regjam.annotations[0].sandbox.scaper['scaper_version'] = \ scaper.__version__ assert jam == regjam # validate txt txt = pd.read_csv(txt_file.name, header=None, sep='\t') regtxt = pd.read_csv(REG_TXT_PATH, header=None, sep='\t') assert (txt == regtxt).all().all() # reverb value must be in (0, 1) range for reverb in [-1, 2]: pytest.raises(ScaperError, sc.generate, wav_file.name, jam_file.name, reverb=reverb, disable_instantiation_warnings=True)