python教程—如何在python中从麦克风获取声音输入,并动态处理它?-Python实用宝典

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

你好,我正在试着用Python写一个程序,它会打印一个字符串,每次它在麦克风点击。当我说“tap”的时候,我的意思是突然有很大的声音或者类似的东西。

问候,

我试着用Python写一个程序,它会打印一个字符串,每次它得到一个麦克风点击。当我说“tap”的时候,我的意思是突然有很大的声音或者类似的东西。

我在SO中搜索,发现了这篇文章:识别音频的音调

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

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

回答

如果您正在使用LINUX,您可以使用 pyalsaaudio < / >。对于windows,我们有PyAudio,还有一个名为 soundanalysis

我找到了一个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)

​Python实用宝典 (pythondict.com)
不只是一个宝典
欢迎关注公众号:Python实用宝典

本文由 Python实用宝典 作者:Python实用宝典 发表,其版权均为 Python实用宝典 所有,文章内容系作者个人观点,不代表 Python实用宝典 对观点赞同或支持。如需转载,请注明文章来源。
1

发表评论