Two Questions: Timer and Sound

5 views (last 30 days)
Jacob Flynt
Jacob Flynt on 23 Apr 2011
Answered: SN MJTHD on 12 Jun 2014
1) In a GUI, how would I assign a timer function to a static text box? The GUI is a game which involves a timer counting down from 60 to 0. I know how to make a timer using the following code:
t=60
c=timer
set(c,'executionMode','FixedRate')
set(c,'TimerFcn','t=t-1')
This works fine in the command window, but I'm not sure how to put it into a GUI so that the variable 't' can be displayed as it changes every second. The reason I need to use a timer function is because I need it to run "in the background" while other things are changing and being pressed.
2) I'm working on a GUI and I would like to import an audio file of a song that would play on a loop while the GUI is open. It is an MP3 file but I can convert it if need be. Basically I need to know how to import the file into MatLab and how I would code it into the GUI. I've barely used sound processing and I really have no idea how to approach this.
Thanks

Answers (3)

Paulo Silva
Paulo Silva on 23 Apr 2011
1)
Add one text box to your GUI to hold the t value (for example text1), tag it
set(handles.text1,'Tag','MyTimerVal')
In the GUI OpeningFcn do:
set(handles.text1,'String','60') %the t value for the string
Create a new function on the GUI like this
function timedecrement(a,b)
handle = findobj('Tag', 'MyTimerVal');
t=str2double(get(handles.text1,'String')) %the t value for the string
t=t-1;
%see if t is 0 and do something (stop the game), if not do
set(handles.text1,'String',num2str(t)) %update t
end
This is the creation of the timer, put it on the OpeningFcn or in a button callback (for example in the start button)
c=timer;
set(c,'executionMode','FixedRate')
set(c,'TimerFcn',@timedecrement)
2)
[y, Fs] = wavread(audiofile); %audio file is a string, ex: 'music.wav'
soundsc(y,Fs) % play the audio
Be aware that soundsc doesn't stop the code execution until the audio is all played so it's good for your game, just see the length of the audio and make another timer to start it again ('fixedRate' or 'fixedSpacing' type)

Matt Fig
Matt Fig on 23 Apr 2011
Here is an example, adapt it to your needs.
function [] = timer_examp()
S.fh = figure('units','pixels',...
'position',[500 500 200 100],...
'menubar','none',...
'name','Timer_examp',...
'numbertitle','off',...
'resize','off');
S.tx = uicontrol('style','text',...
'unit','pix',...
'position',[10 60 180 30],...
'fontsize',14,...
'string','3');
S.tmr = timer('Name','Countdown',...
'Period',1,... % 10 minute snooze time.
'StartDelay',.01,...
'ExecutionMode','fixedSpacing',...
'StopFcn',@deleter); % Function def. below.
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','Start Countdown!',...
'callback',{@pb_call,S});
set(S.tmr,'TimerFcn',{@update_disp,S});
function [] = pb_call(varargin)
% Callback for pushbutton, deletes one line from listbox.
S = varargin{3};
% Need to check string!
set(S.tmr,'TasksToExecute',str2double(get(S.tx,'string')))
start(S.tmr);
function [] = update_disp(varargin)
S = varargin{3};
N = str2double(get(S.tx,'string'));
set(S.tx,'string',num2str(N-1));
function deleter(obj,edata) %#ok M-Lint doesn't know the callback fmt.
% Callback for stopfcn.
wait(obj);
delete(obj);
For the second question, see the help for the SOUND function (and cousins).

SN MJTHD
SN MJTHD on 12 Jun 2014
Dear Matlab experts,
I also have a problem related with GUI. I have a folder of .wav file which I already can play the folder randomly using a pushbutton of a GUI but for any single file a have to push the button. My sound files are directed files i.e your hear them from right center or left sides. What I exactly tried to do is playing the random files continually and push the related button for any direction. for example bush right button when I hear the sound from right and save the condition into a file but not overwrite it. considering a subject listening to this sounds and pressing left right and center buttons of gui and we want to know that for which of the sounds the subject pressed which button.
I really had a problem solving my issue and will appreciate any help.
Thanks in advance

Products

Community Treasure Hunt

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

Start Hunting!