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.

32 lines
893 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)
  6. ids = torch.arange(0, max_len).long().cuda()
  7. mask = (ids < lengths.unsqueeze(1)).byte()
  8. return mask
  9. def load_wav_to_torch(full_path, sr):
  10. sampling_rate, data = read(full_path)
  11. assert sr == sampling_rate, "{} SR doesn't match {} on path {}".format(
  12. sr, sampling_rate, full_path)
  13. return torch.FloatTensor(data.astype(np.float32))
  14. def load_filepaths_and_text(filename, sort_by_length, split="|"):
  15. with open(filename, encoding='utf-8') as f:
  16. filepaths_and_text = [line.strip().split(split) for line in f]
  17. if sort_by_length:
  18. filepaths_and_text.sort(key=lambda x: len(x[1]))
  19. return filepaths_and_text
  20. def to_gpu(x):
  21. x = x.contiguous().cuda(async=True)
  22. return torch.autograd.Variable(x)