|
import torch |
|
import numpy as np |
|
import librosa.util as librosa_util |
|
|
|
from scipy.signal import get_window |
|
from scipy.io.wavfile import read |
|
from maha_tts.config import config |
|
|
|
TACOTRON_MEL_MAX = 2.4 |
|
TACOTRON_MEL_MIN = -11.5130 |
|
|
|
|
|
def denormalize_tacotron_mel(norm_mel): |
|
return ((norm_mel+1)/2)*(TACOTRON_MEL_MAX-TACOTRON_MEL_MIN)+TACOTRON_MEL_MIN |
|
|
|
|
|
def normalize_tacotron_mel(mel): |
|
return 2 * ((mel - TACOTRON_MEL_MIN) / (TACOTRON_MEL_MAX - TACOTRON_MEL_MIN)) - 1 |
|
|
|
|
|
def get_mask_from_lengths(lengths, max_len=None): |
|
if not max_len: |
|
max_len = torch.max(lengths).item() |
|
ids = torch.arange(0, max_len, device=lengths.device, dtype=torch.long) |
|
mask = (ids < lengths.unsqueeze(1)).bool() |
|
return mask |
|
|
|
|
|
def get_mask(lengths, max_len=None): |
|
if not max_len: |
|
max_len = torch.max(lengths).item() |
|
lens = torch.arange(max_len,) |
|
mask = lens[:max_len].unsqueeze(0) < lengths.unsqueeze(1) |
|
return mask |
|
|
|
|
|
|
|
def dynamic_range_compression(x, C=1, clip_val=1e-5): |
|
""" |
|
PARAMS |
|
------ |
|
C: compression factor |
|
""" |
|
return torch.log(torch.clamp(x, min=clip_val) * C) |
|
|
|
|
|
def dynamic_range_decompression(x, C=1): |
|
""" |
|
PARAMS |
|
------ |
|
C: compression factor used to compress |
|
""" |
|
return torch.exp(x) / C |
|
|
|
|
|
def window_sumsquare(window, n_frames, hop_length=200, win_length=800, |
|
n_fft=800, dtype=np.float32, norm=None): |
|
""" |
|
# from librosa 0.6 |
|
Compute the sum-square envelope of a window function at a given hop length. |
|
This is used to estimate modulation effects induced by windowing |
|
observations in short-time fourier transforms. |
|
Parameters |
|
---------- |
|
window : string, tuple, number, callable, or list-like |
|
Window specification, as in `get_window` |
|
n_frames : int > 0 |
|
The number of analysis frames |
|
hop_length : int > 0 |
|
The number of samples to advance between frames |
|
win_length : [optional] |
|
The length of the window function. By default, this matches `n_fft`. |
|
n_fft : int > 0 |
|
The length of each analysis frame. |
|
dtype : np.dtype |
|
The data type of the output |
|
Returns |
|
------- |
|
wss : np.ndarray, shape=`(n_fft + hop_length * (n_frames - 1))` |
|
The sum-squared envelope of the window function |
|
""" |
|
if win_length is None: |
|
win_length = n_fft |
|
|
|
n = n_fft + hop_length * (n_frames - 1) |
|
x = np.zeros(n, dtype=dtype) |
|
|
|
|
|
win_sq = get_window(window, win_length, fftbins=True) |
|
win_sq = librosa_util.normalize(win_sq, norm=norm)**2 |
|
win_sq = librosa_util.pad_center(win_sq, size=n_fft) |
|
|
|
|
|
for i in range(n_frames): |
|
sample = i * hop_length |
|
x[sample:min(n, sample + n_fft)] += win_sq[:max(0, min(n_fft, n - sample))] |
|
return x |
|
|
|
def load_wav_to_torch(full_path): |
|
sampling_rate, data = read(full_path,) |
|
return torch.FloatTensor(data), sampling_rate |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
lens = torch.tensor([2, 3, 7, 5, 4]) |
|
mask = get_mask(lens) |
|
print(mask) |
|
print(mask.shape) |