Page 500 - 3-2
P. 500

yield b''.join(data)






            def _fill_buffer(buff, in_data, frame_count, time_info, status_flags):
                """Continuously collect data from the audio stream, into the buffer."""

                buff.put(in_data)
                return None, pyaudio.paContinue






            # [START audio_stream]
            @contextlib.contextmanager

            def record_audio(rate, chunk):
                """Opens a recording stream in a context manager."""

                # Create a thread-safe buffer of audio data
                buff = queue.Queue()



                audio_interface = pyaudio.PyAudio()

                audio_stream = audio_interface.open(
                    format=pyaudio.paInt16,

                    # 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(_fill_buffer, buff),

                )



                yield _audio_data_generator(buff)



                audio_stream.stop_stream()
                audio_stream.close()

                # Signal the _audio_data_generator to finish
                buff.put(None)



                                                         - 500 -
   495   496   497   498   499   500   501   502   503   504   505