0

I am trying to create an experiment on psychtoolbox and one part of it involves sounding an alarm when the participant fail to respond.

I tried using the beep provided but it does not sound like an alarm at all. Is there any way to achieve that without having to download a external sound?

I have no knowledge of sound or sound waves so please help!

TYL
  • 1,577
  • 20
  • 33

2 Answers2

1

The following code will load a .wav file, and play it through the Psychtoolbox audio system. This allows you to have a timestamp of sound onset, and allows greater control than using sound() or beep. You could alternatively generate a tone using MATLAB itself (it is easy to generate a sine wave of a particular frequency) and use that instead of the .wav data.

%% this block only needs to be performed once, at the start of the experiment

% initialize the Psychtoolbox audio system in low latency mode
InitializePsychSound(1);

% load in a waveform for the warning
[waveform,Fs] = audioread('alarm.wav');
numChannels = size(waveform, 2);

% open the first audio device in low-latency, stereo mode
% if you have more than one device attached, you will need to specify the
% appropriate deviceid
pahandle = PsychPortAudio('Open', 2, [], 1, Fs, numChannels);


%% during the experiment, when you want to play the alarm
PsychPortAudio('FillBuffer', pahandle, waveform' );
startTime = PsychPortAudio('Start', pahandle, 1);

%% at the conclusion of the experiment
PsychPortAudio('Close');

If you'd like to generate your own sound, take a look at the Psychtoolbox function 'MakeBeep', and substitute that in, instead of the waveform, for example, a 1000 Hz tone, lasting 250ms, at a 44.1k sampling rate:

% generate a beep
beepWaveform = MakeBeep(1000,.250,44100);

% make stereo
beepWaveform = repmat(beepWaveform, 2, 1);

% fill buffer, play
PsychPortAudio('FillBuffer', pahandle, beepWaveform );
startTime = PsychPortAudio('Start', pahandle, 1);
DMR
  • 1,479
  • 1
  • 8
  • 11
0

To me, beep can do what you want by playing it multiple times in the loop like this:

% Adjust the no. of loop iterations depending on how long you want to play the alarm
for k=1:100
    beep;  pause(1);
end

Other than that, you can use built-in sounds like this:

load('gong');   % This sound seems suitable to me for alarm. Try others from the list
for k=1:100 
    sound(y,Fs);  pause(1);
end

Here's a list of built-in sounds that you may want to try:

chirp
gong 
handel
laughter
splat
train
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
  • Make sure that you do not use `y` and `Fs` as variables names in your code because these variables will be overwritten by `load('gong')` or others – Sardar Usama Feb 06 '17 at 09:55
  • The issue with using beep is that (1) it is operating system specific so the experiment will not be the same across operating systems (2) you have less control of timing relative to the functions built into Psychtoolbox – DMR Feb 06 '17 at 16:18
  • You can use built-in sounds as mentioned in the answer. No? – Sardar Usama Feb 06 '17 at 16:21
  • Psychtoolbox has been designed for low-latency, controlled stimulus presentation, so it is better to use Psychtoolbox functions than either 'beep' or 'sound.' For example you have no estimate of sound onset latency from either beep or sound. You could present the built-in waveforms via PsychPortAudio, but I would still argue it's better to avoid them, as they could change between versions of MATLAB, or between running the experiment on MATLAB vs. Octave. – DMR Feb 06 '17 at 16:37
  • @DMR the OP didn't express any such concerns!! The only problem he has is that the beep doesn't sound like an alarm to him!! – Sardar Usama Feb 06 '17 at 16:44