Welcome!

Sign in with your CogSci, Facebook, Google, or Twitter account.

Or register to create a new account.

We'll use your information only for signing in to this forum.

Supported by

2 CallBack functions ? - SOLVED

Arnaud BoréArnaud Boré Posts: 8
edited March 31 in Expyriment

Dear Expyriment experts,

I want to create a design where people have to follow a rhythm (at a specific frequency) using a keyboard. What would be the best approach to code this? I need to log the key (rt) and the rt tones as well.
So far I don't know how I can use two callback functions to get info from keyboard and keep playing the tone. It seems that I cant use keyboard.wait() and using keyboard.check() doesn't give me rt.

Thank you in advance for your help
Arnaud

Comments

  • Oops I think I already have my answer. Everything is already logged even using keyboard.check.

  • fladdfladd Posts: 75

    Hi there,

    can you give a code example?

    In general, though, if you need very stable and precise audio timing, I would suggest to have the audio played by a dedicated hardware audio sampler, triggered via MIDI from the PC that records the responses.

    Florian Krause (Developer)
    http://www.expyriment.org

  • Arnaud BoréArnaud Boré Posts: 8
    edited April 3

    So far I was just testing some basic code, but I notice that it may miss a tone sometime. What would be your suggestion ? Thank you in advance.

    from expyriment import control, stimuli, io, design, misc
    from expyriment.misc import constants
    from expyriment.misc._timer import get_time
    
    from config import *
    
    if not windowMode:  # Check WindowMode and Resolution
        control.defaults.window_mode = windowMode
        control.defaults.window_size = misc.get_monitor_resolution()
        windowSize = control.defaults.window_size
    else:
        control.defaults.window_mode = windowMode
        control.defaults.window_size = windowSize
    
    exp = design.Experiment('test')  
    
    trigger = io.Keyboard()
    control.initialize(exp)
    
    tone1 = stimuli.Tone(50, 675)
    tone1.preload()
    
    def my_callback():
        """Quit all event loop with mouse press"""
        global exp
        key = trigger.check([49,50,51,52])
    
    control.register_wait_callback_function(my_callback)
    control.start(exp, auto_create_subject_id=True, skip_ready_screen=True)
    
    while True:
        tone1.play(maxtime=50)
        exp.clock.wait(2000)
    
  • fladdfladd Posts: 75

    To be honest, I do not understand what you are aiming to do in that code. The my_callback function does not do anything it seems (well, it checks for the keys, but it does nothing with the results of that check).

    If I do understand correctly what you are trying to achieve, then here are two ways to achieve this:

    1)

    from expyriment import control, stimuli, io, design, misc
    
    
    control.set_develop_mode()
    exp = control.initialize()
    
    tone1 = stimuli.Tone(50, 675)
    tone1.preload()
    
    control.start(exp, auto_create_subject_id=True, skip_ready_screen=True)
    
    times = []
    while True:
        tone1.play()
        duration = 2000
        while True:
            key, rt = exp.keyboard.wait([49,50,51,52], duration=duration)
            if rt is None:
                break
            else:
                times.append(exp.clock.time)
                duration -= rt
    

    2)

    from expyriment import control, stimuli, io, design, misc
    
    control.set_develop_mode()
    exp = control.initialize()
    
    tone1 = stimuli.Tone(50, 675)
    tone1.preload()
    
    def callback():
        global exp
        global times
        key = exp.keyboard.check([49,50,51,52])
        if key is not None:
            times.append(exp.clock.time)
    
    control.register_wait_callback_function(callback)
    control.start(exp, auto_create_subject_id=True, skip_ready_screen=True)
    
    times = []
    while True:
        tone1.play()
        exp.clock.wait(2000)
    

    I want to stress again, though, that if you need perfect timing, an external device is the way to go.

    Florian Krause (Developer)
    http://www.expyriment.org

  • I know that nothing has been written as a data file but everything is logged in the events folder and it's what I was looking for. Thank you for your advice.

  • I don't think I need perfect timing but what would be the worst case scenario if I keep this kind of script ?

  • fladdfladd Posts: 75

    The worst case scenario is that you have a jitter of unknown size, leading to (a) you not knowing when exactly the sound was actually played via the speakers, and (b) an unstable rhythm, when playing back sounds consecutively like you do.

    Florian Krause (Developer)
    http://www.expyriment.org

Sign In or Register to comment.