Ale*_*lex 50 python microphone
问候,
我正在尝试用Python编写一个程序,每次在麦克风上点击时都会打印一个字符串.当我说'轻拍'时,我的意思是一声巨响或者类似的东西.
我在SO搜索并发现这篇文章:识别音频的音调
我认为PyAudio库可以满足我的需求,但我不太确定如何让我的程序等待音频信号(实时麦克风监控),当我得到一个如何处理它时(我是否需要使用傅里叶变换它是在上面的帖子中指示的)?
提前感谢您提供给我的任何帮助.
jbo*_*chi 41
如果您使用的是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)
Run Code Online (Sandbox Code Playgroud)
...当我得到一个如何处理它时(我是否需要像上面的帖子中指示的那样使用傅里叶变换)?
如果你想要"点击",那么我认为你对频率的振幅感兴趣.因此,傅里叶变换可能对您的特定目标没有用.您可能希望对输入的短期(例如10 ms)幅度进行运行测量,并检测它何时突然增加某个增量.您需要调整以下参数:
虽然我说你对频率不感兴趣,但你可能想先做一些过滤,以滤除特别低频和高频的元件.这可能会帮助你避免一些"误报".你可以用FIR或IIR数字滤波器做到这一点; 傅立叶不是必需的.
我知道这是一个老问题,但如果有人再次查看这里......请参阅https://python-sounddevice.readthedocs.io/en/0.4.1/index.html。
它有一个很好的示例“输入到输出传递”,此处https://python-sounddevice.readthedocs.io/en/0.4.1/examples.html#input-to-output-pass-through。
...还有很多其他例子...
| 归档时间: |
|
| 查看次数: |
97809 次 |
| 最近记录: |