def run(self): global datastream global detected_chord block = True # The first read from the queue is blocking ... data = q.get(block=block) shift = len(data) datastream = np.roll(datastream, -shift, axis=0) datastream[-shift:, :] = data #datastream = data # framing audio nfft = 8192 hop_size = 4096 nFrames = int(np.round(len(datastream) / (nfft - hop_size))) x = np.append(datastream, np.zeros(nfft)) xFrame = np.empty((nfft, nFrames)) start = 0 chroma = np.empty((12, nFrames)) timestamp = np.zeros(nFrames) # compute PCP for n in range(nFrames): xFrame[:, n] = x[start:start + nfft] start = start + nfft - hop_size chroma[:, n] = compute_chroma(xFrame[:, n], int(args.samplerate/args.downsample)) if np.all(chroma[:, n] == 0): chroma[:, n] = np.finfo(float).eps else: chroma[:, n] /= np.max(np.absolute(chroma[:, n])) timestamp[n] = n * (nfft - hop_size) / int(args.samplerate/args.downsample) # get max probability path from Viterbi algorithm (PI, A, B) = initialize(chroma, templates, nested_cof) (path, states) = viterbi(PI, A, B) # normalize path for i in range(nFrames): path[:, i] /= sum(path[:, i]) # choose most likely chord - with max value in 'path' final_chords = [] indices = np.argmax(path, axis=0) final_states = np.zeros(nFrames) # find no chord zone set_zero = np.where(np.max(path, axis=0) < 0.3 * np.max(path))[0] if np.size(set_zero) is not 0: indices[set_zero] = -1 # identify chords for i in range(nFrames): if indices[i] == -1: final_chords.append('NC') else: final_states[i] = states[indices[i], i] final_chords.append(chords[int(final_states[i])]) detected_chord = Counter(final_chords).most_common() detected_chord = detected_chord[0][0] print('Chord: ' + str(detected_chord))
def compute_chord_for_frame(frame, frame_rate): frame = frame[::4] frame_rate = int(frame_rate / 4) chroma = compute_chroma(frame, frame_rate) if np.all(chroma == 0): chroma = np.finfo(float).eps else: chroma /= np.max(np.absolute(chroma)) #get max probability path from Viterbi algorithm (PI,A,B) = initialize(chroma, templates, nested_cof) (path, states) = viterbi(PI,A,B) #normalize path # for i in range(nFrames): path /= sum(path) #choose most likely chord - with max value in 'path' indicator = np.argmax(path,axis=0) #find no chord zone set_zero = np.where(np.max(path,axis=0) < 0.3*np.max(path)) if np.size(set_zero) is not 0: return 'NC' #identify chords final_state = states[indicator] final_chord = chords[int(final_state)] return final_chord
def get_chords(fs,s,chord): x = s[::4] x = x[:,1] fs = int(fs/4) #framing audio, window length = 8192, hop size = 1024 and computing PCP nfft = 8192 hop_size = 1024 nFrames = int(np.round(len(x)/(nfft-hop_size))) #zero padding to make signal length long enough to have nFrames x = np.append(x, np.zeros(nfft)) xFrame = np.empty((nfft, nFrames)) start = 0 chroma = np.empty((12,nFrames)) id_chord = np.zeros(nFrames, dtype='int32') timestamp = np.zeros(nFrames) max_cor = np.zeros(nFrames) #print(f'Chord: {chord}') #print('Time (s)', 'Chord') for n in range(nFrames): xFrame[:,n] = x[start:start+nfft] start = start + nfft - hop_size timestamp[n] = n*(nfft-hop_size)/fs chroma[:,n] = compute_chroma(xFrame[:,n],fs) plt.figure(1) plt.plot(chroma[:,n]) """Correlate 12D chroma vector with each of 24 major and minor chords""" cor_vec = np.zeros(24) for ni in range(24): cor_vec[ni] = np.correlate(chroma[:,n], np.array(templates[ni])) max_cor[n] = np.max(cor_vec) id_chord[n] = np.argmax(cor_vec) + 1 #if max_cor[n] < threshold, then no chord is played #might need to change threshold value id_chord[np.where(max_cor < 0.7*np.max(max_cor))] = 0 ident_chords = [] for n in range(nFrames): #print(timestamp[n],chords[id_chord[n]]) ident_chords.append(chords[id_chord[n]]) return ident_chords
def frameByFrame(self, jName='chord_templates.json'): tempJSON = readJSONChords(jName) templates = createChordTemplate(tempJSON) start = 0 chroma = np.empty((12, self.nFrames)) xFrame = np.empty((self.nfft, self.nFrames)) max_cor = np.zeros(self.nFrames) print('Time(s)', 'Chord') for n in range(self.nFrames): xFrame[:, n] = self.MONO[start:start + self.nfft] start = start + self.nfft - self.hopSize self.timestamp[n] = n * (self.nfft - self.hopSize) / self.fs chroma[:, n] = compute_chroma(xFrame[:, n], self.fs) self.TIME.append(chroma[:, n]) """Correlate 12D chroma vector with each of 24 major and minor chords""" # +1 7th cor_vec = np.zeros(24) for ni in range(24): cor_vec[ni] = np.correlate(chroma[:, n], np.array(templates[ni])) # print("CHORD", chords[ni + 1], " Cor:", cor_vec[ni]) # print("CHROMA") # print(chroma[:, n]) # print(" ") max_cor[n] = np.max(cor_vec) iMax = np.argmax(cor_vec) + 1 self.id_chord[n] = iMax # print(max_cor[n], " ", chords[id_chord[n]]) # if max_cor[n] < threshold, then no chord is played # might need to change threshold value # id_chord[np.where(max_cor[n] < 0.80 * np.max(max_cor))] = 0 for n in range(self.nFrames): if max_cor[n] > 0.80 * np.max(max_cor): self.id_chord[n] = 0 #This maps to N == NO CHORD #else: # ++self.id_chord[n] for n in range(self.nFrames): print(self.timestamp[n], chords[self.id_chord[n]])
#zero padding to make signal length long enough to have nFrames x = np.append(x, np.zeros(nfft)) xFrame = np.empty((nfft, nFrames)) start = 0 chroma = np.empty((12, nFrames)) id_chord = np.zeros(nFrames, dtype='int32') timestamp = np.zeros(nFrames) max_cor = np.zeros(nFrames) print('Time (s)', 'Chord') for n in range(nFrames): xFrame[:, n] = x[start:start + nfft] start = start + nfft - hop_size timestamp[n] = n * (nfft - hop_size) / fs chroma[:, n] = compute_chroma(xFrame[:, n], fs) """Correlate 12D chroma vector with each of 24 major and minor chords""" cor_vec = np.zeros(24) for ni in range(24): cor_vec[ni] = np.correlate(chroma[:, n], np.array(templates[ni])) max_cor[n] = np.max(cor_vec) id_chord[n] = np.argmax(cor_vec) + 1 plt.specgram(x, Fs=fs) #if max_cor[n] < threshold, then no chord is played #might need to change threshold value id_chord[np.where(max_cor < 0.8 * np.max(max_cor))] = 0 for n in range(nFrames): print(timestamp[n], chords[id_chord[n]])
def get_chord_progress(path, win_sec, shift_sec): with open('chord_templates.json', 'r') as fp: templates_json = json.load(fp) for chord in chords: templates.append(templates_json[chord]) """read audio and compute chromagram""" (fs, s) = read(path) #reduce sample rate and convert to mono x = s[::4] x = x[:, 1] fs = int(fs / 4) #framing audio # nfft = 8192 # hop_size = 1024 nfft = int(win_sec * fs) hop_size = int(shift_sec * fs) nFrames = int(np.round(len(x) / (nfft - hop_size))) #zero padding to make signal length long enough to have nFrames x = np.append(x, np.zeros(nfft)) xFrame = np.empty((nfft, nFrames)) start = 0 chroma = np.empty((12, nFrames)) timestamp = np.zeros(nFrames) #compute PCP for n in range(nFrames): xFrame[:, n] = x[start:start + nfft] start = start + nfft - hop_size chroma[:, n] = compute_chroma(xFrame[:, n], fs) if np.all(chroma[:, n] == 0): chroma[:, n] = np.finfo(float).eps else: chroma[:, n] /= np.max(np.absolute(chroma[:, n])) timestamp[n] = n * (nfft - hop_size) / fs #get max probability path from Viterbi algorithm (PI, A, B) = initialize(chroma, templates, nested_cof) (path, states) = viterbi(PI, A, B) #normalize path for i in range(nFrames): path[:, i] /= sum(path[:, i]) #choose most likely chord - with max value in 'path' final_chords = [] indices = np.argmax(path, axis=0) final_states = np.zeros(nFrames) #find no chord zone set_zero = np.where(np.max(path, axis=0) < 0.3 * np.max(path))[0] if np.size(set_zero) is not 0: indices[set_zero] = -1 #identify chords for i in range(nFrames): if indices[i] == -1: final_chords.append('NC') else: final_states[i] = states[indices[i], i] final_chords.append(chords[int(final_states[i])]) # print('Time(s)','Chords') # for i in range(nFrames): # print(timestamp[i], final_chords[i]) return final_chords[1:-1] # if __name__ == '__main__': # get_chord_progress('./score/Disney_Themes_-_Fantasmic.wav',0.5, 0.25)
nFrames = int(np.round(len(x)/(nfft-hop_size))) #zero padding to make signal length long enough to have nFrames x = np.append(x, np.zeros(nfft)) xFrame = np.empty((nfft, nFrames)) start = 0 chroma = np.empty((12,nFrames)) id_chord = np.zeros(nFrames, dtype='int32') timestamp = np.zeros(nFrames) max_cor = np.zeros(nFrames) print 'Time (s)', 'Chord' for n in range(nFrames): xFrame[:,n] = x[start:start+nfft] start = start + nfft - hop_size timestamp[n] = n*(nfft-hop_size)/fs chroma[:,n] = compute_chroma(xFrame[:,n],fs) plt.figure(1) plt.plot(chroma[:,n]) """Correlate 12D chroma vector with each of 24 major and minor chords""" cor_vec = np.zeros(24) for ni in range(24): cor_vec[ni] = np.correlate(chroma[:,n], np.array(templates[ni])) max_cor[n] = np.max(cor_vec) id_chord[n] = np.argmax(cor_vec) + 1 #if max_cor[n] < threshold, then no chord is played #might need to change threshold value id_chord[np.where(max_cor < 0.8*np.max(max_cor))] = 0 for n in range(nFrames):