utils.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import torch
  2. import torch.utils.data
  3. from librosa.filters import mel as librosa_mel_fn
  4. def convert_pad_shape(pad_shape):
  5. l = pad_shape[::-1]
  6. pad_shape = [item for sublist in l for item in sublist]
  7. return pad_shape
  8. def sequence_mask(length, max_length=None):
  9. if max_length is None:
  10. max_length = length.max()
  11. x = torch.arange(max_length, dtype=length.dtype, device=length.device)
  12. return x.unsqueeze(0) < length.unsqueeze(1)
  13. def init_weights(m, mean=0.0, std=0.01):
  14. classname = m.__class__.__name__
  15. if classname.find("Conv") != -1:
  16. m.weight.data.normal_(mean, std)
  17. def get_padding(kernel_size, dilation=1):
  18. return int((kernel_size * dilation - dilation) / 2)
  19. def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
  20. """
  21. PARAMS
  22. ------
  23. C: compression factor
  24. """
  25. return torch.log(torch.clamp(x, min=clip_val) * C)
  26. def dynamic_range_decompression_torch(x, C=1):
  27. """
  28. PARAMS
  29. ------
  30. C: compression factor used to compress
  31. """
  32. return torch.exp(x) / C
  33. def spectral_normalize_torch(magnitudes):
  34. output = dynamic_range_compression_torch(magnitudes)
  35. return output
  36. def spectral_de_normalize_torch(magnitudes):
  37. output = dynamic_range_decompression_torch(magnitudes)
  38. return output
  39. mel_basis = {}
  40. hann_window = {}
  41. def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):
  42. if torch.min(y) < -1.0:
  43. print("min value is ", torch.min(y))
  44. if torch.max(y) > 1.0:
  45. print("max value is ", torch.max(y))
  46. global hann_window
  47. dtype_device = str(y.dtype) + "_" + str(y.device)
  48. wnsize_dtype_device = str(win_size) + "_" + dtype_device
  49. if wnsize_dtype_device not in hann_window:
  50. hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(
  51. dtype=y.dtype, device=y.device
  52. )
  53. y = torch.nn.functional.pad(
  54. y.unsqueeze(1),
  55. (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),
  56. mode="reflect",
  57. )
  58. y = y.squeeze(1)
  59. spec = torch.stft(
  60. y,
  61. n_fft,
  62. hop_length=hop_size,
  63. win_length=win_size,
  64. window=hann_window[wnsize_dtype_device],
  65. center=center,
  66. pad_mode="reflect",
  67. normalized=False,
  68. onesided=True,
  69. return_complex=False,
  70. )
  71. spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
  72. return spec
  73. def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):
  74. global mel_basis
  75. dtype_device = str(spec.dtype) + "_" + str(spec.device)
  76. fmax_dtype_device = str(fmax) + "_" + dtype_device
  77. if fmax_dtype_device not in mel_basis:
  78. mel = librosa_mel_fn(
  79. sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax
  80. )
  81. mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(
  82. dtype=spec.dtype, device=spec.device
  83. )
  84. spec = torch.matmul(mel_basis[fmax_dtype_device], spec)
  85. spec = spectral_normalize_torch(spec)
  86. return spec
  87. def mel_spectrogram_torch(
  88. y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False
  89. ):
  90. if torch.min(y) < -1.0:
  91. print("min value is ", torch.min(y))
  92. if torch.max(y) > 1.0:
  93. print("max value is ", torch.max(y))
  94. global mel_basis, hann_window
  95. dtype_device = str(y.dtype) + "_" + str(y.device)
  96. fmax_dtype_device = str(fmax) + "_" + dtype_device
  97. wnsize_dtype_device = str(win_size) + "_" + dtype_device
  98. if fmax_dtype_device not in mel_basis:
  99. mel = librosa_mel_fn(
  100. sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax
  101. )
  102. mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(
  103. dtype=y.dtype, device=y.device
  104. )
  105. if wnsize_dtype_device not in hann_window:
  106. hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(
  107. dtype=y.dtype, device=y.device
  108. )
  109. y = torch.nn.functional.pad(
  110. y.unsqueeze(1),
  111. (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),
  112. mode="reflect",
  113. )
  114. y = y.squeeze(1)
  115. spec = torch.stft(
  116. y,
  117. n_fft,
  118. hop_length=hop_size,
  119. win_length=win_size,
  120. window=hann_window[wnsize_dtype_device],
  121. center=center,
  122. pad_mode="reflect",
  123. normalized=False,
  124. onesided=True,
  125. return_complex=False,
  126. )
  127. spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
  128. spec = torch.matmul(mel_basis[fmax_dtype_device], spec)
  129. spec = spectral_normalize_torch(spec)
  130. return spec