2

我正在编写一个音频处理脚本,它监听音频并在其上运行语音识别。我正在使用 PyAudio 回调函数来捕获音频帧并在音频电平高于某个阈值时触发录制/停止。

问题是,只要在剪辑上运行语音识别(在主循环中),回调似乎就不会运行。这令人困惑,因为我相信回调在单独的线程中运行。如果我用 替换该行speech = get_text(samples)time.sleep(10)则在主循环阻塞时继续调用回调。当我执行其他操作(例如计算math.sin10 秒)时,回调也继续正常工作。

我的问题是,什么可能导致回调停止在自己的线程中运行,同时在主线程中运行任意代码,同时time.sleep允许它?


def audio_callback(self, in_data, frame_count, time_info, flag):
        """ This gets called whenever the audio stream receives new data. """
    rms = audioop.rms(in_data, 2)
    if rms > AUDIO_TRIGGER_LEVEL:
        logger.debug("Recording")
        self.recording = True
        self.recording_quiet_count = 0
    else:
        if self.recording:
            self.recording_quiet_count += 1
        if self.recording_quiet_count > QUIET_CHUNKS_BEFORE_STOP:
            logger.debug("Stopping recording")
            self.recording = False
            self.recording_quiet_count = 0
            self.data_to_process = True

    if self.recording:
        self.recorded_frames.append(in_data)
    return ('', pyaudio.paContinue)

def run(self):
    while True:
        time.sleep(0.05)
        if self.data_to_process:
            logger.debug("Processing recording")
            tot_frames = CHUNK_SIZE * len(self.recorded_frames)
            frames = b''.join(self.recorded_frames)
            self.recorded_frames = []
            self.data_to_process = False
            samples = struct.unpack_from('<%dh' % tot_frames, frames)

            # This is the part causing issues
            speech = get_text(samples)
            # time.sleep(10)
4

0 回答 0