Skip to content

Instantly share code, notes, and snippets.

@chrisclark
Created November 27, 2023 14:29
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisclark/b9e7ba61654313a1e2d4a796ad5bb8a9 to your computer and use it in GitHub Desktop.
Save chrisclark/b9e7ba61654313a1e2d4a796ad5bb8a9 to your computer and use it in GitHub Desktop.
%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()
@daveroberts
Copy link

daveroberts commented Dec 2, 2023

AUDIO_DEVICE is not defined. Am I missing a library?

Edit: Added this

for i in range(0, numdevices):
  if (audio.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
    print("Input Device id ", i, " - ", audio.get_device_info_by_host_api_device_index(0, i).get('name'))

AUDIO_DEVICE = 0

Still not able to get it working. Python isn't my strong suit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment