Howdy, Stranger!

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

Supported by

Logging multiple variables during audio playback


I am designing an experiment where the subject is asked to press the spacebar "in time" with the 20 music stimuli presented in order to measure their pulse synchronicity. So, there are multiple key presses during the stimulus presentation. I can't seem to figure out how to allow for multiple keyboard responses and how to allow for the responses and (most important) their timing to be logged while the audio file is playing.
Things I want to log:
-timing of first spacebar press in relation to the start of the audio stimulus
-timing of each space bar press
-number of space bar presses during the audio stimulus

How might I go about doing this? Thank you,



  • edited September 2017

    Something like this might work:

    timeout         = 5000 
    c               = canvas()
    c.text('test here some keypresses for %s seconds' %timeout)
    from openexp.keyboard import keyboard
    import pygame
    kb              = keyboard(exp, keylist = [],timeout=0)
    press_times         = []
    start_t         = self.time()
    start_key   = 0
    while True:
        current_t   = self.time()
        duration        = current_t-start_t
        events      = pygame.event.get()
        for event in events:
            if event.type == pygame.KEYDOWN:
                start_key       = self.time()
            if event.type == pygame.KEYUP:
                end_key             = self.time()
                duration_key        = end_key - start_key
                press_times.append((duration_key, event.key))
        if duration > timeout:

    Hope this helps,

  • Roelof,
    Thank you so much for your help. I got the code to work in my experiment, but the log files aren't logging each individual spacebar press. I'm not entirely sure where to/how to define that. I tried including a bit of code I found from a post on this website (albeit, from 2011) to define/rename each individual space press, but that just got me redundant values rather than the actual response times (It doesn't help that I am a beginner when it comes to coding).
    self.experiment.set("response_1", self.get("response"))
    self.experiment.set("response_2", self.get("response"))
    self.experiment.set("response_3", self.get("response"))
    self.experiment.set("response_time_1", self.get("response_time"))
    self.experiment.set("response_time_2", self.get("response_time"))
    self.experiment.set("response_time_3", self.get("response_time"))

    Is there something I'm missing in the code you already provided me? Thank you.
    Side note: The number of times the space bar is pressed in each stimulus is variable, as the tempos are different.

  • Hi Kevie,

    there are several ways in which you can log the variables. I guess since timing is important, it might be best to record all the key presses in one while loop and then set the responses to be logged afterwards, since logging a variable during the while loop might take some time (e.g.:var.responses = press_times).

    Another problem is that there might be a different number of responses for different participants, right?
    This means that if you log each response as a variable, two log files for different participants
    will have different amount of columns, which is a pain in the .. if you want to merge them.

    There are some ways around this:
    1. log the entire list as a single variable, this way each participant has a single response column, but the response is a complete list.
    2. create dummy variables set to 'none' and change those specific responses for which the participant actually gave a response: this means creating many dummies, so many in fact that it is impossible to run out, otherwise the experiment will crash (for 20 seconds maybe 40/50 responses? guess that might be doable)
    3. create a separate log file only for these timing responses, log the rest in the regular log file

    These are not the most elegant solutions, and each has their (dis)advantage, I would suggest trying to figure out which works best in conjunction with your analysis.


  • Thank you so much for your help! Got it working no problem now!

Sign In or Register to comment.