You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
777 B

  1. import numpy as np
  2. from scipy.io.wavfile import read
  3. import torch
  4. def get_mask_from_lengths(lengths):
  5. max_len = torch.max(lengths).item()
  6. ids = torch.arange(0, max_len, out=torch.cuda.LongTensor(max_len))
  7. mask = (ids < lengths.unsqueeze(1)).bool()
  8. return mask
  9. def load_wav_to_torch(full_path):
  10. sampling_rate, data = read(full_path)
  11. return torch.FloatTensor(data.astype(np.float32)), sampling_rate
  12. def load_filepaths_and_text(filename, split="|"):
  13. with open(filename, encoding='utf-8') as f:
  14. filepaths_and_text = [line.strip().split(split) for line in f]
  15. return filepaths_and_text
  16. def to_gpu(x):
  17. x = x.contiguous()
  18. if torch.cuda.is_available():
  19. x = x.cuda(non_blocking=True)
  20. return torch.autograd.Variable(x)