#!/usr/bin/python from numpy import * from numpy.fft import * from pylab import * import wave import struct import os import sys import fcntl def main(): # Set stdin as non-blocking fcntl.fcntl(0, fcntl.F_SETFL, os.O_NONBLOCK) while(1): # Record 1 second of audio from microphone and clear screen os.system('arecord -d 1 -f S16_LE -c1 -r44100 temp.wav 2> /dev/null;clear') print("PyTune - A guitar tuner script! (hit return to quit)") audio = wave.open('temp.wav') frame_rate = audio.getframerate() num_frames = audio.getnframes() frames = audio.readframes(num_frames-1) frame_array = array(struct.unpack("%dh"%(num_frames-1), frames)) # Perform FFT on the recored wave file out_fft = fft(frame_array) freq_list = [] # Scan from 0 to 1Khz for i in range (0, 1000): freq_list.append((out_fft[i].real, i)) # Sort frequencies by magnitude, highest first. it = iter(sorted(freq_list, reverse=True)) # Display note name, if available freq_to_note(it.next()) try: sys.stdin.read() return except: pass def freq_to_note(freq_amp): amp = freq_amp[0] freq = freq_amp[1] notes = ['A','A#','B','C','C#','D', 'D#', 'E','F','F#','G','G#'] last = 0 for i in range (0,24): f = (((2.0)**(1.0/12.0))**i) * 110 if(freq == int(f)): print(notes[i%12]) elif(freq last) and (freq>100)): print(notes[i%12] + '-') last = f if __name__ == "__main__": main()