Skip to content

Instantly share code, notes, and snippets.

@mazzzystar
Last active April 18, 2019 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazzzystar/8132d5f23af8e66c9d6f021a39af2737 to your computer and use it in GitHub Desktop.
Save mazzzystar/8132d5f23af8e66c9d6f021a39af2737 to your computer and use it in GitHub Desktop.
self.power = 1.5
self.max_norm = 1.0
self.preemphasis = 0.97
self.ref_level_db = 20
self.min_level_db = -100
"""
Need to implement:
>>> np.clip
>>> np.power
>>> signal.lfilter
"""
def inv_spectrogram(self, spectrogram):
"""Converts spectrogram to waveform using librosa"""
S = self._denormalize(spectrogram)
S = self._db_to_amp(S + self.ref_level_db) # Convert back to linear
# Reconstruct phase
return self.apply_inv_preemphasis(self._griffin_lim(S**self.power))
def _denormalize(self, S):
"""denormalize values"""
S_denorm = S
S_denorm = np.clip(S_denorm, 0, self.max_norm)
S_denorm = (S_denorm * -self.min_level_db / self.max_norm) + self.min_level_db
return S_denorm
def _db_to_amp(self, x):
return np.power(10.0, x * 0.05)
# 对经过Griffin Lim合成出的audio wav, 使用preemphasis.
def apply_inv_preemphasis(self, x):
return signal.lfilter([1], [1, -self.preemphasis], x)
########### 这个C++里已有实现 ############
def _griffin_lim(self, S):
angles = np.exp(2j * np.pi * np.random.rand(*S.shape))
S_complex = np.abs(S).astype(np.complex)
y = self._istft(S_complex * angles)
for i in range(self.griffin_lim_iters):
angles = np.exp(1j * np.angle(self._stft(y)))
y = self._istft(S_complex * angles)
return y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment