def test_scaper_add_event(): sc = scaper.Scaper(10.0, FG_PATH, BG_PATH) # Initially fg_spec should be empty assert sc.fg_spec == [] # Add one event sc.add_event(label=('const', 'siren'), source_file=('choose', []), source_time=('const', 0), event_time=('uniform', 0, 9), event_duration=('truncnorm', 2, 1, 1, 3), snr=('uniform', 10, 20), pitch_shift=('normal', 0, 1), time_stretch=('uniform', 0.8, 1.2)) # Now should be one event in fg_spec assert len(sc.fg_spec) == 1 fg_event_expected = EventSpec(label=('const', 'siren'), source_file=('choose', []), source_time=('const', 0), event_time=('uniform', 0, 9), event_duration=('truncnorm', 2, 1, 1, 3), snr=('uniform', 10, 20), role='foreground', pitch_shift=('normal', 0, 1), time_stretch=('uniform', 0.8, 1.2)) assert sc.fg_spec[0] == fg_event_expected
def __create_annotation_without_overlapping_events(n_events): ann = jams.Annotation(namespace='scaper') ann.duration = n_events * 10 for ind in range(n_events): instantiated_event = EventSpec(label='siren', source_file='/the/source/file.wav', source_time=0, event_time=ind * 10, event_duration=5, snr=0, role='foreground', pitch_shift=None, time_stretch=None) ann.append(time=ind * 10, duration=5, value=instantiated_event._asdict(), confidence=1.0) return ann
def test_scaper_add_background(): ''' Test Scaper.add_background function ''' sc = scaper.Scaper(10.0, FG_PATH, BG_PATH) # Set concrete background label # label, source_file, source_time sc.add_background(("const", "park"), ("choose", []), ("const", 0)) # Check that event has been added to the background spec, and that the # values that are set automatically by this method (event_time, # event_duration, snr and role) are correctly set to their expected values. bg_event_expected = EventSpec(label=("const", "park"), source_file=("choose", []), source_time=("const", 0), event_time=("const", 0), event_duration=("const", sc.duration), snr=("const", 0), role='background', pitch_shift=None, time_stretch=None) assert sc.bg_spec == [bg_event_expected]
def test_scaper_instantiate_event(): # GF EVENT TO WORK WITH fg_event = EventSpec(label=('const', 'siren'), source_file=('choose', []), source_time=('const', 0), event_time=('uniform', 0, 9), event_duration=('truncnorm', 2, 1, 1, 3), snr=('uniform', 10, 20), role='foreground', pitch_shift=('normal', 0, 1), time_stretch=('uniform', 0.8, 1.2)) # test valid case sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) instantiated_event = sc._instantiate_event( fg_event, isbackground=False, allow_repeated_label=True, allow_repeated_source=True, used_labels=[], used_source_files=[], disable_instantiation_warnings=True) assert instantiated_event.label == 'siren' assert instantiated_event.source_file == ( 'tests/data/audio/foreground/siren/69-Siren-1.wav') assert instantiated_event.source_time == 0 assert 0 <= instantiated_event.event_time <= 9 assert 1 <= instantiated_event.event_duration <= 3 assert 10 <= instantiated_event.snr <= 20 assert instantiated_event.role == 'foreground' assert scaper.util.is_real_number(instantiated_event.pitch_shift) assert 0.8 <= instantiated_event.time_stretch <= 1.2 # when a label needs to be replaced because it's used already fg_event8 = fg_event._replace(label=('choose', [])) # repeat several times to increase chance of hitting the line we need to # test for _ in range(20): instantiated_event = sc._instantiate_event( fg_event8, isbackground=False, allow_repeated_label=False, allow_repeated_source=True, used_labels=['siren', 'human_voice'], disable_instantiation_warnings=True) assert instantiated_event.label == 'car_horn' # when a source file needs to be replaced because it's used already fg_event9 = fg_event._replace(label=('const', 'human_voice')) # repeat several times to increase chance of hitting the line we need to # test for _ in range(20): instantiated_event = sc._instantiate_event( fg_event9, isbackground=False, allow_repeated_label=True, allow_repeated_source=False, used_labels=[], used_source_files=([ 'tests/data/audio/foreground/human_voice/' '42-Human-Vocal-Voice-all-aboard_edit.wav', 'tests/data/audio/foreground/human_voice/' '42-Human-Vocal-Voice-taxi-1_edit.wav' ]), disable_instantiation_warnings=True) assert instantiated_event.source_file == ( 'tests/data/audio/foreground/human_voice/' '42-Human-Vocal-Voice-taxi-2_edit.wav') # Protected labels must have original source duration and source time 0 sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH, protected_labels='human_voice') fg_event10 = fg_event._replace( 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.3), event_duration=('const', 0.4)) instantiated_event = sc._instantiate_event( fg_event10, disable_instantiation_warnings=True) assert instantiated_event.source_time == 0 assert instantiated_event.event_duration == 0.806236 # repeated label when not allowed throws error sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) pytest.raises(ScaperError, sc._instantiate_event, fg_event, isbackground=False, allow_repeated_label=False, allow_repeated_source=True, used_labels=['siren']) # repeated source when not allowed throws error pytest.raises(ScaperError, sc._instantiate_event, fg_event, isbackground=False, allow_repeated_label=True, allow_repeated_source=False, used_labels=['siren'], used_source_files=([ 'tests/data/audio/foreground/siren/69-Siren-1.wav' ])) # event duration longer than source duration: warning fg_event2 = fg_event._replace(label=('const', 'car_horn'), event_duration=('const', 5)) pytest.warns(ScaperWarning, sc._instantiate_event, fg_event2) # event duration longer than soundscape duration: warning fg_event3 = fg_event._replace(event_time=('const', 0), event_duration=('const', 15), time_stretch=None) pytest.warns(ScaperWarning, sc._instantiate_event, fg_event3) # stretched event duration longer than soundscape duration: warning fg_event4 = fg_event._replace(event_time=('const', 0), event_duration=('const', 6), time_stretch=('const', 2)) pytest.warns(ScaperWarning, sc._instantiate_event, fg_event4) # source_time + event_duration > source_duration: warning fg_event5 = fg_event._replace(event_time=('const', 0), event_duration=('const', 8), source_time=('const', 20)) pytest.warns(ScaperWarning, sc._instantiate_event, fg_event5) # event_time + event_duration > soundscape duration: warning fg_event6 = fg_event._replace(event_time=('const', 8), event_duration=('const', 5), time_stretch=None) pytest.warns(ScaperWarning, sc._instantiate_event, fg_event6) # event_time + stretched event_duration > soundscape duration: warning fg_event7 = fg_event._replace(event_time=('const', 5), event_duration=('const', 4), time_stretch=('const', 2)) pytest.warns(ScaperWarning, sc._instantiate_event, fg_event7)
def test_scaper_instantiate_event(): # GF EVENT TO WORK WITH fg_event = EventSpec(label=('const', 'siren'), source_file=('choose', []), source_time=('const', 0), event_time=('uniform', 0, 9), event_duration=('truncnorm', 2, 1, 1, 3), snr=('uniform', 10, 20), role='foreground', pitch_shift=('normal', 0, 1), time_stretch=('uniform', 0.8, 1.2)) # test valid case sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) instantiated_event = sc._instantiate_event( fg_event, isbackground=False, allow_repeated_label=True, allow_repeated_source=True, used_labels=[], used_source_files=[], disable_instantiation_warnings=True) assert instantiated_event.label == 'siren' assert instantiated_event.source_file == ( 'tests/data/audio/foreground/siren/69-Siren-1.wav') assert instantiated_event.source_time == 0 assert 0 <= instantiated_event.event_time <= 9 assert 1 <= instantiated_event.event_duration <= 3 assert 10 <= instantiated_event.snr <= 20 assert instantiated_event.role == 'foreground' assert scaper.util.is_real_number(instantiated_event.pitch_shift) assert 0.8 <= instantiated_event.time_stretch <= 1.2 # when a label needs to be replaced because it's used already fg_event8 = fg_event._replace(label=('choose', [])) # repeat several times to increase chance of hitting the line we need to # test for _ in range(20): instantiated_event = sc._instantiate_event( fg_event8, isbackground=False, allow_repeated_label=False, allow_repeated_source=True, used_labels=['siren', 'human_voice'], disable_instantiation_warnings=True) assert instantiated_event.label == 'car_horn' # when a source file needs to be replaced because it's used already fg_event9 = fg_event._replace(label=('const', 'human_voice')) # repeat several times to increase chance of hitting the line we need to # test for _ in range(20): instantiated_event = sc._instantiate_event( fg_event9, isbackground=False, allow_repeated_label=True, allow_repeated_source=False, used_labels=[], used_source_files=( ['tests/data/audio/foreground/human_voice/' '42-Human-Vocal-Voice-all-aboard_edit.wav', 'tests/data/audio/foreground/human_voice/' '42-Human-Vocal-Voice-taxi-1_edit.wav']), disable_instantiation_warnings=True) assert instantiated_event.source_file == ( 'tests/data/audio/foreground/human_voice/' '42-Human-Vocal-Voice-taxi-2_edit.wav') # Protected labels must have original source duration and source time 0 sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH, protected_labels='human_voice') fg_event10 = fg_event._replace( 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.3), event_duration=('const', 0.4)) instantiated_event = sc._instantiate_event( fg_event10, disable_instantiation_warnings=True) assert instantiated_event.source_time == 0 assert instantiated_event.event_duration == 0.806236 # repeated label when not allowed throws error sc = scaper.Scaper(10.0, fg_path=FG_PATH, bg_path=BG_PATH) pytest.raises(ScaperError, sc._instantiate_event, fg_event, isbackground=False, allow_repeated_label=False, allow_repeated_source=True, used_labels=['siren']) # repeated source when not allowed throws error pytest.raises(ScaperError, sc._instantiate_event, fg_event, isbackground=False, allow_repeated_label=True, allow_repeated_source=False, used_labels=['siren'], used_source_files=( ['tests/data/audio/foreground/siren/69-Siren-1.wav'])) # event duration longer than source duration: warning fg_event2 = fg_event._replace(label=('const', 'car_horn'), event_duration=('const', 5)) pytest.warns(ScaperWarning, sc._instantiate_event, fg_event2) # event duration longer than soundscape duration: warning fg_event3 = fg_event._replace(event_time=('const', 0), event_duration=('const', 15), time_stretch=None) pytest.warns(ScaperWarning, sc._instantiate_event, fg_event3) # stretched event duration longer than soundscape duration: warning fg_event4 = fg_event._replace(event_time=('const', 0), event_duration=('const', 6), time_stretch=('const', 2)) pytest.warns(ScaperWarning, sc._instantiate_event, fg_event4) # source_time + event_duration > source_duration: warning fg_event5 = fg_event._replace(event_time=('const', 0), event_duration=('const', 8), source_time=('const', 20)) pytest.warns(ScaperWarning, sc._instantiate_event, fg_event5) # event_time + event_duration > soundscape duration: warning fg_event6 = fg_event._replace(event_time=('const', 8), event_duration=('const', 5), time_stretch=None) pytest.warns(ScaperWarning, sc._instantiate_event, fg_event6) # event_time + stretched event_duration > soundscape duration: warning fg_event7 = fg_event._replace(event_time=('const', 5), event_duration=('const', 4), time_stretch=('const', 2)) pytest.warns(ScaperWarning, sc._instantiate_event, fg_event7)