| Reference | Downloads | Github

All sound stimuli playing instead of just one

Hi all,

I am trying to run an experiment that asks the user to discern between two different audio stimuli. Ideally, the user hears one music segment (MS1) and then hears one of three possible segments the second time (MS2). When I run the program, MS1 plays correctly, but MS2 keeps playing all three options instead of randomly choosing and playing just one.

This is the outer loop dialog box:

This is the inner loop dialog box:

Any idea what I have done wrong?

A computer will do exactly what you tell it to, which is the cause of nearly all software ‘bugs’ :wink:

I’m guessing that the conditions file for song_loop2 have three rows. You have specified nReps == 1, so the loop will run through that file once, giving three sounds, in random order. If you want only one sound to play, then you need to terminate the loop early. You can do this by inserting a code component in the MS2 routine and putting this in the End routine tab:

# end the loop after the first iteration:
song_loop2.finished = True

That would probably work for you, but i suspect that your overall organisation here is not optimum. i.e. it would probably be better to move to an arrangement where you have fewer conditions files. e.g. try to envisage a way in which the information for each trial could be united in a single row of a larger conditions file.

e.g. you could have three columns within such a file, for sounds 1 through 3, and on any given trial, you randomly choose one of those to play. This would likely simplify things for you a lot (i.e. I suspect you could probably run the whole thing with a single loop and single conditions file).

Thanks for this!

It seemed to solve the inner loop issue, but now it doesn’t run multiple trials; it runs once and then cuts to the end routine.

As for the overall organization, I originally tried something like what you describe in your last paragraph, but I wasn’t successful in implementing it. I had an excel file that was set up in the way that you are talking about, but I could not figure out how to get PsychoPy to randomly choose a parameter in the Builder. I’m still very new to Python, so I wasn’t sure where to attack the issue in Coder.

You don’t need to use Coder: instead you can insert a code component in Builder. This allows you to have the best of both worlds: use the graphical interface of Builder yet be able to add some flexibility by inserting a few snippets of code to run at the correct time as required.

In this case, let’s say you have the typical PsychoPy arrangement where you have a single conditions file that collects all the information for a given trial together on the same row. So you might have three columns specifying the possible sounds that could play on that trial. Let’s say those columns are labelled sound_1, sound_2, and sound_3. So you insert a code component, and in the ‘Begin routine’ tab, insert something like this:

# choose one of the three variables:
chosen_sound = np.random.choice([sound_1, sound_2, sound_3])

# record the result in the datafile for analysis purposes:
thisExp.addData('chosen_sound', chosen_sound)

Then in your sound component, you can just put this in the Sound field, set to update on every routine:


Make sure the code component is above your sound component, so that a new value is selected for the current trial before the sound component needs access to it.