[英]Python Detect Audio by Microphone
我只想檢測 Python 中的音頻並停止我的麥克風。 有什么簡單的方法可以做到嗎? 我試過 pyaudio,但是它很復雜,想快速做一個項目。 無論哪種方式,它都只記錄音頻,但不用於動態檢測。 所以,請提供其他可以使用的模塊。
您可以使用聲音設備sounddevice
。 通過啟動InputStream
並監視傳入的緩沖區級別,您也許可以實現既定目標。
在這種方法中,麥克風始終處於“開啟”狀態,但您僅在沒有語音時捕獲數據。
下面的骨架示例
import sounddevice as sd
import Queue
buffer_size = 4410 # 100 ms worth of samples at 44.1 kHz
mic_stream = sd.InputStream(blocksize=buffer_size) # also need to specify samplerate and device etc.
recording_condition = True # a variable that defines when the recording should be stopped
mic_stream.start() # start the InputStream
audio_queue = Queue.Queue()
while recording_condition:
audio_buffer, success = mic_stream.read(buffer_size)
# here use your speech detection algorithm
speech_in_buffer = your_speech_detector(audio_buffer)
if not speech_in_buffer:
audio_queue.put(audio_buffer)
一旦recording_condition
變為 False,您就可以獲取隊列中的所有音頻緩沖區
all_audio_buffer = [ audio_queue.get()[0] for i in range(self.audio_queue.qsize()) ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.