[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] refresh rate and timing issues



Hi everybody.

I am trying to use python and pygame to run a simple psychological experiment.
So from a gamer's perspective: a very boring game with only one level ;)

The main problem is the exact timing since I want to communicate with other
devices (here: a MAC recording EEG) and need a reliably time stamp to track back
events for later analysis.

I was trying a bit around with the display modes and worked out that FULLSCREEN|
DOUBLEBUF| HWSURFACE should do what I want (negelcting for the moment that I
cannot use the mouse with this mode). I checked and verified that these features
are supported by the system. 
I want pygame to synchronize the presentation of a certain stimulus with the
vertical refresh of the monitor, which is what flip() should do in this mode,
right? Right after the flip() command I ask for the current time of the program
using pygame.time.get_ticks().

The code looks something like this:

def waituntil(start_time, time2wait):
    t_desired = start_time + time2wait
    while True:
        t = pygame.time.get_ticks()
        if t >= t_desired:
            break
        else:
            t = pygame.time.get_ticks()
    return t-start_time

t2wait = 18 
timesTICKS=[]
for i in range(100):
   targimage = pygame.image.load(o1)
   screen.blit(targimage,[ width/2, height/2])
   trialtime = pygame.time.get_ticks()
   waituntil(trialtime, t2wait)
   pygame.display.flip()
   timesTICKS.append(int(pygame.time.get_ticks()))

The 18 ms are a bit longer than 16.66 ms which is time of one frame (the refresh
rate of my monitor is 60Hz). So if I get it right pygame.flip() should wait for
the next refresh and the present the stimulus. Since I ask for the time right
afterwards, the temporal distance from one call to the next should be something
like 33, 33, 33, etc. 
But what I get is somthing like this:
51, 47, 32, 51, 20, 23, 54, 28, 40, 18, 41, 18, 18, 72, 32, 37,...

I understand the longer times, since the pictures are taken from the HD, but the
"18"s, "20"s, etc. I do not understand. How is this possible?

What I really want is the time WHEN pygame REALLY presented something on screen
which can then be used for later analyses.

Does anybody have an idea how to do this?

Andre