Maria@4: # -*- coding: utf-8 -*- Maria@4: """ Maria@4: Created on Tue Feb 2 22:26:10 2016 Maria@4: Maria@4: @author: mariapanteli Maria@4: """ Maria@4: import smoothiecore as s Maria@4: import numpy Maria@4: import scipy.signal Maria@4: # import librosa Maria@4: Maria@4: Maria@4: class PitchBihist: Maria@4: def __init__(self, win2sec=8): Maria@4: self.y = None Maria@4: self.sr = None Maria@4: self.chroma = None Maria@4: self.chromasr = None Maria@4: self.bihist = None Maria@4: self.win2sec = win2sec Maria@4: Maria@4: def bihist_from_chroma(self, filename='test.wav', secondframedecomp=True): Maria@4: self.chroma, self.chromasr = s.get_smoothie_for_bihist(filename=filename, hopinsec=0.005) Maria@4: if secondframedecomp: Maria@4: win2 = int(round(8*self.chromasr)) Maria@4: hop2 = int(round(0.5*self.chromasr)) Maria@4: nbins, norigframes = self.chroma.shape Maria@4: if norigframes0] Maria@4: melody = freqs Maria@4: n_frames = len(melody) Maria@4: melody_cents = hz_to_cents(melody, n_cents=n_bins) Maria@4: melody_octave = wrap_to_octave(melody_cents, octave_length=n_bins) Maria@4: melody_matrix = numpy.zeros((n_bins, n_frames)) Maria@4: for time, pitch in enumerate(melody_octave): Maria@4: if not numpy.isnan(pitch): Maria@4: melody_matrix[int(pitch), time] = 1 Maria@4: if secondframedecomp: Maria@4: win2 = int(round(self.win2sec*self.chromasr)) Maria@4: hop2 = int(round(0.5*self.chromasr)) Maria@4: nbins, norigframes = melody_matrix.shape Maria@4: if norigframes 0] = 0 Maria@4: Maria@4: # multiply with original Maria@4: B = numpy.dot(tra.T, spec) Maria@4: Maria@4: # normalize Maria@4: mxB = numpy.max(B) Maria@4: mnB = numpy.min(B) Maria@4: if mxB != mnB: Maria@4: B = (B - mnB)/float(mxB-mnB) Maria@4: Maria@4: # circshift to highest? Maria@4: if align: Maria@4: ref = numpy.argmax(numpy.sum(spec, axis=0)) Maria@4: B = numpy.roll(B, -ref, axis=0) Maria@4: B = numpy.roll(B, -ref, axis=1) Maria@4: return B Maria@4: Maria@4: def bihist_from_precomp_chroma(self, align=False): Maria@4: win2 = int(round(self.win2sec*self.chromasr)) Maria@4: hop2 = int(round(0.5*self.chromasr)) Maria@4: nbins, norigframes = self.chroma.shape Maria@4: if norigframes