forked from chrplr/audiovis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audiovis_pyglet.py
131 lines (104 loc) · 3.56 KB
/
audiovis_pyglet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#! /usr/bin/env python
# Time-stamp: <2018-03-21 14:34:02 cp983411>
import sys
import io
import os.path as op
import csv
import expyriment.control
from expyriment import stimuli
from expyriment.misc import Clock
from queue import PriorityQueue
# constants (to be modified depending on the paradigm)
WORD_DURATION = 400
PICTURE_DURATION = 1000
TEXT_DURATION = 3000
#TOTAL_EXPE_DURATION = 20000 # 10 sec
exp = expyriment.design.Experiment(name="HiRes Experiment")
#expyriment.control.defaults.open_gl=1
expyriment.control.defaults.window_size=(1280, 1028)
expyriment.control.set_develop_mode(True)
#%
expyriment.control.initialize(exp)
kb = expyriment.io.Keyboard()
bs = stimuli.BlankScreen()
wm = stimuli.TextLine('Waiting for scanner sync (or press \'t\')')
fs = stimuli.FixCross()
events = PriorityQueue() # all stimuli will be queued here
# load stimuli
mapsounds = dict()
mapspeech = dict()
maptext = dict()
mappictures = dict()
mapvideos = dict()
for listfile in sys.argv[1:]:
stimlist = csv.reader(io.open(listfile, 'r', encoding='utf-8'))
bp = op.dirname(listfile)
for row in stimlist:
onset, stype, f = int(row[0]), row[1], row[2]
if stype == 'sound':
if not f in mapsounds:
mapsounds[f] = stimuli.Audio(op.join(bp, f))
mapsounds[f].preload()
events.put((onset, 'sound', f, mapsounds[f]))
elif stype == 'picture':
if not f in mappictures:
mappictures[f] = stimuli.Picture(op.join(bp, f))
mappictures[f].preload()
events.put((onset, 'picture', f, mappictures[f]))
events.put((onset + PICTURE_DURATION, 'blank', 'blank', bs))
elif stype == 'video':
if not f in mapvideos:
mapvideos[f] = stimuli.Video(op.join(bp, f))
mapvideos[f].preload()
event.put((onset, 'video', f, mapvideos[f]))
elif stype == 'text':
if not f in maptext:
maptext[f] = stimuli.TextLine(f)
maptext[f].preload()
events.put((onset, 'text', f, maptext[f]))
events.put((onset + TEXT_DURATION, 'blank', 'blank', bs))
elif stype == 'rsvp':
for i, w in enumerate(f.split()):
if not w in maptext:
maptext[w] = stimuli.TextLine(w)
maptext[w].preload()
events.put((onset + i * WORD_DURATION, 'text', w, maptext[w]))
events.put((onset + (i + 1) * WORD_DURATION, 'blank', 'blank', bs))
#%
expyriment.control.start()
wm.present()
kb.wait_char('t') # wait for scanner TTL
fs.present() # clear screen, presenting fixation cross
a = Clock()
while not(events.empty()):
onset, stype, id, stim = events.get()
print('event {} {} @ {}'.format(stype, id, onset))
if a.time > onset:
print('...delayed @ {}'.format(a.time)) # TODO
while a.time < (onset - 10):
a.wait(10)
k = kb.check()
if k is not None:
print('keypressed: {} @ {}'.format(k, a.time))
exp.data.add([a.time, k])
stim.present()
k = kb.check()
if k is not None:
print('keypressed: {} @ {}'.format(k, a.time))
exp.data.add([a.time, k])
try:
TOTAL_EXPE_DURATION
except NameError:
None
else:
if a.time > TOTAL_EXPE_DURATION:
expyriment.control.stop_audiosystem()
break
try:
TOTAL_EXPE_DURATION
except NameError:
None
else:
while a.time < TOTAL_EXPE_DURATION:
a.wait(100)
expyriment.control.end('Merci !', 2000)