How to properly loop a sound stimulus for certain amount of time?

Hi Jonathan,

I also have this error in my experiment, but I’m mainly using the builder and know little about python coding. Could you please have a look at my experiment?

What I want to achieve:

In my main routine, I have a conditions file where after every 5 trials of stimuli there is 1 trial of filler. For the stimuli, corresponding sounds will be played; for the fillers, no sound is needed, so I put a very short silence sound as a placeholder. All the paths of sound files are in the column “elicit_recording” in the conditions file.

For stimuli trials, participants will press ‘space’ to play sound (and replay it as many times as they want), then press ‘right’ to move to the next trial; for fillers trials, the silence sound should play automatically without key press, so participants will only press ‘right’ to move to the next trial.

I used to run the experiment smoothly, but after I replaced the test sounds with the actual sound files, a problem occurred: the sounds of the first 5 stimuli trials play fine, but after the first filler, the sounds never play and a TypeError occured:

  File "sounddevice.pyc", line 733, in callback_ptr
  File "sounddevice.pyc", line 2508, in _wrap_callback
  File "/Applications/PsychoPy2021.app/Contents/Resources/lib/python3.6/psychopy/sound/backend_sounddevice.py", line 201, in callback
    dat *= thisSound.volume  # Set the volume block by block
TypeError: unsupported operand type(s) for *=: 'NoneType' and 'float'
##### Experiment ended. #####

Below are the codes and components I’m using:

I used a code component to create and play/replay sounds:

## Begin Routine:
# create sound components
sound_replay = sound.Sound(value=elicit_recording, secs=-1)

## Each Frame:
# if stimuli
if elicit_recording != 'silence_0.2s.wav':
    # press space,play sound
    if event.getKeys(keyList=['space']) and sound_replay.status != STARTED:
        sound_replay.play()
# if fillers
elif elicit_recording == 'silence_0.2s.wav':
    # play sound
    sound_replay.play()

(I also tried the codes below, but problem’s the same:)

## Begin Routine:
# create sound components
sound_stimuli = sound.Sound(value=elicit_recording, secs=-1)
sound_filler = sound.Sound(value=elicit_recording, secs=0.1)
sound_filler.setVolume(0.0) # silent sound

if elicit_recording != 'A':
    sound_replay = sound_stimuli
else:
    sound_replay = sound_filler

## Each Frame:
# if stimuli
if sound_replay == sound_stimuli:
    # press space,play sound
    if event.getKeys(keyList=['space']) and sound_replay.status != STARTED:
        sound_replay.play()
# if fillers
elif sound_replay == sound_filler:
    # play sound
    sound_replay.play()

Then, I used a key component to move to the next trial. In key properties >> Start, I chose ‘condition’ and filled in:

sound_replay.status==FINISHED

What I’ve tried:

  1. I’ve tried changing the hardware preferences as in this post: Sounds stop playing after 2/3 stimuli when running the experiment several times. But sounddevice is the only one I can use - I’ve met lots of problems when installing pyo, and the sounds keep off and on when using ptb.

  2. I commented the line sound_filler.setVolume(0.0)

  3. I uninstalled PsychoPy, cleaned all the remaining files, and reinstalled it, to reset the userPrefs.cfg file.

  4. I made sure there’s no empty cell/row/column in my conditions file.

  5. I added an if condition of the key press for sound play before playing the filler sound, just as for the stimuli sound, as below:

# if fillers
elif sound_replay == sound_filler:
    # play sound
    if event.getKeys(keyList=['space']) and sound_replay.status != STARTED:
        sound_replay.play()

and now the stimuli sounds following the first filler can finally play. But I don’t want an extra ‘space’ press for the filler trials.

If I replace if event.getKeys(keyList=['space']) and sound_replay.status != STARTED: to if not event.getKeys(keyList=['space']) and sound_replay.status != STARTED:, the same problem occurred.

So, all I know now is that the problem should be caused by the if condition of dividing the stimuli/fillers sounds to vary in key press, but I don’t know how to solve it.

I’ll be so grateful if any idea/advice could be provided! The error report is about the source file instead of my experiment file, so I have no idea about how to modify my codes at all. Many thanks in advance!