From: Jamie Granek on
Hi,

I want matlab to play continuous sound until a stop command.

Eg

load handel;
player=audioplayer(y,Fs);
play(player);

Will play vector y until finished whilst allowing use of matlab
during the playback.

I want play to loop whilst allowing me to continue to control matlab
(to perform real time analysis), until I type (or the command appears
in my mfile) the stop(player) command.

Any ideas?

I suppose I could make y a very very long vector by concatenating it
with itself many times, but this seems a waste of memory resources.

Regards,
Jamie
From: Dave Robinson on
Jamie Granek wrote:
>
>
> Hi,
>
> I want matlab to play continuous sound until a stop command.
>
> Eg
>
> load handel;
> player=audioplayer(y,Fs);
> play(player);
>
> Will play vector y until finished whilst allowing use of matlab
> during the playback.
>
> I want play to loop whilst allowing me to continue to control
> matlab
> (to perform real time analysis), until I type (or the command
> appears
> in my mfile) the stop(player) command.
>
> Any ideas?
>
> I suppose I could make y a very very long vector by concatenating
> it
> with itself many times, but this seems a waste of memory resources.
>
> Regards,
> Jamie

Just a thought, if you read in your sound file as a wav using wavread
so
[Sound,Fs,Prec] = wavread('mysoundfile.wav')
Here Fs is the sample rate of the file.

Using length(Sound) and Fs you can determine the playtime of your
sound track. Now you should be able to set up a timer object that
triggers at this period of time which uses sound(Sound) or one of the
many other equivelent functions as its callback. I can't remember
which one it is, but one function gives you the option of waiting
until the soundtrack finishes before continuing, or continuing in
parallel with the playing of the sound.

Maybe helpful

Dave Robinson
From: Brian Wherry on
Hi Jamie,

Here's a basic approach that should work:

Create a MATLAB function somewhat like this:

function audioplayerLoopingStopFcn(haudioplayer, eventStruct)
if ~ haudioplayer.UserData.stopPlayback
play(haudioplayer);
end

This uses a field in the audioplayer object's UserData to signal when
playback should *not* continue looping. All you need is to put that
UserData field in there and set the audioplayer object's StopFcn to be the
audioplayerLoopingStopFcn. Something like this:

load handel;
p = audioplayer(y, Fs);
blah.stopPlayback = false;
p.UserData = blah;
p.StopFcn = @audioplayerLoopingStopFcn;

play(p); % should keep playing so long as p doesn't get destroyed and
stopPlayback is false
..
..
..

% stop it
p.UserData.stopPlayback = true;
stop(p);

HTH,

Brian

ps- When you issue another play() inside audioplayerLoopingStopFcn, you
*may* have to first wait until playback has completely finished (inspect the
Running property). IIRC, the StopFcn is called when the audioplayer is
*stopping*, not necessarily when it's *already stopped*.

"Jamie Granek" <jgranek(a)nospam.unimelb.edu.au> wrote in message
news:ef2aa8b.-1(a)webx.raydaftYaTP...
> Hi,
>
> I want matlab to play continuous sound until a stop command.
>
> Eg
>
> load handel;
> player=audioplayer(y,Fs);
> play(player);
>
> Will play vector y until finished whilst allowing use of matlab
> during the playback.
>
> I want play to loop whilst allowing me to continue to control matlab
> (to perform real time analysis), until I type (or the command appears
> in my mfile) the stop(player) command.
>
> Any ideas?
>
> I suppose I could make y a very very long vector by concatenating it
> with itself many times, but this seems a waste of memory resources.
>
> Regards,
> Jamie


From: jamie granek on
Thanks for your suggestions Dave and Brian. Unfortunately there were
problems with both.

Both have timing problems.

I would like the sound to loop with no break between stopping and
re-starting. So after the last sample is played the first should be
played in time for the next sample period (1/fs).

1) Problems with Brian's solution:

a) After each loop, there is a short pause and click.
For my application I want the loop to be continuous as if the
audioplayer was playing a long waveform.

b) I could not get the audioplayer to stop as I couldn't set the stop
flag to true. However, it seemed to stop on its own after a few or
many loops for an unknown reason?

I stepped through the following code in debug mode:
%-------------------------------------------
% Create 1 sec pure tone
Fs = 8192 % standard sampling rate (samples/sec)
cf = 1000; % carrier freq (Hz)
t=0:1/Fs:1; % one second
y2=sin(2*pi*cf*t);

% Setup Audioplayer
p = audioplayer(y2, Fs);
blah.stopPlayback = false;
p.UserData = blah;
p.StopFcn = @audioplayerLoopingStopFcn;

play(p); % should keep playing so long as p doesn't get destroyed and
% stopPlayback is false

% stop it
p.UserData.stopPlayback = true;
stop(p);

function audioplayerLoopingStopFcn(haudioplayer, eventStruct)
% subfunction
if ~ haudioplayer.UserData.stopPlayback
play(haudioplayer);
end

%-------------------------------------------

But at
>> p.UserData.stopPlayback = true
got the error:

??? Error using ==> audioplayer.subsasgn
Inconsistently placed '.' in subscript expression.

I don't know why?

2) Problems with Dave's solution

Also a timing problem. Setting the timer period to (length(y))/Fs
did not cause the audioplayer to replay. I had to add an additional
delay in the the order of 0.3 sec for the player to restart. So
again there was a gap between finishing and re-starting.

Sample code:
%-----------------------------------------------
load handel; % comes with matlab
player=audioplayer(y,Fs);
% Use of timer to restart audioplayer exactly when previously play is
% expected to stop.

t = timer
set(t,'period',((length(y))/Fs)+0.3) % set period of timer
set(t,'taskstoexecute',2) % set max number of times timer runs
t.StartFcn = 'disp(''this is my start fn'')'
% set(t, 'executionmode','fixedDelay')
set(t, 'executionmode','fixedspacing')
set(t,'timerfcn', {@mytimerfn, player})
start(t)
delete(t)

function mytimerfn(obj, event, player)
disp('this is my timer function')
play(player)

%-----------------------------------------------

If anyone has any further ideas, that would be much appreciated.

Regards,
Jamie