| | import librosa.util as librosa_util |
| | import numpy as np |
| | import torch |
| | from scipy.io.wavfile import read |
| | from scipy.signal import get_window |
| |
|
| | |
| | from config import config |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | def normalize_tacotron_mel(data, mu=config.mu, std=config.std): |
| | |
| | if not isinstance(mu, (float, int)): |
| | if isinstance(mu, list): |
| | mu = torch.tensor(mu, dtype=data.dtype, device=data.device) |
| | elif isinstance(mu, torch.Tensor): |
| | mu = mu.to(data.device) |
| | elif isinstance(mu, np.ndarray): |
| | mu = torch.from_numpy(mu).to(data.device) |
| | mu = mu.unsqueeze(-1) |
| |
|
| | if not isinstance(std, (float, int)): |
| | if isinstance(std, list): |
| | std = torch.tensor(std, dtype=data.dtype, device=data.device) |
| | elif isinstance(std, torch.Tensor): |
| | std = std.to(data.device) |
| | elif isinstance(std, np.ndarray): |
| | std = torch.from_numpy(std).to(data.device) |
| | std = std.unsqueeze(-1) |
| |
|
| | return (data - mu) / std |
| |
|
| |
|
| | def denormalize_tacotron_mel(data, mu=config.mu, std=config.std): |
| | |
| | if not isinstance(mu, float): |
| | if isinstance(mu, list): |
| | mu = torch.tensor(mu, dtype=data.dtype, device=data.device) |
| | elif isinstance(mu, torch.Tensor): |
| | mu = mu.to(data.device) |
| | elif isinstance(mu, np.ndarray): |
| | mu = torch.from_numpy(mu).to(data.device) |
| | mu = mu.unsqueeze(-1) |
| |
|
| | if not isinstance(std, float): |
| | if isinstance(std, list): |
| | std = torch.tensor(std, dtype=data.dtype, device=data.device) |
| | elif isinstance(std, torch.Tensor): |
| | std = std.to(data.device) |
| | elif isinstance(std, np.ndarray): |
| | std = torch.from_numpy(std).to(data.device) |
| | std = std.unsqueeze(-1) |
| |
|
| | return data * std + mu |
| |
|
| |
|
| | |
| | |
| |
|
| |
|
| | |
| | |
| |
|
| | |
| |
|
| |
|
| | 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) |
| |
|