diff --git a/audiomentations/augmentations/add_short_noises.py b/audiomentations/augmentations/add_short_noises.py index d78d3cce..09b35ad4 100644 --- a/audiomentations/augmentations/add_short_noises.py +++ b/audiomentations/augmentations/add_short_noises.py @@ -28,10 +28,8 @@ class AddShortNoises(BaseWaveformTransform): def __init__( self, sounds_path: Union[List[Path], List[str], Path, str], - min_snr_in_db: float = None, - max_snr_in_db: float = None, - min_snr_db: float = None, - max_snr_db: float = None, + min_snr_db: float = -6.0, + max_snr_db: float = 18.0, min_time_between_sounds: float = 2.0, max_time_between_sounds: float = 8.0, noise_rms: str = "relative_to_whole_input", @@ -58,8 +56,6 @@ def __init__( :param sounds_path: A path or list of paths to audio file(s) and/or folder(s) with audio files. Can be str or Path instance(s). The audio files given here are supposed to be (short) noises. - :param min_snr_in_db: Deprecated. Use min_snr_db instead. - :param max_snr_in_db: Deprecated. Use max_snr_db instead. :param min_snr_db: Minimum signal-to-noise ratio in dB. A lower value means the added sounds/noises will be louder. This gets ignored if noise_rms is set to "absolute". :param max_snr_db: Maximum signal-to-noise ratio in dB. A lower value means the added @@ -69,11 +65,11 @@ def __init__( :param noise_rms: Choices: ["absolute", "relative", "relative_to_whole_input"]. Defines how the noises will be added to the audio input. "relative": the RMS value of the added noise will be proportional to the RMS value of - the input sound calculated only for the region where the noise is added. + the input sound calculated only for the region where the noise is added. "absolute": the added noises will have an RMS independent of the RMS of the input audio - file. + file. "relative_to_whole_input": the RMS of the added noises will be - proportional to the RMS of the whole input sound. + proportional to the RMS of the whole input sound. :param min_absolute_noise_rms_db: Is only used if noise_rms is set to "absolute". It is the minimum RMS value in dB that the added noise can take. The lower the RMS is, the lower will the added sound be. @@ -144,39 +140,10 @@ def __init__( assert noise_rms in ["relative", "absolute", "relative_to_whole_input"] - if min_snr_db is not None and min_snr_in_db is not None: - raise ValueError( - "Passing both min_snr_db and min_snr_in_db is not supported. Use only" - " min_snr_db." - ) - elif min_snr_db is not None: - self.min_snr_db = min_snr_db - elif min_snr_in_db is not None: - warnings.warn( - "The min_snr_in_db parameter is deprecated. Use min_snr_db instead.", - DeprecationWarning, - ) - self.min_snr_db = min_snr_in_db - else: - self.min_snr_db = -6.0 # the default - - if max_snr_db is not None and max_snr_in_db is not None: - raise ValueError( - "Passing both max_snr_db and max_snr_in_db is not supported. Use only" - " max_snr_db." - ) - elif max_snr_db is not None: - self.max_snr_db = max_snr_db - elif max_snr_in_db is not None: - warnings.warn( - "The max_snr_in_db parameter is deprecated. Use max_snr_db instead.", - DeprecationWarning, - ) - self.max_snr_db = max_snr_in_db - else: - self.max_snr_db = 18.0 # the default - - assert self.min_snr_db <= self.max_snr_db + if min_snr_db > max_snr_db: + raise ValueError("min_snr_db must not be greater than max_snr_db") + self.min_snr_db = min_snr_db + self.max_snr_db = max_snr_db if ( signal_gain_db_during_noise is not None diff --git a/docs/waveform_transforms/add_short_noises.md b/docs/waveform_transforms/add_short_noises.md index 8ca1c1e7..5323725e 100644 --- a/docs/waveform_transforms/add_short_noises.md +++ b/docs/waveform_transforms/add_short_noises.md @@ -26,18 +26,18 @@ Here we add some short noise sounds to a voice recording. ```python from audiomentations import AddShortNoises, PolarityInversion - + transform = AddShortNoises( sounds_path="/path/to/folder_with_sound_files", - min_snr_in_db=3.0, - max_snr_in_db=30.0, + min_snr_db=3.0, + max_snr_db=30.0, noise_rms="relative_to_whole_input", min_time_between_sounds=2.0, max_time_between_sounds=8.0, noise_transform=PolarityInversion(), p=1.0 ) - + augmented_sound = transform(my_waveform_ndarray, sample_rate=16000) ``` @@ -45,7 +45,7 @@ Here we add some short noise sounds to a voice recording. ```python from audiomentations import AddShortNoises, PolarityInversion - + transform = AddShortNoises( sounds_path="/path/to/folder_with_sound_files", min_absolute_noise_rms_db=-50.0, @@ -56,7 +56,7 @@ Here we add some short noise sounds to a voice recording. noise_transform=PolarityInversion(), p=1.0 ) - + augmented_sound = transform(my_waveform_ndarray, sample_rate=16000) ``` @@ -67,11 +67,11 @@ Here we add some short noise sounds to a voice recording. with audio files. Can be str or Path instance(s). The audio files given here are supposed to be (short) noises. -[`min_snr_in_db`](#min_snr_in_db){ #min_snr_in_db }: `float` • unit: Decibel -: :warning: Deprecated as of v0.31.0. Use [`min_snr_db`](#min_snr_db) instead +~~[`min_snr_in_db`](#min_snr_in_db){ #min_snr_in_db }: `float` • unit: Decibel~~ +: :warning: Deprecated as of v0.31.0, removed as of v0.38.0. Use [`min_snr_db`](#min_snr_db) instead -[`max_snr_in_db`](#max_snr_in_db){ #max_snr_in_db }: `float` • unit: Decibel -: :warning: Deprecated as of v0.31.0. Use [`max_snr_db`](#max_snr_db) instead +~~[`max_snr_in_db`](#max_snr_in_db){ #max_snr_in_db }: `float` • unit: Decibel~~ +: :warning: Deprecated as of v0.31.0, removed as of v0.38.0. Use [`max_snr_db`](#max_snr_db) instead [`min_snr_db`](#min_snr_db){ #min_snr_db }: `float` • unit: Decibel : :octicons-milestone-24: Default: `-6.0`. Minimum signal-to-noise ratio in dB. A lower diff --git a/tests/test_add_short_noises.py b/tests/test_add_short_noises.py index 75a8e1cf..cebde98d 100644 --- a/tests/test_add_short_noises.py +++ b/tests/test_add_short_noises.py @@ -318,3 +318,11 @@ def test_add_noises_with_same_level(self): assert len(set(snr_sounds_same_level)) == 1 assert len(set(snr_sounds_different_level)) > 1 + + def test_validation(self): + with pytest.raises(ValueError): + AddShortNoises( + sounds_path=os.path.join(DEMO_DIR, "short_noises"), + min_snr_db=40.0, + max_snr_db=20.0, + )