def match(self, data_fft) : min_distance = da.distance(self.chords[0].chord_fft, data_fft) best_match = self.chords[0] for chord in self.chords : current_distance = da.distance(chord.chord_fft, data_fft) if current_distance < min_distance : best_match = chord min_distance = current_distance return best_match, min_distance
def learn_from_wave_file(self, file_path) : print "Learning for "+ self.name wf = wave.open(file_path, 'rb') dictionaries = [] p = pyaudio.PyAudio() stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True) data = wf.readframes(var.CHUNK) while data != '': dictionaries.append(da.normalize(da.getFft(data, var.RATE))) data = wf.readframes(var.CHUNK) stream.stop_stream() stream.close() # Average the dictionary mean_dictionnary = da.average_dictionary(dictionaries) print "Done Learning" threshold = 0 ; for fft_data in dictionaries : threshold += da.distance(fft_data, mean_dictionnary) threshold /= len(dictionaries) print "Done setting the threshold at " + str(threshold) real_list = [] for fft_data in dictionaries : if da.distance(fft_data, mean_dictionnary) < threshold : real_list.append(fft_data) new_mean = da.average_dictionary(real_list) print "Done filtering" print "Distance between the new and old means is " + str(da.distance(mean_dictionnary, new_mean)) threshold = 0 ; for fft_data in real_list : threshold += da.distance(fft_data, new_mean) threshold /= len(real_list) print "Done resetting the threshold at " + str(threshold) print len(real_list) self.chord_fft = new_mean self.threshold = threshold