tacotron2/utils.py

30 lines
887 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()
2019-06-28 04:16:46 +00:00
ids = torch.arange(0, max_len, out=torch.LongTensor(max_len)) #initially out = torch.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()
2019-06-28 04:16:46 +00:00
#if torch.cuda.is_available(): #initially not commented out
# x = x.cuda(non_blocking=True) # initially not commented out
2018-05-03 22:16:57 +00:00
return torch.autograd.Variable(x)