Search code examples
psychopy

Replay stimulus multiple times in PsychoPy


I'm building an experiment where in each trial, the subjects can replay stimulus as many times as they wish by pressing certain keys. For example, there're 2 sounds: s1 and s2. s1 is associated with key 'a', and s2 is associated with key 'j'. Whenever the subject presses 'a', s1 will be played. Same for s2. Space bar is used to terminate the present trial and jump to the next trial.

Now I'm able to play stimulus after key press, by having '$event.getKeys('a')' in the condition field in s1 sound component, and '$event.getKeys('j')' in s2 sound component. But this will only play each stimulus once in a trial. The second time the keys are pressed, there are no sounds.

So my question is, what should I do so that in each trial, a stimulus can be played EACH time a designated key is pressed?


Solution

  • The Builder sound component is not ideal for this: as you noticed, if a condition is specified for the onset, it can only trigger the sound once per trial.

    An alternative is to create a non-graphical sound object in a code component, and then check for key presses on every frame of each trial, and trigger the sound as required. Insert a Code component. In the "Begin Experiment" tab, put something like this:

    j_sound = sound.Sound(u'A', secs=0.5) 
    a_sound = sound.Sound(u'B', secs=0.5) 
    

    Then, in the "Each frame" tab, put something like this:

    response = event.getKeys(keyList=['a','j'])
    if 'a' in response:
        a_sound.play()
    elif 'j' in response:
        j_sound.play()
    

    This could be improved to handle key presses made while a sound is already playing, but should get you started.