Sometimes, you want to play some music or a tune while people are doing their experiment.

(Or alternatively, sometimes you want a play during the instructions, but stop that during the reaction time measurements — that is possible too).

This is different from playing sounds as stimuli, for which there is a different lesson.
The sound used in this less has been downloaded from Bensound.com. This sound is copyrighted. You can setup your own account with Bensound.com and use their sounds in your experiments. It is a very good website with really nice tunes. The tune used in this study is called scifi, which is listed under electronics.

Running the code

First run the experiment to see what it does. This is a very simple reaction time experiment. You note that the sound starts already during the instructions and runs indefinitely.

The PsyToolkit code

bitmaps
  instructions

sounds
  my_tune bensound_scifi.mp3

fonts
  arial 24

table mytable
   "Press b" 1
   "Press n" 2

task simple
   table mytable
   keys b n q
   show text @1
   readkey @2 5000
   clear -1
   if STATUS == CORRECT
     show text "Well done"
   fi
   if STATUS == WRONG
     show text "You pressed they wrong key"
   fi
   if STATUS == TIMEOUT
     show text "You pressed no key at all. Press b, n, or q to quit"
   fi
   if KEY == 3
     end experiment
   fi
   delay 500
   clear -1
   save STATUS RT

block my_block
   sound my_tune loop
   message instructions
   tasklist
     simple 100
   end

Code explained bit by bit

Below, the different sections are explained one by one:

Defining the stimuli

The bitmaps and sounds sections tell the computer which stimuli are being used. You can read about bitmaps in this lesson.

Download the zip file of this experiment and see each of these stimuli.

The sounds section tells the computer the name of the MP3 file to be used:

sounds
  my_tune bensound_scifi.mp3

The fonts section just tells the computer that the fonts to be used are arial at a size of 24 points.

fonts
  arial 24

The next sections are the table and the task. They are further irrelevant for understanding the playing of the sound. But we still show it here. In short, there are two conditions defined in the table. People have to press the b or the n key. The task chooses one of these conditions at random on each trial and people have to press the key within 5 seconds. This is a trivially easy task, of course.

table mytable
   "Press b" 1
   "Press n" 2

task simple
   table mytable
   keys b n q
   show text @1
   readkey @2 5000
   clear -1
   if STATUS == CORRECT
     show text "Well done"
   fi
   if STATUS == WRONG
     show text "You pressed they wrong key"
   fi
   if STATUS == TIMEOUT
     show text "You pressed no key at all. Press b, n, or q to quit"
   fi
   if KEY == 3
     end experiment
   fi
   delay 500
   clear -1
   save STATUS RT

In the block, the first line tells the computer to play the sound my_tune during the block. The loop option makes sure that the sound is played forever, as long as the person does the task.

block my_block
   sound my_tune loop
   message instructions
   tasklist
     simple 100
   end
If you want to have the tune only during the instruction, add the silence line after the message. See the alternative source below.
block my_block
   sound my_tune loop
   message instructions
   silence my_tune
   tasklist
     simple 100
   end