Sound Feedback when Key is Pressed

570 views
Skip to first unread message

Pawel Dobrowolski

unread,
Jun 5, 2013, 9:46:09 AM6/5/13
to psychop...@googlegroups.com
I'm attempting to add sound feedback to a procedure which I already have up and running.  In this procedure there is no feedback when a key is pressed, and the trial does not end upon receiving keys.  I've noticed that this causes confusion in some participants since they are not sure whether their response registered or not, so I am thinking of adding a very short sound which plays whenever a key is pressed.

In builder, I've attempted to link the start of a sound component to the pressing of a key.  From what I can see when generating a script, it seems that event.getKeys is responsible for catching pressed keys.  Is there any way to tie the exact moment of a key press to the onset of a sound component?  

Any suggestions would be greatly appreciated.

Pawel D.

Michael MacAskill

unread,
Jun 5, 2013, 5:16:49 PM6/5/13
to psychop...@googlegroups.com

On 6 Jun, 2013, at 01:46, Pawel Dobrowolski <p.r.dob...@gmail.com> wrote:

> Is there any way to tie the exact moment of a key press to the onset of a sound component?

Dear Pawel,

Possibly. But the easiest thing might be to use a Code component rather than a Sound component. In that, you would define a sound object via code at the beginning of the experiment. Then during the routine, you check on each screen refresh whether a key has been pressed, and if so, you make the sound object play.

But I assume you're doing something with the key presses (probably via a keyboard component), and this solution would need also encompass that (i.e. the code component might also need to do what you are doing currently with the keyboard component, such as storing a reaction time, etc, because the keys will be detected only by one or the other). So perhaps you could give some more detail of what you do with the key presses currently.

Regards,

Michael



Pawel Dobrowolski

unread,
Jun 6, 2013, 2:19:18 AM6/6/13
to psychop...@googlegroups.com
I have a keyboard component which is registering only the keys 'z' and 'x', with the measures being accuracy and response time.  The response window is 1.3 seconds long, so it is during this time that I would like a sound to be played when 'z' or 'x' is pressed (regardless of accuracy).  I'm also only storing the last key pressed.  


Michael MacAskill

unread,
Jun 9, 2013, 11:40:40 PM6/9/13
to psychop...@googlegroups.com
Hi,

Sorry for the delay in responding.

You could create a Code component. In the Begin Experiment box, put something like:

keyTone = sound.Sound(u'A', secs=0.5) # a brief sound that will play when needed.

Then in the Each Frame box, put something like:

if t < 1.3:
keyPress = event.getKeys(keyList = ['x', 'z'], timeStamped = True) # check for a key
if len(keyPress) > 0: # if there was one, then
keyTone.play() # play the sound
thisExp.addData('response', keyPress) # store the data

This is untested pseudo code and could/should be improved to separate out the keys and the reaction time, but it might get things started?

As mentioned, the sound and keyboard components should be deleted, as this code is starting to handle their roles.

Regards,

Michael

Pawel Dobrowolski

unread,
Jun 10, 2013, 9:03:20 AM6/10/13
to psychop...@googlegroups.com
Thank you for your help and suggestions.  I will try to get this working on the basis of what you have provided, and will report back with my success/failure!

Regards,
Pawel D.

Pawel Dobrowolski

unread,
Jun 19, 2013, 7:30:24 AM6/19/13
to psychop...@googlegroups.com
Generally this code works, but I think I was unclear as to why I wanted the sound.

In any case, I ended up doing the following in coder:

1. Keep the key component.
2. Added keyTone = sound.Sound(u'A', secs = .2) and keyTone.setVolume(1) to the begging of script under Initialize components.
3. Inside of Start Routine and under my key component updates, added the line keyTone.play() after if len(theseKeys) > 0:

This lets the key component handle my answers/response times as it normally would, and also only allows the sound to be played while the key component is active.  Since this is what I wanted as a form of feedback, it seems to work fine.

Thanks a lot for your help!

Pawel D.

Michael MacAskill

unread,
Jun 19, 2013, 11:40:41 PM6/19/13
to psychop...@googlegroups.com
Yes, if you're prepared to "get your hands dirty" with the Coder, you get a lot of flexibility in return (and things can actually be easier to do, without having to jump through some of the hoops required to work around things in Builder). Hope it all works fine.

Regards,

Michael
Reply all
Reply to author
Forward
0 new messages