How to play beep before each trial

2 views (last 30 days)
Takara Sumoto
Takara Sumoto on 9 Sep 2019
Commented: Walter Roberson on 11 Sep 2019
I have 80 audio files and I want to play beep before each audio starts as a cue.
Should I add "Beeper(400, [0.3], [0.5]);" in the loop, or outside of the loop?
for i=1:length(audio_files)
aud_file=strcat('C:\toolbox\aud_file\',audio_files(i).name);
% load sound file (make sure that it is in the same folder as this script)
[soundData freq]=audioread(aud_file);
% opens sound buffer at a different frequency
pahandle = PsychPortAudio('Open', [], [], 2, []);
% loads data into buffer
PsychPortAudio('FillBuffer', pahandle, soundData');
% how many repititions of the sound
repetitions=1;
%starts sound immediatley
PsychPortAudio('Start', pahandle, repetitions,0);
% stop
PsychPortAudio('Stop', pahandle, 1,0);
%wait
WaitSecs(3);
%close
PsychPortAudio('Close', pahandle);
end
  5 Comments
Takara Sumoto
Takara Sumoto on 11 Sep 2019
If I made a code like this below, I can only play beep once before all the audio files are played. (Just for trial, I add 3 audio files instead of 80.)
% opens sound buffer at a different frequency
pahandle = PsychPortAudio('Open', [], [], 2, []);
% generate a beep
beepWaveform = MakeBeep(1000,.250,44100);
% make stereo
beepWaveform = repmat(beepWaveform, 2, 1);
% fill buffer
PsychPortAudio('FillBuffer', pahandle, beepWaveform);
% how many repititions of the sound
repetitions=1;
%starts sound immediatley
PsychPortAudio('Start', pahandle, repetitions,0);
% stop
PsychPortAudio('Stop', pahandle, 1,0);
%wait
WaitSecs(1);
%close
PsychPortAudio('Close', pahandle);
%randomaize
a=randperm(3);
for i=1:length(audio_files)
aud_file=strcat('C:\toolbox\aud_file\',audio_files(a(i)).name);
% load sound file (make sure that it is in the same folder as this script)
[soundData freq]=audioread(aud_file);
% opens sound buffer at a different frequency
pahandle = PsychPortAudio('Open', [], [], 2, []);
% loads data into buffer
PsychPortAudio('FillBuffer', pahandle, soundData');
% how many repititions of the sound
repetitions=1;
%starts sound immediatley
PsychPortAudio('Start', pahandle, repetitions,0);
% stop
PsychPortAudio('Stop', pahandle, 1,0);
%wait
WaitSecs(1);
%close
PsychPortAudio('Close', pahandle);
end
Walter Roberson
Walter Roberson on 11 Sep 2019
% opens sound buffer at a different frequency
pabhandle = PsychPortAudio('Open', [], [], 2, []);
% generate a beep
beepWaveform = MakeBeep(1000,.250,44100);
% make stereo
beepWaveform = repmat(beepWaveform, 2, 1);
% fill buffer
PsychPortAudio('FillBuffer', pabhandle, beepWaveform);
% how many repititions of the sound
repetitions=1;
%randomaize
a=randperm(3);
for i=1:length(audio_files)
aud_file=strcat('C:\toolbox\aud_file\',audio_files(a(i)).name);
% load sound file (make sure that it is in the same folder as this script)
[soundData freq]=audioread(aud_file);
% opens sound buffer at a different frequency
pahandle = PsychPortAudio('Open', [], [], 2, []);
% loads data into buffer
PsychPortAudio('FillBuffer', pahandle, soundData');
%starts beep sound immediatley
PsychPortAudio('Start', pabhandle, repetitions,0);
% stop beep
WaitSecs(1);
PsychPortAudio('Stop', pabhandle, 1,0);
% how many repititions of the sound
repetitions=1;
%starts sound immediatley
PsychPortAudio('Start', pahandle, repetitions,0);
% stop
PsychPortAudio('Stop', pahandle, 1,0);
%wait
WaitSecs(1);
%close
PsychPortAudio('Close', pahandle);
end
%close beep
PsychPortAudio('Close', pabhandle);

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!