我只想确定.mp3或.wav文件的频率并将其打印
import pyaudio
import wave
import numpy as 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 some data
data = wf.readframes(chunk)
# play stream and find the 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
which = fftData[1:].argmax() + 1
if which != 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 is %f Hz." % (thefreq)
else:
thefreq = which*RATE/chunk
print "The freq is %f Hz." % (thefreq)
# read some more data
data = wf.readframes(chunk)
if data:
stream.write(data)
stream.close()
p.terminate()
它不满足而len(数据)== chunk swidth:当我写的时候len(数据)!= chunk swidth:只有这样它才进入循环并显示错误信息valueerror:操作数不能与形状一起广播(4096)(2048)。如何解决这个问题?