web-dev-qa-db-ja.com

pythonでマイクから音声入力を取得し、その場で処理する方法は?

ご挨拶、

私はPythonでプログラムを作成しようとしています。これは、マイクをタップするたびに文字列を印刷します。「タップ」と言うときは、大きな突然のノイズまたは類似のものを意味します。

SOで検索し、この投稿を見つけました: 音声のトーンを認識する

私はPyAudioライブラリが私のニーズに合っていると思いますが、プログラムをオーディオ信号を待機させる方法(リアルタイムのマイクモニタリング)と、それを処理する方法を取得したとき(フーリエ変換を使用する必要がありますか)上記の投稿で指示されました)?

助けてくれてありがとう。

49
Alex

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)
40
jbochi

...そして、私はそれを処理する方法を手に入れたとき(上記の投稿で指示されたようにフーリエ変換を使用する必要がありますか?)

「タップ」が必要な場合は、周波数よりも振幅に興味があると思います。したがって、フーリエ変換はおそらく特定の目的には役立ちません。おそらく、入力の短期(10ミリ秒など)の振幅の実行中の測定を行い、特定のデルタ分だけ突然増加したことを検出する必要があります。以下のパラメーターを調整する必要があります。

  • 「短期」振幅測定とは
  • あなたが探しているデルタの増加は何ですか
  • デルタ変更が発生する速さ

周波数には関心がないと言いましたが、特に低周波数成分と高周波数成分を除去するために、最初にいくつかのフィルター処理を行うことをお勧めします。これは、いくつかの「誤検知」を回避するのに役立ちます。 FIRまたはIIRデジタルフィルターを使用してこれを行うことができます。フーリエは必要ありません。

6
Craig McQueen