Play a wav sound in a loop without blocking it

35 views (last 30 days)
Hi, i'm new to matlab and am still unclear on what it can and cannot do.
The program I am working on has a loop that displays a flickering pattern at 100Hz. I tried to include the option to trigger a wav sound with a keypress, but it appears that Matlab freezes the loop for the duration of the sound, and restarts it once it's finished.
Is it possible to trigger the sound in a non-blocking way ? Thanks
My code
[y,Fs] = wavread('_path_');
while ( ~program.params.kd(program.params.key.ExitKey))
% frame refresh
if program.params.kd(program.params.key.SoundKey)
wavplay(y, Fs, 'sync');

Accepted Answer

Daniel Shub
Daniel Shub on 4 May 2012
You can use audioplayer instead of wavplay
doc audioplayer
Retriggering your sound card 100 times a second is going to be problematic. You probably want to create a long sound and trigger the soundcard once from outside the loop.
  1 Comment
Vitali Roubtsov
Vitali Roubtsov on 4 May 2012
Hey thanks :) Audioplayer works great.
The sound card isn't refreshed at the flickering rate though, only when the SoundKey is pressed.

Sign in to comment.

More Answers (1)

Vitali Roubtsov
Vitali Roubtsov on 14 May 2012
Hello again !
Audioplayer worked great with Matlab 2007 but now I am trying to make this work on Matlab 2011 and it seems there's a freeze on my display at some point during the sound (not at the beginning, nor at the end).
Any idea where this freeze may come from and how to correct it ?
  1 Comment
Jan on 14 May 2012
There are some bugs in AUDIOPLAYER in Matlab 2011b. E.g. when you store an AUDIOPLAYER object persistently, shutting down Matlab causes a crash. See:

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!