# One background music during a loop

**URL:** <https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803>\
**Category:** Builder\
**Created:** [November 13, 2018, 2:08am UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803 "2018-11-13T02:08:54Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![shellcottage](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shellcottage/32/3285_2.png) [@shellcottage](https://discourse.psychopy.org/u/shellcottage)\
**Post date:** [November 13, 2018, 2:08am UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/1 "2018-11-13T02:08:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [November 13, 2018, 4:05am UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/2 "2018-11-13T04:05:00Z")

</div>

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:

```auto
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:

```auto
if your_loop_name.thisN == 89:
    background_sound.stop()

```

---

<div class="post-metadata">

**Author:** ![shellcottage](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shellcottage/32/3285_2.png) [@shellcottage](https://discourse.psychopy.org/u/shellcottage)\
**Post date:** [November 13, 2018, 5:56pm UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/3 "2018-11-13T17:56:19Z")

</div>

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 😃

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [November 13, 2018, 9:47pm UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/4 "2018-11-13T21:47:18Z")

</div>

> [@shellcottage](#):
>
> Been having a rough time looking for all the tabs but finally it’s done!

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.

 ![04](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/5/c/5c77d4e87f466ea6532b82bf28bca8a18d359d62.jpeg)

---

<div class="post-metadata">

**Author:** ![shellcottage](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shellcottage/32/3285_2.png) [@shellcottage](https://discourse.psychopy.org/u/shellcottage)\
**Post date:** [November 13, 2018, 11:37pm UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/5 "2018-11-13T23:37:56Z")

</div>

Ah, should I say, bad news?😅

 ![07](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/2/c2a543ba8f9f04348b15aa5bd65a972a0a9d51f6.jpeg)

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [November 14, 2018, 12:46am UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/6 "2018-11-14T00:46:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shellcottage](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shellcottage/32/3285_2.png) [@shellcottage](https://discourse.psychopy.org/u/shellcottage)\
**Post date:** [November 14, 2018, 1:19am UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/7 "2018-11-14T01:19:12Z")

</div>

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

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!

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [July 7, 2019, 1:52am UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/9 "2019-07-07T01:52:04Z")

</div>

> [@Xia\_Tao](#):
>
> I have defined the loops=-1

What does this mean?

> [@Xia\_Tao](#):
>
> I have a 9 s white noise and want to play it all the experiment.

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.

---

<div class="post-metadata">

**Author:** ![Xia\_Tao](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/xia_tao/32/5138_2.png) [@Xia\_Tao](https://discourse.psychopy.org/u/Xia_Tao)\
**Post date:** [July 7, 2019, 3:05am UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/10 "2019-07-07T03:05:39Z")

</div>

OK, Thanks

---

<div class="post-metadata">

**Author:** ![Xia\_Tao](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/xia_tao/32/5138_2.png) [@Xia\_Tao](https://discourse.psychopy.org/u/Xia_Tao)\
**Post date:** [July 8, 2019, 8:44am UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/11 "2019-07-08T08:44:33Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/a/0/a0db7392bb356035981c7e39911a5bb6c93ad115.png) ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/e/c/ecf44d71d30049f0bcfcc5ea97e744272de59515.png)

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [July 9, 2019, 2:19am UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/12 "2019-07-09T02:19:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Xia\_Tao](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/xia_tao/32/5138_2.png) [@Xia\_Tao](https://discourse.psychopy.org/u/Xia_Tao)\
**Post date:** [July 9, 2019, 7:56am UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/13 "2019-07-09T07:56:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![LAIDA\_ELORZA\_OTAEGUI](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/laida_elorza_otaegui/32/16308_2.png) [@LAIDA\_ELORZA\_OTAEGUI](https://discourse.psychopy.org/u/LAIDA_ELORZA_OTAEGUI)\
**Post date:** [May 20, 2021, 12:47pm UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/14 "2021-05-20T12:47:05Z")

</div>

HI!  
Thanks for the code, it’s working. Kind of.  
I can get it to find the audio file and play it at the beginning of the loop, but it’s not stopping at the end of the loop. It almost looks like it can’t read the “if your\_loop\_name.thisN == 89:  
background\_sound.stop()” part.  
I’ve tried changing the number to the number of trials (which is 9), I’ve tried to changing it to 1, as in one go of all of the trials, but nothing is working.  
It’s not giving any errors either.  
Any thoughts?

Thanks!

---

<div class="post-metadata">

**Author:** ![rh4818](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@rh4818](https://discourse.psychopy.org/u/rh4818)\
**Post date:** [July 21, 2021, 1:40pm UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/15 "2021-07-21T13:40:27Z")

</div>

Laida,

Did you get it to work in the end/still need this?

If it’s simply background noise you need playing, I have got the code to work and inserted it into a visual search Task I am building and it works very well.
