client.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:python-speech-recog 作者: whittlbc 项目源码 文件源码
def record_audio(self, rate, chunk):
        """Opens a recording stream in a context manager."""
        # Create a thread-safe buffer of audio data
        buff = queue.Queue()

        audio_stream = self.audio_interface.open(
            format=self.FORMAT,
            # The API currently only supports 1-channel (mono) audio
            # https://goo.gl/z757pE
            channels=1, rate=rate,
            input=True, frames_per_buffer=chunk,
            # Run the audio stream asynchronously to fill the buffer object.
            # This is necessary so that the input device's buffer doesn't overflow
            # while the calling thread makes network requests, etc.
            stream_callback=functools.partial(self._fill_buffer, buff),
        )

        yield buff

        audio_stream.stop_stream()
        audio_stream.close()

        # Signal the _audio_data_generator to finish
        buff.put(None)
        self.audio_interface.terminate()  # [END audio_stream]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号