# Sound feedback after a key response

**URL:** https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533
**Category:** Builder
**Created:** [November 30, 2020, 10:34pm UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533 "2020-11-30T22:34:57Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Moe](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@Moe](https://discourse.psychopy.org/u/Moe)
#### Post date: [November 30, 2020, 10:34pm UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533/1 "2020-11-30T22:34:58Z")

</div>

Hi guys,

I’m trying to conduct the stroop test and I want to include a buzz whenever the participant makes a mistake. I went through YouTube videos and some of the posts about the same topic and I tried to mimic the procedure. However, whenever the experiment starts and when it’s supposed to give a noise it crashes.

I’m attaching snippets from the experiment, the code and the error that I got. Just as a note in the code you’ll find the “a” variable, it’s just a dummy variable to fill in the if statement.

Your help is much appreciated.  
Thank you in advance.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/a/3/a394f211e8ed697a4266202fe60ebe510abf5128.png)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/d/9/d91dff47ac1208b7d7bec8ba091e44f7bcf9c41c.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: [November 30, 2020, 10:51pm UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533/2 "2020-11-30T22:51:44Z")

</div>

Use something like Audacity to convert your sound file to 44.1 kHz rather than its current 48 kHz.

---

<div class="post-metadata">

### Author: ![Moe](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@Moe](https://discourse.psychopy.org/u/Moe)
#### Post date: [December 1, 2020, 5:24am UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533/5 "2020-12-01T05:24:59Z")

</div>

Hi,

Thank you for reply. It worked. However, now, whether the keyboard response is correct or not the buzz sound will play. Do you have any idea why is this happening?

---

<div class="post-metadata">

### Author: ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)
#### Post date: [December 1, 2020, 9:44am UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533/6 "2020-12-01T09:44:27Z")

</div>

Hello Moe,

you don’t need an if…else construction, if you don’t use the else-part.

```auto
if not key_resp_4.corr:
    buzz = soundfile[0]

```

should do the work. The reason that the sound is always played means that the if…else construction always evalutes to the else-part.

When you want to play the sound after the participants has reacted you have to move the code-element from the **Begin Routine** tab to the **End Routine** tab.

Best Jens

---

<div class="post-metadata">

### Author: ![Moe](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@Moe](https://discourse.psychopy.org/u/Moe)
#### Post date: [December 1, 2020, 5:20pm UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533/7 "2020-12-01T17:20:11Z")

</div>

Hello,

Thanks for your reply. I removed the else part and shifted the code from the “Begin Routine” to the “End Routine” tab. When I run the experiment, the following error occurs.

> File “C:\Users\Other user\Desktop\Noise Experiment\Noise Experiment\_1\_lastrun.py”, line 387, in   
> sound\_1.setSound(buzz, hamming=True)  
> NameError: name ‘buzz’ is not defined
> 
> ##### Experiment ended.

The “buzz” is defined in the sound component shown in the figure below.  
I tried to move the sound component above the code component (maybe it was an order problem) but the same error occurred.

Any ideas?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/4/9/49343dc5e6dc13bc430222cf5e22c4e558121002.png)

---

<div class="post-metadata">

### Author: ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)
#### Post date: [December 2, 2020, 10:26am UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533/8 "2020-12-02T10:26:53Z")

</div>

Hello,

did you ever define the variable **buzz** somewhere before you use it for the first time, e.g. in the tab **Begin experiment**? The code-element should appear before the sound-element.

Best Jens

---

<div class="post-metadata">

### Author: ![chayabenyehuda](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/chayabenyehuda/32/12429_2.png) [@chayabenyehuda](https://discourse.psychopy.org/u/chayabenyehuda)
#### Post date: [December 3, 2020, 6:49am UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533/9 "2020-12-03T06:49:01Z")

</div>

Hi  
I am stock in the same problem.  
how can i define the vairable buzz? i know how to define a massege. e.g :msg=" "  
but how to define a sound?  
i tried  
soundfile = “incorrectBUZZER.wav”

---

<div class="post-metadata">

### Author: ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)
#### Post date: [December 3, 2020, 7:55am UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533/10 "2020-12-03T07:55:03Z")

</div>

Hello

> [@chayabenyehuda](#):
>
> soundfile = “incorrectBUZZER.wav”

This defines a variable **soundfile** with the content **incorrectBUZZER.wav**.

Best Jens

---

<div class="post-metadata">

### Author: ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)
#### Post date: [December 4, 2020, 11:59am UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533/12 "2020-12-04T11:59:34Z")

</div>

Hello,

here a small example which plays a sound when inccorrect and no sound when correct. I simply set the loudness to 0 when the response is correct while defining the same sound file for correct and incorrect answers.

[https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/7/d/7ddf9af6e7519e803dcf88275e9fba8672f70507.wav](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/7/d/7ddf9af6e7519e803dcf88275e9fba8672f70507.wav) [SoundFeedback.psyexp](https://discourse.psychopy.org/uploads/short-url/lvGhyEiM21yib0qJM96Y5pWxomR.psyexp) (12.4 KB) [Stimuli.xlsx](https://discourse.psychopy.org/uploads/short-url/iJjHdJqjIhz7dMVCphsXrWXNyZ3.xlsx) (8.4 KB)

Best Jens

---

<div class="post-metadata">

### Author: ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)
#### Post date: [December 5, 2020, 11:45am UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533/13 "2020-12-05T11:45:41Z")

</div>

Hello,

as a matter of fact the code could be even simpler. You only need to set the loudness in your if…else construction. You do not have to set your feedback-file in the if…else construction.

Begin Experiment tab

```auto
msg = " "
soundFeedBack = 'CriticalStop.wav'
loudness = 1

```

Begin Routine tab

```auto
if key_resp.corr:
    msg = "correct"
    msgcolor = "green"
    loudness = 0
else:
    msg = "incorrect"
    msgcolor = "red"
    loudness = 1

```

Best wishes Jens

---

<div class="post-metadata">

### Author: ![chayabenyehuda](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/chayabenyehuda/32/12429_2.png) [@chayabenyehuda](https://discourse.psychopy.org/u/chayabenyehuda)
#### Post date: [December 6, 2020, 6:58am UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533/14 "2020-12-06T06:58:58Z")

</div>

Thank you! i actually did that and it worked 🙂

---

<div class="post-metadata">

### Author: ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)
#### Post date: [December 7, 2020, 10:21am UTC](https://discourse.psychopy.org/t/sound-feedback-after-a-key-response/18533/15 "2020-12-07T10:21:10Z")

</div>

@chayabenyehuda

Do you mind marking it as solution? Then it is found more easily.

Best Jens
