[英]Audio Frequency in Python
I have made a script which checks the frequency of the sound from a wave file.我制作了一个脚本,用于检查波形文件中的声音频率。 But I keep getting an error in the 24 line which is
while len(data) == chunk*swidth:
.但是我一直在第 24 行出现错误,即
while len(data) == chunk*swidth:
。 Can anyone help me ??谁能帮我 ?? I am using PyAudio, wave and Numpy to do this.
我正在使用 PyAudio、wave 和 Numpy 来做到这一点。
# Read in a WAV and find the freq's
import pyaudio
import wave
import numpy as np
chunk = 2048
# open up a wave
wf = wave.open('output.wav', 'rb')
swidth = wf.getsampwidth()
RATE = wf.getframerate()
# use a Blackman window
window = np.blackman(chunk)
# open stream
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:
# 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)/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
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()
This is the error message Traceback (most recent call last): File "C:\\Users\\Admin\\Desktop\\Ishan\\Python\\Sublime Text Projects\\Sound Frequencies second.py", line 23, in <module> while len(data) == chunk*swidth: NameError: name 'data' is not defined
这是错误消息
Traceback (most recent call last): File "C:\\Users\\Admin\\Desktop\\Ishan\\Python\\Sublime Text Projects\\Sound Frequencies second.py", line 23, in <module> while len(data) == chunk*swidth: NameError: name 'data' is not defined
you use data in while, which defined later.您在 while 中使用数据,稍后定义。 use flag in while with post check, whittled preset as True to start while 1st round:
在进行后期检查时使用标志,减少预设为 True 以在第一轮开始:
# Read in a WAV and find the freq's
import pyaudio
import wave
import numpy as np
chunk = 2048
# open up a wave
wf = wave.open('output.wav', 'rb')
swidth = wf.getsampwidth()
RATE = wf.getframerate()
# use a Blackman window
window = np.blackman(chunk)
# open stream
p = pyaudio.PyAudio()
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = RATE,
output = True)
# play stream and find the frequency of each chunk
flag = True # << define flag and set to True to make while 1st stap
while flag:
# read some data << move under while to this plase
data = wf.readframes(chunk)
# 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)/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
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) << comment this line
if (len(data) != chunk*swidth): # << put check while condition
flag=False
if data:
stream.write(data)
# ^^^ I think you need to put in to the while loop upper
stream.close()
p.terminate()
I didn't watch working capacity as a whole, and only logical mistake at this stage with the cycle while and an entry condition.我没有从整体上看工作能力,只有这个阶段的逻辑错误,循环时间和进入条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.