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

标签 python microphone

您好,

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

我在 SO 中搜索并找到了这篇文章:Recognising tone of the audio

我认为 PyAudio 库会满足我的需求,但我不太确定如何让我的程序等待音频信号(实时麦克风监控),以及当我得到一个如何处理它时(我需要使用傅里叶变换就像上面帖子中所指示的那样)?

提前感谢您能给我的任何帮助。

最佳答案

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

我找到了一个 Linux 示例 here :

#!/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)

关于python - 如何从 python 中的麦克风获取声音输入,并即时处理它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1936828/

相关文章:

python - 按字典中的值过滤项目

python - 使用 Passengers 在 Dreamhost 上执行 Django 教程时遇到问题

python matplotlib : how to automatically save figures in . 图格式?

javascript - Brave 浏览器中的 SpeechRecognition 发出网络错误事件

audio - 如何从 C++ 或 C 中的 USB 麦克风获取数据?

python - 如何在记录错误的同时向用户显示错误?

python - NLTK SVM 分类器终止

javascript - 从缓冲区播放声音

cordova - Ionic 2 - 使用蓝牙耳机录音

javascript - Mobile Safari 中 Javascript 中的麦克风访问