Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TTS] Fix TTS audio preprocessing bugs #6628

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions nemo/collections/tts/parts/preprocessing/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,14 @@ def __init__(
n_fft=win_length,
lowfreq=lowfreq,
highfreq=highfreq,
mag_power=1.0,
log=log,
log_zero_guard_type=log_zero_guard_type,
log_zero_guard_value=log_zero_guard_value,
mel_norm=mel_norm,
normalize=None,
preemph=None,
dither=0.0,
)

def compute_mel_spec(self, manifest_entry: dict, audio_dir: Path) -> Tensor:
Expand Down
3 changes: 3 additions & 0 deletions nemo/collections/tts/parts/utils/tts_dataset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def normalize_volume(audio: np.array, volume_level: float) -> np.array:
if not (0.0 <= volume_level <= 1.0):
raise ValueError(f"Volume must be in range [0.0, 1.0], received {volume_level}")

if audio.size == 0:
return audio

max_sample = np.max(np.abs(audio))
if max_sample == 0:
return audio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def _process_entry(
if audio_trimmer is not None:
audio, start_i, end_i = audio_trimmer.trim_audio(audio=audio, sample_rate=sample_rate, audio_id=audio_path)

if output_sample_rate is not None:
if output_sample_rate:
audio = librosa.resample(y=audio, orig_sr=sample_rate, target_sr=output_sample_rate)
sample_rate = output_sample_rate

Expand All @@ -140,7 +140,7 @@ def _process_entry(
original_duration = librosa.get_duration(filename=audio_path)
output_duration = librosa.get_duration(filename=output_path)

entry["duration"] = output_duration
entry["duration"] = round(output_duration, 2)
redoctopus marked this conversation as resolved.
Show resolved Hide resolved

if os.path.isabs(audio_filepath):
entry["audio_filepath"] = output_path
Expand Down
76 changes: 0 additions & 76 deletions tests/collections/tts/data/test_data_utils.py

This file was deleted.

68 changes: 67 additions & 1 deletion tests/collections/tts/parts/utils/test_tts_dataset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

from pathlib import Path

import numpy as np
import pytest

from nemo.collections.tts.parts.utils.tts_dataset_utils import get_abs_rel_paths, get_audio_filepaths
from nemo.collections.tts.parts.utils.tts_dataset_utils import get_abs_rel_paths, get_audio_filepaths, normalize_volume


class TestTTSDatasetUtils:
Expand Down Expand Up @@ -53,3 +54,68 @@ def test_get_audio_paths(self):

assert abs_path == Path("/home/audio/examples/example.wav")
assert rel_path == audio_rel_path

@pytest.mark.run_only_on('CPU')
@pytest.mark.unit
def test_normalize_volume(self):
input_audio = np.array([0.0, 0.1, 0.3, 0.5])
expected_output = np.array([0.0, 0.18, 0.54, 0.9])

output_audio = normalize_volume(audio=input_audio, volume_level=0.9)

np.testing.assert_array_almost_equal(output_audio, expected_output)

@pytest.mark.run_only_on('CPU')
@pytest.mark.unit
def test_normalize_volume_negative_peak(self):
input_audio = np.array([0.0, 0.1, -0.3, -1.0, 0.5])
expected_output = np.array([0.0, 0.05, -0.15, -0.5, 0.25])

output_audio = normalize_volume(audio=input_audio, volume_level=0.5)

np.testing.assert_array_almost_equal(output_audio, expected_output)

@pytest.mark.run_only_on('CPU')
@pytest.mark.unit
def test_normalize_volume_zero(self):
input_audio = np.array([0.0, 0.1, 0.3, 0.5])
expected_output = np.array([0.0, 0.0, 0.0, 0.0])

output_audio = normalize_volume(audio=input_audio, volume_level=0.0)

np.testing.assert_array_almost_equal(output_audio, expected_output)

@pytest.mark.run_only_on('CPU')
@pytest.mark.unit
def test_normalize_volume_max(self):
input_audio = np.array([0.0, 0.1, 0.3, 0.5])
expected_output = np.array([0.0, 0.2, 0.6, 1.0])

output_audio = normalize_volume(audio=input_audio, volume_level=1.0)

np.testing.assert_array_almost_equal(output_audio, expected_output)

@pytest.mark.run_only_on('CPU')
@pytest.mark.unit
def test_normalize_volume_zeros(self):
input_audio = np.array([0.0, 0.0, 0.0])

output_audio = normalize_volume(audio=input_audio, volume_level=0.5)

np.testing.assert_array_almost_equal(output_audio, input_audio)

@pytest.mark.run_only_on('CPU')
@pytest.mark.unit
def test_normalize_volume_empty(self):
input_audio = np.array([])

output_audio = normalize_volume(audio=input_audio, volume_level=1.0)

np.testing.assert_array_almost_equal(output_audio, input_audio)

@pytest.mark.run_only_on('CPU')
@pytest.mark.unit
def test_normalize_volume_out_of_range(self):
input_audio = np.array([0.0, 0.1, 0.3, 0.5])
with pytest.raises(ValueError, match="Volume must be in range"):
normalize_volume(audio=input_audio, volume_level=2.0)
Loading