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!

 채택된 답변

Daniel Shub
Daniel Shub 2012년 8월 24일

0 개 추천

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

Tobi S
Tobi S 2012년 8월 24일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jan
Jan 2012년 8월 24일
편집: Jan 2012년 8월 24일

0 개 추천

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

Tobi S
Tobi S 2012년 8월 24일
True, but none of the documentation for pause or drawnow mention audioplayer objects (or vice versa) - at least in my version of Matlab.

댓글을 달려면 로그인하십시오.

Roberto
Roberto 2014년 3월 24일
편집: Roberto 2014년 3월 24일

0 개 추천

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).

카테고리

도움말 센터File Exchange에서 COM Component Integration에 대해 자세히 알아보기

질문:

2012년 8월 23일

편집:

2014년 3월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by