Created
November 27, 2023 14:29
-
-
Save chrisclark/b9e7ba61654313a1e2d4a796ad5bb8a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AUDIO_DEVICE is not defined. Am I missing a library?
Edit: Added this
Still not able to get it working. Python isn't my strong suit