data_function.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # *****************************************************************************
  2. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. # * Neither the name of the NVIDIA CORPORATION nor the
  12. # names of its contributors may be used to endorse or promote products
  13. # derived from this software without specific prior written permission.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. # DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
  19. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. #
  26. # *****************************************************************************\
  27. import torch
  28. import random
  29. import common.layers as layers
  30. from common.utils import load_wav_to_torch, load_filepaths_and_text, to_gpu
  31. class MelAudioLoader(torch.utils.data.Dataset):
  32. """
  33. 1) loads audio,text pairs
  34. 2) computes mel-spectrograms from audio files.
  35. """
  36. def __init__(self, dataset_path, audiopaths_and_text, args):
  37. self.audiopaths_and_text = load_filepaths_and_text(dataset_path, audiopaths_and_text)
  38. self.max_wav_value = args.max_wav_value
  39. self.sampling_rate = args.sampling_rate
  40. self.stft = layers.TacotronSTFT(
  41. args.filter_length, args.hop_length, args.win_length,
  42. args.n_mel_channels, args.sampling_rate, args.mel_fmin,
  43. args.mel_fmax)
  44. self.segment_length = args.segment_length
  45. random.seed(1234)
  46. random.shuffle(self.audiopaths_and_text)
  47. def get_mel_audio_pair(self, filename):
  48. audio, sampling_rate = load_wav_to_torch(filename)
  49. if sampling_rate != self.stft.sampling_rate:
  50. raise ValueError("{} {} SR doesn't match target {} SR".format(
  51. sampling_rate, self.stft.sampling_rate))
  52. # Take segment
  53. if audio.size(0) >= self.segment_length:
  54. max_audio_start = audio.size(0) - self.segment_length
  55. audio_start = random.randint(0, max_audio_start)
  56. audio = audio[audio_start:audio_start+self.segment_length]
  57. else:
  58. audio = torch.nn.functional.pad(
  59. audio, (0, self.segment_length - audio.size(0)), 'constant').data
  60. audio = audio / self.max_wav_value
  61. audio_norm = audio.unsqueeze(0)
  62. audio_norm = torch.autograd.Variable(audio_norm, requires_grad=False)
  63. melspec = self.stft.mel_spectrogram(audio_norm)
  64. melspec = melspec.squeeze(0)
  65. return (melspec, audio, len(audio))
  66. def __getitem__(self, index):
  67. return self.get_mel_audio_pair(self.audiopaths_and_text[index][0])
  68. def __len__(self):
  69. return len(self.audiopaths_and_text)
  70. def batch_to_gpu(batch):
  71. x, y, len_y = batch
  72. x = to_gpu(x).float()
  73. y = to_gpu(y).float()
  74. len_y = to_gpu(torch.sum(len_y))
  75. return ((x, y), y, len_y)