|
14 | 14 |
|
15 | 15 | from pathlib import Path
|
16 | 16 |
|
| 17 | +import numpy as np |
17 | 18 | import pytest
|
18 | 19 |
|
19 |
| -from nemo.collections.tts.parts.utils.tts_dataset_utils import get_abs_rel_paths, get_audio_filepaths |
| 20 | +from nemo.collections.tts.parts.utils.tts_dataset_utils import get_abs_rel_paths, get_audio_filepaths, normalize_volume |
20 | 21 |
|
21 | 22 |
|
22 | 23 | class TestTTSDatasetUtils:
|
@@ -53,3 +54,68 @@ def test_get_audio_paths(self):
|
53 | 54 |
|
54 | 55 | assert abs_path == Path("/home/audio/examples/example.wav")
|
55 | 56 | assert rel_path == audio_rel_path
|
| 57 | + |
| 58 | + @pytest.mark.run_only_on('CPU') |
| 59 | + @pytest.mark.unit |
| 60 | + def test_normalize_volume(self): |
| 61 | + input_audio = np.array([0.0, 0.1, 0.3, 0.5]) |
| 62 | + expected_output = np.array([0.0, 0.18, 0.54, 0.9]) |
| 63 | + |
| 64 | + output_audio = normalize_volume(audio=input_audio, volume_level=0.9) |
| 65 | + |
| 66 | + np.testing.assert_array_almost_equal(output_audio, expected_output) |
| 67 | + |
| 68 | + @pytest.mark.run_only_on('CPU') |
| 69 | + @pytest.mark.unit |
| 70 | + def test_normalize_volume_negative_peak(self): |
| 71 | + input_audio = np.array([0.0, 0.1, -0.3, -1.0, 0.5]) |
| 72 | + expected_output = np.array([0.0, 0.05, -0.15, -0.5, 0.25]) |
| 73 | + |
| 74 | + output_audio = normalize_volume(audio=input_audio, volume_level=0.5) |
| 75 | + |
| 76 | + np.testing.assert_array_almost_equal(output_audio, expected_output) |
| 77 | + |
| 78 | + @pytest.mark.run_only_on('CPU') |
| 79 | + @pytest.mark.unit |
| 80 | + def test_normalize_volume_zero(self): |
| 81 | + input_audio = np.array([0.0, 0.1, 0.3, 0.5]) |
| 82 | + expected_output = np.array([0.0, 0.0, 0.0, 0.0]) |
| 83 | + |
| 84 | + output_audio = normalize_volume(audio=input_audio, volume_level=0.0) |
| 85 | + |
| 86 | + np.testing.assert_array_almost_equal(output_audio, expected_output) |
| 87 | + |
| 88 | + @pytest.mark.run_only_on('CPU') |
| 89 | + @pytest.mark.unit |
| 90 | + def test_normalize_volume_max(self): |
| 91 | + input_audio = np.array([0.0, 0.1, 0.3, 0.5]) |
| 92 | + expected_output = np.array([0.0, 0.2, 0.6, 1.0]) |
| 93 | + |
| 94 | + output_audio = normalize_volume(audio=input_audio, volume_level=1.0) |
| 95 | + |
| 96 | + np.testing.assert_array_almost_equal(output_audio, expected_output) |
| 97 | + |
| 98 | + @pytest.mark.run_only_on('CPU') |
| 99 | + @pytest.mark.unit |
| 100 | + def test_normalize_volume_zeros(self): |
| 101 | + input_audio = np.array([0.0, 0.0, 0.0]) |
| 102 | + |
| 103 | + output_audio = normalize_volume(audio=input_audio, volume_level=0.5) |
| 104 | + |
| 105 | + np.testing.assert_array_almost_equal(output_audio, input_audio) |
| 106 | + |
| 107 | + @pytest.mark.run_only_on('CPU') |
| 108 | + @pytest.mark.unit |
| 109 | + def test_normalize_volume_empty(self): |
| 110 | + input_audio = np.array([]) |
| 111 | + |
| 112 | + output_audio = normalize_volume(audio=input_audio, volume_level=1.0) |
| 113 | + |
| 114 | + np.testing.assert_array_almost_equal(output_audio, input_audio) |
| 115 | + |
| 116 | + @pytest.mark.run_only_on('CPU') |
| 117 | + @pytest.mark.unit |
| 118 | + def test_normalize_volume_out_of_range(self): |
| 119 | + input_audio = np.array([0.0, 0.1, 0.3, 0.5]) |
| 120 | + with pytest.raises(ValueError, match="Volume must be in range"): |
| 121 | + normalize_volume(audio=input_audio, volume_level=2.0) |
0 commit comments