Before reading this section you should first read about cells and possibly images since they're simpler than sounds.
To play a sound, the sound has to be loaded into a cell. You can have up to 26 different sounds loaded at a time. If a cell with a sound in it is showing, the participant will see a controller for the sound. If it's hidden, there will be no sign on the screen that the sound is loaded, but the sound can still be played. Although you can have 26 sounds loaded at the same time, if you try to have them all playing at the same time they will probably not play smoothly because your computer will not have enough processor power to decompress and interpret 26 different sounds at the same time.
Loading a sound into a cell and showing the cell just allows PsyScript to display its controller. This will allow your participant to play the sound by clicking on the controller but you're probably going to want to use script commands to play the sound.
There are two script commands you can use to play sounds. One starts the sound playing and waits until it has finished playing before allowing the script to continue. The other starts the sound playing then continues with the next line of the script. If you use the second of those then you can use the pause sound command later in the script to pause the sound. This may be useful if you want to play just the beginning of the sound, or to play the sound until the participant reacts to it and stop at that point.
The smallest amount of code required to play a sound would be …
The smallest amount of code required to let a participant play a sound for themself would be …
Loading resources (like sounds) from disk or across the internet are not only slow, but unpredictably slow, varying by as much as a second or two depending on when caches run out of space, when anti-virus software cuts in, and all sorts of other matters. Consequently, do your load operations outside any time-critical parts of your script.
PsyScript understands any sound file formats that your web browser understands. The most common format is AIFF, which gives you files ending in .aif
or .aiff
. If your web browser can play the sound in a normal static web page, PsyScript should be able to load it into a cell.
However, at time of writing there's a problem if you want one script to run in lots of different browsers, for example if you want participants to be able to run your experiment on their own devices at home. The problem is that different browsers don't all understand the same sound formats: some can play MPEG files, some can play OGG files, and can play .m4a
files, and I've been unable to identify a file format they can all play. Which makes it impossible to make an experiment that runs on all browsers.
To compensate for this, PsyScript allows you to list several different files in a sound command, which would make it look like this:
This tells PsyScript to try each of these files in turn until it finds one the browser can play.
load cell value with sound value
Loads the sound from a file in the same folder as the script file into a cell
load cell A with sound llama.aiff|llama.mp3|llama.ogg
load cell L with sound $varsoundFileName
load cell $varTargetCell with sound target.mov
load cell value with sound value from folder value
Loads the sound from a file in a subfolder of the script folder into a cell
load cell A with sound growl.mp3 from folder animals
load cell L with sound $varImageFileName from folder female
load cell $varTargetCell with sound whistle.m4a from folder $varSelectedTone
play sound in cell value
Starts the sound in that cell playing from its current point. Pauses the script until the sound is finished. If the sound is already playing, does nothing. If the cell is visible, the sound's controller will animate as the sound plays.
play sound in cell R
play sound in cell $varTargetCell
play sound in cell value without waiting
Starts the sound in that cell playing from its current point. Allows the script to continue executing while the sound is playing. If the sound is already playing, does nothing. If the cell is visible, the sound's controller will animate as the sound plays.
play sound in cell R without waiting
play sound in cell $varTargetCell without waiting
pause sound in cell value
Pauses the sound in that cell at its current point. If that sound is not playing, does nothing.
pause sound in cell R
pause sound in cell $varTargetCell