Presenting auditory stimuli
For some experiments you will need to play an audio file for the participant. This section focuses on implementing audio files into your Psychopy2 script. We will specifically be focusing on how to add .wav type files to your experiment.
There is a website here that can convert other types of audio files into the .wav type, which is handy - link here.
Note: remember to cut your sound files so they are in the right time length you need for your experiment.
Simple audio file code
Playing audio files in an experiment window is simple. First we import the sound module and the other necessary modules.
from psychopy import sound, visual, event
Next we create a window, just like we did for the visual stimuli.
win = visual.Window(fullscr=True )
Then we create and object for our sound waves. I have used three sound waves, one that makes dog noises, another that makes cat noises and a third that makes chicken noises.
Make sure the sound waves are located in the folder that your code is saved in.
stimuli = ["dog.wav", "cat.wav", "chicken.wav"]
Then we create a loop to loop through our stimuli and play them. Inside the loop we use the sound.Sound()
function to specify that we are dealing with sound files.
Then we use the.play()
function to play the audio files.
We also specify that we want the next sound file to be played after we have pressed a key on the keyboard, using the event.waitKeys()
and thewin.flip()
functions, that you should be familiar with by now.
for trial in range(len(stimuli)):
# select stimulus
stimulus = stimuli[trial]
# prepare stimulus
recording = sound.Sound(stimulus)
# play stimulus
recording.play()
#flip window
win.flip()
event.waitKeys()
win.close()
If you run your code now, it should play the two audio files each one after you press a key on the keyboard.
Randomization
Notice that the sound waves play in the order that you specified them to be in when you created the "stimuli" object.
If you want to randomize the order in which the audio files are played, import the module for randomization:
import random
Then before you run the loop, specify that you want to randomize the stimuli, using the random.shuffle()
function.
random.shuffle(stimuli)
Repetition
If you have two sound files, but you want them to be played more than once during the experiment you can add the following code.
First create a variable that contains the amount of times you want each sound wave to be repeated.
REP = 3
Then create your stimuli object containing your audio files, like we did before:
stimuli = ["dog.wav", "cat.wav"]
Then update your stimuli object, by timing it with your repetition object, REP:
stimuli = stimuli*REP
Now Psychopy thinks you have 6 different audio files that each will play after the participant presses a key on the keyboard.
Experiment example using audio files and image files
To understand how you can further implement audio files into your experiment, we will create an experiment using audio files.
In the experiment we will test whether image stimuli can distract audio perception. The participant will hear a sounds play of either a cat or dog.
The participant will be instructed to press the left key on the keyboard if they hear a cat sound, and the right key on the keyboard if they hear a dog sound.
At the same time the experiment will show the participant a picture of either a dog or cat. This picture is irrelevant to what key they need to press, however it will try to distract them a little.
Variables
We expect the reaction time might be slightly longer when an image of a dog is shown but the sound of a cat is played, so we will record the participant's reaction time.
We will also record the correctness of the participant's answers - whether they correctly pressed the key for cat when a cat sound was played or if they got it wrong and pressed the key for dog.
The third variable we are interested in is whether the sound and image were equal or different in each trial. We will call this variable Equalness.
Importing Modules
First we will need to import the relevant modules.
Importing necessary modules:
from psychopy import sound, visual, event, gui, core, data
You need to import the random module to sample/randomize things
import random
import pandas
Specify your images and sound location folder
See chapter 3 - Images and Image Manipulation for more info explaining how you find your path.
path = "C://Users//Christina//Desktop//"
Dialogue Box
Now we want to create a dialogue box to gather general information like ID, name, age or gender from the participant.
# ID, age, gender box display
myDlg = gui.Dlg(title="Cat and Dog Experiment")
myDlg.addField('ID:')
myDlg.addField('Age:')
myDlg.addField('Gender:', choices = ['Female', 'Male'])
myDlg.show() #It shows the dialogue inputs to the participant
if myDlg.OK:
ID = myDlg.data[0]
Age = myDlg.data[1]
Gender = myDlg.data[2]
else:
core.quit()