Sound component only plays randomly

If this template helps then use it. If not then just delete and start from scratch.

OS: Mac OS 10.15.7
PsychoPy version 2021.2.3
Standard Standalone? (y/n) yes
What are you trying to achieve?:
I have a sound stimuli bank of hundreds of pop songs sorted by year. I also have a series of routines that correspond to the same years and need to present the correct songs to the correct years. I also want to include a space bar key response to end the routine early if I need to skip a song.

What did you try to make it work?:
I have a series of routines that present audio stimuli within a giant loop. The loop pulls from a master csv that has hundreds of candidate stimuli (see screen shot below). The stimuli are sorted by year, and each routine, also corresponding to a year, draws from a specific $column in the spreadsheet so whatever randomize stimuli is played corresponds to the year that the routine represents. Audio is “set every repeat” and starts at 0.0. (See screenshot below for the builder view)
For the key component, I used the preset to end routine when ‘space’ is pressed.
What specifically went wrong when you tried that?:
The program does not crash, but will simply not play stimuli from some of the years. See the log below (note that most of the trials did play, but a few did not - they say ‘none’ in the logs)
|12.2622 |EXP |New trial (rep=0, index=0): OrderedDict([(‘year_2005’, ‘2023_Original_38.wav’), (‘year_2004’, ‘Original_2022_38.wav’), (‘year_2003’, ‘2021_Original_38.wav’), (‘year_2002’, ‘2020_Original_38.wav’), (‘year_2001’, ‘2019_Original_38.wav’), (‘year_2000’, ‘2018_Original_38.wav’), (‘year_1999’, ‘2017_Original_38.wav’), (‘year_1998’, ‘2016_Original_39.wav’), (‘year_1997’, ‘2015_Original_38.wav’), (‘year_1996’, ‘2014_Original_38.wav’), (‘year_1995’, ‘2013_Original_38.wav’), (‘year_1994’, ‘2012_Original_38.wav’), (‘year_1993’, ‘2011_Original_38.wav’), (‘year_1992’, ‘2010_Original_38.wav’), (‘year_1991’, ‘2009_Original_38.wav’), (‘year_1990’, ‘2008_Original_38.wav’), (‘year_1989’, None), (‘year_1988’, None), (‘year_1987’, None), (‘year_1986’, ‘2004_Original_i_want_you.wav’), (‘year_1985’, ‘2003_Original_holidae_in_album_version_.wav’), (‘year_1984’, ‘2002_Original_groove_me.wav’), (‘year_1983’, ‘2001_Original_get_ur_freak_on.wav’), (‘year_1982’, ‘2000_Original_i_cry.wav’), (‘year_1981’, ‘1999_Original_meeting_in_my_bedroom.wav’), (‘year_1980’, ‘1998_Original_i_gotta_be.wav’), (‘year_1979’, ‘1997_Original_i_love_you_always_forever.wav’), (‘year_1978’, ‘1996_Original_in_the_hood.wav’), (‘year_1977’, ‘1995_Original_hold_me_thrill_me_kiss_me_kill_me.wav’), (‘year_1976’, ‘1994_Original_groove_thang.wav’), (‘year_1975’, ‘1993_Original_i_belong_to_you.wav’)])|

In other trials, I get nearly none to only half of the stimuli to play at all example below:

1.7511 EXP Created sequence: random, trialTypes=148, nReps=1, seed=None
11.7524 EXP New trial (rep=0, index=0): OrderedDict([(‘year_2005’, None), (‘year_2004’, None), (‘year_2003’, None), (‘year_2002’, None), (‘year_2001’, None), (‘year_2000’, None), (‘year_1999’, None), (‘year_1998’, None), (‘year_1997’, None), (‘year_1996’, None), (‘year_1995’, None), (‘year_1994’, None), (‘year_1993’, None), (‘year_1992’, None), (‘year_1991’, None), (‘year_1990’, None), (‘year_1989’, None), (‘year_1988’, None), (‘year_1987’, None), (‘year_1986’, ‘2004_Original_so_sexy.wav’), (‘year_1985’, ‘2003_Original_pump_it_up.wav’), (‘year_1984’, ‘2002_Original_oops_oh_my_.wav’), (‘year_1983’, ‘2001_Original_most_girls.wav’), (‘year_1982’, ‘2000_Original_step_into_a_world_rapture_s_delight_.wav’), (‘year_1981’, ‘1999_Original_whatever_radio_version_.wav’), (‘year_1980’, ‘1998_Original_sock_it_2_me_featuring_da_brat_.wav’), (‘year_1979’, ‘1997_Original_you_bring_me_up.wav’), (‘year_1978’, ‘1996_Original_before_you_walk_out_of_my_life.wav’), (‘year_1977’, ‘1995_Original_sprinkle_me.wav’), (‘year_1976’, ‘1994_Original_sweet_potatoe_pie.wav’), (‘year_1975’, ‘1993_Original_the_floor.wav’)])

I have tried specifying start times for the key and audio component (different from one another) in case this was the issue, as well as taking away the key component all together and I still have the same issue.

I would also like to point out that each year has a different number of candidate stimuli - some columns have as few as 12 items while others have 110. (I am not aware of this being an issue but let me know if it is!). My first question is if the issue is related to the spreadsheet columns being different lengths or not. And, if not, what might be conflict with the component?

Thank you so much for your help in advance!


Do you mind showing us the loop you are using, just a screenshot of the loop definition will help.

In some cases you will probably get None because the number of stimuli per column is different.

It is often better to arrange everything in a long rather than a wide format, and to reuse a routine rather than adding routine after routine as you have done. So a routine for stimulus presentation and control, which year is played by a condition file or a combination of condition files.

So in your case a condition file that calls each year-condition file containing the audios to be presented might do the job.

Best wishes Jens

I have attached it to this reply! Thank you for the response!

This is what I was afraid of. I admit that this is probably not the best way to do this. My end goal is that the participant will enter their birth year and ONLY the routine (and matching stimuli) corresponding to that input will present for them. I had initially tried to do this with one trial routine as you suggest, and create a condition with just the stimuli, but could not make it work at all. My solution was instead to create conditional routines that could be skipped based on input, if that makes sense. Happy to consider a different approach if you know of one!


you could make use of the Selected rows-parameter of make the condition that you use to present the stimuli dependent on the birthyear. The latter suggestion is probably the easiest to implement. What time range of birthyears do you intend to investigate?

Best wishes Jens

Thanks so much for your help! Forgive me, I don’t come from a coding background - I am under the impression that the stimuli pull from columns and not rows? I had attempted rows initially (I have a former post about this - see here ) However, I never was able to make it work. I have had trouble finding resources that address conditional stimuli like this as opposed to routines, so I went with this route. It certainly didn’t pull the stimuli directly from rows, so I attempted to make every row a separate csv for each stimuli year and try a nested loop, which also did not work. If you know a place where I can read about how to cue a row of stimuli in a sound component (and a loop) I am happy to look into it!

The only thing I can think of currently is to keep the 63 routines, each with its own loop within the larger loop, and assign rows of stimuli to the routine loops, and create code to skip all 62 routines that do NOT correspond to the birth year input. This equally seems not elegant - I am sure there is a better way!

There are 63 possible years (conditions).

Thanks so much


Here is a small toy experiment that selects the condition file based on the year of birth entered by the participant. Currently there are only two condition files, 2000.xlsx, 2001.xlsx.

2000.xlsx (8.4 KB)
2001.xlsx (8.5 KB)
Birthyear.psyexp (10.8 KB)

The condition files vary in length.

The year of birth is a mandatory field in expInfo, it is not checked for validity. So if a participant enters a year of birth for which there is no condition file, the experiment will crash. But this can be dealt with later. For example, the year of birth is asked in a separate routine, which allows the information provided by the participant to be checked, instead of using expInfo.

This toy experiment is intended to be a proof of concept.

Best wishes Jens

Thank you so much! I see the hypothetical logic and this would work wonderfully. I did, however, test this just now (with both the 2021 PsychoPy as well as the newest version I just downloaded in addition) and did not get the text to display in either one. On 2023.2.3 psychopy crashed and displayed this error: File “/Users/libbyroberts/Downloads/”, line 379, in run
trialComponents = [trial_2, textFile]
NameError: name ‘trial_2’ is not defined
################ Experiment ended with exit code 1 [pid:14057] #################

On my original version it did not crash, but did not display text either. The following is the log:

13.3790 WARNING User requested fullscreen with size [2048 1152], but screen is actually [1280, 800]. Using actual size
14.2184 EXP Created window1 = Window(allowGUI=True, allowStencil=False, autoLog=True, backendConf=UNKNOWN, backgroundFit=<method-wrapper ‘getattribute’ of attributeSetter object at 0x7f8c26e44580>, backgroundImage=<method-wrapper ‘getattribute’ of attributeSetter object at 0x7f8c26e44520>, bitsMode=UNKNOWN, blendMode=‘avg’, bpc=(8, 8, 8), color=array([0, 0, 0]), colorSpace=‘rgb’, depthBits=8, fullscr=<method-wrapper ‘getattribute’ of attributeSetter object at 0x7f8c26e443a0>, gamma=None, gammaErrorPolicy=‘raise’, lms=UNKNOWN, monitor=<psychopy.monitors.calibTools.Monitor object at 0x7f8c262979a0>, multiSample=False, name=‘window1’, numSamples=2, pos=[0.0, 0.0], screen=0, size=array([1280, 800]), stencilBits=0, stereo=False, title=‘PsychoPy’, units=‘height’, useFBO=True, useRetina=False, viewOri=0.0, viewPos=None, viewScale=None, waitBlanking=True, winType=‘pyglet’)
14.2186 EXP window1: mouseVisible = True
14.2186 EXP window1: backgroundImage = ‘’
14.2186 EXP window1: backgroundFit = ‘none’
14.2223 EXP window1: Attempting to measure frame rate of screen (0) …
14.2224 EXP window1: recordFrameIntervals = False
14.4005 EXP window1: recordFrameIntervals = True
14.5839 EXP Screen (0) actual frame rate measured at 60.09Hz
14.5839 EXP window1: recordFrameIntervals = False
14.5847 EXP window1: mouseVisible = False
17.4985 EXP Birthyear: status = STARTED
17.4993 EXP window1: mouseVisible = True
17.5106 EXP Created textFile = TextStim(class=<class ‘psychopy.visual.text.TextStim’>, alignHoriz=method-wrapper(…), alignText=‘center’, alignVert=method-wrapper(…), anchorHoriz=‘center’, anchorVert=‘center’, antialias=True, autoDraw=False, autoLog=True, bold=False, color=array([1., 1., 1.]), colorSpace=‘rgb’, contrast=1.0, depth=-2.0, draggable=False, flipHoriz=False, flipVert=False, font=‘Open Sans’, fontFiles=, height=0.05, italic=False, languageStyle=‘LTR’, name=‘textFile’, opacity=1.0, ori=0.0, pos=array([0., 0.]), rgb=UNKNOWN, text=‘’, units=‘height’, win=Window(…), wrapWidth=1)
0.0319 EXP Imported 2001.xlsx as conditions, 10 conditions, 1 params
0.0329 EXP Created sequence: random, trialTypes=10, nReps=1, seed=None
0.0331 EXP New trial (rep=0, index=0): {‘FileNr’: 2001}
0.3419 EXP window1: mouseVisible = True

Both versions seem to be pulling up the correct spreadsheet correlating to entered birth year (either 2000 or 2001) so that’s a HUGE step, thank you!


I downloaded the experiment and ran it again on my laptop. It ran without any problems. There is no trial_2 in the experiment. You should try to compile the experiment and look for trial_2 in the Python script.

Another reason to switch to the long format is that PsychoPy copies all 63 columns into your result file, making analysis very difficult.

Best wishes Jens

Thanks! Yes, I looked through the script, and I attached the original (lastrun file). The issue seems to be line 379. I deleted trial_2 from that line and it seems to work now. So we might have a solution! Do you have any idea where the trial_2 came from? (20.5 KB)

Edit: attaching screengrab of the trial_2 component from the builder view. This was present when I opened your original toy experiment from the download.

I think that means that you are using a pre 2023.2 version without the routine settings option. Routine settings are being interpreted as an unknown component.

This happened using 2023.2.3 and also when I tried it on my older version. But it definitely seems to be some sort of accidental byproduct either way. I deleted the component and everything seems to be functional now. Thanks!