def plot_spectral_rolfoff(number): example_mp3, sr, song_name = load_music.load_song(number) fig, ax = plt.subplots() rolloff = librosa.feature.spectral_rolloff(y=example_mp3, sr=sr, roll_percent=0.99) rolloff_min = librosa.feature.spectral_rolloff(y=example_mp3, sr=sr, roll_percent=0.01) S, phase = librosa.magphase(librosa.stft(example_mp3)) librosa.display.specshow(librosa.amplitude_to_db(S, ref=np.max), y_axis='log', x_axis='time', ax=ax) ax.plot(librosa.times_like(rolloff), rolloff[0], label='Roll-off frequency (0.99)') ax.plot(librosa.times_like(rolloff), rolloff_min[0], color='w', label='Roll-off frequency (0.01)') ax.legend(loc='lower right') ax.set(title='log Power spectrogram') fig.suptitle('Spectral rolfoff on ' + song_name, fontsize=8) plt.show()
def Beat_PLP_Show(self): #主要本地脉冲PLP估计 fig, ax = plt.subplots(nrows=3, sharex=True) librosa.display.specshow(librosa.power_to_db(self.__melspec, ref=np.max), x_axis='time', y_axis='mel', ax=ax[0]) ax[0].set(title='Mel spectrogram') ax[0].label_outer() ax[1].plot(librosa.times_like(self.__onset_env), librosa.util.normalize(self.__onset_env), label='Onset strength') ax[1].plot(librosa.times_like(self.__pulse), librosa.util.normalize(self.__pulse), label='Predominant local pulse (PLP)') ax[1].set(title='Uniform tempo prior [30, 300]') ax[1].label_outer() ax[2].plot(librosa.times_like(self.__onset_env), librosa.util.normalize(self.__onset_env), label='Onset strength') ax[2].plot(librosa.times_like(self.__pulse_lognorm), librosa.util.normalize(self.__pulse_lognorm), label='Predominant local pulse (PLP)') ax[2].set(title='Log-normal tempo prior, mean=120', xlim=[5, 20]) ax[2].legend() plt.tight_layout() plt.show()
def Beat_Track_Show(self): #节拍跟踪器 hop_length = 512 fig, ax = plt.subplots(nrows=2, ncols=2, sharex=True) times = librosa.times_like(self.__onset_env_record, sr=self.__sr_record, hop_length=hop_length) M = librosa.feature.melspectrogram(y=self.__y_record, sr=self.__sr_record, hop_length=hop_length) # librosa.display.specshow(librosa.power_to_db(M, ref=np.max), # y_axis='mel', x_axis='time', hop_length=hop_length, # ax=ax[0]) librosa.display.specshow(self.__logmelspec_record, sr=self.__sr_record, x_axis='time', y_axis='mel', ax=ax[0][0]) ax[0][0].label_outer() ax[0][0].set(title='Recording Mel spectrogram') ax[1][0].plot(times, librosa.util.normalize(self.__onset_env_record), label='Onset strength') ax[1][0].vlines( times[self.__beats_music], 0, 1, alpha=0.5, color='r', ############ linestyle='--', label='Beats') ax[1][0].legend() times = librosa.times_like(self.__onset_env_music, sr=self.__sr_music, hop_length=hop_length) M = librosa.feature.melspectrogram(y=self.__y_music, sr=self.__sr_music, hop_length=hop_length) librosa.display.specshow(self.__logmelspec_music, sr=self.__sr_music, x_axis='time', y_axis='mel', ax=ax[0][1]) ax[0][1].label_outer() ax[0][1].set(title='Music Mel spectrogram') ax[1][1].plot(times, librosa.util.normalize(self.__onset_env_music), label='Onset strength') ax[1][1].vlines(times[self.__beats_music], 0, 1, alpha=0.5, color='r', linestyle='--', label='Beats') ax[1][1].legend() plt.show()
def __init__(self, TEST, SAMPLE): self.test = TEST self.sample = SAMPLE self.dict = { "Larghissimo": 0, "Grave": 1, "Lento": 2, "Larghetto": 3, "Adagio": 4, "Andante": 5, "Moderato": 6, "Allegro": 7, "Vivace": 8, "Presto": 9, "Prestissimo": 10 } self.charact = [ "very, very slow (25 bpm and under)", "slow and solemn(25–45 bpm)", "slowly(45–60 bpm)", "rather broadly (60–66 bpm)", "slowly with great expression(66–76 bpm)", "at a walking pace (76–100 bpm)", "at a moderate speed (100–120 bpm)", "fast, quick, and bright (120–156 bpm) ", "lively and fast (156–176 bpm)", "very fast (176–200 bpm)", "very very fast (200 bpm and over)" ] self.tempoScore = 0 self.strengthScore = 0 self.accuracyScore = 0 self.result = "" self.result2 = "" self.y, self.sr = librosa.load(SAMPLE, sr=None, duration=None) self.y2, self.sr2 = librosa.load(TEST, sr=None, duration=None) self.o_env = librosa.onset.onset_strength(self.y, sr=self.sr) self.tempo = librosa.beat.tempo(onset_envelope=self.o_env, sr=self.sr) self.o_env2 = librosa.onset.onset_strength(self.y2, sr=self.sr2) self.tempo2 = librosa.beat.tempo(onset_envelope=self.o_env2, sr=self.sr2) self.onset_frames = librosa.onset.onset_detect(y=self.y, sr=self.sr) self.onset_time = librosa.frames_to_time(self.onset_frames, sr=self.sr) self.onset_frames2 = librosa.onset.onset_detect(y=self.y2, sr=self.sr2) self.onset_time2 = librosa.frames_to_time(self.onset_frames2, sr=self.sr2) self.chroma = librosa.feature.chroma_cqt(y=self.y, sr=self.sr) self.chroma2 = librosa.feature.chroma_cqt(y=self.y2, sr=self.sr2) self.note_values = self.getNotes(self.chroma) self.note_values2 = self.getNotes(self.chroma2) self.times = librosa.times_like(self.o_env, sr=self.sr) self.times2 = librosa.times_like(self.o_env2, sr=self.sr2)
def plotGraph(vocalsName, beatsName): vocalsFile = './spedUpVocals/{}'.format(vocalsName) + '.mp3' beatsFile = './spedUpBeats/{}'.format(beatsName) + '.mp3' # Compute local onset autocorrelation # y, sr = librosa.load('./output/funk/accompaniment.wav', duration=60) # musica1 y, sr = librosa.load(vocalsFile, duration=60) hop_length = 512 oenv = librosa.onset.onset_strength(y=y, sr=sr, hop_length=hop_length) tempogram = librosa.feature.tempogram(onset_envelope=oenv, sr=sr, hop_length=hop_length) # Compute global onset autocorrelation ac_global = librosa.autocorrelate(oenv, max_size=tempogram.shape[0]) ac_global = librosa.util.normalize(ac_global) # Estimate the global tempo for display purposes tempo = librosa.beat.tempo(onset_envelope=oenv, sr=sr, hop_length=hop_length)[0] fig, ax = plt.subplots(nrows=1, figsize=(120, 15)) times = librosa.times_like(oenv, sr=sr, hop_length=hop_length) # ax.plot(times, oenv, label='Onset strength') ax.plot(times, oenv, label='Vocal') ax.label_outer() ax.legend(frameon=True) # y, sr = librosa.load('./output/recairei/vocals.wav', duration=60) # musica2 y, sr = librosa.load(beatsFile, duration=60) hop_length = 512 oenv = librosa.onset.onset_strength(y=y, sr=sr, hop_length=hop_length) tempogram = librosa.feature.tempogram(onset_envelope=oenv, sr=sr, hop_length=hop_length) # Compute global onset autocorrelation ac_global = librosa.autocorrelate(oenv, max_size=tempogram.shape[0]) ac_global = librosa.util.normalize(ac_global) # Estimate the global tempo for display purposes tempo = librosa.beat.tempo(onset_envelope=oenv, sr=sr, hop_length=hop_length)[0] times = librosa.times_like(oenv, sr=sr, hop_length=hop_length) ax.plot(times, oenv, 'C2', label='Accompaniment') # ax.plot(times, oenv, 'C2',label='Onset strength') ax.label_outer() ax.legend(frameon=True) graphFileName = './graphs/{}-{}'.format(vocalsName, beatsName) os.makedirs(os.path.dirname(graphFileName), exist_ok=True) plt.savefig(graphFileName) plt.close()
def extract_bar_cqt(sr, wav_data): """ :param sr: Sample Rate of the Wav file :param wav_data: Single Channel Wav Data :return: splits of wav_data into bars by finding tempo dynamically """ onset_env = librosa.onset.onset_strength(y=wav_data, sr=sr) prior = scipy.stats.lognorm(loc=np.log(120), scale=120, s=1) pulse = librosa.beat.plp(onset_envelope=onset_env, sr=sr, hop_length=Config.HOP_LENGTH, prior=prior) beats_plp = np.flatnonzero(librosa.util.localmax(pulse)) times = librosa.times_like(pulse, sr=sr) frequencies = librosa.cqt_frequencies( n_bins=Config.N_BINS, fmin=Config.F_MIN, bins_per_octave=Config.BINS_PER_OCTAVE) cqt = np.abs( librosa.cqt(wav_data, sr=sr, fmin=Config.F_MIN, n_bins=Config.N_BINS, bins_per_octave=Config.BINS_PER_OCTAVE)) cqt_db = librosa.amplitude_to_db(cqt, ref=np.max) cqt_split = [] for i, b in enumerate(beats_plp[:-1]): cqt_split.append(cqt_db[:, b:beats_plp[i + 1]]) cqt_split.append(cqt_db[:, beats_plp[-1]:]) return cqt_split, times[beats_plp]
def get_student_music_f0(user_id, music_id): session = create_session() """ user = session.query(User).filter_by( name=user_name).first() """ user_id = user_id.replace("%", "|") music = session.query(Music).filter_by(user_id=user_id, id=music_id).first() f0 = music.fundamental_frequency(session) average = frequency_ave_data(music) times = librosa.times_like(f0, sr=48000) start, end = find_start_end(music) data = [] j = 0 if end < len(f0) - 2: end += 1 for i in range(max(0, start), end): if f0[i] >= 0: dic = {"x": round(times[i], 2), "y": round(f0[i], 4)} else: dic = {"x": round(times[i], 2), "y": 0} j += 1 data.append(dic) session.close() return jsonify({'average': average[1], 's': average[0], 'values': data})
def get_music_f0(music_id): session = create_session() user_id = g.current_user['sub'] music = session.query(Music).filter_by(user_id=user_id, id=music_id).first() f0 = music.fundamental_frequency(session) times = librosa.times_like(f0, sr=48000) average = frequency_ave_data(music) start, end = find_start_end(music) data = [] if end < len(f0) - 2: end += 1 j = 0 for i in range(max(0, start), end): if f0[i] >= 0: dic = {"x": round(times[i], 2), "y": round(f0[i], 4)} else: dic = {"x": round(times[i], 2), "y": 0} data.append(dic) j += 1 session.close() return jsonify({'average': average[1], 's': average[0], 'values': data})
def plot_poly_features(number): example_mp3, sr, song_name = load_music.load_song(number) S = np.abs(librosa.stft(example_mp3)) p0 = librosa.feature.poly_features(S=S, order=0) p1 = librosa.feature.poly_features(S=S, order=1) p2 = librosa.feature.poly_features(S=S, order=2) fig, ax = plt.subplots(nrows=4, sharex=True, figsize=(8, 8)) times = librosa.times_like(p0) ax[0].plot(times, p0[0], label='order=0', alpha=0.8) ax[0].plot(times, p1[1], label='order=1', alpha=0.8) ax[0].plot(times, p2[2], label='order=2', alpha=0.8) ax[0].legend() ax[0].label_outer() ax[0].set(ylabel='Constant term ') ax[1].plot(times, p1[0], label='order=1', alpha=0.8) ax[1].plot(times, p2[1], label='order=2', alpha=0.8) ax[1].set(ylabel='Linear term') ax[1].label_outer() ax[1].legend() ax[2].plot(times, p2[0], label='order=2', alpha=0.8) ax[2].set(ylabel='Quadratic term') ax[2].legend() librosa.display.specshow(librosa.amplitude_to_db(S, ref=np.max), y_axis='log', x_axis='time', ax=ax[3]) fig.suptitle('Poly features on ' + song_name, fontsize=8) plt.show()
def zero_crossing_rate_graph(data): fig, ax = plt.subplots(figsize=(10, 10)) times = librosa.times_like(data) ax.plot(times, data[0], label='zero crossing rate') ax.legend() ax.label_outer() plt.show()
def poly_graph(data): fig, ax = plt.subplots(figsize=(10, 10)) times = librosa.times_like(data) ax.plot(times, data[1].T, alpha=0.8, label='Poly Feature') ax.legend() ax.label_outer() plt.show()
def speech_onset(audio, sr, threshold = 0): # converting audio audio = audio_to_ndarray(audio) # tracking energy in speech onset_tracker = librosa.onset.onset_strength(y=audio, sr=sr) # determining corresponding time stamps (x_values) times = librosa.times_like(onset_tracker, sr=sr) # determining energy differential peaks (i.e. onset of speech) onset_peaks = librosa.onset.onset_detect(y=audio, sr=sr) # saving peaks that are above a threshold peak_max = 0 peaks_thresh = [] for peak in onset_peaks: if onset_tracker[peak] > threshold: peaks_thresh.append(peak) if onset_tracker[peak] > onset_tracker[peak_max]: peak_max = peak if peaks_thresh == []: onset_peaks = [peak_max] else: onset_peaks = peaks_thresh return times, onset_tracker, onset_peaks
def gen_plot(y, sr, hop_length, loc, **kwargs): import numpy as np import librosa import librosa.display import matplotlib.pyplot as plt o_env = librosa.onset.onset_strength(y=y, sr=sr, hop_length=hop_length) times = librosa.times_like(o_env, sr=sr) onset_frames = librosa.onset.onset_detect(onset_envelope=o_env, sr=sr, hop_length=hop_length, **kwargs) D = np.abs(librosa.stft(y)) plt.figure() ax1 = plt.subplot(2, 1, 1) librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max), x_axis='time', y_axis='log') plt.title('Power spectrogram') plt.subplot(2, 1, 2, sharex=ax1) plt.plot(times, o_env, label='Onset strength') plt.vlines(times[onset_frames], 0, o_env.max(), color='r', alpha=0.9, linestyle='--', label='Onsets') plt.axis('tight') plt.legend(frameon=True, framealpha=0.75) plt.savefig(loc)
def onsetplot(): song = song = song_box.get(ACTIVE) song = f'/home/ashwani/hamr-project-final/assets/audio1/{song}' y, sr = librosa.load(song) o_env = librosa.onset.onset_strength(y, sr=sr) times = librosa.times_like(o_env, sr=sr) onset_frames = librosa.onset.onset_detect(onset_envelope=o_env, sr=sr) D = np.abs(librosa.stft(y)) fig, ax = plt.subplots(nrows=2, sharex=True) librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max), x_axis='time', y_axis='log', ax=ax[0]) ax[0].set(title='Power spectrogram') ax[0].label_outer() ax[1].plot(times, o_env, label='Onset strength') ax[1].vlines(times[onset_frames], 0, o_env.max(), color='r', alpha=0.9, linestyle='--', label='Onsets') ax[1].legend() plt.show()
def plot_slices(path): wav, sr = librosa.load(path) librosa.display.waveplot(wav) o_env = librosa.onset.onset_strength(wav, sr=sr) times = librosa.times_like(o_env, sr=sr) onset_frames = librosa.onset.onset_detect(onset_envelope=o_env, sr=sr) D = np.abs(librosa.stft(wav)) plt.figure() ax1 = plt.subplot(2, 1, 1) librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max), x_axis='time', y_axis='log') plt.title('Power Spectrogram') plt.subplot(2, 1, 2, sharex=ax1) plt.plot(times, o_env, label='Onset strength') plt.vlines(times[onset_frames], 0, o_env.max(), color='r', alpha=0.9, linestyle='--', label='Onsets') plt.axis('tight') plt.legend(frameon=True, framealpha=0.75) plt.show()
def strikes_and_notes(path): y, fs = librosa.core.load(path, offset=0.0, duration=None) t = librosa.times_like(y, sr=fs) strikes = librosa.onset.onset_detect(y, sr=fs, units='samples') played_times = [] played_notes = [] for i in range(len(strikes)): if i == len(strikes) - 1: window = y[strikes[i]:min(len(y), 2 * strikes[i] - strikes[i - 1])] else: window = y[strikes[i]:strikes[i + 1]] f0, voiced_flag, voiced_probs = librosa.pyin( window, fmin=librosa.note_to_hz('C2'), fmax=librosa.note_to_hz('C7'), fill_na=None) f0_est = np.median(f0[~np.isnan(f0)]) if ~np.isnan(f0_est): played_notes.append(f0_est) played_times.append(librosa.samples_to_time(strikes, sr=fs)) return played_times, played_notes
def plot_spectral_bandwidth(number): example_mp3, sr, song_name = load_music.load_song(number) fig, ax = plt.subplots(nrows=2, sharex=True) spec_bw = librosa.feature.spectral_bandwidth(y=example_mp3, sr=sr) S, phase = librosa.magphase(librosa.stft(y=example_mp3)) times = librosa.times_like(spec_bw) centroid = librosa.feature.spectral_centroid(S=S) ax[0].semilogy(times, spec_bw[0], label='Spectral bandwidth') ax[0].set(ylabel='Hz', xticks=[], xlim=[times.min(), times.max()]) ax[0].legend() ax[0].label_outer() librosa.display.specshow(librosa.amplitude_to_db(S, ref=np.max), y_axis='log', x_axis='time', ax=ax[1]) ax[1].set(title='log Power spectrogram') ax[1].fill_between(times, centroid[0] - spec_bw[0], centroid[0] + spec_bw[0], alpha=0.5, label='Centroid +- bandwidth') ax[1].plot(times, centroid[0], label='Spectral centroid', color='w') ax[1].legend(loc='lower right') fig.suptitle('Spectral bandwidth on ' + song_name, fontsize=7) plt.show()
def main(): y, sr = librosa.load('/Users/asnyder/Documents/bbox.wav') o_env = librosa.onset.onset_strength(y, sr=sr) times = librosa.times_like(o_env, sr=sr) onset_frames = librosa.onset.onset_detect(onset_envelope=o_env, sr=sr, hop_length=128) D = np.abs(librosa.stft(y)) plt.figure() ax1 = plt.subplot(2, 1, 1) librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max), x_axis='time', y_axis='log') plt.title('Power spectrogram') plt.subplot(2, 1, 2, sharex=ax1) plt.plot(times, o_env, label='Onset strength') plt.vlines(times[onset_frames], 0, o_env.max(), color='r', alpha=0.9, linestyle='--', label='Onsets') plt.axis('tight') plt.legend(frameon=True, framealpha=0.75) plt.show()
def old_stuff(): """backup: old lingering stuff""" y, f0 = get_f0_series('/home/rafa/dev/sound/440-10-partials/440-10-partials.wav') # Overlay F0 over a spectrogram import matplotlib.pyplot as plt import numpy as np import librosa.display amplitude = np.abs(librosa.stft(y)) spectrum = librosa.amplitude_to_db(amplitude, ref=np.max) frequencies = librosa.fft_frequencies() fig, ax = plt.subplots() img = librosa.display.specshow(spectrum, x_axis='time', y_axis='log', ax=ax) ax.set(title='pYIN fundamental frequency estimation') fig.colorbar(img, ax=ax, format="%+2.f dB") times = librosa.times_like(f0) ax.plot(times, f0*FIFTY_CENTS_BWD, label='bwd', color='red', linewidth=1) ax.plot(times, f0, label='f0', color='cyan', linewidth=1) ax.plot(times, f0*FIFTY_CENTS_FWD, label='fwd', color='red', linewidth=1) ax.legend(loc='upper right') fig.savefig('plot.png')
def stft_from_sig( sig_wf: np.ndarray, frequency_sample_rate_hz: float, band_order_Nth: float ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: """ Librosa STFT is complex FFT grid, not power :param sig_wf: array with input signal :param frequency_sample_rate_hz: sample rate of frequency in Hz :param band_order_Nth: Nth order of constant Q bands :return: four numpy ndarrays with STFT, STFT_bits, time_stft_s, frequency_stft_hz """ sig_duration_s = len(sig_wf) / frequency_sample_rate_hz _, min_frequency_hz = scales.from_duration(band_order_Nth, sig_duration_s) order_Nth, cycles_M, quality_Q, \ frequency_center, frequency_start, frequency_end = \ scales.frequency_bands_g2f1(scale_order_input=band_order_Nth, frequency_low_input=min_frequency_hz, frequency_sample_rate_input=frequency_sample_rate_hz) # Choose the spectral resolution as the key parameter frequency_resolution_min_hz = np.min(frequency_end - frequency_start) frequency_resolution_max_hz = np.max(frequency_end - frequency_start) frequency_resolution_hz_geo = np.sqrt(frequency_resolution_min_hz * frequency_resolution_max_hz) stft_time_duration_s = 1 / frequency_resolution_hz_geo stft_points_per_seg = int(frequency_sample_rate_hz * stft_time_duration_s) # From CQT stft_points_hop, _, _, _, _ = \ scales.cqt_frequency_bands_g2f1(band_order_Nth, min_frequency_hz, frequency_sample_rate_hz, is_power_2=False) print('STFT Duration, NFFT, HOP:', len(sig_wf), stft_points_per_seg, stft_points_hop) STFT_Scaling = 2 * np.sqrt(np.pi) / stft_points_per_seg STFT = librosa.core.stft(sig_wf, n_fft=stft_points_per_seg, hop_length=stft_points_hop, win_length=None, window='hann', center=True, pad_mode='reflect') # Must be scaled to match scipy psd STFT *= STFT_Scaling STFT_bits = utils.log2epsilon(STFT) time_stft_s = librosa.times_like(STFT, sr=frequency_sample_rate_hz, hop_length=stft_points_hop) frequency_stft_hz = librosa.core.fft_frequencies( sr=frequency_sample_rate_hz, n_fft=stft_points_per_seg) return STFT, STFT_bits, time_stft_s, frequency_stft_hz
def get_beat_times(audio_file): y, sr = librosa.load(audio_file) onset_env = librosa.onset.onset_strength(y=y, sr=sr) pulse = librosa.beat.plp(onset_envelope=onset_env, sr=sr) beats_plp = np.flatnonzero(librosa.util.localmax(pulse)) times = librosa.times_like(onset_env, sr=sr) beat_times = times[beats_plp] return beat_times
def detect_onsets(y, lag=2): onset_env = librosa.onset.onset_strength(y, sr=global_sr, aggregate=np.median, lag=lag) _, beats = librosa.beat.beat_track(onset_envelope=onset_env, sr=global_sr) times = librosa.times_like(onset_env, sr=global_sr) return onset_env, beats, times
def wav2f0(y, sr): f0, voiced_flag, voiced_probs = librosa.pyin(y, fmin=librosa.note_to_hz('C2'), fmax=librosa.note_to_hz('C6')) # f0 = np.nan_to_num(f0) # get rid of nans f0_times = librosa.times_like(f0) # D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max) return f0, voiced_flag, voiced_probs, f0_times
def tatum_detect(self): onset = librosa.onset.onset_strength(y=self.audio, sr=self.fs) times = librosa.times_like(onset, sr=self.fs) onset_detect = librosa.onset.onset_detect(onset_envelope=onset, sr=self.fs) tatum_times = times[onset_detect] return tatum_times, onset
def test_times_like_scalar(): X = 7 sr = 22050 hop_length = 512 times = librosa.times_like(X, sr=sr, hop_length=hop_length) expected_times = np.arange(7) * hop_length / float(sr) assert np.allclose(times, expected_times)
def getbeats(self): self.preprocess() file = 'preprocessed/' + self.getmusicname()+'/drums.wav' y, sr = librosa.load(file) onset_env = librosa.onset.onset_strength(y, sr=sr, aggregate=np.median) pulse = librosa.beat.plp(onset_envelope=onset_env, sr=sr) #array de frames du rythme beats_plp = np.flatnonzero(librosa.util.localmax(pulse)) times = librosa.times_like(pulse, sr=sr) tempo = librosa.beat.tempo(onset_envelope=onset_env, sr=sr)[0] duration = librosa.get_duration(y, sr) return tempo, duration, times[beats_plp]
def compute_onset(self): print("Computing onset envelope and times...") self.onset_list = [] self.time_list = [] for i, raw in enumerate(tqdm(self.raw_list)): # compute onset envelopes oenv = librosa.onset.onset_strength(y=np.array(raw).astype('float'), sr=sample_rate, hop_length=hop_length) t = librosa.times_like(oenv, sr=sample_rate, hop_length=hop_length) self.onset_list.append(oenv) self.time_list.append(t)
def test_times_like_scalar(): X = 7 sr = 22050 hop_length = 512 times = librosa.times_like(X, sr=sr, hop_length=hop_length) expected_times = np.arange(7)*hop_length/float(sr) assert np.allclose(times, expected_times)
def pitch_estimation(self, wavpath): if os.path.exists(wavpath): y, sr = librosa.load(wavpath) f0, voiced_flag, voiced_probs = librosa.pyin( y, fmin=librosa.note_to_hz('B3'), fmax=librosa.note_to_hz('C5')) f0 = f0[~np.isnan(f0)] times = librosa.times_like(f0) level = optimize.curve_fit(lambda x, b: b, times, np.nan_to_num(f0))[0] pitch = np.around(level[0], decimals=3).astype(float) return pitch
def plot_energy(ax, pcen_full): # Now we'll plot the pcen curves times = librosa.times_like(pcen_full, sr=cfg['sample_rate'], hop_length=cfg['fft_hop_length']) ax.plot(times, pcen_full, linewidth=3, alpha=0.25, label='Full signal PCEN') #times = librosa.times_like(pcen_blocks, sr=sr, hop_length=cfg['fft_hop_length']) #ax.plot(times, pcen_blocks, linestyle=':', label='Block-wise PCEN') ax.legend()
def test_times_like(): X = np.ones((3, 4, 5)) sr = 22050 hop_length = 512 for axis in (0, 1, 2, -1): times = librosa.times_like(X, sr=sr, hop_length=hop_length, axis=axis) expected_times = np.arange(X.shape[axis]) * hop_length / float(sr) assert np.allclose(times, expected_times)
def test_times_like(): X = np.ones((3, 4, 5)) sr = 22050 hop_length = 512 for axis in (0, 1, 2, -1): times = librosa.times_like(X, sr=sr, hop_length=hop_length, axis=axis) expected_times = np.arange(X.shape[axis])*hop_length/float(sr) assert np.allclose(times, expected_times)
# Compute pcen on the magnitude spectrum. # We don't need to worry about initial and final filter delays if # we're doing everything in one go. P = librosa.pcen(np.abs(D), sr=sr, hop_length=hop_length) pcen_full = np.mean(P, axis=0) ##################################################################### # Plot the PCEN spectrum and the resulting magnitudes plt.figure() # First, plot the spectrum ax = plt.subplot(2,1,1) librosa.display.specshow(P, sr=sr, hop_length=hop_length, x_axis='time', y_axis='log') plt.title('PCEN spectrum') # Now we'll plot the pcen curves plt.subplot(2,1,2, sharex=ax) times = librosa.times_like(pcen_full, sr=sr, hop_length=hop_length) plt.plot(times, pcen_full, linewidth=3, alpha=0.25, label='Full signal PCEN') times = librosa.times_like(pcen_blocks, sr=sr, hop_length=hop_length) plt.plot(times, pcen_blocks, linestyle=':', label='Block-wise PCEN') plt.legend() # Zoom in to a short patch to see the fine details plt.xlim([30, 40]) # render the plot plt.tight_layout() plt.show()