%matplotlib qt import pyaudio import numpy as np import matplotlib.pyplot as plt from queue import Queue import threading import time from IPython.display import display, clear_output FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100 CHUNK = 1024 audio_queue = Queue() def flush_queue(): while not audio_queue.empty(): audio_queue.get() def audio_stream(): def audio_callback(in_data, frame_count, time_info, status): audio_queue.put(np.frombuffer(in_data, dtype=np.int16)) return (in_data, pyaudio.paContinue) audio = pyaudio.PyAudio() stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK, input_device_index=AUDIO_DEVICE, stream_callback=audio_callback) stream.start_stream() while stream.is_active(): time.sleep(0.1) stream.stop_stream() stream.close() audio.terminate() # Start audio stream in a separate thread thread = threading.Thread(target=audio_stream) thread.daemon = True thread.start() # Initialize Matplotlib plot fig, ax = plt.subplots() x = np.arange(0, 2 * CHUNK, 2) line, = ax.plot(x, np.zeros(CHUNK)) ax.set_ylim(-16000, 16000) ax.set_xlim(0, CHUNK) ax.axis('off') def update_plot(): try: while True: if not audio_queue.empty(): data = audio_queue.get() flush_queue() line.set_ydata(data) ax.relim() # Recompute the ax.dataLim ax.autoscale_view() # Update axes limits fig.canvas.draw() # Redraw the figure fig.canvas.flush_events() # Process GUI events time.sleep(0.1) except KeyboardInterrupt: pass update_plot()