简体   繁体   English

如何从python中的麦克风获取声音输入,并动态处理它?

[英]How get sound input from microphone in python, and process it on the fly?

Greetings,问候,

I'm trying to write a program in Python which would print a string every time it gets a tap in the microphone.我正在尝试用 Python 编写一个程序,该程序每次在麦克风中轻敲时都会打印一个字符串。 When I say 'tap', I mean a loud sudden noise or something similar.当我说“敲击”时,我的意思是突然发出很大的噪音或类似的声音。

I searched in SO and found this post: Recognising tone of the audio我在 SO 中搜索并找到了这篇文章: 识别音频的音调

I think PyAudio library would fit my needs, but I'm not quite sure how to make my program wait for an audio signal (realtime microphone monitoring), and when I got one how to process it (do I need to use Fourier Transform like it was instructed in the above post)?我认为 PyAudio 库会满足我的需要,但我不太确定如何让我的程序等待音频信号(实时麦克风监控),以及当我得到一个如何处理它(我是否需要使用傅立叶变换像它是在上面的帖子中指示的)?

Thank you in advance for any help you could give me.预先感谢您能给我的任何帮助。

If you are using LINUX, you can use pyALSAAUDIO .如果您使用的是 LINUX,则可以使用pyALSAAUDIO For windows, we have PyAudio and there is also a library called SoundAnalyse .对于 Windows,我们有PyAudio ,还有一个名为SoundAnalyse的库。

I found an example for Linux here :我在这里找到了一个 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)

...and when I got one how to process it (do I need to use Fourier Transform like it was instructed in the above post)? ...当我得到一个如何处理它时(我是否需要像上一篇文章中所指示的那样使用傅立叶变换)?

If you want a "tap" then I think you are interested in amplitude more than frequency.如果你想要一个“抽头”,那么我认为你对幅度比频率更感兴趣。 So Fourier transforms probably aren't useful for your particular goal.所以傅立叶变换可能对您的特定目标没有用。 You probably want to make a running measurement of the short-term (say 10 ms) amplitude of the input, and detect when it suddenly increases by a certain delta.您可能想要对输入的短期(比如 10 毫秒)幅度进行运行测量,并检测它何时突然增加某个增量。 You would need to tune the parameters of:您需要调整以下参数:

  • what is the "short-term" amplitude measurement什么是“短期”幅度测量
  • what is the delta increase you look for您寻找的增量增量是多少
  • how quickly the delta change must occur增量变化必须以多快的速度发生

Although I said you're not interested in frequency, you might want to do some filtering first, to filter out especially low and high frequency components.虽然我说你对频率不感兴趣,但你可能想先做一些过滤,过滤掉特别是低频和高频成分。 That might help you avoid some "false positives".这可能会帮助您避免一些“误报”。 You could do that with an FIR or IIR digital filter;你可以用 FIR 或 IIR 数字滤波器做到这一点; Fourier isn't necessary.傅立叶不是必需的。

I know it's an old question, but if someone is looking here again... see https://python-sounddevice.readthedocs.io/en/0.4.1/index.html .我知道这是一个老问题,但如果有人再次在这里查看...请参阅https://python-sounddevice.readthedocs.io/en/0.4.1/index.html

It has a nice example "Input to Ouput Pass-Through" here https://python-sounddevice.readthedocs.io/en/0.4.1/examples.html#input-to-output-pass-through .它有一个很好的例子“输入到输出传递”在这里https://python-sounddevice.readthedocs.io/en/0.4.1/examples.html#input-to-output-pass-through

... and a lot of other examples as well ... ……还有很多其他的例子……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM