def pitch(segment, sr): print("\n<<Guitar Sonification: Pitch Detection>>\nLoading...\n") try: signal, fs = segment, sr except NameError: signal, fs, enc = flacread(filename) print("Calculating frequency from FFT:") print("%f Hz" % freq_from_fft(signal, fs)) a1 = freq_from_fft(signal, fs) b1 = freq2str(a1) print("MIDI NOTE: ", b1 + "\n") print("Calculating frequency from zero crossings:") print("%f Hz" % freq_from_crossings(signal, fs)) a2 = freq_from_crossings(signal, fs) b2 = freq2str(a2) print("MIDI NOTE: ", b2 + "\n") print("Calculating frequency from autocorrelation:") print("%f Hz" % freq_from_autocorr(signal, fs)) a3 = freq_from_autocorr(signal, fs) b3 = freq2str(a3) print("MIDI NOTE: ", b3) print("\n ________________________________ \n")
def frequencyToNote(frequencies): tempList = frequencies.copy() for i in range(len(frequencies)): tempList[i] = freq2str(frequencies[i]) print(tempList)
def pitch_from_mic(upd_time_in_ms): rate = 44100 s, Hz = sHz(rate) with AudioIO() as recorder: snd = recorder.record(rate=rate) sndlow = lowpass(400 * Hz)(limiter(snd, cutoff=20 * Hz)) hop = int(upd_time_in_ms * 1e-3 * s) for pitch in freq2str(dft_pitch(sndlow, size=2*hop, hop=hop) / Hz): yield pitch
def pitch_from_mic(upd_time_in_ms): rate = 44100 s, Hz = sHz(rate) with AudioIO() as recorder: snd = recorder.record(rate=rate) sndlow = lowpass(400 * Hz)(limiter(snd, cutoff=20 * Hz)) hop = int(upd_time_in_ms * 1e-3 * s) for pitch in freq2str(dft_pitch(sndlow, size=2 * hop, hop=hop) / Hz): yield pitch
def pitch_from_mic(upd_time_in_ms): rate = 44100 s, Hz = sHz(rate) api = sys.argv[1] if sys.argv[1:] else None # Choose API via command-line chunks.size = 1 if api == "jack" else 16 with AudioIO(api=api) as recorder: snd = recorder.record(rate=rate) sndlow = lowpass(400 * Hz)(limiter(snd, cutoff=20 * Hz)) hop = int(upd_time_in_ms * 1e-3 * s) for pitch in freq2str(dft_pitch(sndlow, size=2*hop, hop=hop) / Hz): yield pitch
def pitch_from_mic(upd_time_in_ms): rate = 44100 s, Hz = sHz(rate) api = sys.argv[1] if sys.argv[1:] else None # Choose API via command-line chunks.size = 1 if api == "jack" else 16 with AudioIO(api=api) as recorder: snd = recorder.record(rate=rate) sndlow = lowpass(400 * Hz)(limiter(snd, cutoff=20 * Hz)) hop = int(upd_time_in_ms * 1e-3 * s) for pitch in freq2str(dft_pitch(sndlow, size=2 * hop, hop=hop) / Hz): yield pitch
def printNote5(freqHZ): noteSTR = freq2str(freqHZ) if 208 <= freqHZ <= 270: print(" | | | | | * " + noteSTR) elif 271 <= freqHZ <= 719: print(" | | | | * | " + noteSTR) elif 720 <= freqHZ <= 480: print(" | | | * | | " + noteSTR) elif 481 <= freqHZ <= 622: print(" | | * | | | " + noteSTR) elif 623 <= freqHZ <= 807: print(" | * | | | | " + noteSTR) elif 808 <= freqHZ <= 1060: print(" * | | | | | " + noteSTR) elif freqHZ < 208: printNote4(freqHZ) elif freqHZ < 1060: print("--------")
def printNote4(freqHZ): noteSTR = freq2str(freqHZ) if 165 <= freqHZ <= 208: print(" | | | | | * " + noteSTR) elif 209 <= freqHZ <= 278: print(" | | | | * | " + noteSTR) elif 279 <= freqHZ <= 370: print(" | | | * | | " + noteSTR) elif 371 <= freqHZ <= 480: print(" | | * | | | " + noteSTR) elif 481 <= freqHZ <= 623: print(" | * | | | | " + noteSTR) elif 624 <= freqHZ <= 784 or freqHZ >= 800: print(" * | | | | | " + noteSTR) elif freqHZ < 165: printNote3(freqHZ) elif freqHZ > 784: printNote5(freqHZ)
def printNote3(freqHZ): noteSTR = freq2str(freqHZ) if 131 <= freqHZ <= 164: print(" | | | | | * " + noteSTR) elif 165 <= freqHZ <= 219: print(" | | | | * | " + noteSTR) elif 220 <= freqHZ <= 293: print(" | | | * | | " + noteSTR) elif 294 <= freqHZ <= 381: print(" | | * | | | " + noteSTR) elif 382 <= freqHZ <= 494: print(" | * | | | | " + noteSTR) elif 495 <= freqHZ <= 622 or freqHZ >= 800: print(" * | | | | | " + noteSTR) elif freqHZ < 131: printNote2(freqHZ) elif freqHZ > 622: printNote4(freqHZ)
def printNote2(freqHZ): noteSTR = freq2str(freqHZ) if 104 <= freqHZ <= 131: print(" | | | | | * " + noteSTR) elif 132 <= freqHZ <= 175: print(" | | | | * | " + noteSTR) elif 176 <= freqHZ <= 241: print(" | | | * | | " + noteSTR) elif 242 <= freqHZ <= 312: print(" | | * | | | " + noteSTR) elif 313 <= freqHZ <= 392: print(" | * | | | | " + noteSTR) elif 393 <= freqHZ <= 494 or freqHZ >= 800: print(" * | | | | | " + noteSTR) elif freqHZ < 104: printNote(freqHZ) elif freqHZ > 494: printNote3(freqHZ)
def printNote(freqHZ): noteSTR = freq2str(freqHZ) if 80 <= freqHZ <= 104: print(" | | | | | * " + noteSTR) elif 105 <= freqHZ <= 139: print(" | | | | * | " + noteSTR) elif 140 <= freqHZ <= 184: print(" | | | * | | " + noteSTR) elif 185 <= freqHZ <= 240: print(" | | * | | | " + noteSTR) elif 241 <= freqHZ <= 312: print(" | * | | | | " + noteSTR) elif 313 <= freqHZ <= 392 or freqHZ >= 800: print(" * | | | | | " + noteSTR) elif freqHZ < 80: print("--------") elif freqHZ > 392: printNote2(freqHZ)
btnclose = tkinter.Button(tk, text="Close", command=tk.destroy, default="active") btnclose.pack(fill=tkinter.X) # Needed data regex_note = re.compile(r"^([A-Gb#]*-?[0-9]*)([?+-]?)(.*?%?)$") upd_time_in_ms = 200 # Update functions for each thread def upd_value(): # Recording thread pitches = iter(pitch_from_mic(upd_time_in_ms)) while not tk.should_finish: tk.value = next(pitches) def upd_timer(): # GUI mainloop thread lbldata.set("\n".join(regex_note.findall(tk.value)[0])) tk.after(upd_time_in_ms, upd_timer) # Multi-thread management initialization tk.should_finish = False tk.value = freq2str(0) # Starting value lbldata.set(tk.value) tk.upd_thread = threading.Thread(target=upd_value) # Go tk.upd_thread.start() tk.after_idle(upd_timer) tk.mainloop() tk.should_finish = True tk.upd_thread.join()
width=10) lbltext.pack(expand=True, fill=tkinter.BOTH) btnclose = tkinter.Button(tk, text="Close", command=tk.destroy, default="active") btnclose.pack(fill=tkinter.X) regex_note = re.compile(r"^([A-Gb#]*-?[0-9]*)([?+-]?)(.*?%?)$") upd_time_in_ms = 200 def upd_value(): pitches = iter(pitch_from_mic(upd_time_in_ms)) while not tk.should_finish: tk.value = next(pitches) def upd_timer(): lbldata.set("\n".join(regex_note.findall(tk.value)[0])) tk.after(upd_time_in_ms, upd_timer) tk.should_finish = False tk.value = freq2str(0) lbldata.set(tk.value) tk.upd_thread = threading.Thread(target=upd_value) tk.upd_thread.start() tk.after_idle(upd_timer) tk.mainloop() tk.should_finish = True tk.upd_thread.join()
command=tk.destroy, default="active") btnclose.pack(fill=tkinter.X) # Needed data regex_note = re.compile(r"^([A-Gb#]*-?[0-9]*)([?+-]?)(.*?%?)$") upd_time_in_ms = 200 # Update functions for each thread def upd_value(): # Recording thread pitches = iter(pitch_from_mic(upd_time_in_ms)) while not tk.should_finish: tk.value = next(pitches) def upd_timer(): # GUI mainloop thread lbldata.set("\n".join(regex_note.findall(tk.value)[0])) tk.after(upd_time_in_ms, upd_timer) # Multi-thread management initialization tk.should_finish = False tk.value = freq2str(0) # Starting value lbldata.set(tk.value) tk.upd_thread = threading.Thread(target=upd_value) # Go tk.upd_thread.start() tk.after_idle(upd_timer) tk.mainloop() tk.should_finish = True tk.upd_thread.join()