# Sound plays 2 times then crashed in 'each frame' code component, but runs in 'begin routine'

**URL:** <https://discourse.psychopy.org/t/sound-plays-2-times-then-crashed-in-each-frame-code-component-but-runs-in-begin-routine/10074>\
**Category:** Coding\
**Tags:** sound\
**Created:** [November 27, 2019, 12:52pm UTC](https://discourse.psychopy.org/t/sound-plays-2-times-then-crashed-in-each-frame-code-component-but-runs-in-begin-routine/10074 "2019-11-27T12:52:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CDawes](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@CDawes](https://discourse.psychopy.org/u/CDawes)\
**Post date:** [November 27, 2019, 12:52pm UTC](https://discourse.psychopy.org/t/sound-plays-2-times-then-crashed-in-each-frame-code-component-but-runs-in-begin-routine/10074/1 "2019-11-27T12:52:49Z")

</div>

Hi all,

I’m currently building a gambling experiment. At each round, participants bet money using a keypad which I ask them to click with the mouse.

What I would like is for a click sound to play when they press a button. However, I currently have this error.

```auto
File "C:\Program Files\PsychoPy3\lib\site-packages\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'

```

In practice, clicking makes the sound play once but the code inside that statement fails to run (doesn’t update their current bet), I can click to run the sound again, but on the third time the sound will run continuously then crash.

**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](https://discourse.psychopy.org/t/sounds-stop-playing-after-2-3-stimuli-when-running-the-experiment-several-times/4709) .
2. And i’ve tried editing the code suggested here ([Sounddevice backend issue?](https://discourse.psychopy.org/t/sounddevice-backend-issue/3082)) but I couldn’t find the line they were suggesting.
3. I set the volume where I define the sound clip: click = sound.Sound(“click.wav”, volume=1.0)

I have other audio files in a separate routine which work fine. This is either a winSound or lossSound depending if they won/lost the bet. I wondered maybe my error is due to the code being in each frame, which may example the # Set the volume block by block error?

Doesn’t work in each frame (1st routine)

```auto
if ((onePad.contains(PadMouse) and PadMouse.getPressed()[0] == 1)) or (betInput.keys == '1'):
    click.play()
    if int(len(currentBet)) <= 1:
        currentBet = (currentBet + "1");
        betInput.keys =[]
        core.wait(0.3)

```

Does work in begin routine (2nd routine)

```auto
if correct == 1:
    winSound.play()
else:
    lossSound.play()

```

Any advice would be appreciated 🙂

Chris

---

<div class="post-metadata">

**Author:** ![LukasPsy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lukaspsy/32/4582_2.png) [@LukasPsy](https://discourse.psychopy.org/u/LukasPsy)\
**Post date:** [December 2, 2019, 8:07am UTC](https://discourse.psychopy.org/t/sound-plays-2-times-then-crashed-in-each-frame-code-component-but-runs-in-begin-routine/10074/2 "2019-12-02T08:07:10Z")

</div>

> [@CDawes](#):
>
> dat \*= thisSound.volume

Why would you want to multiply the dat variable with the current volume of your sound?

> [@CDawes](#):
>
> TypeError: unsupported operand type(s) for \*=: ‘NoneType’ and ‘float’

Apparently at the point of assignment, your dat variable is set to None and you cannot multiply None and the float thisSound.volume.

---

<div class="post-metadata">

**Author:** ![CDawes](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@CDawes](https://discourse.psychopy.org/u/CDawes)\
**Post date:** [December 2, 2019, 6:12pm UTC](https://discourse.psychopy.org/t/sound-plays-2-times-then-crashed-in-each-frame-code-component-but-runs-in-begin-routine/10074/3 "2019-12-02T18:12:25Z")

</div>

Not a clue, its just the error that’s being generated by PsychoPy, I haven’t coded it myself. I was hoping someone else might know what it means.

The type error is why I tried to set the volume when I instantiated the variable at the start using:

```
click = sound.Sound(“click.wav”, volume=1.0)

```

By it doesn’t seem to work sadly - assuming that its the volume that its trying to work out from “thisSound.volume”.

---

<div class="post-metadata">

**Author:** ![LukasPsy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lukaspsy/32/4582_2.png) [@LukasPsy](https://discourse.psychopy.org/u/LukasPsy)\
**Post date:** [December 2, 2019, 8:22pm UTC](https://discourse.psychopy.org/t/sound-plays-2-times-then-crashed-in-each-frame-code-component-but-runs-in-begin-routine/10074/4 "2019-12-02T20:22:59Z")

</div>

Could you upload a version of your experiment containing only the part which produces the error?

---

<div class="post-metadata">

**Author:** ![CDawes](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@CDawes](https://discourse.psychopy.org/u/CDawes)\
**Post date:** [December 7, 2019, 9:27pm UTC](https://discourse.psychopy.org/t/sound-plays-2-times-then-crashed-in-each-frame-code-component-but-runs-in-begin-routine/10074/5 "2019-12-07T21:27:43Z")

</div>

Sorry for the delay - the experiment appears to work now although I haven’t changed the code at all (strange). May have been other processes in the background, as all I did was restart my laptop(?).
