1 Jouer une liste de fichiers sons

import pygame
pygame.mixer.init(22050)
def play(file):
        sound=pygame.mixer.Sound(file)
        channel=sound.play()
        while channel.get_busy():
               pygame.time.wait(10)
liste=['ph1.wav','ph2.wav','ph3.wav','ph4.wav']
for stimulus in liste: play(stimulus)

2 Enregistrer une rponse chaque essai

import pygame
import random
from pygame.locals import *
liste=['tone1.wav','noise.wav']*4
random.shuffle(liste)
pygame.mixer.pre_init(22050,-16,1,2048)
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.event.set_allowed(QUIT|MOUSEBUTTONDOWN|KEYDOWN)
responses=[]
try:
    for stimulus in liste:
        sound = pygame.mixer.Sound(stimulus)
        pygame.event.clear()
        channel = sound.play()
        while (pygame.mixer.get_busy()>0): pass
        resp = 0
        while resp == 0:
            event = pygame.event.wait()
            if event.type == QUIT: raise
            elif event.type == KEYDOWN and event.key == K_ESCAPE: raise
            elif event.type == MOUSEBUTTONDOWN: resp = event.button
        responses.append(resp)

pygame.time.delay(1000)
finally:
for i in range(len(responses)):
print i,liste[i],responses[i]
results = file('results.dat','a+');
results.write('#\n')
for i in range(len(responses)):
results.write(str(i)+'\t'+str(liste[i])+'\t'+str(responses[i])+'\n')
results.close()

3 Mesurer le temps de raction depuis le dbut du son

...
sound=mixer.Sound(file)
a0 = time.get_ticks()
channel=sound.play() # demarre le son
stim_start = time.get_ticks()
time0,time1,response = 0,0,0

while (response==0 and time.get_ticks()-stim_start<5000):
    time0=time1
    time1=time.get_ticks()
    for event in pygame.event.get():
        if event.type == QUIT:
            quitpressed = 1
            response = -1
        elif event.type == MOUSEBUTTONDOWN:
            response=event.button
            rtime1 = time1-stim_start
            rtime0 = time0-stim_start
display(str(rtime0)+' '+str(rtime1))
while channel.get_busy():
    time.wait(10)
...