Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: running and stopping wave files
Date: Thu, 4 Dec 2008 10:07:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 103
Message-ID: <gh8a45$q3i$1@fred.mathworks.com>
References: <gh813i$22c$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1228385221 26738 172.30.248.37 (4 Dec 2008 10:07:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 4 Dec 2008 10:07:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 533792
Xref: news.mathworks.com comp.soft-sys.matlab:504908


"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in message <gh813i$22c$1@fred.mathworks.com>...
> Hi,
> 
> I am doing sound simulation. Here i need to run a wave file corresponding to user input. THere is no problem in doing so. 
> 
> But the problem is when a sound file is running and at the same time if i get an input from user, the sound which is running currently has to be stopped and new file has to be run as per the input.
> 
> How do i do this ?
> Any help in this regards would be appreciated .
> 
> Thanks !
> Ashwini

Hello.

I think you are currently using 'waveplay'?

Did you investigate how to use 'analogoutput'? I would propose to use the analogobject and put data into your soundcard.

On the fly:

(we need 3 m files)
- main.m (gui) for handling userinputs <- this is your part
- init.m to initialize objects, routines etc. <- i will try to give help
- analogoutput.m <- i will try to give help

INIT.M
--------
function handles = init(handles)

handles.Samprate = 44100;
handles.N = 2^12;
handles.step = 1; %used in update_plot
handles.Track = wavread('my_nice_sound_file'); % get the track

% we have to calculate the splits of the track
handles.L = length(handles.Track);
handles.N; %this is our framelength
handles.Nframes = floor(handles.L / handles.N);

% create output object %
handles.ao = analogoutput('winsound');
addchannel(handles.ao, 1);

% configure your soundcard %
set(handles.ao, 'SampleRate', handles.SampRate);
set(handles.ao, 'TriggerType', 'Manuel');
set(handles.ao, 'ManualTriggerHwOn', 'Trigger');
set(handles.ao, 'SamplesAcquiredFcnCount', handles.N); % how many samples should be packed into the object
set(handles.ao, 'SamplesAcquiredFcn', {@update_plot_out, handles});
% the last line is very important! here we set into our analogobject that if the amount 
% of data is gone, call our function (update_plot_out)
% it is like I would say "fill that hole, and there (update_plot) you can find (rolling) stones

setappdata(gcf, 'apphandles', handles);

UPDATE_PLOT_OUT
-----------------------------
function handles = update_plot_out(hObject, eventdata, handles)

% small problem: if your pc is to slow and data is gone without refilling, the object will be automatically deleted. to avoid that we do following:
if strcmp( get(handles.ao, 'Running'), 'Off')
data = zeros(15000, 1);
putdata(handles.ao, data);
start(handles.ao);
trigger(handles.ao);
end

% defined in init
handles.step = handles.step + 1;
handles.ind_out = handles.k * handles.N;
handles.ind_in = handles.ind_out - handles.N + 1;

% reset or go on
if handles.ind_out > handles.L
handles.k = 1;
else
putdata(handles.ao, handles.Track(handles.ind_in:handles.ind_out);
end


guidata(handles.figure1, handles);


MAIN.M (GUI)
--------------------
%define a start, stop button

%inside gui in the beginning
handles = init(handles);

%startbutton
start(handles.ao);
trigger(handles.ao);

%stopbutton
stop(handles.ao)

%closebutton
%check if stopped
delete(handles.ao)