audio_processing.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 librosa.util as librosa_util
  28. import numpy as np
  29. import torch
  30. from scipy.signal import get_window
  31. def window_sumsquare(window, n_frames, hop_length=200, win_length=800,
  32. n_fft=800, dtype=np.float32, norm=None):
  33. """
  34. # from librosa 0.6
  35. Compute the sum-square envelope of a window function at a given hop length.
  36. This is used to estimate modulation effects induced by windowing
  37. observations in short-time fourier transforms.
  38. Parameters
  39. ----------
  40. window : string, tuple, number, callable, or list-like
  41. Window specification, as in `get_window`
  42. n_frames : int > 0
  43. The number of analysis frames
  44. hop_length : int > 0
  45. The number of samples to advance between frames
  46. win_length : [optional]
  47. The length of the window function. By default, this matches `n_fft`.
  48. n_fft : int > 0
  49. The length of each analysis frame.
  50. dtype : np.dtype
  51. The data type of the output
  52. Returns
  53. -------
  54. wss : np.ndarray, shape=`(n_fft + hop_length * (n_frames - 1))`
  55. The sum-squared envelope of the window function
  56. """
  57. if win_length is None:
  58. win_length = n_fft
  59. n = n_fft + hop_length * (n_frames - 1)
  60. x = np.zeros(n, dtype=dtype)
  61. # Compute the squared window at the desired length
  62. win_sq = get_window(window, win_length, fftbins=True)
  63. win_sq = librosa_util.normalize(win_sq, norm=norm)**2
  64. win_sq = librosa_util.pad_center(win_sq, size=n_fft)
  65. # Fill the envelope
  66. for i in range(n_frames):
  67. sample = i * hop_length
  68. x[sample:min(n, sample + n_fft)] += win_sq[:max(0, min(n_fft, n - sample))]
  69. return x
  70. def griffin_lim(magnitudes, stft_fn, n_iters=30):
  71. """
  72. PARAMS
  73. ------
  74. magnitudes: spectrogram magnitudes
  75. stft_fn: STFT class with transform (STFT) and inverse (ISTFT) methods
  76. """
  77. angles = np.angle(np.exp(2j * np.pi * np.random.rand(*magnitudes.size())))
  78. angles = angles.astype(np.float32)
  79. angles = torch.autograd.Variable(torch.from_numpy(angles))
  80. signal = stft_fn.inverse(magnitudes, angles).squeeze(1)
  81. for i in range(n_iters):
  82. _, angles = stft_fn.transform(signal)
  83. signal = stft_fn.inverse(magnitudes, angles).squeeze(1)
  84. return signal
  85. def dynamic_range_compression(x, C=1, clip_val=1e-5):
  86. """
  87. PARAMS
  88. ------
  89. C: compression factor
  90. """
  91. return torch.log(torch.clamp(x, min=clip_val) * C)
  92. def dynamic_range_decompression(x, C=1):
  93. """
  94. PARAMS
  95. ------
  96. C: compression factor used to compress
  97. """
  98. return torch.exp(x) / C