Many experiments will involve playing sounds, and Experimentor makes it easy to play sounds as part of your codes.  Experimentor can play many types of sound files, including raw Wave files (.wav), Windows Media Audio files (.wma), MP4 Audio files (.m4a), and Windows audio file (.aac, .adt, .adts).

Internally, Experimentor use the Windows Media Player (you never actually see Windows Media Player) to play the sound, so for a full list of the types of sounds supported see the Windows Media Player supported file types page.

Creating a sound object

Similar to rectangles, circles, text, images, a sound object must be created before it can be used.   To define a sound we can use the following code:

In this case, the sound to be played would be located on your computer at:  d:\MySounds\Test.wav

You can also use the long object type definition style to create a sound object:

Each sound object has a number of properties:

Source The full file path (or a URL) of the sound file.
Speed The speed at which the sound will be played; 0 is stopped, 1 is normal speed, and 2 is double speed
Volume The volume at which the sound will be played; 0 is no sound and 1 is full sound.
Length The length (in milliseconds) of the sound if it was played at normal speed. This property will be incorrectly zero until the sound file has finished loading into memory.
Completed A count of how many times the sound has either finished playing to the end, or was stopped (using the stop keyword described below).

Regardless of which method you use, when you create your sound object Experimentor will immediately start loading the sound file into the computer’s memory asynchronously, so that it can be played quickly and on-demand.  Loading very large sound files (several megabytes) may take a few moments, so Experimentor will display an information message to the application messages like:

Loading sound file: “d:\MySounds\Test.wav”…

The benefit of pre-loading the sound files into memory is that when the trial is running a sound will start playing immediately, without a delay to load the sound from disk.

Sound actions

There are currently only two actions that can be performed with a sound:  playing it and stopping it.

Playing a sound object

To play a sound simply use the “playkeyword and then the name of your sound object:

This will immediately play the previously loaded sound file asynchronously.  That is, the sound file will start to play and the program codes will continue to run!  This means that while the sound is playing the program codes will also be continuing to run.  This allows you to play a sound while for example showing things on the screen.   If you want to play the sound and do nothing until it is competed you will need to put in a wait on the sound (see below).

Short-cut way to play a sound file

It is also possible to play a sound file directly like this:

However, you can’t stop such a sound once it is started.  Additionally, Experimentor must load the sound file when that line is run.  For small sound files this may be fine, but for larger sound files Experimentor may have to wait for the sound file to be loaded from disk, which could cause noticeable delays during your trials.  So with this approach Experimentor does not cache the sound in memory, and therefore Experimentor will have to load the sound file from disk every time this line of code is run.  For these reasons we generally recommend the approach of creating a sound object and playing that instead.

Stopping a playing sound

To stop a sound that is playing simply use the “stop” keyword and then the name of your sound object:

So for example, if mySound is 30 seconds long, we could stop it after 5 seconds with:

Waiting for a sound to finish

It is very easy to wait for a sound to finish:

Doing something when a sound stops

You can use the Completed property of the sound object to perform some action when the sound stops playing.  For example:

A Sound example

The following example demonstrates both ways of playing a sound file.  (Obviously you will need to provide your own path to a sound file on your computer for this example to work.)


Note: Audacity is an excellent free program for sound editing, and along with the FFmpeg plug-in can easily create, edit and save “.wav” files and many other types of sound files.