LeFleur808/Free-View_Expressive_Talking_Head_Video_Editing
0
1import librosa2import librosa.filters3import numpy as np4 5# import tensorflow as tf6from scipy import signal7from scipy.io import wavfile8 9hp_num_mels = 8010hp_rescale = True11hp_rescaling_max = 0.912hp_use_lws = False13hp_n_fft = 80014hp_hop_size = 20015hp_win_size = 80016hp_sample_rate = 1600017hp_frame_shift_ms = None18hp_signal_normalization = True19hp_allow_clipping_in_normalization = True20hp_symmetric_mels = True21hp_max_abs_value = 4.022hp_preemphasize = True23hp_preemphasis = 0.9724hp_min_level_db = -10025hp_ref_level_db = 2026hp_fmin = 5527hp_fmax = 760028 29 30def load_wav(path, sr):31 return librosa.core.load(path, sr=sr)[0]32 33 34def save_wav(wav, path, sr):35 wav *= 32767 / max(0.01, np.max(np.abs(wav)))36 # proposed by @dsmiller37 wavfile.write(path, sr, wav.astype(np.int16))38 39 40def save_wavenet_wav(wav, path, sr):41 librosa.output.write_wav(path, wav, sr=sr)42 43 44def preemphasis(wav, k, preemphasize=True):45 if preemphasize:46 return signal.lfilter([1, -k], [1], wav)47 return wav48 49 50def inv_preemphasis(wav, k, inv_preemphasize=True):51 if inv_preemphasize:52 return signal.lfilter([1], [1, -k], wav)53 return wav54 55 56def get_hop_size():57 hop_size = hp_hop_size58 if hop_size is None:59 assert hp_frame_shift_ms is not None60 hop_size = int(hp_frame_shift_ms / 1000 * hp_sample_rate)61 return hop_size62 63 64def linearspectrogram(wav):65 D = _stft(preemphasis(wav, hp_preemphasis, hp_preemphasize))66 S = _amp_to_db(np.abs(D)) - hp_ref_level_db67 if hp_signal_normalization:68 return _normalize(S)69 return S70 71 72def melspectrogram(wav):73 D = _stft(preemphasis(wav, hp_preemphasis, hp_preemphasize))74 S = _amp_to_db(_linear_to_mel(np.abs(D))) - hp_ref_level_db75 if hp_signal_normalization:76 return _normalize(S)77 return S78 79 80def _lws_processor():81 import lws82 83 return lws.lws(hp_n_fft, get_hop_size(), fftsize=hp_win_size, mode="speech")84 85 86def _stft(y):87 if hp_use_lws:88 return _lws_processor(hp).stft(y).T89 else:90 return librosa.stft(y=y, n_fft=hp_n_fft, hop_length=get_hop_size(), win_length=hp_win_size)91 92 93##########################################################94# Those are only correct when using lws!!! (This was messing with Wavenet quality for a long time!)95def num_frames(length, fsize, fshift):96 """Compute number of time frames of spectrogram"""97 pad = fsize - fshift98 if length % fshift == 0:99 M = (length + pad * 2 - fsize) // fshift + 1100 else:101 M = (length + pad * 2 - fsize) // fshift + 2102 return M103 104 105def pad_lr(x, fsize, fshift):106 """Compute left and right padding"""107 M = num_frames(len(x), fsize, fshift)108 pad = fsize - fshift109 T = len(x) + 2 * pad110 r = (M - 1) * fshift + fsize - T111 return pad, pad + r112 113 114##########################################################115# Librosa correct padding116def librosa_pad_lr(x, fsize, fshift):117 return 0, (x.shape[0] // fshift + 1) * fshift - x.shape[0]118 119 120# Conversions121_mel_basis = None122 123 124def _linear_to_mel(spectogram):125 global _mel_basis126 if _mel_basis is None:127 _mel_basis = _build_mel_basis()128 return np.dot(_mel_basis, spectogram)129 130 131def _build_mel_basis():132 assert hp_fmax <= hp_sample_rate // 2133 return librosa.filters.mel(hp_sample_rate, hp_n_fft, n_mels=hp_num_mels, fmin=hp_fmin, fmax=hp_fmax)134 135 136def _amp_to_db(x):137 min_level = np.exp(hp_min_level_db / 20 * np.log(10))138 return 20 * np.log10(np.maximum(min_level, x))139 140 141def _normalize(S):142 if hp_allow_clipping_in_normalization:143 if hp_symmetric_mels:144 return np.clip(145 (2 * hp_max_abs_value) * ((S - hp_min_level_db) / (-hp_min_level_db)) - hp_max_abs_value,146 -hp_max_abs_value,147 hp_max_abs_value,148 )149 else:150 return np.clip(151 hp_max_abs_value * ((S - hp_min_level_db) / (-hp_min_level_db)),152 0,153 hp_max_abs_value,154 )155 156 assert S.max() <= 0 and S.min() - hp_min_level_db >= 0157 if hp_symmetric_mels:158 return (2 * hp_max_abs_value) * ((S - hp_min_level_db) / (-hp_min_level_db)) - hp_max_abs_value159 else:160 return hp_max_abs_value * ((S - hp_min_level_db) / (-hp_min_level_db))161 