How to record the time at which a sound was played?

3 views (last 30 days)
I want to know how to record the time at which a sound plays and put it into my data file.
Currently I am using Cogent to record the time at which a visual stimulus was presented:
Time_target = drawpict(2);
addresults(Time_target);
Later on that is added to my results file.
I would like to be able to do the same with the sound file. I have tried:
'time_sound = play(p);' and 'time_sound = sound(p);'
but both come back with the error that there are too many output arguments. Is there a way to record the time at which the sound was played?
Thank you!

Answers (1)

Star Strider
Star Strider on 24 Mar 2015
I don’t know precisely what you’re asking. If you want to know the approximate start time, use the clock funciton. If you want to know how long the sound file is in seconds, divide the length of the file in samples by the sampling frequency:
locomotive = load('train.mat');
% Clock Time
t0 = clock
sound(locomotive.y, locomotive.Fs)
% Time Length (sec)
time_length = length(locomotive.y)/locomotive.Fs
Note: ‘train.mat’ is a stock sound file included with MATLAB.
  2 Comments
Star Strider
Star Strider on 24 Mar 2015
Lizzy’s Answer moved here ...
Thanks for your reply.
I am running an experiment in which stimuli are presented throughout, and I want to know at what point during the experiment the audio is played.
I can get the time at which the visual stimuli are presented using the code I mentioned in the question, e.g. myTime = drawpict(1). Later lines of code add the time at which 'drawpict(1)' was executed into a data file.
I'm not sure if clock would work as I want the time within the experiment itself (e.g. 1500 would indicate 1500ms after it began) rather than the external time/GMT?
Star Strider
Star Strider on 24 Mar 2015
If you want the time relative to the time the experiment began, use that time as ‘t0’, and the time you called the sound function as ‘t1’, then use the etime function:
% START EXPERIMENT
t0 = clock;
... EXPERIMENT CODE ...
t1 = clock
sound(locomotive.y, locomotive.Fs)
sound_time = etime(t1,t0);
Here, ‘sound_time’ records the elapsed time in seconds.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!