ben.james.arm
ben.james.arm

Reputation: 21

PyAudio callback not running during blocking operation

I'm writing an audio processing script which listens for audio and runs speech recognition on it. I'm using a PyAudio callback function to capture audio frames and trigger recording/stop when the audio level is above a certain threshold.

The problem is that whenever speech recognition is running on a clip (in the main loop), the callback does not seem to run. This is confusing because I believe that the callback runs in a separate thread. If I replace the line speech = get_text(samples) with time.sleep(10), then the callback continues to be called whilst the main loop blocks. The callback also continues to work fine when I do something else like compute math.sin for 10 seconds.

My question is, what could possibly cause the callback to stop running in its own thread whilst running arbitrary code in the main thread, whilst time.sleep allows it?


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)

Upvotes: 2

Views: 364

Answers (0)

Related Questions