def song_pitch(song, sr, threshold=None, freq_range=None, fft_step=None, fft_size=None): """Return an array of pitch values for the whole song.""" if threshold is None: threshold = 0.9 windows = get_windows(song, fft_step, fft_size) nb_windows = windows.shape[0] win_s = windows.shape[1] pitch_o = aubio_pitch("yin", 2048, win_s, sr) pitch_o.set_unit("freq") pitch_o.set_tolerance(threshold) pitches = np.zeros(nb_windows) for i, window in enumerate(windows): pitches[i] = pitch_o(window.astype(np.float32)) pitches[pitches > sr / 2] = 0 return pitches
def pitch(self, **options): algorithm = options.get("algorithm") or "yin" pitchUnit = options.get("pitchUnit") or "freq" tolerance = options.get("tolerance") or 0.85 sourceBuffer = source(self.audioFilename, self.samplerate, self.hopSize) samplerate = sourceBuffer.samplerate pitchSampler = aubio_pitch(algorithm, self.fftWindowSize, self.hopSize, self.samplerate) pitchSampler.set_unit(pitchUnit) pitchSampler.set_tolerance(tolerance) timings = [] frames = [] pitches = [] confidences = [] # total number of frames read totalFrames = 0 while True: samples, read = sourceBuffer() pitchSample = pitchSampler(samples)[0] confidence = pitchSampler.get_confidence() timings += [float(totalFrames) / samplerate] frames += [totalFrames] pitches += [pitchSample] confidences += [confidence] totalFrames += read if read < self.hopSize: break return { "timings": timings, "frames": frames, "pitches": pitches, "confidences": confidences }
def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): # Frame parameters setup if self._blocksize_s: self.input_blocksize = nextpow2(self._blocksize_s * samplerate) else: self.input_blocksize = 2048 if self._stepsize_s: self.input_stepsize = int(np.round(self._stepsize_s * samplerate)) else: self.input_stepsize = self.input_blocksize / 2 # Now that frames size metadata are properly set, we can do the set-up super(AubioPitch, self).setup(channels, samplerate, blocksize, totalframes) # Aubio Pitch set-up self.aubio_pitch = aubio_pitch("default", self.input_blocksize, self.input_stepsize, samplerate) self.aubio_pitch.set_unit("freq")
def notes(self, **options): onsetAlgorithm = options.get("onsetAlgorithm") or "default" algorithm = options.get("algorithm") or "yinfft" pitchUnit = options.get("pitchUnit") or "freq" tolerance = options.get("tolerance") or 0.85 sourceBuffer = source(self.audioFilename, self.samplerate, self.hopSize) onsetSampler = aubio_onset(onsetAlgorithm, self.fftWindowSize, self.hopSize, self.samplerate) pitchSampler = aubio_pitch(algorithm, self.fftWindowSize * 4, self.hopSize, self.samplerate) pitchSampler.set_unit(pitchUnit) pitchSampler.set_tolerance(tolerance) median = 6 isReady = 0 note_buffer = [] timeStarts = [] timeStops = [] frameStarts = [] frameStops = [] pitches = [] confidences = [] isOn = False totalFrames = 0 while True: samples, read = sourceBuffer() new_pitch = pitchSampler(samples)[0] curlevel = aubio_level_detection(samples, -90) note_buffer.insert(0, new_pitch) if len(note_buffer) > median: note_buffer.pop() if onsetSampler(samples): if curlevel == 1.: isReady = 0 else: isReady = 1 else: if isReady > 0: isReady += 1 if isReady == median: new_note = self.pseudo_median(note_buffer) if isOn: timeStops += [float(totalFrames) / self.samplerate] frameStops += [totalFrames] isOn = False if new_note > 45: confidence = pitchSampler.get_confidence() timeStarts += [float(totalFrames) / self.samplerate] frameStarts += [totalFrames] pitches += [new_note] confidences += [confidence] isOn = True totalFrames += read if read < self.hopSize: break if len(frameStops) < len(frameStarts): frameStops += [totalFrames] if len(timeStops) < len(timeStarts): timeStops += [timeStops] # timeStops += [timeStops] return { "timeStarts": timeStarts, "timeStops": timeStops, "frameStarts": frameStarts, "frameStops": frameStops, "pitches": pitches, "confidences": confidences }