Skip to content

Instantly share code, notes, and snippets.

@chrisclark
Created November 27, 2023 14:29
Show Gist options
  • Save chrisclark/b9e7ba61654313a1e2d4a796ad5bb8a9 to your computer and use it in GitHub Desktop.
Save chrisclark/b9e7ba61654313a1e2d4a796ad5bb8a9 to your computer and use it in GitHub Desktop.

Revisions

  1. chrisclark created this gist Nov 27, 2023.
    72 changes: 72 additions & 0 deletions speech_visualization.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    %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()