function SoundToPlay = GenerateSound(whichsound, screennumber)

% stimuli (creation)
sr = 44100;
d = .05;
f = 440;
% tone synthesis
t = linspace(0, 0.1, sr*d);
tone = sin(2*pi*f*t);
% ONSET AND OFFSET GATING
gatedur = .005; % the duration of the gate in seconds (i.e., 5 ms)

ongate = cos(linspace(pi, 2*pi, sr*gatedur));
ongate = ongate+1;
ongate = ongate/2;
offgate = fliplr(ongate);
sustain = ones(1, (length(tone)-2*length(ongate)));
envelope = [ongate, sustain, offgate];
tone = tone .* envelope;
refreshrate = Screen(screennumber, 'FrameRate'); % get the frame rate of the monitor

silencepre_dur = (1/refreshrate) * 70;
silencepre = zeros(1, round(sr * silencepre_dur));
SoundToPlay = [silencepre, tone];
if whichsound == 1
    SoundToPlay = SoundToPlay * 0;
end