小编典典

如何从Python中的麦克风获取声音输入,并即时处理它?

python

问候,

我正在尝试用Python编写一个程序,该程序每次在麦克风中被点击时都会打印一个字符串。当我说“敲击”时,是指突然的大声喧or或类似的声音。

我在SO中搜索,发现了以下信息:识别音频的音调

我认为PyAudio库可以满足我的需求,但是我不太确定如何使程序等待音频信号(实时麦克风监视),以及在获得如何处理它时(是否需要使用Fourier
Transform是在上面的帖子中指示的)?

预先感谢您能给我的任何帮助。


阅读 509

收藏
2020-12-20

共1个答案

小编典典

如果您使用的是LINUX,则可以使用pyALSAAUDIO。对于Windows,我们有PyAudio,还有一个名为SoundAnalyse的库。

我在这里找到了Linux的示例:

#!/usr/bin/python
## This is an example of a simple sound capture script.
##
## The script opens an ALSA pcm for sound capture. Set
## various attributes of the capture, and reads in a loop,
## Then prints the volume.
##
## To test it out, run it and shout at your microphone:

import alsaaudio, time, audioop

# Open the device in nonblocking capture mode. The last argument could
# just as well have been zero for blocking mode. Then we could have
# left out the sleep call in the bottom of the loop
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)

# Set attributes: Mono, 8000 Hz, 16 bit little endian samples
inp.setchannels(1)
inp.setrate(8000)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)

# The period size controls the internal number of frames per period.
# The significance of this parameter is documented in the ALSA api.
# For our purposes, it is suficcient to know that reads from the device
# will return this many frames. Each frame being 2 bytes long.
# This means that the reads below will return either 320 bytes of data
# or 0 bytes of data. The latter is possible because we are in nonblocking
# mode.
inp.setperiodsize(160)

while True:
    # Read data from device
    l,data = inp.read()
    if l:
        # Return the maximum of the absolute value of all samples in a fragment.
        print audioop.max(data, 2)
    time.sleep(.001)
2020-12-20