Files
TTS/tests/aux_tests/test_audio_processor.py

191 lines
7.6 KiB
Python
Raw Permalink Normal View History

2018-11-02 16:13:51 +01:00
import os
import unittest
2020-08-04 14:07:47 +02:00
from tests import get_tests_input_path, get_tests_output_path, get_tests_path
2021-05-10 15:27:23 +02:00
from TTS.config import BaseAudioConfig
from TTS.utils.audio.processor import AudioProcessor
2018-11-02 16:13:51 +01:00
TESTS_PATH = get_tests_path()
OUT_PATH = os.path.join(get_tests_output_path(), "audio_tests")
WAV_FILE = os.path.join(get_tests_input_path(), "example_1.wav")
2018-11-02 16:13:51 +01:00
os.makedirs(OUT_PATH, exist_ok=True)
conf = BaseAudioConfig(mel_fmax=8000, pitch_fmax=640, pitch_fmin=1)
2018-11-02 16:13:51 +01:00
2020-04-23 15:46:45 +02:00
# pylint: disable=protected-access
2018-11-02 16:13:51 +01:00
class TestAudio(unittest.TestCase):
def __init__(self, *args, **kwargs):
2021-04-09 00:38:08 +02:00
super().__init__(*args, **kwargs)
2021-05-10 15:27:23 +02:00
self.ap = AudioProcessor(**conf)
2018-11-02 16:13:51 +01:00
def test_audio_synthesis(self):
2021-04-09 01:17:15 +02:00
"""1. load wav
2. set normalization parameters
3. extract mel-spec
4. invert to wav and save the output
2018-11-02 16:13:51 +01:00
"""
print(" > Sanity check for the process wav -> mel -> wav")
def _test(max_norm, signal_norm, symmetric_norm, clip_norm):
self.ap.max_norm = max_norm
self.ap.signal_norm = signal_norm
self.ap.symmetric_norm = symmetric_norm
self.ap.clip_norm = clip_norm
wav = self.ap.load_wav(WAV_FILE)
2018-11-02 16:13:51 +01:00
mel = self.ap.melspectrogram(wav)
2020-03-09 21:03:40 +01:00
wav_ = self.ap.inv_melspectrogram(mel)
2021-04-09 01:17:15 +02:00
file_name = "/audio_test-melspec_max_norm_{}-signal_norm_{}-symmetric_{}-clip_norm_{}.wav".format(
max_norm, signal_norm, symmetric_norm, clip_norm
)
2018-11-02 16:13:51 +01:00
print(" | > Creating wav file at : ", file_name)
self.ap.save_wav(wav_, OUT_PATH + file_name)
2018-11-02 16:13:51 +01:00
# maxnorm = 1.0
2021-04-09 01:17:15 +02:00
_test(1.0, False, False, False)
_test(1.0, True, False, False)
_test(1.0, True, True, False)
_test(1.0, True, False, True)
_test(1.0, True, True, True)
2018-11-02 16:13:51 +01:00
# maxnorm = 4.0
2021-04-09 01:17:15 +02:00
_test(4.0, False, False, False)
_test(4.0, True, False, False)
_test(4.0, True, True, False)
_test(4.0, True, False, True)
_test(4.0, True, True, True)
2018-11-02 16:13:51 +01:00
def test_normalize(self):
2021-05-03 14:26:35 +02:00
"""Check normalization and denormalization for range values and consistency"""
2018-11-02 16:13:51 +01:00
print(" > Testing normalization and denormalization.")
wav = self.ap.load_wav(WAV_FILE)
2020-03-17 18:22:55 +01:00
wav = self.ap.sound_norm(wav) # normalize audio to get abetter normalization range below.
2018-11-02 16:13:51 +01:00
self.ap.signal_norm = False
x = self.ap.melspectrogram(wav)
x_old = x
self.ap.signal_norm = True
self.ap.symmetric_norm = False
self.ap.clip_norm = False
self.ap.max_norm = 4.0
x_norm = self.ap.normalize(x)
2021-04-09 01:17:15 +02:00
print(
f" > MaxNorm: {self.ap.max_norm}, ClipNorm:{self.ap.clip_norm}, SymmetricNorm:{self.ap.symmetric_norm}, SignalNorm:{self.ap.signal_norm} Range-> {x_norm.max()} -- {x_norm.min()}"
)
2018-11-02 16:13:51 +01:00
assert (x_old - x).sum() == 0
# check value range
assert x_norm.max() <= self.ap.max_norm + 1, x_norm.max()
assert x_norm.min() >= 0 - 1, x_norm.min()
# check denorm.
x_ = self.ap.denormalize(x_norm)
2018-11-02 16:13:51 +01:00
assert (x - x_).sum() < 1e-3, (x - x_).mean()
self.ap.signal_norm = True
self.ap.symmetric_norm = False
self.ap.clip_norm = True
self.ap.max_norm = 4.0
x_norm = self.ap.normalize(x)
2021-04-09 01:17:15 +02:00
print(
f" > MaxNorm: {self.ap.max_norm}, ClipNorm:{self.ap.clip_norm}, SymmetricNorm:{self.ap.symmetric_norm}, SignalNorm:{self.ap.signal_norm} Range-> {x_norm.max()} -- {x_norm.min()}"
)
2020-03-17 18:22:55 +01:00
2018-11-02 16:13:51 +01:00
assert (x_old - x).sum() == 0
# check value range
assert x_norm.max() <= self.ap.max_norm, x_norm.max()
assert x_norm.min() >= 0, x_norm.min()
# check denorm.
x_ = self.ap.denormalize(x_norm)
2018-11-02 16:13:51 +01:00
assert (x - x_).sum() < 1e-3, (x - x_).mean()
self.ap.signal_norm = True
self.ap.symmetric_norm = True
self.ap.clip_norm = False
self.ap.max_norm = 4.0
x_norm = self.ap.normalize(x)
2021-04-09 01:17:15 +02:00
print(
f" > MaxNorm: {self.ap.max_norm}, ClipNorm:{self.ap.clip_norm}, SymmetricNorm:{self.ap.symmetric_norm}, SignalNorm:{self.ap.signal_norm} Range-> {x_norm.max()} -- {x_norm.min()}"
)
2020-03-17 18:22:55 +01:00
2018-11-02 16:13:51 +01:00
assert (x_old - x).sum() == 0
# check value range
assert x_norm.max() <= self.ap.max_norm + 1, x_norm.max()
2021-04-09 01:17:15 +02:00
assert x_norm.min() >= -self.ap.max_norm - 2, x_norm.min() # pylint: disable=invalid-unary-operand-type
2018-11-02 16:13:51 +01:00
assert x_norm.min() <= 0, x_norm.min()
# check denorm.
x_ = self.ap.denormalize(x_norm)
2018-11-02 16:13:51 +01:00
assert (x - x_).sum() < 1e-3, (x - x_).mean()
self.ap.signal_norm = True
self.ap.symmetric_norm = True
self.ap.clip_norm = True
self.ap.max_norm = 4.0
x_norm = self.ap.normalize(x)
2021-04-09 01:17:15 +02:00
print(
f" > MaxNorm: {self.ap.max_norm}, ClipNorm:{self.ap.clip_norm}, SymmetricNorm:{self.ap.symmetric_norm}, SignalNorm:{self.ap.signal_norm} Range-> {x_norm.max()} -- {x_norm.min()}"
)
2020-03-17 18:22:55 +01:00
2018-11-02 16:13:51 +01:00
assert (x_old - x).sum() == 0
# check value range
assert x_norm.max() <= self.ap.max_norm, x_norm.max()
2021-04-09 01:17:15 +02:00
assert x_norm.min() >= -self.ap.max_norm, x_norm.min() # pylint: disable=invalid-unary-operand-type
2018-11-02 16:13:51 +01:00
assert x_norm.min() <= 0, x_norm.min()
# check denorm.
x_ = self.ap.denormalize(x_norm)
2018-11-02 16:13:51 +01:00
assert (x - x_).sum() < 1e-3, (x - x_).mean()
self.ap.signal_norm = True
self.ap.symmetric_norm = False
self.ap.max_norm = 1.0
x_norm = self.ap.normalize(x)
2021-04-09 01:17:15 +02:00
print(
f" > MaxNorm: {self.ap.max_norm}, ClipNorm:{self.ap.clip_norm}, SymmetricNorm:{self.ap.symmetric_norm}, SignalNorm:{self.ap.signal_norm} Range-> {x_norm.max()} -- {x_norm.min()}"
)
2020-03-17 18:22:55 +01:00
2018-11-02 16:13:51 +01:00
assert (x_old - x).sum() == 0
assert x_norm.max() <= self.ap.max_norm, x_norm.max()
assert x_norm.min() >= 0, x_norm.min()
x_ = self.ap.denormalize(x_norm)
2018-11-02 16:13:51 +01:00
assert (x - x_).sum() < 1e-3
self.ap.signal_norm = True
self.ap.symmetric_norm = True
self.ap.max_norm = 1.0
x_norm = self.ap.normalize(x)
2021-04-09 01:17:15 +02:00
print(
f" > MaxNorm: {self.ap.max_norm}, ClipNorm:{self.ap.clip_norm}, SymmetricNorm:{self.ap.symmetric_norm}, SignalNorm:{self.ap.signal_norm} Range-> {x_norm.max()} -- {x_norm.min()}"
)
2020-03-17 18:22:55 +01:00
2018-11-02 16:13:51 +01:00
assert (x_old - x).sum() == 0
assert x_norm.max() <= self.ap.max_norm, x_norm.max()
2021-04-09 01:17:15 +02:00
assert x_norm.min() >= -self.ap.max_norm, x_norm.min() # pylint: disable=invalid-unary-operand-type
2018-11-02 16:13:51 +01:00
assert x_norm.min() < 0, x_norm.min()
x_ = self.ap.denormalize(x_norm)
2018-11-02 16:13:51 +01:00
assert (x - x_).sum() < 1e-3
def test_scaler(self):
2021-04-09 01:17:15 +02:00
scaler_stats_path = os.path.join(get_tests_input_path(), "scale_stats.npy")
2021-05-10 15:27:23 +02:00
conf.stats_path = scaler_stats_path
conf.preemphasis = 0.0
conf.do_trim_silence = True
conf.signal_norm = True
2021-05-10 15:27:23 +02:00
ap = AudioProcessor(**conf)
mel_mean, mel_std, linear_mean, linear_std, _ = ap.load_stats(scaler_stats_path)
ap.setup_scaler(mel_mean, mel_std, linear_mean, linear_std)
self.ap.signal_norm = False
self.ap.preemphasis = 0.0
2020-04-23 15:46:45 +02:00
# test scaler forward and backward transforms
wav = self.ap.load_wav(WAV_FILE)
mel_reference = self.ap.melspectrogram(wav)
mel_norm = ap.melspectrogram(wav)
mel_denorm = ap.denormalize(mel_norm)
2020-05-20 14:00:31 +02:00
assert abs(mel_reference - mel_denorm).max() < 1e-4
2021-07-06 09:48:00 +02:00
2021-09-06 14:30:15 +00:00
def test_compute_f0(self): # pylint: disable=no-self-use
2021-07-06 09:48:00 +02:00
ap = AudioProcessor(**conf)
wav = ap.load_wav(WAV_FILE)
pitch = ap.compute_f0(wav)
mel = ap.melspectrogram(wav)
assert pitch.shape[0] == mel.shape[1]