def test_griffinlim_cqt_init(y_chirp, init): C = librosa.cqt(y=y_chirp, sr=22050, res_type="polyphase") y_rec = librosa.griffinlim_cqt( np.abs(C), sr=22050, n_iter=2, init=init, res_type="polyphase" ) assert np.all(np.isfinite(y_rec))
def test_griffinlim_cqt(y_chirp, hop_length, window, use_length, over_sample, fmin, res_type, pad_mode, scale, momentum, init, random_state, dtype): if use_length: length = len(y_chirp) else: length = None sr = 22050 bins_per_octave = 12 * over_sample n_bins = 6 * bins_per_octave C = librosa.cqt(y_chirp, sr=sr, hop_length=hop_length, window=window, fmin=fmin, bins_per_octave=bins_per_octave, n_bins=n_bins, scale=scale, pad_mode=pad_mode, res_type=res_type) Cmag = np.abs(C) y_rec = librosa.griffinlim_cqt(Cmag, hop_length=hop_length, window=window, sr=sr, fmin=fmin, bins_per_octave=bins_per_octave, scale=scale, pad_mode=pad_mode, n_iter=3, momentum=momentum, random_state=random_state, length=length, res_type=res_type, init=init, dtype=dtype) y_inv = librosa.icqt(Cmag, sr=sr, fmin=fmin, hop_length=hop_length, window=window, bins_per_octave=bins_per_octave, scale=scale, length=length, res_type=res_type) # First check for length if use_length: assert len(y_rec) == length assert y_rec.dtype == dtype # Check that the data is okay assert np.all(np.isfinite(y_rec))
def test_griffinlim_cqt_rng(y_chirp, random_state): C = librosa.cqt(y=y_chirp, sr=22050, res_type="polyphase") y_rec = librosa.griffinlim_cqt( np.abs(C), sr=22050, n_iter=2, random_state=random_state, res_type="polyphase" ) assert np.all(np.isfinite(y_rec))
def test_griffinlim_cqt_momentum(y_chirp, momentum): C = librosa.cqt(y=y_chirp, sr=22050, res_type="polyphase") y_rec = librosa.griffinlim_cqt( np.abs(C), sr=22050, n_iter=2, momentum=momentum, res_type="polyphase" ) assert np.all(np.isfinite(y_rec))
def test_griffinlim_cqt_multi(y_multi): y, sr = y_multi # Compute the stft C = librosa.cqt(y, sr=sr) # Run a couple of iterations of griffin-lim yout = librosa.griffinlim_cqt(np.abs(C), n_iter=2, length=y.shape[-1]) # Check the lengths assert np.allclose(y.shape, yout.shape)
def extract_audio( Z, feature, params): # if normalized Z: unnormalize first, then pass to func. # convert to audio if feature == "Stft": # undo log-magnitude scaling S = librosa.db_to_amplitude(Z) # upsample S = _upsample_fft(S, params["fft_sample_rate"], params["stft_window_length"]) yhat = librosa.griffinlim(S, hop_length=params["stft_hop_length"]) elif feature == "Mel": # undo log-power scaling S = librosa.db_to_power(Z) yhat = librosa.feature.inverse.mel_to_audio( S, sr=params["fft_sample_rate"], n_fft=params["stft_window_length"], hop_length=params["stft_hop_length"], ) elif feature == "Cqt": # undo log-amplitude scaling S = librosa.db_to_amplitude(Z) yhat = librosa.griffinlim_cqt( S, sr=params["fft_sample_rate"], hop_length=params["stft_hop_length"], fmin=librosa.note_to_hz(params["cqt_min_frequency"]), ) elif feature == "Mfcc": yhat = librosa.feature.inverse.mfcc_to_audio( Z, n_mels=params["frequency_bins"], sr=params["fft_sample_rate"], n_fft=params["stft_window_length"], hop_length=params["stft_hop_length"], ) else: print("Error: feature invalid") # throw/raise something return -1 return yhat, params["fft_sample_rate"]
def spec_to_audio(spec, config): if config.spec_type == 'mel': return librosa.feature.inverse.mel_to_audio( spec, sr=config.sample_rate, n_fft=config.n_fft, n_mels=config.n_mels, hop_length=config.hop_length, center=config.center) elif config.spec_type == 'cqt': return librosa.griffinlim_cqt(spec, sr=config.sample_rate, hop_length=config.hop_length, bins_per_octave=config.bins_per_octave)
def test_griffinlim_cqt_momentum_warn(): x = np.zeros((33, 3)) with pytest.warns(UserWarning): librosa.griffinlim_cqt(x, momentum=2)
def test_griffinlim_cqt_momentum(): x = np.zeros((33, 3)) librosa.griffinlim_cqt(x, momentum=-1)
def test_griffinlim_cqt_badinit(): x = np.zeros((33, 3)) librosa.griffinlim_cqt(x, init="garbage")