简体   繁体   English

Python 通过麦克风检测音频

[英]Python Detect Audio by Microphone

I just want to detect audio in Python and stop my Microphone.我只想检测 Python 中的音频并停止我的麦克风。 Any easy ways to do it?有什么简单的方法可以做到吗? I have tried pyaudio but, it is complex and want to do a project quickly.我试过 pyaudio,但是它很复杂,想快速做一个项目。 Either way it only records audio but is not used for on the fly detection.无论哪种方式,它都只记录音频,但不用于动态检测。 So, please provide other modules which can be used.所以,请提供其他可以使用的模块。

You could use the sounddevice package.您可以使用声音设备sounddevice By starting an InputStream and monitoring the incoming buffer levels, you could perhaps achieve the stated goal.通过启动InputStream并监视传入的缓冲区级别,您也许可以实现既定目标。

In this approach, the mic is always 'on', but you're only capturing the data when there is no speech.在这种方法中,麦克风始终处于“开启”状态,但您仅在没有语音时捕获数据。

A skeleton example below下面的骨架示例

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)

Once the recording_condition becomes False, you can then get all the audio buffers in the queue with一旦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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM