parallel processing - How can play an audio file with plotting a diagram simultaneously in matlab? -
i play audio file meanwhile plotting , updating diagram. however, audio file interrupted. play audio file smoothly in background , update figure @ same time.
for i=1:10 player = audioplayer(audio, fs); play(player); scatter(x(i),y(i),'r.') end
your problem play asynchronous call: means program execution continues after call 'play(player)'.
if intend play different files @ each iteration, try waiting till current file finishes, can use like:
while player.isplaying pause(0.001) end if meant play 1 signal , change plots, move play(player) outside of loop, , add delay between each plotting point example code:
player = audioplayer(audio, fs); play(player); i=1:10 scatter(x(i),y(i),'r.') pause(0.1) end example 1 signal being played , plot being updated:
build chirp signal:
fs = 16e3; t = 10; t = 0:1/fs:t; f0 = 100; phi = 2*pi*t.^2*f0; sig = 0.1*sin(phi); % start playing sound: player = audioplayer(sig,fs); play(player); % plotting stuff: dphi = gradient(phi)*fs; figure; numplots = 20; n = numel(t); n = 1 : numplots pause(t/numplots) ind = 1:n/numplots*n; plot(t(ind), dphi(ind)) end in general when plotting 'real-time' better use tic-toc figure current time compared time audio started playing. improve performance better set xdata & ydata of plots instead of re-plotting every time action faster (doesn't update other properties of axes).
you can @ old script once shared 'real-time' plotting: https://www.mathworks.com/matlabcentral/fileexchange/14397-real-time-scope-display--simple-script-
Comments
Post a Comment