Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] Need to play a sound every time a key is pressed

edited May 2012 in OpenSesame

Hello again,

Its me, creating my second experiment with OS. Thank you very much for the nice programm, first one worked (after your help) perfectly.

I have a trial of 10 times 3 Pictures (randomized, that works).
Each Picture gets presented for 5 secs, during that time they shall press a key and rate the picture that way. The response is logged and the next pictures is randomized presented.
What i want to do now is play a sound if they pressed a key (there is no correct or incorrect answer its just a rating they give to the pictures im presenting them). I have searched the Tutorial and your function-page, but im not able to find it.

How can i just play a sound in a sequence any time a key is pressed? (Under the conditition that there is no right or wrong answer and it shall only be played IF they pressed a key)

Part 2 of my problem, i want to show them what rating they gave to each picture, is there any "easy" way to present them the keypress on the same sketchpad like the picture?

Best regards and thanks again for your good support (sorry if my questions are dumb -.-)

Henrik

Comments

  • edited May 2012

    Hi Henrik,

    Good to hear that your first experiment worked smoothly. Hopefully you'll get some nice data!

    How can i just play a sound in a sequence any time a key is pressed? (Under the conditition that there is no right or wrong answer and it shall only be played IF they pressed a key)

    The easiest way is probably to add a sampler/ synth item right after the keyboard_response and use the following 'run if' statement so that it isn't played when the response timed out:

    [response] != timeout
    
    Part 2 of my problem, i want to show them what rating they gave to each picture, is there any "easy" way to present them the keypress on the same sketchpad like the picture?

    So you want to give them back their own response as feedback at the end of every trial, do I understand that correctly? In order to do that, you can insert a feedback item at the end of the trial and put something like the following text in it:

    You rated the response as [response]

    Does that help you?

    Cheers,
    Sebastiaan

  • edited 5:13PM

    Hey,

    Actually the [response] != timeout plays the ton only at the end of the sketchpad, but the sketchpad runs always for 5 secs so they see any picture the entire 5 secs (they will be recalled later) which mean that term doesnt work (sorry for my unclear problem).

    I dont want to feedback them at the end of each trial, i want to show them which rating they gave to the picture, so lets say for example if they pressed a "3" i would like to give them a ton for "yes you pressed a key" and let the "3" on the sketchpad so they can see what response they logged in for the item.

    So each picture is presented for 5secs, it shall be rated from number 1-5. If they press a key i want a ton to tell them they pressed a key and to show them the number they pressed on the sketchpad.
    Later on we do a free recall, to see how many of the pictures they remembered, the rating is just a distraction and a controll for us to see if the ratings are (hopefully) not significant different.
    The free recall will be done as pen and paper work.

    Thanks for your help,

    Henrik

    P.S.: The data hasnt yet been analysed but i hope we got some findings.

  • edited 5:13PM

    Ah, I see now. So you want to collect a response while the sketchpad is visible, for 5 seconds, and display the last response on top of the sketchpad, right? The script below should do approximately what you need. Please see the Python inline code documentation for more documentation: http://osdoc.cogsci.nl/python-inline-code

    from openexp.keyboard import keyboard
    from openexp.synth import synth
    from openexp.canvas import canvas
    
    # Uncomment for 0.25 and earlier
    # exp = self.experiment
    
    # The name of the sketchpad to draw on
    src_sketchpad = 'my_sketchpad'
    
    # Allowed keys
    allowed_keys = ['1','2','3','4','5']
    
    # Timeout
    timeout = 5000
    
    # Create objects
    my_keyboard = keyboard(exp, keylist=allowed_keys, timeout=5)
    my_synth = synth(exp)
    my_canvas = canvas(exp)
    
    # Collect a response until timeout, show the latest response
    # and play a sound everytime that a response is given
    response = 'timeout'
    start_time = self.get('time_%s' % src_sketchpad)
    while self.time()-start_time < timeout:
        key, time = my_keyboard.get_key()
        if key != None:
            response = my_keyboard.to_chr(key)
            my_synth.play()
            my_canvas.copy(exp.items[src_sketchpad].canvas)
            my_canvas.text('You pressed %s' % response)
            my_canvas.show()
            
    # Log the response
    exp.set('response', response)
  • edited 5:13PM

    Hey,

    Yeah that is what i want to do. If i enter your lines in a inline script, i get the following error message:
    "runtime_error: Error: Runtime error
    Description: Variable 'time_my_sketchpad' is not set in item 'inline_script'.

    You are trying to use a variable that does not exist. Make sure that you have spelled and capitalized the variable name correctly. You may wish to use the variable inspector (Control + I) to find the intended variable. "

    If i check the Variable inspector there is no such variable. Since im not very familiar with python im not sure how to fix it.

    Thanks once more for your support.

    Henrik

  • edited 5:13PM

    I should have been a bit clearer: The idea is that you change the line that says

    src_sketchpad = 'my_sketchpad'

    to match the name of the sketchpad that precedes the script. So my_sketchpad might become target_display or something.

    Cheers!

  • edited 5:13PM

    Hey,

    I am really sorry to ask again, but after i fixed that i get the same mistake.

    Python traceback:
    runtime_error: Error: Runtime error
    Description: Variable 'time_pic3' is not set in item 'inline_script'.

    Im using 3 Sketchpads to display 3 items in randomized order in one trial might that be the problem? So im having every item 3 times, i got 3 Sketchpads, 3 Keyloggers and 3 keyboard responses or is that not nessesarcy?

    For the setup i have 3 different categorize of pictures and in order to control measuring errors i wanted to present one per category and than start again with one per category in randomized order.

    Thank you once more for your great support...

    Henrik

  • edited 5:13PM

    If you have an item named pic3 than there is definitely a variable named time_pic3. Make sure that the inline_script is included in the run phase and is executed after the pic3 sketchpad has been shown. Does that fix it?

  • edited 5:13PM

    Hey,

    Yeah i put all the sketchpad names in now, moved the inlinescript behind all three sketchpads. It works now but doesnt show the number nor plays the sound when the key is pressed. The number doesn´t appear at all and the sound is played right after the 5secs of the picture beeing displayed.

    Do i have to put the entire code in an inline script or anything of it in the script of my sketchpads?

    Do i have to edit anything on my sketchpads like "draw image -400 400 You pressed %s' % response" or is the inline script doing all of it? (Or do i have to make changes in all scripts of all my items?)
    Do i have to make an adjusts means due to the fact that i got more items? (Otherwise than:
    src_sketchpad = 'Pic1'
    src_sketchpad = 'Pic2'
    src_sketchpad = 'Pic3'

    My Experiment now looks like:

    • Experiment
    • Inlinescript (to randomize my items)
    • Instructions
    • Phaseloop

      • Inlinescript (to get the randomized items to be displayed on the different sketchpads)
      • Pic1
      • Keyboardresponse1
      • Keylogger1
      • Sound1
      • Pic2
      • Keyboardresponse2
      • Keylogger2
      • Sound2
      • Pic3
      • Keyboardresponse3
      • Keylogger3
      • Sound3
      • Inlinescript (the script you wrote me)
    • Goodbye sketchpad

    Thanks again for your time and support.

  • edited 5:13PM

    Right no, the script works much simpler than that. It simply replaces the keyboard_response and sampler. So you simply insert the script after each sketchpad (make sure that the src_sketchpad variable matches and that the sketchpad duration is set to 0!) and it should behave as you expect. So in your case:

    Pic1
    inline_script1
    logger
    Pic2
    inline_script2
    logger
    Pic3
    inline_script3
    logger
    Pic4
    inline_script4
    logger

    For more information and settings, please see the code comments in the script.

    Good luck!

  • edited 5:13PM

    Hey,

    Thank you very much for that great support, it works perfectly fine now.

    Best regards

    Henrik

  • edited October 2015

    Hi,

    This post massively helped me to play a response after a keypress. I used the code given above and it worked just fine on the version of OS I was originally using. However, when I tried to open my experiment with a newer version, I got this error message:

    "File "dist\libopensesame\inline_script.py", line 96, in run
      File "dist\libopensesame\python_workspace.py", line 160, in _exec
      File "<string>", line 32, in <module>
      File "dist\openexp\_canvas\xpyriment.py", line 61, in copy
    TypeError: set_config() takes exactly 1 argument (2 given)"
    

    This is the inline script I'm using. I'd be grateful if you could help me figure out where I'm going wrong. Thanks!

    from openexp.keyboard import keyboard
    from openexp.synth import synth
    from openexp.canvas import canvas
    
    # Uncomment for 0.25 and earlier
    # exp = self.experiment
    
    # The name of the sketchpad to draw on
    src_sketchpad = 'JOL'
    
    # Allowed keys
    allowed_keys = ['0','1','2','3','4','5','6','7','8','9','*']
    
    # Timeout
    timeout = 3000
    
    # Create objects
    my_keyboard = keyboard(exp, keylist=allowed_keys, timeout=3)
    my_synth = synth(exp)
    my_canvas = canvas(exp)
    
    # Collect a response until timeout, show the latest response
    # and play a sound everytime that a response is given
    response = 'timeout'
    start_time = self.get('time_%s' % src_sketchpad)
    while self.time()-start_time < timeout:
        key, time = my_keyboard.get_key()
        if key != None:
            response = my_keyboard.to_chr(key)
            my_synth.play()
            my_canvas.copy(exp.items[src_sketchpad].canvas)
            my_canvas.text('You pressed %s' % response)
            my_canvas.show()
    
    # Log the response
    exp.set('responseJOL', response)
    
  • edited 5:13PM

    Hi Susanna,

    There's a bug in canvas.copy() (#355). This will be fixed for 3.0.2, but what you can do for now is the following.

    First, add a custom (and correct) copy function to an inline_script at the start of your experiment:

    def copy_canvas(c1, c2):
    
        """Copy c2 onto c1. Only works for the xpyriment backend."""
    
        c1.set_config(**c2.get_config())
        c1.auto_prepare = c2.auto_prepare
        c1.aa = c2.aa
        c1.prepared = False
        c1.clear()
        c1.stim_list = [stim.copy() for stim in c2.stim_list]
        if c1.auto_prepare:
            c1.prepare()
        c2.prepared = False
    

    Next, update your script to use this copy function (copy_canvas()). You can also simplify the script a bit, using the new 3.0 Python API--but that's not necessary, only neater:

    # The name of the sketchpad to draw on
    src_sketchpad = 'JOL'
    
    # Allowed keys
    allowed_keys = ['0','1','2','3','4','5','6','7','8','9','*']
    
    # Timeout
    timeout = 3000
    
    # Create objects
    my_keyboard = keyboard(keylist=allowed_keys, timeout=3)
    my_synth = synth()
    my_canvas = canvas()
    
    # Collect a response until timeout, show the latest response
    # and play a sound everytime that a response is given
    response = 'timeout'
    start_time = var.get('time_%s' % src_sketchpad)
    while clock.time()-start_time < timeout:
        key, time = my_keyboard.get_key()
        if key is not None:
            response = my_keyboard.to_chr(key)
            my_synth.play()
            copy_canvas(my_canvas, items[src_sketchpad].canvas)
            my_canvas.text('You pressed %s' % response)
            my_canvas.show()
    
    # Set the response
    var.responseJOL = response
    

    Hope this helps!
    Sebastiaan

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games