def raypitch(tid): count = 0 counttwo = 0 nownote = 0.0 lastnote = 0.0 #mydict = {} while True: #read data from audio input [length, data]=recorder.read() # convert to an array of floats floats = struct.unpack('f'*length,data) # copy floats into structure for i in range(len(floats)): fvec_write_sample(buf, floats[i], 0, i) # find pitch of audio frame freq = aubio_pitchdetection(detect,buf) # find energy of audio frame energy = vec_local_energy(buf) nownote = round(pitchtools.f2m( freq ),1) #if 36 != tid and 36.0 == nownote: # nownote = 101.0 #print "{:.2f} {:.2f} {:.2f}".format(freq, energy, nownote) if freq < 5000.0 and nownote < 100.0 and energy > 0.1: #1 and energy < 7 : #if freq in mydict: #if nownote in mydict: #mydict[freq] = mydict[freq] + 1 #mydict[nownote] = mydict[nownote] + 1 #else: #mydict[nownote] = 1 #mydict[freq] = 1 count = count + 1 if 100 > count: #for key, value in sorted(mydict.iteritems(), key=lambda (k,v): (v,k), reverse = True): #nownote = key #if nownote != lastnote: # lastnote = nownote # counttwo = 0 #else: # counttwo += 1 # if 1 == counttwo: # counttwo = 0 #mydict.clear() #count = 0 print( nownote ) #break return nownote
import pitchtools while True: ts_cmd = raw_input('Direction--> ') print (pitchtools.f2m(float(ts_cmd)))
# while len(data) == chunk*swidth: # write data out to the audio stream #stream.write(data) # unpack the data and times by the hamming window indata = np.array(wave.struct.unpack("%dh"%(len(data)*4/swidth),\ data))*window # Take the fft and square each value fftData=abs(np.fft.rfft(indata))**2 # find the maximum which = fftData[1:].argmax() + 1 # use quadratic interpolation around the max if which != len(fftData)-1: y0,y1,y2 = np.log(fftData[which-1:which+2:]) x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0) # find the frequency and output it thefreq = (which+x1)*RATE/chunk else: thefreq = which*RATE/chunk thenote = pitchtools.f2m(thefreq) #if thenote > 30 and thenote < 90: print "The freq is %f Hz. Note is %f" % (thefreq, thenote) except IOError, e: if e.args[1] == pyaudio.paInputOverflowed: pass else: raise stream.close() p.terminate()