def __test(sr, scale, hop_length, over_sample, y): bins_per_octave = over_sample * 12 n_bins = 7 * bins_per_octave C = librosa.cqt(y, sr=sr, n_bins=n_bins, bins_per_octave=bins_per_octave, scale=scale, hop_length=hop_length) yinv = librosa.icqt(C, sr=sr, scale=scale, hop_length=hop_length, bins_per_octave=bins_per_octave) # Only test on the middle section yinv = librosa.util.fix_length(yinv, len(y)) y = y[sr // 2:-sr // 2] yinv = yinv[sr // 2:-sr // 2] residual = np.abs(y - yinv) # We'll tolerate 11% RMSE # error is lower on more recent numpy/scipy builds resnorm = np.sqrt(np.mean(residual**2)) assert resnorm <= 1.1e-1, resnorm
def __test(sr, scale, hop_length, over_sample, y): bins_per_octave = over_sample * 12 n_bins = 7 * bins_per_octave C = librosa.cqt(y, sr=sr, n_bins=n_bins, bins_per_octave=bins_per_octave, scale=scale, hop_length=hop_length) yinv = librosa.icqt(C, sr=sr, scale=scale, hop_length=hop_length, bins_per_octave=bins_per_octave) # Only test on the middle section yinv = librosa.util.fix_length(yinv, len(y)) y = y[sr//2:-sr//2] yinv = yinv[sr//2:-sr//2] residual = np.abs(y - yinv) # We'll tolerate 11% RMSE # error is lower on more recent numpy/scipy builds resnorm = np.sqrt(np.mean(residual**2)) assert resnorm <= 1.1e-1, resnorm
def inverse_cqt(mag, phase, nfft=1024): S = mag_phase_to_S(mag, phase) audio = librosa.icqt(S, hop_length=int(nfft / 2**3), bins_per_octave=12 * 6, fmin=librosa.note_to_hz('A0')) return audio
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 icqt(sample_rate, hop_size, x): # return librosa.core.icqt( # x, # sr=sample_rate, # hop_length=hop_size) return librosa.icqt( x, sr=sample_rate, hop_length=hop_size)
def test_icqt_odd_hop(y_cqt_110, sr_cqt): C = librosa.cqt(y=y_cqt_110, sr=sr_cqt, hop_length=1001, res_type="polyphase") yi = librosa.icqt(C, sr=sr_cqt, hop_length=1001, res_type="polyphase", length=len(y_cqt_110))
def generateWavFromCQT(amp, phase, sr, hl): Mdb_inormed = np.interp(amp, (amp.min(), amp.max()), (-15, 65)) iM = librosa.db_to_amplitude(Mdb_inormed) bins_per_octave = 12 * 12 D = iM * np.exp(1j*phase) iy = librosa.icqt(C=D, sr=sr, hop_length=hl, bins_per_octave=bins_per_octave) iy = librosa.util.normalize(iy) return iy
def test_icqt_multi(y_multi, scale, length): y, sr = y_multi # Assuming the forward transform is well-behaved C = librosa.cqt(y=y, sr=sr, scale=scale) yboth = librosa.icqt(C, sr=sr, scale=scale, length=length) y0 = librosa.icqt(C[0], sr=sr, scale=scale, length=length) y1 = librosa.icqt(C[1], sr=sr, scale=scale, length=length) if length is not None: assert yboth.shape[-1] == length # Check each channel assert np.allclose(yboth[0], y0) assert np.allclose(yboth[1], y1) # Check that they're not the same assert not np.allclose(yboth[0], yboth[1])
def test_icqt(y_icqt, sr_icqt, scale, hop_length, over_sample, length, res_type, dtype): bins_per_octave = over_sample * 12 n_bins = 7 * bins_per_octave C = librosa.cqt( y_icqt, sr=sr_icqt, n_bins=n_bins, bins_per_octave=bins_per_octave, scale=scale, hop_length=hop_length, ) if length: _len = len(y_icqt) else: _len = None yinv = librosa.icqt( C, sr=sr_icqt, scale=scale, hop_length=hop_length, bins_per_octave=bins_per_octave, length=_len, res_type=res_type, dtype=dtype, ) assert yinv.dtype == dtype # Only test on the middle section if length: assert len(y_icqt) == len(yinv) else: yinv = librosa.util.fix_length(yinv, len(y_icqt)) y_icqt = y_icqt[sr_icqt // 2:-sr_icqt // 2] yinv = yinv[sr_icqt // 2:-sr_icqt // 2] residual = np.abs(y_icqt - yinv) # We'll tolerate 10% RMSE # error is lower on more recent numpy/scipy builds resnorm = np.sqrt(np.mean(residual**2)) assert resnorm <= 0.1, resnorm
import librosa import librosa.display import matplotlib.pyplot as plt import numpy as np x, sr = librosa.load('test.wav') C = librosa.cqt( x, sr=sr) #, fmin=30, n_bins=16, bins_per_octave=2, hop_length=2**8) # print (C[:,5]) # C[0:3,:] = 0 # C[4:8,:] = 0 librosa.display.specshow(librosa.amplitude_to_db(np.abs(C), ref=np.max), sr=sr, x_axis='time', y_axis='cqt_note') plt.colorbar(format='%+2.0f dB') plt.title('Constant-Q power spectrum') plt.tight_layout() y = librosa.icqt(C, sr) #, fmin=30, bins_per_octave=2, hop_length=2**8) librosa.output.write_wav('testOut.wav', y, sr, norm=True) plt.show()
def gl_cqt(S, n_iter=32, sr=22050, hop_length=512, bins_per_octave=12, fmin=None, window='hann', dtype=np.float32, length=None, momentum=0.99, random_state=None, res_type='kaiser_fast'): if fmin is None: fmin = librosa.note_to_hz('C1') if random_state is None: rng = np.random elif isinstance(random_state, int): rng = np.random.RandomState(seed=random_state) elif isinstance(random_state, np.random.RandomState): rng = random_state if momentum > 1: warnings.warn( 'Griffin-Lim with momentum={} > 1 can be unstable. Proceed with caution!' .format(momentum)) elif momentum < 0: raise ParameterError( 'griffinlim() called with momentum={} < 0'.format(momentum)) # randomly initialize the phase angles = np.exp(2j * np.pi * rng.rand(*S.shape)) # And initialize the previous iterate to 0 rebuilt = 0. for _ in range(n_iter): # Store the previous iterate tprev = rebuilt __import__('pdb').set_trace() # Invert with our current estimate of the phases inverse = icqt( S * angles, sr=sr, hop_length=hop_length, bins_per_octave=bins_per_octave, fmin=fmin, #window=window, length=length, res_type=res_type) window=window) # Rebuild the spectrogram rebuilt = cqt(inverse, sr=sr, bins_per_octave=bins_per_octave, n_bins=S.shape[0], hop_length=hop_length, fmin=fmin, window=window, res_type=res_type) # Update our phase estimates angles[:] = rebuilt - (momentum / (1 + momentum)) * tprev angles[:] /= np.abs(angles) + 1e-16 # Return the final phase estimates return icqt(S * angles, sr=sr, hop_length=hop_length, bins_per_octave=bins_per_octave, fmin=fmin, window=window, length=length, res_type=res_type)
# %% plt.figure(figsize=(20, 10), dpi=1200) plt.subplots_adjust(left=0,right=1,bottom=0,top=1, wspace=0, hspace=0) plt.margins(0, 0) plt.axis('off') STFT = librosa.stft(y) axes = librosa.display.specshow(librosa.amplitude_to_db(np.abs(STFT), ref=np.max), y_axis='log') axes.get_xaxis().set_visible(False) axes.get_yaxis().set_visible(False) plt.savefig('diss/spectrogram/stft.png', dpi=1200, bbox_inches='tight', pad_inches=0) plt.close() print('Saved sfft.png') # %% y_hat = librosa.icqt(C=CQT, sr=sr) librosa.output.write_wav('diss/data/librosa_cqt.wav', y_hat, sr) # %% y_hat = librosa.istft(STFT) librosa.output.write_wav('diss/data/librosa_stft.wav', y_hat, sr) # %% print(STFT) print(y_hat) print(y ) #%% STFT = librosa.stft(y) print(STFT.shape) print(STFT[:, 0 ])
def icqt(y, **kwargs): return np.stack([librosa.icqt(y[i, :], **kwargs) for i in range(y.shape[0])])
for o_slice in range(sz[1]): new_slice = [] for o_bin in range(sz[0]): index = o_slice * sz[0] + o_bin r = Rf[index] r *= 3 phi = Gf[index] phi *= 1800 phi -= (np.pi * 2.) # delta phase if (o_slice != 0): phi += o_previous_phases[o_bin] phi = phi % (np.pi * 2.) o_previous_phases[o_bin] = phi else: o_previous_phases.append(phi) new_bin = cmath.rect(r, phi) new_slice.append(new_bin) C_output.append(new_slice) C_output_np = np.asarray(C_output) y_hat = librosa.icqt(C=C_output_np, sr=sr, hop_length=hop_length, bins_per_octave=bins_per_octave) librosa.output.write_wav(output_path, y_hat, sr)