Play sounds
The Sound Card plays waveforms stored in its onboard memory and pure tones from an internal sine wave generator. Upload a sound to sound index 2 with the Sound Card app or in Bonsai, which we will use for these examples.
Note
Pure tone playback from the internal sine wave generator is currently limited (as of firmware v3.0) to the Right Channel. Connect the amplifier and speaker to this channel if you plan to play pure tones.
This article covers how to play and stop a stored sound, play and stop a pure tone, and visualize sound events in Bonsai.
The complete workflow is shown below. Copy and paste it into Bonsai to get started immediately, or build each section by following the step-by-step instructions in the following sections.
Play sound index
Play sounds from the onboard sound bank by writing the sound index to the Start register. Values from 2 to 31 select a sound index, and values from 32 upwards are interpreted as a frequency for the sine wave generator instead (covered in the next section). Sound duration and amplitude are determined by the properties of the stored waveform.
A stored sound plays to the end unless it is stopped early. Writing a non-zero value to the Stop register ends the sound that is playing, once the audio already queued for output has played.
- Insert a
KeyDownoperator and set theFilterproperty toA. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectStartPayload.Start- Set the index of the sound you want to play from the onboard memory (e.g.2).
- Insert a
MulticastSubjectoperator namedSoundCard Commands.
In a separate branch:
- Insert a
KeyDownoperator and set theFilterproperty toS. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectStopPayload.Stop- Set the value to1(any non-zero value stops playback).
- Insert a
MulticastSubjectoperator namedSoundCard Commands.
Run the workflow, press the A key to play the sound, and press the S key to stop it early.
Tip
Starting another sound index replaces the one that is playing, and starting an empty sound index does not interrupt it. A pure tone, covered in the next section, started while a stored sound is playing waits and starts when the sound ends. Until then, the device refuses further Start commands.
Play pure tone
Pure tones are played from the internal sine wave generator by writing the frequency in Hz (32–40000) to Start. A tone plays continuously until a Stop command is sent.
- Insert a
KeyDownoperator and set theFilterproperty toD. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectStartPayload.Start- Set the desired frequency in Hz (e.g.500).
- Insert a
MulticastSubjectoperator namedSoundCard Commands.
In a separate branch:
- Insert a
KeyDownoperator and set theFilterproperty toF. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectStopPayload.Stop- Set the value to1(any non-zero value stops playback).
- Insert a
MulticastSubjectoperator namedSoundCard Commands.
Run the workflow, press the D key to play the tone, and press the F key to stop it.
Note
Closing the connection to the device also stops any sound or pure tone that is playing.
Visualize sound events
The Sound Card reports every sound start as a Start event, and every sound stop as a Stop event. This includes sounds started or stopped from the digital inputs. A stored sound that plays to the end reports no Stop event, and neither does a stop command sent while nothing is playing. The workflow below will show you how to filter, decode and visualize the events:
- Insert a
SubscribeSubjectoperator namedSoundCard Events. - Insert a
FilterMessageTypeoperator and set theMessageTypeproperty toEvent. The device responds to eachStartandStopcommand with aWritereply when it receives the command and anEventmessage when it is executed. Filtering forEventkeeps the execution message and timestamps. - Insert a
Parseoperator and set theRegisterproperty toTimestampedStart. - Insert a
VisualizerWindowoperator.
In a separate branch from the FilterMessageType operator:
- Insert a
Parseoperator and set theRegisterproperty toTimestampedStop. - Insert a
VisualizerWindowoperator.
Run the workflow and press the D key to play a pure tone. The first visualizer will display:
500@350.214528
The first number is the Start sound index or frequency that was played, and the second number is the timestamp on the device clock.
Press the F key to stop the tone. The second visualizer will display:
1@352.880416
The first number is the non-zero value we used for Stop, and the second number is the timestamp on the device clock when the Stop command was executed.
Alternative: play sound with Timer
You can replace KeyDown with other operators to play sounds with other triggers in Bonsai, for instance a Timer to send triggers after a set time has elapsed.
- Insert a
Timeroperator and configure the following properties:DueTime- Set the number of seconds to wait after the workflow starts to play the sound (e.g.00:00:02for two seconds).Period- Leave this unspecified so that theTimeronly triggers once.
- Insert a
CreateMessageoperator and configure the following properties:Payload- SelectStartPayload.Start- Set the index of the sound you want to play from the onboard memory (e.g.2).
- Insert a
MulticastSubjectoperator namedSoundCard Commands.
Run the workflow. The sound plays 2 seconds after the workflow starts.
Documentation for release v3.0.0