This lesson is being upgraded during January and February 2022.

Many examples show how to present visual stimuli, such as bitmaps, rectangles, or texts. This lesson shows you on how to play sounds as stimuli.

This lesson also explains all the other instructions used.

There are two different ways computers can generate sounds, please understand this clearly:

1. Play a sound file, such as an MP3 sound/music file (PsyToolkit syntax)
2. Play a specific sound frequency ( PsyToolkit syntax)

The MP3 sounds in this lessons have been created with the free audacity software and are based on square waves. This produces a bit of a technical beep sound, which can sometimes be quite nice. With audacity you can make any sort of sound (watch this video or look on Youtube for audacity create tone or something like that).

In this lesson, we use MP3 files.

Running the code

First run the experiment to see what it does.

The PsyToolkit code

bitmaps
  instruction
  rectangle
  circle
  feedback_correct
  feedback_wrong

sounds
  low_sound  lo.mp3
  high_sound hi.mp3

table my_sounds
  rectangle low_sound  1 "lo_rectangle"
  circle    high_sound 2 "hi_circle"

task my_task
  keys a l
  table my_sounds
  show bitmap @1
  sound @2
  readkey @3 3000
  clear -1
  if STATUS == CORRECT
    show bitmap feedback_correct
  fi
  if STATUS == WRONG
    show bitmap feedback_wrong
  fi
  delay 500
  clear -1
  save TASKNAME TABLEROW @4 RT STATUS

block test
  message instruction
  tasklist
    my_task 5
  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. There are visual stimuli (bitmaps) and sounds. The computer assumes that the bitmaps are PNG files, although other bitmap formats can be used as well. For example, instruction tells the computer that there is a file instruction.png and that it will be referred to simply as instruction. The same is the case for rectangle, circle, feedback_correct, and feedback_wrong. Each of them is a (small) image.

Download the zip file of this experiment and see each of these stimuli.
bitmaps
  instruction
  rectangle
  circle
  feedback_correct
  feedback_wrong
sounds
  low_sound  lo.mp3
  high_sound hi.mp3

The table

You do not always need a table, but it makes life easier. In short, a table defines the different conditions. Each line represents a condition. Because there are 2 lines, there are thus 2 different conditions.

In this example, there are four different colums, each of which can be referred to with the @ sign. @1 refers to the first column, @2 to the second, etc.

Here is what the 4 columns stand for in this example (you can have as many columns as you like).

  1. The bitmap to be used

  2. The sound to be used

  3. The correct key (1 refers to A, and 2 to L, see keys line in the task)

  4. A human readable descriptor. In quotes, it is text which can be saved later

The computer selects one line on each trial when the task is being run through.
table my_sounds
  rectangle low_sound  1 "lo_rectangle"
  circle    high_sound 2 "hi_circle"

The task

A task is a crucial element of an experiment. A task has a name (here my_task) and there are multiple instructions, one on each line. The various parts of the task are described in detail below. The first line is the name.

task my_task

Some initial stuff

All lines folling the task my_task will be indented so that you can see that they belong to this task.

The indentation makes the code easier to read but is not compulsory.

We define first that there are two keys to be used in this task, namely the keys "a" and "l".

  keys a l

Then we say that this task will use the table names my_sounds (see the actual table explanation above). This use of the table will allow us to refer to its column with the @ sign. @1 refers to the first column, etc.

Essential tip: When we say we use a table, the computer will automatically choose randomly one of its line. This helps us to randomly use a condition.
  table my_sounds

Showing and sounding stuff

Now we just show a bitmap, namely the item that is on the first column of the chosen line, and we sound what is on the second column of the chosen line.

  show bitmap @1
  sound @2

Waiting for keyboard response and feedback

The readkey instruction is used to wait for a participants response. There are two possible keys (as defined with the earlier keys a l instruction. The readkey instruction knows that the value as set in the third column of the table is the correct one.

  readkey @3 3000

Immediately after the response, the stimulus is erased from the screen with the clear instruction. The -1 clears the last presented stimulus.

Depending of the STATUS of the readkey, feedback is given. If the participant responsed correctly, it shows the feedback_correct image, and if wrong, the feedback_wrong image. These are bitmaps loaded in memory in the bitmaps section.

  clear -1
  if STATUS == CORRECT
    show bitmap feedback_correct
  fi
  if STATUS == WRONG
    show bitmap feedback_wrong
  fi

After that, the image stays on screen for 500 ms (there is a delay of 500 ms). Then the image is erased.

  delay 500
  clear -1

Finally, the variables relavant are saved. You can choose whatever you want to save, in this case, they are as follows.

  • TASKNAME is the name of the task

  • TABLEROW is the number of the randomly chosen line of the table of the current trial

  • @4 is the 4th column of the currently chosen line of the table, which is a human readable description of the trial, which is useful during data analysis.

  • RT is the reaction time in missiseconds

  • STATUS is 1, 2, or 3 (1=CORRECT, 2=WRONG, 3=TIMEOUT).

  save TASKNAME TABLEROW @4 RT STATUS

The block

The last part is the block. Blocks are crucially important. This is actually where the code starts, so to speak. The block has a sequence as follows:

  • A message, showing the bitmap instruction

A message instruction shows a bitmap and then waits for a key press on the space bar. You can have multple messages in a block. For example, you could have a thank you message after the task list.
  • A tasklist, which calls the task my_task exactly 5 times.

You can have more than one task, but here we have just one. The different conditions, etc, are being handled by my_task.
block test
  message instruction
  tasklist
    my_task 5
  end