python 2.7 - How to separate frequency of audiofile -
i want determine frequencies of .mp3 or .wav file , print it
import pyaudio import wave import numpy np chunk = 2048 wf = wave.open('/home/pi/adhuri.wav', 'rb') swidth = wf.getsampwidth() rate = wf.getframerate() window = np.blackman(chunk) p = pyaudio.pyaudio() stream = p.open(format = p.get_format_from_width(wf.getsampwidth()), channels = wf.getnchannels(), rate = rate, output = true) # read data data = wf.readframes(chunk) # play stream , find frequency of each chunk while len(data) == chunk*swidth: print "ok" stream.write(data) indata = np.array(wave.struct.unpack("%dh"%(len(data)/swidth),\ data))*window fftdata=abs(np.fft.rfft(indata))**2 = fftdata[1:].argmax() + 1 if != len(fftdata)-1: y0,y1,y2 = np.log(fftdata[which-1:which+2:]) x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0) thefreq = (which+x1)*rate/chunk print "the freq %f hz." % (thefreq) else: thefreq = which*rate/chunk print "the freq %f hz." % (thefreq) # read more data data = wf.readframes(chunk) if data: stream.write(data) stream.close() p.terminate()
it not satisfy while len(data) == chunkswidth: , when writing while len(data) != chunkswidth: enters loop , shows error message valueerror: operands not broadcast shapes (4096)(2048) . how solve problem?
Comments
Post a Comment