psychopy.org | Reference | Downloads | Github

One background music during a loop

Hello everyone.

I think this is a basic problem but by now I’m still unable to find the solution, sorry to ask but please help TAT

I’m using Mac OS and psychopy v3.0.0b10.
What I want to achieve is showing a random presence of 90 words, while a continuous background music playing from beginning till the end of the presentation. But I don’t know how to put this sound file played over a loop but not repeated with every word.
What’s worse, I haven’t learnt anything about coding so couldn’t see anything wrong from the codes.

Sorry for my English, please ask if you found my description a mess.

Ren

You need to use some tiny code snippets here to do this, as all Builder stimuli are designed to start and stop within a routine, while you want one that will span multiple iterations over a routine.

Insert a code component on the routine where the sound will play. In the "Begin experiment" tab, insert code something like this to create the sound object in advance:

background_sound = sound.Sound('your_sound_file.wav')

In the "Begin routine" tab, put something like this, so that the sound starts playing, but only on the first iteration:

if your_loop_name.thisN == 0:
    background_sound.play()

I guess you might also need to stop it after the 90th trial, so put something like this in the "End routine" tab:

if your_loop_name.thisN == 89:
    background_sound.stop()
1 Like

Thanks a lot!
Been having a rough time looking for all the tabs but finally it’s done!
First time with codes even before a “hello world” and I’m dancing now :smiley:

1 Like

Just wanted to check that you inserted a graphical code component (as in the screenshot below), and haven’t been trying to edit the Builder-generated Python script directly? The latter is definitely something to avoid.

Ah, should I say, bad news?:sweat_smile:

OK, that is quite a heroic approach! As you’ve seen, Builder-generated scripts are quite verbose and it can be hard to get it right when trying to customise them. Using a graphical code component has (at least) two advantages:

  • You just select which tab to put the code into, and Builder will automatically insert it in the correct place in the script, without breaking anything.
  • You can continue working within the graphical Builder interface. Manually customising the .py file is a one way street: you have to keep working with that script from then on, and can’t tweak it by working on the .psyexp file via the Builder GUI,

So I’d recommend that you discard this edited script and go back to the Builder GUI, insert a code component from the “custom” section of the components panel, and put the code in the tabs as suggested above. This will just take 1-2 minutes and then you’ll still have a graphical Builder .psyexp file that you can continue to develop.

This isn’t just a suggestion for new users: we would recommend this approach in almost all cases, even for people quite comfortable with Python programming.

Merlin’s beard…Almost too awkward to admit I’ve never realized a “code component” is supported to be something from component section :rofl::rofl:

Haha can’t believe how I spent a day to get something that’s been given that directly, laughing really loudly…

Really appreciate all this, thank you!

What does this mean?

This sounds like actually a different requirement than the original poster in this topic. It might be best if you create a new topic and give much more detail of what you specifically want to achieve.

OK, Thanks

Hi, Michael. I used the code you provided to play background sound. The background sound play success, but the foreground sound(sound_1 in following picture) did not play. there is only background sound. what is the problem? Many thanks in advance for you.

image

You need to remove that keypress check in the “Begin routine” tab. The .waitKeys() function will break the timing of Builder scripts and should not be used except in code you rite yourself. Also, the .getKeys() function is unlikely to worth the way you expect here: it is an instantaneous check: unless the person happens to just have press the key, you are unlikely to detect it if you are only running that line of code once at the start of each routine.

In the sound component, remove the second $ symbol.

I don’t know what neg_list[] contains or what the current index is. You should insert this line in the “Begin routine” tab as some (temporary) debugging code:

print(neg_list[current])

Having said that, I have no idea what happens if one tries to play two sound simultaneously. Have you tested that, just with simple sound components set to point to constant sound files?

Yeah, I have tested two simple sound components(white noise & auditory word). there is always whites noise play but not auditory word sound. when I just play auditory word, it worked.