Skip to content

Real-time plotting example #106

Description

@vyhyb

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:

importnumpyasnpimportqueueimportsysimportthreadingbuffersize=3clientname="ImpedanceTubeTest2"rec_time=10q=queue.Queue(maxsize=buffersize*10)
globalrec_endrec_end=Falseevent=threading.Event()
defprint_error(*args):
print(*args, file=sys.stderr)
defxrun(delay):
print_error("An xrun occured, increase JACK's period size?")
defshutdown(status, reason):
print_error('JACK shutdown!')
print_error('status:', status)
print_error('reason:', reason)
event.set()
defstop_callback(msg=''):
ifmsg:
print_error(msg)
event.set()
raisejack.CallbackExitdefprocess(frames):
ifframes!=blocksize:
stop_callback('blocksize must not be changed, I quit!')
ifrec_end:
stop_callback() # Recording is finishedtry:
q.put_nowait(client.inports[0].get_array()[:])
except (queue.Full):
print("Full Queue")
stop_callback()
fromIPython.displayimportclear_outputfromtimeimportsleepfromscipy.signalimportstftimportmatplotlib.pyplotaspltfrommatplotlib.animationimportFuncAnimationsize=8olap=2olap_inv=size-olapglobalfig, axfig, ax=plt.subplots()
defanimate(i, spectrum):
line.set_ydata(spectrum) # update the data.fig.canvas.draw()
returnline,
try:
importjackclient=jack.Client(clientname, no_start_server=True)
blocksize=client.blocksizesamplerate=client.samplerateFFT_size=size*blocksizeoverlap=olap*blocksizeoverlap_inv=olap_inv*blocksizeclient.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_sizespectrum=init[2].T[0].realline, =ax.plot(init[0],spectrum)
ani=FuncAnimation(
fig, animate, interval=1, fargs=[spectrum])
plt.show(block=False)
sleep(2)
withclient:
target_ports=client.get_ports(
is_physical=True, is_output=True, is_audio=True)
client.inports[0].connect(target_ports[0])
timeout=blocksize*buffersize/samplerateforiinrange(olap):
data[i*blocksize:(i+1)*blocksize] =q.get(timeout=timeout)
whileTrue:
try:
foriinrange(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) #referencedata[0:overlap] =data[overlap_inv:FFT_size]
exceptKeyboardInterrupt:
print("Interrupted by user")
rec_end=Trueevent.wait() # # # Wait until recording is finishedexcept (queue.Empty):
# A timeout occured, i.e. there was an error in the callbackprint("Empty Queue")

Metadata

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