Python中的音频

Posted

技术标签:

【中文标题】Python中的音频【英文标题】:Audio Frequency in Python 【发布时间】:2020-10-04 06:46:32 【问题描述】:

我制作了一个脚本,用于检查波形文件中声音的频率。但是我在第 24 行中不断收到错误,即while len(data) == chunk*swidth:。谁能帮我 ??我正在使用 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()

这是错误信息 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

【问题讨论】:

错误信息是什么?还有data的声明/流程在哪里? 给你了,你没有声明data 变量,但是你尝试得到它的长度 好吧,当您编写while len(data) == chunk*swidth: 时,您希望它如何确定len(data) 等于什么? data 的值应该从哪里来? # read some data data = wf.readframes(chunk) 您的意思是不是要将其中的一部分放在单独的行上,而不是放在评论中? 【参考方案1】:

你在while中使用data,稍后定义。 在后检查中使用标志,在第一轮开始时将预设为 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()

我没有看整体的工作能力,只是这个阶段的逻辑错误,循环while和一个进入条件。

【讨论】:

我已经修改了所有代码的帖子。我没有看整体的工作能力,只是在这个阶段与循环while和进入条件的逻辑错误。 我不使用流方法,但我认为:if data: stream.write(data) - 你需要放入while循环

以上是关于Python中的音频的主要内容,如果未能解决你的问题,请参考以下文章

Python3 批量提取视频中的音频

Python3 批量提取视频中的音频

python中的视频和音频处理库

Python 中的 Google Talk/XMPP 音频支持

Python中的音频频率

如何使用Python来提取视频中的音频