drawnow function in App Designer stops working?

12 views (last 30 days)
Hello,
I'm making audio player in Matlab's App Designer for my thesis. However, I got stuck with one issue I got, so I'll describe it here on simple example, where the same issue also happens.
First, in my private properties, I define variables for AudioFileReader and AudioDeviceWriter, so I can access these objects later from callbacks:
afr = dsp.AudioFileReader;
adw = audioDeviceWriter;
Then, I make two state buttons, StateButton1 and StateButton2. When you press one of them, their purpose is meant to change app.afr.Filename to corresponding track (first button is for playing one track, like 01.wav, second button for another track, 02.wav, for example), play the track while the button is pressed (while it's value property equals to 1), and to "unpress" the other button, which is not currently playing. This way, when one button is pressed and it's corresponding audio file is playing and I press the second button, the first button gets "unpressed", second button gets pressed and the audio file corresponding to second button starts playing. When I unpress the button coressponding to the currently playing audio file, it simply stops.
Here is the code of StateButton1ValueChanged callback:
app.StateButton2.Value = 0;
release(app.afr);
release(app.adw);
app.afr.Filename = '01.wav';
app.afr.SamplesPerFrame = 512;
app.adw.SampleRate = app.afr.SampleRate;,
while ~isDone(app.afr) & app.StateButton1.Value == 1
audio = app.afr();
app.adw(audio);
drawnow;
end
And here is practically the same code for StateButton2ValueChanged callback, only with different parameters:
app.StateButton1.Value = 0;
release(app.afr);
release(app.adw);
app.afr.Filename = '02.wav';
app.afr.SamplesPerFrame = 512;
app.adw.SampleRate = app.afr.SampleRate;,
while ~isDone(app.afr) & app.StateButton2.Value == 1
audio = app.afr();
app.adw(audio);
drawnow;
end
It works fine like this for some time, however the problem starts after few changes from track 1 to track 2 and vice versa with these two state buttons. When I click on them alternately to change the currently playing track for like ten times, then it gets stuck and I can't turn off the currently playing track with it's corresponding button, can't change the currently playing track with the second button to the second audio file and the first button even doesn't get unpressed, as it should, when I press the second button to the "pressed" Value = 1 state. Only the track that was playing before the freeze keeps playing.
Now I'm seriously getting clueless, it seems to me like that the "drawnow;" function in each callback stops working after few changes from first callback to the second one and vice versa for some reason, but why? Any ideas? I'll really appreciate any idea or clue why this is happening, as I need to finish my thesis in like two months and this is still such a hurdle for me.
Thank you very much for any response.

Accepted Answer

hagege ruben
hagege ruben on 22 Mar 2020
did you try pause(0.001)?
It did work best for me on 2019b
  2 Comments
Michael Svejcar
Michael Svejcar on 23 Mar 2020
Hello, thank you very much for the answer!
I already figured out yesterday that what was happening was that I was overfilling my callback queue. Because there are while loops in every callback and drawnow in each cycle of the callbacks, this way, when I switched from callback 1 to callback 2, because of the drawnow, it was waiting for callback 2 to finish so it could get back to callback 1 to finish too. But because I never let callback 2 to finish and switched it few more times, I simply clogged my callback queue after 10 switches or so. Firstly I didn't even realize it worked like this, but when I did, putting "pause(0)" just before "drawnow" worked fine too.
Thanks for your help and stay safe during these times!
hagege ruben
hagege ruben on 23 Mar 2020
well maybe you could also add a Busy property to you'r code so that it would skip updating when already working on previous update.
also you should try to make a function out of redundant code, it's a good practice and helps for debugging/maintainance of the code

Sign in to comment.

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!