%% include at the start of the experiment
% initialize sound, requesting low latency (parameter = 1)
InitializePsychSound(1);
% open stereo audio handle
% here, use the first audio device with stereo output, if you have an
% external sound card you may have more than one output device available
samplingRate = 44100;
devices = PsychPortAudio('GetDevices');
outputDevice = devices(find([devices.NrOutputChannels] == 2, 1, 'first')).DeviceIndex;
pahandle = PsychPortAudio('Open', outputDevice, 1, 1, samplingRate, 2);
% generate the waveform of a sound, here a 1000 Hz sine wav, 200 ms long
% you could instead load a .wav file from a file, or generate the waveform
% using any MATLAB functions
beepWav = MakeBeep(1000, .2, samplingRate);
% make the beep stereo
beepWav = repmat(beepWav, [2 1]);
% fill the audio buffer with the beep waveform
PsychPortAudio('FillBuffer', pahandle, beepWav );
%% include whenever you want to play the sound
PsychPortAudio('Start', pahandle, 1, 0, 1);