Skip to content

Real-time plotting example #106

Open
@vyhyb

Description

Hi, thank you again for your great library. I have a bit problems with real time plotting of gathered audio data (and its spectrum). I guess it is probably some threading issue, is it? Don't you know how to deal with this? I've tried several ways but I don't have any luck with updating the graph. Thanks in advance!

Code here:

import numpy as np
import queue
import sys
import threading

buffersize = 3
clientname = "ImpedanceTubeTest2"
rec_time = 10
q = queue.Queue(maxsize=buffersize*10)
global rec_end
rec_end = False
event = threading.Event()

def print_error(*args):
    print(*args, file=sys.stderr)


def xrun(delay):
    print_error("An xrun occured, increase JACK's period size?")


def shutdown(status, reason):
    print_error('JACK shutdown!')
    print_error('status:', status)
    print_error('reason:', reason)
    event.set()


def stop_callback(msg=''):
    if msg:
        print_error(msg)
    event.set()
    raise jack.CallbackExit


def process(frames):
    if frames != blocksize:
        stop_callback('blocksize must not be changed, I quit!')
    if rec_end:
        stop_callback()  # Recording is finished
    try:
        q.put_nowait(client.inports[0].get_array()[:])
    except (queue.Full):
        print("Full Queue")
        stop_callback()

from IPython.display import clear_output
from time import sleep
from scipy.signal import stft
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
size = 8
olap = 2
olap_inv = size - olap

global fig, ax
fig, ax = plt.subplots()

def animate(i, spectrum):
    line.set_ydata(spectrum)  # update the data.
    fig.canvas.draw()
    return line,

try:
    import jack

    client = jack.Client(clientname, no_start_server=True)
    blocksize = client.blocksize
    samplerate = client.samplerate
    FFT_size = size * blocksize
    overlap = olap * blocksize
    overlap_inv = olap_inv*blocksize
    client.set_xrun_callback(xrun)
    client.set_shutdown_callback(shutdown)
    client.set_process_callback(process)
    client.inports.register('in_1')
    data = np.zeros(FFT_size)
    init = stft(data, fs= samplerate, nperseg=FFT_size
    spectrum = init[2].T[0].real
    line, = ax.plot(init[0],spectrum)
   
    ani = FuncAnimation(
        fig, animate, interval=1, fargs=[spectrum])
    plt.show(block=False)
    sleep(2)
    with client:
        target_ports = client.get_ports(
            is_physical=True, is_output=True, is_audio=True)
        
        client.inports[0].connect(target_ports[0])
        timeout = blocksize * buffersize / samplerate
        for i in range(olap):
            data[i*blocksize:(i+1)*blocksize] = q.get(timeout=timeout)
        while True:
            try:
                for i in range(olap,size):
                    data[i*blocksize:(i+1)*blocksize] = q.get(timeout=timeout)
                
                stft_data = stft(data, fs= samplerate, nperseg=FFT_size)
                spectrum = stft_data[2]*np.conj(stft_data[2])/FFT_size
                # print(spectrum.max()) #peak output for reference
                # clear_output(wait=True) #reference
                data[0:overlap] = data[overlap_inv:FFT_size]
            except KeyboardInterrupt:
                print("Interrupted by user")
        rec_end = True
        event.wait()  # # # Wait until recording is finished
except (queue.Empty):
    # A timeout occured, i.e. there was an error in the callback
    print("Empty Queue")

Activity

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions