Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

mel_processing.py 3.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  1. import torch
  2. import torch.utils.data
  3. from librosa.filters import mel as librosa_mel_fn
  4. MAX_WAV_VALUE = 32768.0
  5. def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
  6. """
  7. PARAMS
  8. ------
  9. C: compression factor
  10. """
  11. return torch.log(torch.clamp(x, min=clip_val) * C)
  12. def dynamic_range_decompression_torch(x, C=1):
  13. """
  14. PARAMS
  15. ------
  16. C: compression factor used to compress
  17. """
  18. return torch.exp(x) / C
  19. def spectral_normalize_torch(magnitudes):
  20. output = dynamic_range_compression_torch(magnitudes)
  21. return output
  22. def spectral_de_normalize_torch(magnitudes):
  23. output = dynamic_range_decompression_torch(magnitudes)
  24. return output
  25. mel_basis = {}
  26. hann_window = {}
  27. def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):
  28. if torch.min(y) < -1.:
  29. print('min value is ', torch.min(y))
  30. if torch.max(y) > 1.:
  31. print('max value is ', torch.max(y))
  32. global hann_window
  33. dtype_device = str(y.dtype) + '_' + str(y.device)
  34. wnsize_dtype_device = str(win_size) + '_' + dtype_device
  35. if wnsize_dtype_device not in hann_window:
  36. hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)
  37. y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect')
  38. y = y.squeeze(1)
  39. spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],
  40. center=center, pad_mode='reflect', normalized=False, onesided=True, return_complex=False)
  41. spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
  42. return spec
  43. def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):
  44. global mel_basis
  45. dtype_device = str(spec.dtype) + '_' + str(spec.device)
  46. fmax_dtype_device = str(fmax) + '_' + dtype_device
  47. if fmax_dtype_device not in mel_basis:
  48. mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)
  49. mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(dtype=spec.dtype, device=spec.device)
  50. spec = torch.matmul(mel_basis[fmax_dtype_device], spec)
  51. spec = spectral_normalize_torch(spec)
  52. return spec
  53. def mel_spectrogram_torch(y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False):
  54. if torch.min(y) < -1.:
  55. print('min value is ', torch.min(y))
  56. if torch.max(y) > 1.:
  57. print('max value is ', torch.max(y))
  58. global mel_basis, hann_window
  59. dtype_device = str(y.dtype) + '_' + str(y.device)
  60. fmax_dtype_device = str(fmax) + '_' + dtype_device
  61. wnsize_dtype_device = str(win_size) + '_' + dtype_device
  62. if fmax_dtype_device not in mel_basis:
  63. mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)
  64. mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(dtype=y.dtype, device=y.device)
  65. if wnsize_dtype_device not in hann_window:
  66. hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)
  67. y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect')
  68. y = y.squeeze(1)
  69. spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],
  70. center=center, pad_mode='reflect', normalized=False, onesided=True)
  71. spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
  72. spec = torch.matmul(mel_basis[fmax_dtype_device], spec)
  73. spec = spectral_normalize_torch(spec)
  74. return spec
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...