Is SDL right for this?

Hi,

I’m doing some preliminary work for an experimental control system in
psychology. The typical experiment is to display or in the case of sound
play some sort of stimulus to the subject and then record a timed
response. What makes this tricky are the psuedo real-time requirements.

Assuming the stimulus is some sort of image, a typical trial would work
as follows.

  1. Load the image into an offscreen buffer.
  2. Draw the image to the screen.
    2.5 When the vertical refresh begins start a timer.
  3. When the subject presses a key, stop the timer and record the time.

Drawing images, playing sounds and polling the keyboard are natural
things for a game development library to do so I figure SDL might be the
right tool here.

What I’m concerned about is

  1. whether firing an event is possible when a vertical refresh begins.
    (The point being that the timer has to start when the actual drawing to
    the screen begins).

  2. Key press notification latency. The specs call for something like
    100% of keypresses notified in < 5 ms with 99% < 1 ms. This may not even
    be possible with a keyboard but we are trying to avoid using "custom"
    hardware. What this means, I suppose, is that polling the keyboard in a
    while loop should detect a keypress hopefully within less than 1 ms of
    the subject actually pressing a key. As I said above this may not be
    possible with keyboard hardware.

This is on Linux, and we’re hoping not to have to go with some RTLinux
variant, but we may have no choice.

Any comments appreciated and thanks for reading this slightly off-topic
post,

Nick–
Nick Collier
Social Science Research Computing
University of Chicago
http://repast.sourceforge.net

  1. whether firing an event is possible when a vertical refresh begins.
    (The point being that the timer has to start when the actual drawing to
    the screen begins).

Use page-flipping since it’s fast and syncs to vertical refresh.
Then immediately after SDL_Flip() start your timer.
Give the process real-time priority (SCHED_FIFO), but be careful (easy
locking your machine with a stupid mistake)
Your timer might start a bit too early, SDL_Flip() could very well return
before the vertical retrace is over since page flipping doesn’t take long.
That (constant) delay could be determined experimentally in some way.

  1. Key press notification latency. The specs call for something like
    100% of keypresses notified in < 5 ms with 99% < 1 ms. This may not even
    be possible with a keyboard but we are trying to avoid using "custom"
    hardware.

If you don’t use X11 then this should be definitely attainable; maybe
even with X11 and a fast machine. Try it. For timing, use
gettimeofday() for best precision (microsecond resolution should be
possible)

Other things to try, if the scheduler granularity gets in the way:

  • increasing the kernel tick frequency from 100 Hz to 1000 Hz
  • use an almost-real-time kernel extension like KURT