|
1 |
| -import numpy as np |
| 1 | +import librosa |
2 | 2 | import torch
|
3 |
| -import torchaudio |
4 | 3 | import pytest
|
| 4 | +import numpy as np |
5 | 5 |
|
6 | 6 | from torchaudio_augmentations import (
|
7 | 7 | Compose,
|
@@ -162,14 +162,45 @@ def test_pitch_shift_fast_ratios():
|
162 | 162 |
|
163 | 163 | def test_pitch_shift_no_fast_ratios():
|
164 | 164 | with pytest.raises(ValueError):
|
165 |
| - ps = PitchShift( |
| 165 | + _ = PitchShift( |
166 | 166 | n_samples=num_samples,
|
167 | 167 | sample_rate=sample_rate,
|
168 | 168 | pitch_shift_min=4,
|
169 | 169 | pitch_shift_max=4,
|
170 | 170 | )
|
171 | 171 |
|
172 | 172 |
|
| 173 | +def test_pitch_shift_transform_with_pitch_detection(): |
| 174 | + """To check semi-tone values, check: http://www.homepages.ucl.ac.uk/~sslyjjt/speech/semitone.html""" |
| 175 | + |
| 176 | + source_frequency = 440 |
| 177 | + max_semitone_shift = 4 |
| 178 | + expected_frequency_shift = 554 |
| 179 | + |
| 180 | + num_channels = 1 |
| 181 | + audio = generate_waveform( |
| 182 | + sample_rate, num_samples, num_channels, frequency=source_frequency |
| 183 | + ) |
| 184 | + pitch_shift = PitchShift( |
| 185 | + n_samples=num_samples, |
| 186 | + sample_rate=sample_rate, |
| 187 | + pitch_shift_min=max_semitone_shift, |
| 188 | + pitch_shift_max=max_semitone_shift + 1, |
| 189 | + ) |
| 190 | + |
| 191 | + t_audio = pitch_shift(audio) |
| 192 | + librosa_audio = t_audio[0].numpy() |
| 193 | + f0_hz, _, _ = librosa.pyin(librosa_audio, fmin=10, fmax=1000) |
| 194 | + |
| 195 | + # remove nan values: |
| 196 | + f0_hz = f0_hz[~np.isnan(f0_hz)] |
| 197 | + |
| 198 | + detected_f0_hz = np.max(f0_hz) |
| 199 | + |
| 200 | + # the detected frequency vs. expected frequency should not be smaller than 20Hz. |
| 201 | + assert abs(detected_f0_hz - expected_frequency_shift) < 20 |
| 202 | + |
| 203 | + |
173 | 204 | @pytest.mark.parametrize("num_channels", [1, 2])
|
174 | 205 | def test_reverb(num_channels):
|
175 | 206 | audio = generate_waveform(sample_rate, num_samples, num_channels)
|
|
0 commit comments