Spaces:
Build error
Build error
File size: 2,171 Bytes
b88cc47 |
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 |
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib
import librosa
from scipy.io.wavfile import write
k = 1e-16
def np_log10(x):
numerator = np.log(x + 1e-16)
denominator = np.log(10)
return numerator / denominator
def sigmoid(x):
s = 1 / (1 + np.exp(-x))
return s
def inv_sigmoid(s):
x = np.log((s / (1 - s)) + 1e-16)
return x
def spc_to_VAE_input(spc):
"""Restrict value range from 0 to 1."""
return spc / (1 + spc)
def VAE_out_put_to_spc(o):
"""Inverse transform of function 'spc_to_VAE_input'."""
return o / (1 - o + k)
def denoise(spc):
"""Filter back ground noise. (Not used.)"""
return np.maximum(0, spc - (2e-5))
hop_length = 256
win_length = 1024
def np_power_to_db(S, amin=1e-16, top_db=80.0):
"""Helper method for scaling."""
ref = np.max(S)
# set fixed value for ref
# 每个元素取max
log_spec = 10.0 * np_log10(np.maximum(amin, S))
log_spec -= 10.0 * np_log10(np.maximum(amin, ref))
log_spec = np.maximum(log_spec, np.max(log_spec) - top_db)
return log_spec
def show_spc(spc, resolution=(512, 256)):
"""Show a spectrogram."""
spc = np.reshape(spc, resolution)
magnitude_spectrum = np.abs(spc)
log_spectrum = np_power_to_db(magnitude_spectrum)
plt.imshow(np.flipud(log_spectrum))
plt.show()
def save_results(spectrogram, spectrogram_image_path, waveform_path):
"""Save the input 'spectrogram' and its waveform (reconstructed bu Griffin Lim)
to path provided by 'spectrogram_image_path' and 'waveform_path'."""
# save image
magnitude_spectrum = np.abs(spectrogram)
log_spc = np_power_to_db(magnitude_spectrum)
log_spc = np.reshape(log_spc, (512, 256))
matplotlib.pyplot.imsave(spectrogram_image_path, log_spc, vmin=-100, vmax=0,
origin='lower')
# save waveform
abs_spec = np.zeros((513, 256))
abs_spec[:512, :] = abs_spec[:512, :] + np.sqrt(np.reshape(spectrogram, (512, 256)))
rec_signal = librosa.griffinlim(abs_spec, n_iter=32, hop_length=256, win_length=1024)
write(waveform_path, 16000, rec_signal)
|