layers.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from functools import partial
  15. from typing import Optional, Union
  16. import paddle
  17. from paddle import Tensor, nn
  18. from ..functional import compute_fbank_matrix, create_dct, power_to_db
  19. from ..functional.window import get_window
  20. class Spectrogram(nn.Layer):
  21. """Compute spectrogram of given signals, typically audio waveforms.
  22. The spectrogram is defined as the complex norm of the short-time Fourier transformation.
  23. Args:
  24. n_fft (int, optional): The number of frequency components of the discrete Fourier transform. Defaults to 512.
  25. hop_length (Optional[int], optional): The hop length of the short time FFT. If `None`, it is set to `win_length//4`. Defaults to None.
  26. win_length (Optional[int], optional): The window length of the short time FFT. If `None`, it is set to same as `n_fft`. Defaults to None.
  27. window (str, optional): The window function applied to the signal before the Fourier transform. Supported window functions: 'hamming', 'hann', 'kaiser', 'gaussian', 'exponential', 'triang', 'bohman', 'blackman', 'cosine', 'tukey', 'taylor'. Defaults to 'hann'.
  28. power (float, optional): Exponent for the magnitude spectrogram. Defaults to 2.0.
  29. center (bool, optional): Whether to pad `x` to make that the :math:`t \times hop\\_length` at the center of `t`-th frame. Defaults to True.
  30. pad_mode (str, optional): Choose padding pattern when `center` is `True`. Defaults to 'reflect'.
  31. dtype (str, optional): Data type of input and window. Defaults to 'float32'.
  32. Returns:
  33. :ref:`api_paddle_nn_Layer`. An instance of Spectrogram.
  34. Examples:
  35. .. code-block:: python
  36. >>> import paddle
  37. >>> from paddle.audio.features import Spectrogram
  38. >>> sample_rate = 16000
  39. >>> wav_duration = 0.5
  40. >>> num_channels = 1
  41. >>> num_frames = sample_rate * wav_duration
  42. >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
  43. >>> waveform = wav_data.tile([num_channels, 1])
  44. >>> feature_extractor = Spectrogram(n_fft=512, window = 'hann', power = 1.0)
  45. >>> feats = feature_extractor(waveform)
  46. """
  47. def __init__(
  48. self,
  49. n_fft: int = 512,
  50. hop_length: Optional[int] = 512,
  51. win_length: Optional[int] = None,
  52. window: str = 'hann',
  53. power: float = 1.0,
  54. center: bool = True,
  55. pad_mode: str = 'reflect',
  56. dtype: str = 'float32',
  57. ) -> None:
  58. super().__init__()
  59. assert power > 0, 'Power of spectrogram must be > 0.'
  60. self.power = power
  61. if win_length is None:
  62. win_length = n_fft
  63. self.fft_window = get_window(
  64. window, win_length, fftbins=True, dtype=dtype
  65. )
  66. self._stft = partial(
  67. paddle.signal.stft,
  68. n_fft=n_fft,
  69. hop_length=hop_length,
  70. win_length=win_length,
  71. window=self.fft_window,
  72. center=center,
  73. pad_mode=pad_mode,
  74. )
  75. self.register_buffer('fft_window', self.fft_window)
  76. def forward(self, x: Tensor) -> Tensor:
  77. """
  78. Args:
  79. x (Tensor): Tensor of waveforms with shape `(N, T)`
  80. Returns:
  81. Tensor: Spectrograms with shape `(N, n_fft//2 + 1, num_frames)`.
  82. """
  83. stft = self._stft(x)
  84. spectrogram = paddle.pow(paddle.abs(stft), self.power)
  85. return spectrogram
  86. class MelSpectrogram(nn.Layer):
  87. """Compute the melspectrogram of given signals, typically audio waveforms. It is computed by multiplying spectrogram with Mel filter bank matrix.
  88. Args:
  89. sr (int, optional): Sample rate. Defaults to 22050.
  90. n_fft (int, optional): The number of frequency components of the discrete Fourier transform. Defaults to 512.
  91. hop_length (Optional[int], optional): The hop length of the short time FFT. If `None`, it is set to `win_length//4`. Defaults to None.
  92. win_length (Optional[int], optional): The window length of the short time FFT. If `None`, it is set to same as `n_fft`. Defaults to None.
  93. window (str, optional): The window function applied to the signal before the Fourier transform. Supported window functions: 'hamming', 'hann', 'kaiser', 'gaussian', 'exponential', 'triang', 'bohman', 'blackman', 'cosine', 'tukey', 'taylor'. Defaults to 'hann'.
  94. power (float, optional): Exponent for the magnitude spectrogram. Defaults to 2.0.
  95. center (bool, optional): Whether to pad `x` to make that the :math:`t \times hop\\_length` at the center of `t`-th frame. Defaults to True.
  96. pad_mode (str, optional): Choose padding pattern when `center` is `True`. Defaults to 'reflect'.
  97. n_mels (int, optional): Number of mel bins. Defaults to 64.
  98. f_min (float, optional): Minimum frequency in Hz. Defaults to 50.0.
  99. f_max (Optional[float], optional): Maximum frequency in Hz. Defaults to None.
  100. htk (bool, optional): Use HTK formula in computing fbank matrix. Defaults to False.
  101. norm (Union[str, float], optional): Type of normalization in computing fbank matrix. Slaney-style is used by default. You can specify norm=1.0/2.0 to use customized p-norm normalization. Defaults to 'slaney'.
  102. dtype (str, optional): Data type of input and window. Defaults to 'float32'.
  103. Returns:
  104. :ref:`api_paddle_nn_Layer`. An instance of MelSpectrogram.
  105. Examples:
  106. .. code-block:: python
  107. >>> import paddle
  108. >>> from paddle.audio.features import MelSpectrogram
  109. >>> sample_rate = 16000
  110. >>> wav_duration = 0.5
  111. >>> num_channels = 1
  112. >>> num_frames = sample_rate * wav_duration
  113. >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
  114. >>> waveform = wav_data.tile([num_channels, 1])
  115. >>> feature_extractor = MelSpectrogram(sr=sample_rate, n_fft=512, window = 'hann', power = 1.0)
  116. >>> feats = feature_extractor(waveform)
  117. """
  118. def __init__(
  119. self,
  120. sr: int = 22050,
  121. n_fft: int = 2048,
  122. hop_length: Optional[int] = 512,
  123. win_length: Optional[int] = None,
  124. window: str = 'hann',
  125. power: float = 2.0,
  126. center: bool = True,
  127. pad_mode: str = 'reflect',
  128. n_mels: int = 64,
  129. f_min: float = 50.0,
  130. f_max: Optional[float] = None,
  131. htk: bool = False,
  132. norm: Union[str, float] = 'slaney',
  133. dtype: str = 'float32',
  134. ) -> None:
  135. super().__init__()
  136. self._spectrogram = Spectrogram(
  137. n_fft=n_fft,
  138. hop_length=hop_length,
  139. win_length=win_length,
  140. window=window,
  141. power=power,
  142. center=center,
  143. pad_mode=pad_mode,
  144. dtype=dtype,
  145. )
  146. self.n_mels = n_mels
  147. self.f_min = f_min
  148. self.f_max = f_max
  149. self.htk = htk
  150. self.norm = norm
  151. if f_max is None:
  152. f_max = sr // 2
  153. self.fbank_matrix = compute_fbank_matrix(
  154. sr=sr,
  155. n_fft=n_fft,
  156. n_mels=n_mels,
  157. f_min=f_min,
  158. f_max=f_max,
  159. htk=htk,
  160. norm=norm,
  161. dtype=dtype,
  162. )
  163. self.register_buffer('fbank_matrix', self.fbank_matrix)
  164. def forward(self, x: Tensor) -> Tensor:
  165. """
  166. Args:
  167. x (Tensor): Tensor of waveforms with shape `(N, T)`
  168. Returns:
  169. Tensor: Mel spectrograms with shape `(N, n_mels, num_frames)`.
  170. """
  171. spect_feature = self._spectrogram(x)
  172. mel_feature = paddle.matmul(self.fbank_matrix, spect_feature)
  173. return mel_feature
  174. class LogMelSpectrogram(nn.Layer):
  175. """Compute log-mel-spectrogram feature of given signals, typically audio waveforms.
  176. Args:
  177. sr (int, optional): Sample rate. Defaults to 22050.
  178. n_fft (int, optional): The number of frequency components of the discrete Fourier transform. Defaults to 512.
  179. hop_length (Optional[int], optional): The hop length of the short time FFT. If `None`, it is set to `win_length//4`. Defaults to None.
  180. win_length (Optional[int], optional): The window length of the short time FFT. If `None`, it is set to same as `n_fft`. Defaults to None.
  181. window (str, optional): The window function applied to the signal before the Fourier transform. Supported window functions: 'hamming', 'hann', 'kaiser', 'gaussian', 'exponential', 'triang', 'bohman', 'blackman', 'cosine', 'tukey', 'taylor'. Defaults to 'hann'.
  182. power (float, optional): Exponent for the magnitude spectrogram. Defaults to 2.0.
  183. center (bool, optional): Whether to pad `x` to make that the :math:`t \times hop\\_length` at the center of `t`-th frame. Defaults to True.
  184. pad_mode (str, optional): Choose padding pattern when `center` is `True`. Defaults to 'reflect'.
  185. n_mels (int, optional): Number of mel bins. Defaults to 64.
  186. f_min (float, optional): Minimum frequency in Hz. Defaults to 50.0.
  187. f_max (Optional[float], optional): Maximum frequency in Hz. Defaults to None.
  188. htk (bool, optional): Use HTK formula in computing fbank matrix. Defaults to False.
  189. norm (Union[str, float], optional): Type of normalization in computing fbank matrix. Slaney-style is used by default. You can specify norm=1.0/2.0 to use customized p-norm normalization. Defaults to 'slaney'.
  190. ref_value (float, optional): The reference value. If smaller than 1.0, the db level of the signal will be pulled up accordingly. Otherwise, the db level is pushed down. Defaults to 1.0.
  191. amin (float, optional): The minimum value of input magnitude. Defaults to 1e-10.
  192. top_db (Optional[float], optional): The maximum db value of spectrogram. Defaults to None.
  193. dtype (str, optional): Data type of input and window. Defaults to 'float32'.
  194. Returns:
  195. :ref:`api_paddle_nn_Layer`. An instance of LogMelSpectrogram.
  196. Examples:
  197. .. code-block:: python
  198. >>> import paddle
  199. >>> from paddle.audio.features import LogMelSpectrogram
  200. >>> sample_rate = 16000
  201. >>> wav_duration = 0.5
  202. >>> num_channels = 1
  203. >>> num_frames = sample_rate * wav_duration
  204. >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
  205. >>> waveform = wav_data.tile([num_channels, 1])
  206. >>> feature_extractor = LogMelSpectrogram(sr=sample_rate, n_fft=512, window = 'hann', power = 1.0)
  207. >>> feats = feature_extractor(waveform)
  208. """
  209. def __init__(
  210. self,
  211. sr: int = 22050,
  212. n_fft: int = 512,
  213. hop_length: Optional[int] = None,
  214. win_length: Optional[int] = None,
  215. window: str = 'hann',
  216. power: float = 2.0,
  217. center: bool = True,
  218. pad_mode: str = 'reflect',
  219. n_mels: int = 64,
  220. f_min: float = 50.0,
  221. f_max: Optional[float] = None,
  222. htk: bool = False,
  223. norm: Union[str, float] = 'slaney',
  224. ref_value: float = 1.0,
  225. amin: float = 1e-10,
  226. top_db: Optional[float] = None,
  227. dtype: str = 'float32',
  228. ) -> None:
  229. super().__init__()
  230. self._melspectrogram = MelSpectrogram(
  231. sr=sr,
  232. n_fft=n_fft,
  233. hop_length=hop_length,
  234. win_length=win_length,
  235. window=window,
  236. power=power,
  237. center=center,
  238. pad_mode=pad_mode,
  239. n_mels=n_mels,
  240. f_min=f_min,
  241. f_max=f_max,
  242. htk=htk,
  243. norm=norm,
  244. dtype=dtype,
  245. )
  246. self.ref_value = ref_value
  247. self.amin = amin
  248. self.top_db = top_db
  249. def forward(self, x: Tensor) -> Tensor:
  250. """
  251. Args:
  252. x (Tensor): Tensor of waveforms with shape `(N, T)`
  253. Returns:
  254. Tensor: Log mel spectrograms with shape `(N, n_mels, num_frames)`.
  255. """
  256. mel_feature = self._melspectrogram(x)
  257. log_mel_feature = power_to_db(
  258. mel_feature,
  259. ref_value=self.ref_value,
  260. amin=self.amin,
  261. top_db=self.top_db,
  262. )
  263. return log_mel_feature
  264. class MFCC(nn.Layer):
  265. """Compute mel frequency cepstral coefficients(MFCCs) feature of given waveforms.
  266. Args:
  267. sr (int, optional): Sample rate. Defaults to 22050.
  268. n_mfcc (int, optional): [description]. Defaults to 40.
  269. n_fft (int, optional): The number of frequency components of the discrete Fourier transform. Defaults to 512.
  270. hop_length (Optional[int], optional): The hop length of the short time FFT. If `None`, it is set to `win_length//4`. Defaults to None.
  271. win_length (Optional[int], optional): The window length of the short time FFT. If `None`, it is set to same as `n_fft`. Defaults to None.
  272. window (str, optional): The window function applied to the signal before the Fourier transform. Supported window functions: 'hamming', 'hann', 'kaiser', 'gaussian', 'exponential', 'triang', 'bohman', 'blackman', 'cosine', 'tukey', 'taylor'. Defaults to 'hann'.
  273. power (float, optional): Exponent for the magnitude spectrogram. Defaults to 2.0.
  274. center (bool, optional): Whether to pad `x` to make that the :math:`t \times hop\\_length` at the center of `t`-th frame. Defaults to True.
  275. pad_mode (str, optional): Choose padding pattern when `center` is `True`. Defaults to 'reflect'.
  276. n_mels (int, optional): Number of mel bins. Defaults to 64.
  277. f_min (float, optional): Minimum frequency in Hz. Defaults to 50.0.
  278. f_max (Optional[float], optional): Maximum frequency in Hz. Defaults to None.
  279. htk (bool, optional): Use HTK formula in computing fbank matrix. Defaults to False.
  280. norm (Union[str, float], optional): Type of normalization in computing fbank matrix. Slaney-style is used by default. You can specify norm=1.0/2.0 to use customized p-norm normalization. Defaults to 'slaney'.
  281. ref_value (float, optional): The reference value. If smaller than 1.0, the db level of the signal will be pulled up accordingly. Otherwise, the db level is pushed down. Defaults to 1.0.
  282. amin (float, optional): The minimum value of input magnitude. Defaults to 1e-10.
  283. top_db (Optional[float], optional): The maximum db value of spectrogram. Defaults to None.
  284. dtype (str, optional): Data type of input and window. Defaults to 'float32'.
  285. Returns:
  286. :ref:`api_paddle_nn_Layer`. An instance of MFCC.
  287. Examples:
  288. .. code-block:: python
  289. >>> import paddle
  290. >>> from paddle.audio.features import MFCC
  291. >>> sample_rate = 16000
  292. >>> wav_duration = 0.5
  293. >>> num_channels = 1
  294. >>> num_frames = sample_rate * wav_duration
  295. >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
  296. >>> waveform = wav_data.tile([num_channels, 1])
  297. >>> feature_extractor = MFCC(sr=sample_rate, n_fft=512, window = 'hann')
  298. >>> feats = feature_extractor(waveform)
  299. """
  300. def __init__(
  301. self,
  302. sr: int = 22050,
  303. n_mfcc: int = 40,
  304. n_fft: int = 512,
  305. hop_length: Optional[int] = None,
  306. win_length: Optional[int] = None,
  307. window: str = 'hann',
  308. power: float = 2.0,
  309. center: bool = True,
  310. pad_mode: str = 'reflect',
  311. n_mels: int = 64,
  312. f_min: float = 50.0,
  313. f_max: Optional[float] = None,
  314. htk: bool = False,
  315. norm: Union[str, float] = 'slaney',
  316. ref_value: float = 1.0,
  317. amin: float = 1e-10,
  318. top_db: Optional[float] = None,
  319. dtype: str = 'float32',
  320. ) -> None:
  321. super().__init__()
  322. assert (
  323. n_mfcc <= n_mels
  324. ), 'n_mfcc cannot be larger than n_mels: %d vs %d' % (n_mfcc, n_mels)
  325. self._log_melspectrogram = LogMelSpectrogram(
  326. sr=sr,
  327. n_fft=n_fft,
  328. hop_length=hop_length,
  329. win_length=win_length,
  330. window=window,
  331. power=power,
  332. center=center,
  333. pad_mode=pad_mode,
  334. n_mels=n_mels,
  335. f_min=f_min,
  336. f_max=f_max,
  337. htk=htk,
  338. norm=norm,
  339. ref_value=ref_value,
  340. amin=amin,
  341. top_db=top_db,
  342. dtype=dtype,
  343. )
  344. self.dct_matrix = create_dct(n_mfcc=n_mfcc, n_mels=n_mels, dtype=dtype)
  345. self.register_buffer('dct_matrix', self.dct_matrix)
  346. def forward(self, x: Tensor) -> Tensor:
  347. """
  348. Args:
  349. x (Tensor): Tensor of waveforms with shape `(N, T)`
  350. Returns:
  351. Tensor: Mel frequency cepstral coefficients with shape `(N, n_mfcc, num_frames)`.
  352. """
  353. log_mel_feature = self._log_melspectrogram(x)
  354. mfcc = paddle.matmul(
  355. log_mel_feature.transpose((0, 2, 1)), self.dct_matrix
  356. ).transpose(
  357. (0, 2, 1)
  358. ) # (B, n_mels, L)
  359. return mfcc