TTS/utils/audio.py

340 lines
13 KiB
Python
Raw Normal View History

2019-11-11 10:30:23 +00:00
import librosa
import soundfile as sf
import numpy as np
import scipy.io
import scipy.signal
from TTS.utils.data import StandardScaler
2019-11-11 10:30:23 +00:00
class AudioProcessor(object):
def __init__(self,
sample_rate=None,
num_mels=None,
min_level_db=None,
frame_shift_ms=None,
frame_length_ms=None,
2020-02-12 22:54:33 +00:00
hop_length=None,
win_length=None,
2019-11-11 10:30:23 +00:00
ref_level_db=None,
num_freq=None,
power=None,
preemphasis=0.0,
2019-11-11 10:30:23 +00:00
signal_norm=None,
symmetric_norm=None,
max_norm=None,
mel_fmin=None,
mel_fmax=None,
clip_norm=True,
griffin_lim_iters=None,
do_trim_silence=False,
trim_db=60,
2019-11-11 10:30:23 +00:00
sound_norm=False,
stats_path=None,
2019-11-11 10:30:23 +00:00
**_):
print(" > Setting up Audio Processor...")
2020-03-09 20:04:13 +00:00
# setup class attributed
2019-11-11 10:30:23 +00:00
self.sample_rate = sample_rate
self.num_mels = num_mels
self.min_level_db = min_level_db or 0
self.frame_shift_ms = frame_shift_ms
self.frame_length_ms = frame_length_ms
self.ref_level_db = ref_level_db
self.num_freq = num_freq
self.power = power
self.preemphasis = preemphasis
self.griffin_lim_iters = griffin_lim_iters
self.signal_norm = signal_norm
self.symmetric_norm = symmetric_norm
self.mel_fmin = mel_fmin or 0
self.mel_fmax = mel_fmax
self.max_norm = 1.0 if max_norm is None else float(max_norm)
self.clip_norm = clip_norm
self.do_trim_silence = do_trim_silence
self.trim_db = trim_db
2020-03-10 10:30:13 +00:00
self.do_sound_norm = sound_norm
self.stats_path = stats_path
2020-03-09 20:05:10 +00:00
# setup stft parameters
2020-02-12 22:54:33 +00:00
if hop_length is None:
self.n_fft, self.hop_length, self.win_length = self._stft_parameters()
else:
self.hop_length = hop_length
self.win_length = win_length
self.n_fft = (self.num_freq - 1) * 2
assert min_level_db != 0.0, " [!] min_level_db is 0"
2019-11-11 10:30:23 +00:00
members = vars(self)
for key, value in members.items():
print(" | > {}:{}".format(key, value))
2020-03-09 20:05:10 +00:00
# create spectrogram utils
self.mel_basis = self._build_mel_basis()
self.inv_mel_basis = np.linalg.pinv(self._build_mel_basis())
# setup scaler
if stats_path:
mel_mean, mel_std, linear_mean, linear_std, _ = self.load_stats(stats_path)
self.setup_scaler(mel_mean, mel_std, linear_mean,linear_std)
self.signal_norm = True
self.max_norm = None
self.clip_norm = None
self.symmetric_norm = None
2019-11-11 10:30:23 +00:00
2020-03-09 20:05:10 +00:00
### setting up the parameters ###
2019-11-11 10:30:23 +00:00
def _build_mel_basis(self, ):
if self.mel_fmax is not None:
assert self.mel_fmax <= self.sample_rate // 2
return librosa.filters.mel(
self.sample_rate,
2020-01-27 14:42:56 +00:00
self.n_fft,
2019-11-11 10:30:23 +00:00
n_mels=self.num_mels,
fmin=self.mel_fmin,
fmax=self.mel_fmax)
2020-03-09 20:05:10 +00:00
def _stft_parameters(self, ):
"""Compute necessary stft parameters with given time values"""
n_fft = (self.num_freq - 1) * 2
factor = self.frame_length_ms / self.frame_shift_ms
assert (factor).is_integer(), " [!] frame_shift_ms should divide frame_length_ms"
hop_length = int(self.frame_shift_ms / 1000.0 * self.sample_rate)
win_length = int(hop_length * factor)
return n_fft, hop_length, win_length
2020-03-09 20:05:10 +00:00
### normalization ###
2019-11-11 10:30:23 +00:00
def _normalize(self, S):
"""Put values in [0, self.max_norm] or [-self.max_norm, self.max_norm]"""
#pylint: disable=no-else-return
S = S.copy()
2019-11-11 10:30:23 +00:00
if self.signal_norm:
# mean-var scaling
if hasattr(self, 'mel_scaler'):
if S.shape[0] == self.num_mels:
return self.mel_scaler.transform(S.T).T
elif S.shape[0] == self.n_fft / 2:
return self.linear_scaler.transform(S.T).T
else:
raise RuntimeError(' [!] Mean-Var stats does not match the given feature dimensions.')
# range normalization
2019-11-11 10:30:23 +00:00
S_norm = ((S - self.min_level_db) / - self.min_level_db)
if self.symmetric_norm:
S_norm = ((2 * self.max_norm) * S_norm) - self.max_norm
if self.clip_norm:
S_norm = np.clip(S_norm, -self.max_norm, self.max_norm)
return S_norm
else:
S_norm = self.max_norm * S_norm
if self.clip_norm:
S_norm = np.clip(S_norm, 0, self.max_norm)
return S_norm
else:
return S
def _denormalize(self, S):
"""denormalize values"""
#pylint: disable=no-else-return
S_denorm = S.copy()
2019-11-11 10:30:23 +00:00
if self.signal_norm:
# mean-var scaling
if hasattr(self, 'mel_scaler'):
if S_denorm.shape[0] == self.num_mels:
return self.mel_scaler.inverse_transform(S_denorm.T).T
elif S_denorm.shape[0] == self.n_fft / 2:
return self.linear_scaler.inverse_transform(S_denorm.T).T
else:
raise RuntimeError(' [!] Mean-Var stats does not match the given feature dimensions.')
2019-11-11 10:30:23 +00:00
if self.symmetric_norm:
if self.clip_norm:
S_denorm = np.clip(S_denorm, -self.max_norm, self.max_norm)
S_denorm = ((S_denorm + self.max_norm) * -self.min_level_db / (2 * self.max_norm)) + self.min_level_db
return S_denorm
else:
if self.clip_norm:
S_denorm = np.clip(S_denorm, 0, self.max_norm)
S_denorm = (S_denorm * -self.min_level_db /
self.max_norm) + self.min_level_db
return S_denorm
else:
return S_denorm
### Mean-STD scaling ###
def load_stats(self, stats_path):
stats = np.load(stats_path, allow_pickle=True).item()
mel_mean = stats['mel_mean']
mel_std = stats['mel_std']
linear_mean = stats['linear_mean']
linear_std = stats['linear_std']
stats_config = stats['audio_config']
# check all audio parameters used for computing stats
skip_parameters = ['griffin_lim_iters', 'stats_path']
for key in stats_config.keys():
if key in skip_parameters:
continue
assert stats_config[key] == self.__dict__[
key], f" [!] Audio param {key} does not match the value used for computing mean-var stats. {stats_config[key]} vs {self.__dict__[key]}"
return mel_mean, mel_std, linear_mean, linear_std, stats_config
# pylint: disable=attribute-defined-outside-init
def setup_scaler(self, mel_mean, mel_std, linear_mean, linear_std):
self.mel_scaler = StandardScaler()
self.mel_scaler.set_stats(mel_mean, mel_std)
self.linear_scaler = StandardScaler()
self.linear_scaler.set_stats(linear_mean, linear_std)
2019-11-11 10:30:23 +00:00
2020-03-09 20:05:10 +00:00
### DB and AMP conversion ###
2019-11-11 10:30:23 +00:00
def _amp_to_db(self, x):
return 20 * np.log10(np.maximum(1e-5, x))
2019-11-11 10:30:23 +00:00
2020-03-09 20:05:10 +00:00
def _db_to_amp(self, x):
2019-11-11 10:30:23 +00:00
return np.power(10.0, x * 0.05)
2020-03-09 20:05:10 +00:00
### Preemphasis ###
2019-11-11 10:30:23 +00:00
def apply_preemphasis(self, x):
if self.preemphasis == 0:
raise RuntimeError(" [!] Preemphasis is set 0.0.")
2019-11-11 10:30:23 +00:00
return scipy.signal.lfilter([1, -self.preemphasis], [1], x)
def apply_inv_preemphasis(self, x):
if self.preemphasis == 0:
raise RuntimeError(" [!] Preemphasis is set 0.0.")
2019-11-11 10:30:23 +00:00
return scipy.signal.lfilter([1], [1, -self.preemphasis], x)
2020-03-09 20:05:10 +00:00
### SPECTROGRAMs ###
def _linear_to_mel(self, spectrogram):
return np.dot(self.mel_basis, spectrogram)
def _mel_to_linear(self, mel_spec):
return np.maximum(1e-10, np.dot(self.inv_mel_basis, mel_spec))
2019-11-11 10:30:23 +00:00
def spectrogram(self, y):
if self.preemphasis != 0:
D = self._stft(self.apply_preemphasis(y))
else:
D = self._stft(y)
S = self._amp_to_db(np.abs(D)) - self.ref_level_db
return self._normalize(S)
def melspectrogram(self, y):
if self.preemphasis != 0:
D = self._stft(self.apply_preemphasis(y))
else:
D = self._stft(y)
S = self._amp_to_db(self._linear_to_mel(np.abs(D))) - self.ref_level_db
return self._normalize(S)
def inv_spectrogram(self, spectrogram):
"""Converts spectrogram to waveform using librosa"""
S = self._denormalize(spectrogram)
2020-03-09 20:05:10 +00:00
S = self._db_to_amp(S + self.ref_level_db)
2019-11-11 10:30:23 +00:00
# Reconstruct phase
if self.preemphasis != 0:
return self.apply_inv_preemphasis(self._griffin_lim(S**self.power))
return self._griffin_lim(S**self.power)
2020-03-09 20:05:10 +00:00
def inv_melspectrogram(self, mel_spectrogram):
'''Converts melspectrogram to waveform using librosa'''
2019-11-11 10:30:23 +00:00
D = self._denormalize(mel_spectrogram)
S = self._db_to_amp(D + self.ref_level_db)
S = self._mel_to_linear(S) # Convert back to linear
if self.preemphasis != 0:
return self.apply_inv_preemphasis(self._griffin_lim(S**self.power))
return self._griffin_lim(S**self.power)
def out_linear_to_mel(self, linear_spec):
S = self._denormalize(linear_spec)
S = self._db_to_amp(S + self.ref_level_db)
S = self._linear_to_mel(np.abs(S))
S = self._amp_to_db(S) - self.ref_level_db
mel = self._normalize(S)
return mel
2020-03-09 20:05:10 +00:00
### STFT and ISTFT ###
2019-11-11 10:30:23 +00:00
def _stft(self, y):
return librosa.stft(
y=y,
n_fft=self.n_fft,
hop_length=self.hop_length,
win_length=self.win_length,
2020-01-27 14:42:56 +00:00
pad_mode='constant'
2019-11-11 10:30:23 +00:00
)
def _istft(self, y):
return librosa.istft(
y, hop_length=self.hop_length, win_length=self.win_length)
2020-03-09 20:05:10 +00:00
def _griffin_lim(self, S):
angles = np.exp(2j * np.pi * np.random.rand(*S.shape))
S_complex = np.abs(S).astype(np.complex)
y = self._istft(S_complex * angles)
for _ in range(self.griffin_lim_iters):
angles = np.exp(1j * np.angle(self._stft(y)))
y = self._istft(S_complex * angles)
return y
### Audio Processing ###
2019-11-11 10:30:23 +00:00
def find_endpoint(self, wav, threshold_db=-40, min_silence_sec=0.8):
window_length = int(self.sample_rate * min_silence_sec)
hop_length = int(window_length / 4)
threshold = self._db_to_amp(threshold_db)
for x in range(hop_length, len(wav) - window_length, hop_length):
if np.max(wav[x:x + window_length]) < threshold:
return x + hop_length
return len(wav)
def trim_silence(self, wav):
""" Trim silent parts with a threshold and 0.01 sec margin """
margin = int(self.sample_rate * 0.01)
wav = wav[margin:-margin]
return librosa.effects.trim(
wav, top_db=self.trim_db, frame_length=self.win_length, hop_length=self.hop_length)[0]
2019-11-11 10:30:23 +00:00
2020-03-10 10:30:13 +00:00
@staticmethod
def sound_norm(x):
2020-03-09 20:05:10 +00:00
return x / abs(x).max() * 0.9
2020-03-10 10:30:13 +00:00
### save and load ###
2020-03-09 20:05:10 +00:00
def load_wav(self, filename, sr=None):
if sr is None:
x, sr = sf.read(filename)
else:
x, sr = librosa.load(filename, sr=sr)
2020-03-10 10:30:13 +00:00
if self.do_trim_silence:
try:
x = self.trim_silence(x)
except ValueError:
print(f' [!] File cannot be trimmed for silence - {filename}')
assert self.sample_rate == sr, "%s vs %s"%(self.sample_rate, sr)
if self.do_sound_norm:
x = self.sound_norm(x)
2020-03-09 20:05:10 +00:00
return x
2020-03-10 10:30:13 +00:00
2020-03-09 20:05:10 +00:00
def save_wav(self, wav, path):
wav_norm = wav * (32767 / max(0.01, np.max(np.abs(wav))))
scipy.io.wavfile.write(path, self.sample_rate, wav_norm.astype(np.int16))
2019-11-11 10:30:23 +00:00
@staticmethod
def mulaw_encode(wav, qc):
mu = 2 ** qc - 1
# wav_abs = np.minimum(np.abs(wav), 1.0)
signal = np.sign(wav) * np.log(1 + mu * np.abs(wav)) / np.log(1. + mu)
# Quantize signal to the specified number of levels.
signal = (signal + 1) / 2 * mu + 0.5
return np.floor(signal,)
@staticmethod
def mulaw_decode(wav, qc):
"""Recovers waveform from quantized values."""
mu = 2 ** qc - 1
x = np.sign(wav) / mu * ((1 + mu) ** np.abs(wav) - 1)
return x
@staticmethod
def encode_16bits(x):
return np.clip(x * 2**15, -2**15, 2**15 - 1).astype(np.int16)
@staticmethod
def quantize(x, bits):
return (x + 1.) * (2**bits - 1) / 2
@staticmethod
def dequantize(x, bits):
return 2 * x / (2**bits - 1) - 1