audioplayer/isplaying won't exit tight loop

When trying to determine when an audioplayer object has finished playing a sound, the following code enters an infinite loop:
aobj = audioplayer(rand(10000,1), 48000, 16, 2);
play(aobj);
while isplaying(aobj)
end
while this code doesn't:
aobj = audioplayer(rand(10000,1), 48000, 16, 2);
play(aobj);
while isplaying(aobj)
pause(0.00001);
end
Can somebody explain what's happening? I'm running Matlab 7.13.0.564 (R2011b) on a Mac, OS 10.7.4. Thank you!

 Accepted Answer

I believe AUDIOPLAYER underwent a poorly, possibly undocumented, change at some point. The AUDIOPLAYER object now makes use of an ASYNCIO object, which I do not believe was always the case (although again I am not sure).
Probing your problem a little further, it appears that the isplaying method does not become false until the event queue is flushed no matter how long intervening computations take between PLAY and ISPLAYING.
obj = audioplayer(rand(44.1e3, 1), 44.1e3);
play(obj);
t0 = clock;
cnt = 0;
while etime(clock, t0) < 2
cnt = cnt+1;
end
isplaying(obj)
At a minimum this needs to be documented, and seems like a bug that you should report.
Looking at the code, the isplaying method of an AUDIOPLAYER object is essentially a wrapper for the isOpen method of an +asyncio.Channel object, which is essentially a wrapper for the proprietary asyncioimpl.Channel.isOpen method. Based on as far as TMW will let us look, there is no way to know why this requires the event queue to be flushed. A hint might be the fact that +asyncio.Channel object has an event called "Closed". Maybe the proprietary asyncioimpl.Channel object also has an event that it is listening for (which would require a flush of the event queue), but that is just a guess.
A simple work around is to use some of the available portaudio implementations for presenting sounds.

1 Comment

Thanks for the behind-the-scenes sleuthing! As a somewhat new user to Matlab, these details were all new to me.
In any case, the problem is that the event cue needs to be flushed. While pause(10^-6) works to do so, drawnow might be even faster (as suggested by Jan above).
Thank you both!

Sign in to comment.

More Answers (2)

Jan
Jan on 24 Aug 2012
Edited: Jan on 24 Aug 2012
pause and drawnow allow pending events to be processed. This triggers the update of windows, gui elements and even the audioplayer object. See doc pause and doc drawnow .
A smarter solution, see doc audioplayer:
playblocking(aobj);

1 Comment

True, but none of the documentation for pause or drawnow mention audioplayer objects (or vice versa) - at least in my version of Matlab.

Sign in to comment.

Roberto
Roberto on 24 Mar 2014
Edited: Roberto on 24 Mar 2014
I have the same problem, but I kind of solved it using:
isempty(aobj)
Return true if the object is empty, which mean, you have not yet load the objet. So, doesn't make sense,at least, stop,pause or resume. I hope that helps.
(Sorry my english).

Categories

Asked:

on 23 Aug 2012

Edited:

on 24 Mar 2014

Community Treasure Hunt

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

Start Hunting!