tacotron2/utils.py

30 lines
777 B
Python
Raw Normal View History

2018-05-03 22:16:57 +00:00
import numpy as np
from scipy.io.wavfile import read
import torch
def get_mask_from_lengths(lengths):
2018-11-26 06:34:38 +00:00
max_len = torch.max(lengths).item()
ids = torch.arange(0, max_len, out=torch.cuda.LongTensor(max_len))
2018-05-03 22:16:57 +00:00
mask = (ids < lengths.unsqueeze(1)).byte()
return mask
2018-11-26 06:34:38 +00:00
def load_wav_to_torch(full_path):
2018-05-03 22:16:57 +00:00
sampling_rate, data = read(full_path)
2018-11-26 06:34:38 +00:00
return torch.FloatTensor(data.astype(np.float32)), sampling_rate
2018-05-03 22:16:57 +00:00
2018-11-26 06:34:38 +00:00
def load_filepaths_and_text(filename, split="|"):
2018-05-03 22:16:57 +00:00
with open(filename, encoding='utf-8') as f:
filepaths_and_text = [line.strip().split(split) for line in f]
return filepaths_and_text
def to_gpu(x):
2018-11-26 06:34:38 +00:00
x = x.contiguous()
if torch.cuda.is_available():
x = x.cuda(non_blocking=True)
2018-05-03 22:16:57 +00:00
return torch.autograd.Variable(x)