PsychToolboxSoundServer

PsychToolboxSoundServer()

Description

Plays sounds from the governing computer using an Asus Xonar DX, DSX or U7 sound cards.

  • Low latency and jitter (7-8ms) are achievable when running Bpod on Ubuntu 14.04 with the low-latency kernel and PsychToolbox installed. Comparable performance is possible on a Windows 7-10 computer (Core i7, >=8GB Ram) with only MATLAB running, no processing-intensive background processes, and the latest ASUS ASIO driver installed.

  • Sounds are sampled at 192kHz, 7 channel, with left and right speakers on channels 1 and 2 ("front/left" and "front/right").

  • By default, channels 3-5 output a 1ms TTL pulse at the onset of each sound. Connecting any of these channels to a BNC or wire input channel will provide a precise record of sound onset. On Linux, to ensure that the pulse can be read by Bpod, verify that you have configured ALSAmixer correctly during installation.

    • Up to 32 sounds can be loaded before each trial, and are known to the sound server as sounds 1-32. The number of sounds is currently limited in software to 32, but theoretically depends on available RAM. Sounds can be quickly re-loaded between trials to change their waveform.

  • Sounds are triggered by sending a soft code back to the governing computer from a trial's state matrix, and calling PsychToolboxSoundServer from a predetermined soft code handler function.

Syntax

To initialize:

PsychToolboxSoundServer('init')

To load a sound:

PsychToolboxSoundServer('load', SoundID, Waveform)

To play a sound:

PsychToolboxSoundServer('play', SoundID)

To stop playback:

PsychToolboxSoundServer('stop', SoundID)

To stop all playback:

PsychToolboxSoundServer('stopall')

To close the sound server:

PsychToolboxSoundServer('close')

Parameters

  • SoundID: A byte specifying which sound to load, play or clear (1-32).

  • Waveform: A vector containing the waveform of the sound to load. Samples must be between -1 and 1, and the sampling rate is 192kHz.

    • For mono, use a 1Xn vector. Both speakers will play the sound.

    • For stereo, use a 2Xn vector. Row 1 is the left channel, and row 2 is right.

Returns

-None

Example

% 1. This code creates a noise waveform, loads it to the sound server, plays it and then closes the server.

SoundDuration = 3; SamplingRate = 192000; % Set parameters

MyWaveform = rand(1,SoundDuration*SamplingRate); % Create waveform

PsychToolboxSoundServer('init'); % Initialize sound server

PsychToolboxSoundServer('load', 1, MyWaveform); % load the sound

PsychToolboxSoundServer('play', 1); % Play the sound

pause(3);

PsychToolboxSoundServer('close'); % Close sound server

% 2. This code loads a sound, sets a soft-code handler to play it, and sets a soft code to be sent from a state matrix.

%%-------In main protocol file-------

SoundDuration = 3; SamplingRate = 192000; % Set parameters

MyWaveform = rand(1,SoundDuration*SamplingRate); % Create waveform

PsychToolboxSoundServer('init'); % Initialize sound server

PsychToolboxSoundServer('load', 3, MyWaveform); % load the sound

sma = NewStateMatrix();

sma = AddState(sma, 'Name', 'State1', ...

'Timer', 1,...

'StateChangeConditions', {'Tup', 'exit'},...

'OutputActions', {'SoftCode', 3});

BpodSystem.SoftCodeHandlerFunction = 'SoftCodeHandler_PlaySound';

%%-------Soft code handler file in protocol folder (SoftCodeHandler_PlaySound.m)-------

function SoftCodeHandler_PlaySound(SoundID)

if SoundID ~= 255

PsychToolboxSoundServer('Play', SoundID);

else

PsychToolboxSoundServer('StopAll');

end