簡體   English   中英

如何從python中的麥克風獲取聲音輸入,並動態處理它?

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

問候,

我正在嘗試用 Python 編寫一個程序,該程序每次在麥克風中輕敲時都會打印一個字符串。 當我說“敲擊”時,我的意思是突然發出很大的噪音或類似的聲音。

我在 SO 中搜索並找到了這篇文章: 識別音頻的音調

我認為 PyAudio 庫會滿足我的需要,但我不太確定如何讓我的程序等待音頻信號(實時麥克風監控),以及當我得到一個如何處理它(我是否需要使用傅立葉變換像它是在上面的帖子中指示的)?

預先感謝您能給我的任何幫助。

如果您使用的是 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)

...當我得到一個如何處理它時(我是否需要像上一篇文章中所指示的那樣使用傅立葉變換)?

如果你想要一個“抽頭”,那么我認為你對幅度比頻率更感興趣。 所以傅立葉變換可能對您的特定目標沒有用。 您可能想要對輸入的短期(比如 10 毫秒)幅度進行運行測量,並檢測它何時突然增加某個增量。 您需要調整以下參數:

  • 什么是“短期”幅度測量
  • 您尋找的增量增量是多少
  • 增量變化必須以多快的速度發生

雖然我說你對頻率不感興趣,但你可能想先做一些過濾,過濾掉特別是低頻和高頻成分。 這可能會幫助您避免一些“誤報”。 你可以用 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

……還有很多其他的例子……

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM