【问题标题】:Python Detect Audio by MicrophonePython 通过麦克风检测音频
【发布时间】:2021-02-08 11:52:38
【问题描述】:

我只想在 Python 中检测音频并停止我的麦克风。有什么简单的方法可以做到吗?我试过 pyaudio,但是它很复杂,想快速做一个项目。无论哪种方式,它都只记录音频,但不用于动态检测。所以,请提供其他可以使用的模块。

【问题讨论】:

  • 你能详细解释一下你到底想要什么吗?
  • 我想制作一个可以检测声音并使麦克风静音的机器人。当我们停止说话时,它会打开麦克风。我唯一坚持的是声音的主动检测。

标签: python audio


【解决方案1】:

您可以使用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()) ]

【讨论】:

    猜你喜欢
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 2011-09-05
    • 2013-09-18
    • 2013-04-23
    • 2017-10-26
    • 2018-07-29
    相关资源
    最近更新 更多