Skip to content

Commit

Permalink
Merge pull request #351 from iver56/ij/update-add-short-noises
Browse files Browse the repository at this point in the history
Remove deprecated _in_db args in AddShortNoises. Update validation.
  • Loading branch information
iver56 authored Sep 26, 2024
2 parents 36ef386 + 3e0363b commit 223bcfd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 52 deletions.
51 changes: 9 additions & 42 deletions audiomentations/augmentations/add_short_noises.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions docs/waveform_transforms/add_short_noises.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ 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)
```

=== "Absolute RMS"

```python
from audiomentations import AddShortNoises, PolarityInversion

transform = AddShortNoises(
sounds_path="/path/to/folder_with_sound_files",
min_absolute_noise_rms_db=-50.0,
Expand All @@ -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)
```

Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/test_add_short_noises.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

0 comments on commit 223bcfd

Please sign in to comment.