@contextlib.contextmanager
def record_audio(rate, chunk):
 buff = queue.Queue()
 audio_interface = pyaudio.PyAudio()
 audio_stream = audio_interface.open(format=pyaudio.paInt16, channels=1, rate=rate, input=True, frames_per_buffer=chunk, stream_callback=functools.partial(_fill_buffer, buff))
 (yield _audio_data_generator(buff))
 audio_stream.stop_stream()
 audio_stream.close()
 buff.put(None)
 audio_interface.terminate()

@ contextlib . contextmanager def record_audio ( rate , chunk ) : buff = queue . Queue ( ) audio_interface = pyaudio . PyAudio ( ) audio_stream = audio_interface . open ( format = pyaudio . paInt16 , channels = 1 , rate = rate , input = True , frames_per_buffer = chunk , stream_callback = functools . partial ( _fill_buffer , buff ) ) ( yield _audio_data_generator ( buff ) ) audio_stream . stop_stream ( ) audio_stream . close ( ) buff . put ( None ) audio_interface . terminate ( )
opens a recording stream in a context manager .