def __init__(self, freq, start_time, duration, volume=1): # detremine note and MIDI value using librosa self._actual_freq = freq self.freq = librosa.note_to_hz(librosa.hz_to_note(freq)) self.note = librosa.hz_to_note(self.freq) self.midi = int(librosa.hz_to_midi(self.freq)) self.start_time = start_time self.duration = duration self.volume = volume
def bank(indata): # segment cycle into quarter-notes. # llen / 4 sized segments. # try to find the pitch of each quarter note. # save the audio to bank - marked with its pitch l, _ = indata.shape #q_size = int(l/4) q_size = int(l / 8) q = np.zeros((q_size, 2), dtype='float32') count = 0 for x in range(1, 8): q[:] = indata[count:count + q_size] pitches, magnetude = librosa.piptrack(y=q[:, 1], sr=SAMPLE_RATE, fmin=250.0, fmax=1050.0) _, ts = magnetude.shape print(ts) print(pitches.shape) index = max(magnetude[:, i].argmax() for i in range(ts)) prev = magnetude[:, 0].argmax() best_ts = 0 for i in range(ts): if magnetude[:, i].argmax() > prev: best_ts = i prev = magnetude[:, i].argmax() pitch = pitches[index, best_ts] if pitch != 0.0: note_info = librosa.hz_to_note(pitch, cents=True) # if your pretty close - >< 40cent its added to the notebank print(note_info) cents = int(note_info[0].replace('+', '-').split('-', 1)[-1]) print("Cents: ", cents) if cents < 25: note = librosa.hz_to_note(pitch, octave=False) print("Bank: ", note) keyId = note_names.index(note[0]) #if keyId in noteBank: #noteBank[keyId].append(q) #else: #noteBank[keyId] = list(q) noteBank[keyId] = q count += q_size
def melodia(self): logging.debug("Generating melody") df = self.df melodia = vamp.collect(self.y, self.sr, "mtg-melodia:melodia") hop, melody = melodia['vector'] timestamps = 8 * 128 / self.sr + np.arange( len(melody)) * (128 / self.sr) timestamps /= 2 melody_pos = melody[:] melody_pos[melody <= 0] = None df_melodia = pd.DataFrame(np.vstack([timestamps, melody]).T) #df_melodia.to_csv('/dataset/test/melodia.csv', index=False) df_melodia[1] = df_melodia[1].fillna(-1) #df_melodia = df_melodia[df_melodia[1]>0] #df_melodia[2] = librosa.hz_to_note(df_melodia[1], octave=False) #df_melodia[[0, 0, 2]].to_csv('%s.melody_note_all.txt' % self.file, sep='\t', header=False, index=False) tmp_df = pd.merge_asof(df_melodia, self.df_downbeats, left_on=0, right_on='beat_time') tmp_df = tmp_df[(tmp_df.beat_time > 0) & (tmp_df[1] > 0)] tmp_df['melody_note'] = tmp_df[1].map(self.write_melody_note) tmp_df['melody_note_ori'] = librosa.hz_to_note(tmp_df[1], octave=False) tmp_df = tmp_df.groupby([ 'beat_time' ]).agg(lambda x: scipy.stats.mode(x)[0]).reset_index(drop=False) tmp_df = tmp_df[['beat_time', 'melody_note', 'melody_note_ori']] self.df_melodia = tmp_df
def to_note_vector(indata): global song_key # TO DO: # overtones? are added as well, set some higher threshhold for mag or limit freq range to one reasonable for voice? # TRANSPOSE TO C pitches, magnetude = librosa.piptrack(y=indata[:,1],fmin=250.0,fmax=1050.0, sr=SAMPLE_RATE) nv = np.zeros(12) step = 1 _, ts_max = magnetude.shape for ts in range(0, ts_max, step): index = magnetude[:, ts].argmax() pitch = pitches[index, ts] if pitch != 0.0: note = librosa.hz_to_note(pitch, octave=False) note_idx = note_names.index(note[0]) nv[note_idx] += 1. for x in range(len(nv)): if nv[x] != 0: nv[x] = nv[x] / ts_max #transpose to C nv = np.roll(nv, transpose(song_key, note_names.index('C'))) print("Note vector: ", nv) return nv
def findnote(data, chunk, fs): freqlist = [] swidth = 1 window = np.blackman(chunk) while len(data) == chunk * swidth: # write data out to the audio stream # stream.write(data) # unpack the data and times by the hamming window indata = np.array(wave.struct.unpack("%dh" % (len(data) / swidth), \ data)) * window # Take the fft and square each value fftData = abs(np.fft.rfft(indata))**2 # find the maximum which = fftData[1:].argmax() + 1 # use quadratic interpolation around the max if which != len(fftData) - 1: y0, y1, y2 = np.log(fftData[which - 1:which + 2:]) x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0) # find the frequency and output it thefreq = (which + x1) * fs / chunk freqlist.append(thefreq) else: thefreq = which * fs / chunk freqlist.append(thefreq) hz = actualhertz(freqlist) note = librosa.hz_to_note(hz) print(note)
def pitchToNote(hz): note = "-" try: note = librosa.hz_to_note(hz) except OverflowError: pass return note
def hz_to_pcp(x, octave_invariant=True): n = np.array([['' if item==0 else \ librosa.hz_to_note(item, octave=not octave_invariant) \ for item in row] for row in x]) n_i = np.ndarray(n.shape, dtype=int) if octave_invariant: occ = ['', 'C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B'] else: occ = [''] + np.array([ \ [f'C{i}', f'C♯{i}', f'D{i}', f'D♯{i}', f'E{i}', f'F{i}', f'F♯{i}', f'G{i}', f'G♯{i}', f'A{i}', f'A♯{i}', f'B{i}'] \ for i in range(2,7)]).flatten().tolist() for (i, note) in enumerate(occ): n_i[np.where(n==note)] = i n_i = np.vstack((np.repeat(np.array(range(len(occ)))[...,None], x.shape[1], axis=1), n_i)) encoded = to_categorical(n_i[:,0]) for v in range(1,x.shape[1]): encoded += to_categorical(n_i[:,v]) encoded = encoded[len(occ):,...] encoded /= np.max(encoded) return occ, encoded
def get_notes(filename, duration): y, sr = librosa.load(filename) fmin = 73 fmax = 1108 n_bins = 256 harmonic = librosa.effects.harmonic(y) onsets = librosa.onset.onset_detect(harmonic) # get silence states rms = librosa.feature.rms(y=harmonic)[0] r_normalized = (rms - 0.01) / (np.std(rms) + 1e-9) p = np.exp(r_normalized) / (1 + np.exp(r_normalized)) transition = librosa.sequence.transition_loop(2, [0.5, 0.6]) full_p = np.vstack([1 - p, p]) states = librosa.sequence.viterbi_discriminative(full_p, transition) # drop silent onsets onsets_filtered = onsets[states[onsets] > 0] # silence start borders silence = np.nonzero(states[:-1] - states[1:] > 0)[0] # note borders borders = np.hstack([silence.reshape(1, -1), onsets_filtered.reshape(1, -1)])[0] borders = np.sort(borders) # get frequencies and aggregate them pitches, magnitudes = librosa.piptrack(harmonic, sr=sr, fmin=fmin, fmax=fmax) freq = pitches.max(axis=-1) bins = np.argmax(magnitudes, axis=0) bins_sync = librosa.util.sync(bins, borders, aggregate=np.median) states_sync = librosa.util.sync(states, borders, aggregate=np.median) pitch_sync = freq[bins_sync] pitch_sync[pitch_sync == 0] = 1e-6 # get notation and midi keys notes = librosa.hz_to_note(pitch_sync) midi = list(librosa.hz_to_midi(pitch_sync)) # check pauses pauses = np.nonzero(states_sync == 0)[0] for x in list(pauses): notes[int(x)] = 'P' midi[int(x)] = 'P' # check wrong notes for i, note in enumerate(notes): if note[:-1] not in notes_to_numbers.keys(): notes[i] = 'P' # add borders to borders and define notes lengths borders = np.append(borders, pitches.shape[-1]) borders = np.concatenate([np.array([0]), borders]) lengths = borders[1:] - borders[:-1] bpm = librosa.beat.tempo(y, sr=sr) melody = dict(notes=notes, lengths=list(lengths), midi=midi, bpm=bpm, duration=duration, raw_filename=filename.split('/')[-1]) return melody
def get_pitch(filename= read_and_write_audio()): x, sr_ = librosa.load(filename) pitches, mags = librosa.piptrack(y=x,sr=sr_) index = mags[:,128].argmax() pitch = pitches[index, 128] note_name = librosa.hz_to_note(pitch) if len(note_name) > 2: return note_name[0:2] return note_name[0]
def updateNotesFromHzes(self): """ 由notes更新频率 :return:None """ if self.__notes is None: self.__notes = [""] * 7 for i in np.arange(7): self.__notes[i] = librosa.hz_to_note(self.__hzes[i] / self.__scaling)
def toggle_y_axis(self): freqs = librosa.cqt_frequencies(300, fmin=librosa.note_to_hz('C2'), bins_per_octave=60) f_axis_dict = [] if self.radio_y_frequency.isChecked( ) and self.radio_cqt_option.isChecked(): freqs_formatted = ['%.2f' % elem for elem in freqs] f_axis_dict = list(dict(enumerate(freqs_formatted)).items()) self.p1.setLabel('left', "Frequency", units='Hz') major_f_ticks = f_axis_dict[::300 // 4] del f_axis_dict[::300 // 4] minor_f_ticks = f_axis_dict elif self.radio_y_note.isChecked() and self.radio_cqt_option.isChecked( ): notes = librosa.hz_to_note(freqs)[::5] temp_dict = {} for i, note in enumerate(notes): temp_dict[i * 5] = note f_axis_dict = list(temp_dict.items()) self.p1.setLabel('left', "Notes", units='') major_f_ticks = f_axis_dict[::60 // 4] del f_axis_dict[::60 // 4] minor_f_ticks = f_axis_dict elif self.radio_y_frequency.isChecked( ) and self.radio_plca_option.isChecked(): freqs_formatted = ['%.2f' % elem for elem in freqs] freqs = freqs_formatted[::5] f_axis_dict = list(dict(enumerate(freqs)).items()) self.p1.setLabel('left', "Frequency", units='Hz') major_f_ticks = f_axis_dict[::60 // 4] del f_axis_dict[::60 // 4] minor_f_ticks = f_axis_dict elif self.radio_y_note.isChecked( ) and self.radio_plca_option.isChecked(): notes = librosa.hz_to_note(freqs)[::5] f_axis_dict = list(dict(enumerate(notes)).items()) self.p1.setLabel('left', "Notes", units='') major_f_ticks = f_axis_dict[::60 // 4] del f_axis_dict[::60 // 4] minor_f_ticks = f_axis_dict newLeftTicks = self.p1.getAxis('left') newLeftTicks.setTicks([major_f_ticks, minor_f_ticks])
def get_pitch(filename): x, sr_ = librosa.load(filename) pitches, mags = librosa.piptrack(y=x, sr=sr_) index = mags[:, 128].argmax() pitch = pitches[index, 128] note_name = librosa.hz_to_note(pitch) if len(note_name) > 2: return f'{note_name[0]}#' return note_name[0]
def estimate_pitch_and_duration(x, onset_samples, i, sr): n0 = onset_samples[i] n1 = onset_samples[i + 1] f0 = estimate_pitch(x[n0:n1], sr) duration = (n1 - n0) / sr # return [librosa.hz_to_note(f0), f0, round(duration, 2)] return { 'onset': round(n0 / sr, 2), 'note': librosa.hz_to_note(f0), 'duration': round(duration, 2) }
def format_coord(x, y): # x, y are data coordinates # convert to display coords display_coord = current.transData.transform((x, y)) inv = other.transData.inverted() # convert back to data coords with respect to ax ax_coord = inv.transform(display_coord) coords = [ax_coord, (x, y)] return ('Time: {:.3f}, Frequency: {:.2f}, Note: {}.'.format( ax_coord[0], ax_coord[1], librosa.hz_to_note(ax_coord[1], cents=True)))
def audio_to_notes(audio,dur=None): print "###################################################" print "Audio to Notes" print "###################################################" if dur is not None: waveform,srate=librosa.load(audio,duration=dur) freq=np.abs(librosa.stft(waveform)) print "Frequencies:",freq notes=librosa.hz_to_note(freq) print "Notes:",notes return notes
def music2note2(i, unit_rate): note = [] for j in range(len(i)): #第一个非0值 if i[j] > 0 and len(note) == 0: #频率转换音符 note_str = librosa.hz_to_note((j + 1) * unit_rate) note.append(note_str) if len(note) == 1: return note[0] return None
def audio_to_notes(audio, dur=None): print "###################################################" print "Audio to Notes" print "###################################################" if dur is not None: waveform, srate = librosa.load(audio, duration=dur) freq = np.abs(librosa.stft(waveform)) print "Frequencies:", freq notes = librosa.hz_to_note(freq) print "Notes:", notes return notes
def compute_pitches(self, display_plot_frame=-1): overall_chromagram = Chromagram() for frame, x_frame in enumerate(frame_cutter(self.x, self.ham_samples)): x = wfir(x_frame, self.fs, 12) x_hi = _highpass_filter(x, self.fs) x_hi = numpy.clip(x_hi, 0, None) # half-wave rectification x_hi = lowpass_filter(x_hi, self.fs, 1000) # paper wants it x_lo = lowpass_filter(x, self.fs, 1000) x_sacf = _sacf([x_lo, x_hi]) x_esacf, harmonic_elim_plots = _esacf(x_sacf, self.n_peaks_elim, True) peak_indices = peakutils.indexes(x_esacf, thres=self.peak_thresh, min_dist=self.peak_min_dist) peak_indices_interp = peakutils.interpolate(numpy.arange( x_esacf.shape[0]), x_esacf, ind=peak_indices) chromagram = Chromagram() for i, tau in enumerate(peak_indices_interp): pitch = self.fs / tau try: note = librosa.hz_to_note(pitch, octave=False) chromagram[note] += x_esacf[peak_indices[i]] except ValueError: continue overall_chromagram += chromagram if frame == display_plot_frame: _display_plots( self.clip_name, self.fs, self.ham_samples, frame, x, x_lo, x_hi, x_sacf, x_esacf, harmonic_elim_plots, peak_indices, peak_indices_interp, ) return overall_chromagram
def compute_pitches(self, display_plot_frame=-1): overall_chromagram = Chromagram() # first C = C3 notes = librosa.cqt_frequencies(12, fmin=librosa.note_to_hz('C3')) self.specgram_to_plot = [] for n in range(12): for octave in range(1, self.num_octave + 1): for harmonic in range(1, self.num_harmonic + 1): f_candidate = notes[n] * octave * harmonic window_size = int((8 / f_candidate) * self.fs) chromagram = Chromagram() for frame, x_t in enumerate(frame_cutter(self.x, window_size)): real_window_size = max(x_t.shape[0], window_size) window = numpy.hanning(real_window_size) s, f = mlab.magnitude_spectrum(x_t, Fs=self.fs, window=window) s = s[:int(s.shape[0]/2)] f = f[:int(f.shape[0]/2)] s[s < 0] = 0.0 # clip might_append_1 = s.copy() might_append_2 = [] for _ in range(self.harmonic_elim_runs): max_freq_idx = s.argmax(axis=0) max_f = f[max_freq_idx] try: note = librosa.hz_to_note(max_f, octave=False) chromagram[note] += s[max_freq_idx] might_append_2.append((max_freq_idx, max_f, note)) except (ValueError, OverflowError): continue eliminated = [] for harmonic_index_multiple in range( 1, self.harmonic_multiples_elim ): elim_freq = harmonic_index_multiple * max_f elim_index = numpy.where(f == elim_freq) s[elim_index] -= s[elim_index] might_append_3 = s.copy() if frame == display_plot_frame: # plot once and stop display_plot_frame = -1 _display_plots(self.clip_name, self.x, ((might_append_1, might_append_2, might_append_3))) overall_chromagram += chromagram return overall_chromagram
def sort_bars(y, sr, f0, tempo): actual_notes = [] for f in f0: if not np.isnan(f): note = librosa.hz_to_note(f)[:-1].replace( "♯", "#") # strip off octave digit and fix formatting actual_notes.append(note) # actual_notes.append(librosa.hz_to_note(f)) # midi_notes = librosa.hz_to_midi(f0) # else: # actual_notes.append('') num_chords = get_num_bars(y, sr, tempo) formatted_notes = format_notes(actual_notes, num_chords) return formatted_notes
def pitch_spectral_hps(self, fft, f_s, buffer_rms): # fft.shape = (N/2+1), in which N is the fft window size # in fft array: # [0:amplitude(f_s/N), 1:amplitude(2*f_s/N), ..., N/2:applitude((N/2+1)*f_s/N)] order = 4; f_min = 27.5; # frequency of A_0 N = (fft.shape[0] - 1) * 2; # fft window size f_delta = f_s / N; # frequency increment step min_index = round(f_min / f_delta - 1); # index of the min frequency concerned in the fft result array concern_indices = np.arange(0, N // 2 // order); # concern_indices.shape = (N/2/order,) weights = [0.5, 0.3, 0.1, 0.1]; hps = weights[0] * fft[concern_indices]; # get amplitudes of concerned frequencies for i in range(1, order): hps += weights[i] * fft[(concern_indices + 1)*(i+1)-1]; #''' freqs = [i * f_s / N for i in range(hps.shape[0])]; plt.plot(freqs, hps); plt.title('hps'); plt.xlabel('frequency'); plt.ylabel('hps'); max_index = round(4186.009 / f_delta - 1); plt.axhline(np.median(hps[min_index:max_index+1]), 0, 1, color = 'green', label = 'median of hps'); plt.axhline(np.mean(hps[min_index:max_index+1]), 0, 1, color = 'yellow', label = 'mean of hps'); plt.axhline(order * buffer_rms, 0, 1, color = 'purple', label = 'rms'); plt.show(); #''' threshold = 2.5 * order * buffer_rms; filtered_indices = min_index + np.where(hps[np.arange(min_index, hps.shape[0])] > threshold)[0]; filtered_freqs = (filtered_indices + 1) * f_s / N; filtered_amp = fft[filtered_indices]; detected_notes = dict(); for (freq, value) in zip(filtered_freqs, filtered_amp): if hz_to_note(freq) not in detected_notes: detected_notes[hz_to_note(freq)] = value; else: detected_notes[hz_to_note(freq)] += value; return detected_notes;
def findnote2(filename): chunk = 2048 # open up a wave wf = wave.open(filename, 'rb') swidth = wf.getsampwidth() RATE = wf.getframerate() # use a Blackman window window = np.blackman(chunk) # open stream p = pyaudio.PyAudio() stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=RATE, output=True) # read some data data = wf.readframes(chunk) # play stream and find the frequency of each chunk freqlist = [] while len(data) == chunk * swidth: # write data out to the audio stream #stream.write(data) # unpack the data and times by the hamming window indata = np.array(wave.struct.unpack("%dh" % (len(data) / swidth), \ data)) * window # Take the fft and square each value fftData = abs(np.fft.rfft(indata))**2 # find the maximum which = fftData[1:].argmax() + 1 # use quadratic interpolation around the max if which != len(fftData) - 1: y0, y1, y2 = np.log(fftData[which - 1:which + 2:]) x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0) # find the frequency and output it thefreq = (which + x1) * RATE / chunk freqlist.append(thefreq) else: thefreq = which * RATE / chunk freqlist.append(thefreq) # read some more data data = wf.readframes(chunk) hz = actualhertz(freqlist) note = librosa.hz_to_note(hz) if data: stream.write(data) stream.close() p.terminate() return [note, hz]
def make_jam(freq_dict,sr,track_duration): """ this function creates a jam according to a dictionary that specifies each frequency's presence dict: keys are frequencies values are list of tuples (start_time, duration) of that frequency """ jam = jams.JAMS() # Store the track duration jam.file_metadata.duration = track_duration pitch_co = jams.Annotation(namespace='pitch_contour') note_h = jams.Annotation(namespace='note_hz') note_m = jams.Annotation(namespace='note_midi') pitch_cl = jams.Annotation(namespace='pitch_class') pitch_h = jams.Annotation(namespace='pitch_hz') pitch_m = jams.Annotation(namespace='pitch_midi') pitch_co.annotation_metadata = jams.AnnotationMetadata(data_source='synth') note_h.annotation_metadata = jams.AnnotationMetadata(data_source='synth') note_m.annotation_metadata = jams.AnnotationMetadata(data_source='synth') pitch_cl.annotation_metadata = jams.AnnotationMetadata(data_source='synth') pitch_h.annotation_metadata = jams.AnnotationMetadata(data_source='synth') pitch_m.annotation_metadata = jams.AnnotationMetadata(data_source='synth') #assign frequencies to each start_time freqs = freq_dict.keys() for f in freqs: time_dur = freq_dict[f] #list of tuples (start_time,duration) for t, dur in time_dur: pitch_co.append(time=t, duration=dur, value={"index":0,"frequency":f,"voiced":True}) note_h.append(time=t, duration=dur,value=f) note_m.append(time=t, duration=dur, value=librosa.hz_to_midi(f)) pclass = librosa.hz_to_note(f) pitch_cl.append(time=t, duration=dur,value={"tonic":pclass[:-1],"pitch":int(pclass[-1])}) pitch_h.append(time=t, duration=dur,value=f) pitch_m.append(time=t, duration=dur, value=librosa.hz_to_midi(f)) # Store the new annotation in the jam jam.annotations.append(pitch_co) jam.annotations.append(note_h) jam.annotations.append(note_m) jam.annotations.append(pitch_cl) jam.annotations.append(pitch_h) jam.annotations.append(pitch_m) return jam
def hz_to_note_zeros(annotation): ''' Special function so that zeros represent silence Input: Annotation List taken straight from mtrack Output: 1d np.array containing note names instead of frequencies ''' new_values = np.array([]) for a in annotation: new_a = '0' if a != 0: new_a = librosa.hz_to_note(a, cents=False) new_values = np.append(new_values, new_a) return new_values
def __init__(self, **kw): """ :param __notes: 七条弦依次对应的note(十二平均律标识) :param __a4_hz: a4对应的频率 :param __do: 唱名为__do的note标识 """ self.__notes = None self.__do = None self.__a4_hz = None self.__hzes = None self.__tones = None self.__scaling = 1 self.pos = self.cal_notesposition(0.125) self.__hui = None self.__harmony = None for key in kw: try: if key == "__notes": setattr(self, key, kw[key]) string_num = len(self.__notes) if string_num != 7: raise Exception( "string number err:__notes number err %d!" % string_num) self.__hzes = np.zeros(7) for i in np.arange(string_num): self.__hzes[i] = librosa.note_to_hz( self.__notes[i]) * self.__scaling if key == "__hzes": setattr(self, key, kw[key]) string_num = len(self.__hzes) if string_num != 7: raise Exception( "string number err:__hzes number err %d!" % string_num) self.__notes = [None] * 7 for i in np.arange(string_num): self.__notes[i] = librosa.hz_to_note(self.__hzes[i], cents=True) if key == "__a4_hz": setattr(self, key, kw[key]) self.__scaling = self.__a4_hz / 440.0 except Exception as e: print(e)
def music_to_note(data, rate): data = np.transpose(data) data = data / data.max() data[data < 0.75] = 0 unit_rate = rate / (data.shape[1] - 1) note_list = [] for i in data: note = [] for j in range(len(i)): if i[j] > 0 and len(note) == 0: note.append(librosa.hz_to_note((j + 1) * unit_rate)) if len(note) != 0: note_list.append(note[0]) return note_list
def note_calification(df_audio, df_mid): df_audio = df_audio.round(4) df_mid = df_mid.round(4) """The divisor is always the reference""" result = [] for i in range(0, len(df_audio)): delta = np.log2(df_audio.pitch[i] / df_mid.pitch[i]) * 1200 print(delta) if ((delta >= 1) and (delta <= 50)): nota = 0.5 elif ((delta <= -1) and (delta >= -50)): nota = -0.5 elif ((delta > -1) and (delta < 1)): nota = 1 else: nota = 0 if (df_audio.tempo[i] == df_mid.tempo[i]): dif = (df_audio.duration[i] - abs(df_audio.duration[i] - df_mid.duration[i])) / df_audio.duration[i] else: #I determine the note that should have been played #note : 'String' note name nota_base = delay_note(df_mid.tempo[i], df_mid.duration[i]) #Exercise dictionary dic_ejx = sec_to_note(df_audio.tempo[i]) ideal = dic_ejx.get(nota_base) dif = (ideal - abs(df_audio.duration[i] - ideal)) / ideal if dif <= 0.20: nota_tempo = 1 elif (dif > 0.20) & (dif < 0.50): nota_tempo = 0.5 else: nota_tempo = 0 result.append( [librosa.hz_to_note(df_mid.pitch[i]), nota, nota_base, nota_tempo]) r = pd.DataFrame(result, columns=['note', 'intonation', 'figure', 'duration']) wr_note = np.sum(r.intonation != 1) wr_time = np.sum(r.duration != 1) intonation = r.intonation.abs() overall = intonation.mean() + r.duration.mean() / 2 overall = np.round(overall, 2) * 100 return r, wr_note, wr_time, overall
def find_songKey(indata): # find key of recent layer global song_key pitches, magnetude = librosa.piptrack(y=indata[:,1], sr=SAMPLE_RATE, fmin=250.0,fmax=1050.0) time_slice = 1 index = magnetude[:, time_slice].argmax() pitch = pitches[index, time_slice] while pitch == 0.0: time_slice = (time_slice + 10)%len(pitches) index = magnetude[:, time_slice].argmax() pitch = pitches[index, time_slice] key = librosa.hz_to_note(pitch, octave=False) print("findKey: ",key) keyId = note_names.index(key[0]) song_key = keyId
def _display_plots(clip_name, fs, frame_size, x_dft, x, x_frame, dft_maxes): pltlen = frame_size samples = numpy.arange(pltlen) dftlen = int(x_dft.shape[0] / 2) dft_samples = numpy.arange(dftlen) fig1, (ax1, ax2) = plt.subplots(2, 1) ax1.set_title("x[n] - {0}".format(clip_name)) ax1.set_xlabel("n (samples)") ax1.set_ylabel("amplitude") ax1.plot(samples, x[:pltlen], "b", alpha=0.3, linestyle=":", label="x[n]") ax1.plot( samples, x_frame[:pltlen], "r", alpha=0.4, linestyle="--", label="x[n], frame + ham", ) ax1.grid() ax1.legend(loc="upper right") ax2.set_title("X (DFT)") ax2.set_xlabel("fft bin") ax2.set_ylabel("magnitude") ax2.plot(dft_samples, x_dft[:dftlen], "b", alpha=0.5, label="X(n)") for i, dft_max in enumerate(dft_maxes): left, mid, right = dft_max ax2.plot(left, x_dft[:dftlen][left], "rx") ax2.plot(mid, x_dft[:dftlen][mid], "go") ax2.plot(right, x_dft[:dftlen][right], color="purple", marker="x") pitch = fs / mid note = librosa.hz_to_note(pitch, octave=False) pitch = round(pitch, 2) if (i % 17) == 0: # displaying too many of these clutters the graph ax2.text(mid, 1.2 * x_dft[:dftlen][mid], "{0}\n{1}".format(pitch, note)) ax2.grid() ax2.legend(loc="upper right") plt.show()
def doScore(): print("1") # In[2]: input_wav = os.path.dirname(__file__) + r"/music/recorder.wav" y, sr = librosa.load(input_wav, sr=22050, duration=100) cent = librosa.feature.spectral_centroid(y=y, sr=sr) mod_r = cent[0].shape[0] % number_notes cut_count = int(cent[0].shape[0] - mod_r) merge_count = int(cut_count / number_notes) c = cent[0][:cut_count].reshape(number_notes, merge_count) print("2") frequencies = [] notes = [] for fs in c: fm = frequency_limit(fs.mean()) frequencies.append(fm) note = librosa.hz_to_note(fm, octave=True) str_list = list(note.replace('#', '')) str_list.insert(len(str_list) - 1, '-') note = ''.join(str_list) notes.append(note.upper()) unit = (totail_beats / number_notes) - (totail_beats / number_notes) % 0.25 temp_beat = 4 print("3") i = 0 beat_summry = 0 for note in notes: if unit * 2 > temp_beat: beat_summry += temp_beat note = "%s|%.2f" % (note, beat_summry) temp_beat = 4 else: temp_beat -= unit beat_summry += unit note = "%s|%.2f" % (note, beat_summry) notes[i] = note i += 1 return notes
def findnotestream(stream): chunk = 512 data = stream.read(chunk) fs = 48000 freqlist = [] swidth = 2 window = np.blackman(chunk) p = pyaudio.PyAudio() stream2 = p.open(format=pyaudio.paInt16, channels=1, rate=fs, output=True, frames_per_buffer=chunk) while len(data) == chunk * swidth: # write data out to the audio stream start = datetime.datetime.now() stream2.write(data) # unpack the data and times by the hamming window indata = np.array( wave.struct.unpack("%dh" % (len(data) / swidth), data)) * window # Take the fft and square each value fftData = abs(np.fft.rfft(indata))**2 # find the maximum which = fftData[1:].argmax() + 1 # use quadratic interpolation around the max if which != len(fftData) - 1: y0, y1, y2 = np.log(fftData[which - 1:which + 2:]) x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0) # find the frequency and output it thefreq = (which + x1) * fs / chunk freqlist.append(thefreq) else: thefreq = which * fs / chunk freqlist.append(thefreq) try: note = librosa.hz_to_note(thefreq) print(note, "delay", str((datetime.datetime.now() - start).microseconds / 100000)) except: pass data = stream.read(chunk, exception_on_overflow=False) stream.close() p.terminate() return [note, hz]
def __test(hz, note, octave, cents): note_out = librosa.hz_to_note(hz, octave=octave, cents=cents) eq_(note_out, note)
def __test(hz, note, octave, cents): note_out = librosa.hz_to_note(hz, octave=octave, cents=cents) assert note_out == note
def __test(hz, note, octave, cents): note_out = librosa.hz_to_note(hz, octave=octave, cents=cents) eq_(list(note_out), list([note]))