TeensySoundServer
TeensySoundServer()
Description
Plays sounds using the Teensy 3.1 (+ the Teensy Audio Board) as a serial slave device controlled by Bpod.
Low latency and jitter (~6-8ms) .
Sounds are sampled at 44.1kHz, stereo, 16 bit. For higher resolution and bit depth, see PsychToolboxSoundServer.
A Sandisk Ultra SD card with speed class 10 or UHS class is recommended.
Load the sound server firmware to the Teensy board: /Bpod/Firmware/TeensySoundServer/TeensySoundServer.ino
Connect Bpod Serial port pins 3 (TX) and 4(RX) to Teensy pins 0 and 1 respectively, and the ground pin (pin 2) to Teensy ground.
Up to 254 sounds can be loaded before each trial via USB, and are known to the sound server as sounds 1-254. Sounds can be re-loaded between trials, but re-loading is slower than for PsychToolboxSoundServer because it involves sound transmission over a virtual serial port and writing to an SD card.
Bytes received on the hardware serial port (not USB) trigger the corresponding sound to play. Byte 255 is a dedicated code to stop all playback.
Sounds are triggered by sending a serial code back to the governing computer from a trial's state matrix.
Syntax
To initialize:
TeensySoundServer('init', SerialPort)
To load a sound from a waveform in MATLAB:
TeensySoundServer('load', SoundID, Waveform)
To load a sound from a 44.1kHz 16-bit stereo .wav file:
TeensySoundServer('load', SoundID, FilePath)
To test-play a sound:
TeensySoundServer('play', SoundID)
To close the sound server:
TeensySoundServer('close')
Parameters
SerialPort: A string specifying the Teensy serial port (e.g. 'COM3' or '/dev/ttyUSBS101')
SoundID: A byte specifying which sound to load or play (1-254).
Waveform: A vector containing the waveform of the sound to load. Samples must be between -1 and 1, and the sampling rate is 44.1kHz.
The waveform must be stereo (a 2xn vector).
FilePath: The complete path to a 44.1kHz 16-bit stereo .wav file to send to the sound server.
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 = 44100; % Set parameters
MyWaveform = rand(2,SoundDuration*SamplingRate); % Create waveform
TeensySoundServer('init', 'COM3'); % Initialize sound server on port COM3
TeensySoundServer('load', 1, MyWaveform); % load the sound
TeensySoundServer('play', 1); % Play the sound
pause(3);
TeensySoundServer('close'); % Close sound server
% 2. This code loads a sound, and creates a state matrix to trigger it.
% the code assumes that the Teensy sound server is connected to Bpod serial port 1 (of 2).
SoundDuration = 3; SamplingRate = 192000; % Set parameters
MyWaveform = rand(1,SoundDuration*SamplingRate); % Create waveform
TeensySoundServer('init', 'COM3'); % Initialize sound server
TeensySoundServer('load', 3, MyWaveform); % load the sound into slot 3 of 254
sma = NewStateMatrix();
sma = AddState(sma, 'Name', 'State1', ...
'Timer', 1,...
'StateChangeConditions', {'Tup', 'exit'},...
'OutputActions', {'Serial1Code', 3}); % Trigger sound 3