client.py 文件源码

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

项目:python-speech-recog 作者: whittlbc 项目源码 文件源码
def listen(self):
        service = cloud_speech_pb2.SpeechStub(self.make_channel('speech.googleapis.com', 443))

        # For streaming audio from the microphone, there are three threads.
        # First, a thread that collects audio data as it comes in
        with self.record_audio(self.RATE, self.CHUNK) as buff:
            # Second, a thread that sends requests with that data
            overlap_buffer = collections.deque(maxlen=self.SECS_OVERLAP * self.RATE / self.CHUNK)
            requests = self.request_stream(self._audio_data_generator(buff, overlap_buffer), self.RATE)
            # Third, a thread that listens for transcription responses
            recognize_stream = service.StreamingRecognize(
                requests, self.DEADLINE_SECS)

            # Exit things cleanly on interrupt
            signal.signal(signal.SIGINT, lambda *_: recognize_stream.cancel())

            # Now, put the transcription responses to use.
            try:
                while True:
                    self.listen_print_loop(recognize_stream, buff)

                    # Discard this stream and create a new one.
                    # Note: calling .cancel() doesn't immediately raise an RpcError
                    # - it only raises when the iterator's next() is requested
                    recognize_stream.cancel()

                    requests = self.request_stream(self._audio_data_generator(
                        buff, overlap_buffer), self.RATE)
                    # Third, a thread that listens for transcription responses
                    recognize_stream = service.StreamingRecognize(
                        requests, self.DEADLINE_SECS)

            except grpc.RpcError:
                # This happens because of the interrupt handler
                pass
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号