-
Notifications
You must be signed in to change notification settings - Fork 0
/
radiotask.py
335 lines (269 loc) · 12.1 KB
/
radiotask.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import os
import random
import pyglet
import subprocess
#from mutagen.mp3 import MP3
import pygame
import time
import globals
from helpers import r3
from datacollector import DataCollector
class Radio(object):
def __init__(self, show=-1):
self.started = False
self.condition = ''
self.fullCondition = '?'
self.preselectedShow = show
self.clip = 0
self.lastClip = 0
self.conversation = 0
self.player = pyglet.media.ManagedSoundPlayer()
self.llPlayer = None
self.clipDuration = 0
self.clipStartTime = 0
self.currentClip = None
self.curShow = None
#sound = pyglet.resource.media(BALL_SOUND, streaming=False)
#self.hardConvDB = ['show1', 'show2']
#self.easyConvDB = ['show3', 'show4']
#self.convAnswers = []
self.shows = [['show3', 'show4'], ['show1', 'show2']] # easy / hard
self.numFragments = [[1,1], [32,34]]
self.answers = []
#self.convPool = range(len(self.convDB))
self.questionOrder = []
self.questionIndx = 0
self.correctAnswer = 0
self.responded = False
self.playedLetter = False
self.playedRepeat = False
self.phase = 0
self.responseCountdown = 0.0
self.responseLimit = 5.0
self.clipInterval = 1.5
self.isi = 0.0
self.run = -1
self.doPractice = False
self.roadCond = 'simple'
#self.incorrectSound = pyglet.resource.media('incorrect.wav')
#self.correctSound = pyglet.resource.media('correct.wav')
self.db = None
def setConversation(self, show):
self.preselectedShow = show
def clearConversation(self):
self.preselectedShow = -1
def startTask(self, cond, driving='?', blockNum=0):
self.condition = cond
self.fullCondition = driving+'_'+cond
self.run += 1
datacond = '_' + self.roadCond + '_hard'
if self.doPractice and self.run == 0:
datacond = '_prac' + datacond
if cond == 'hard':
self.db = DataCollector('Radio DB', 'data/'+globals.participant+datacond+'_radio.dat', ['pp', 'cond', 'correct',
'answer', 'conversation', 'question',
'condtime', 'time'])
self.db.open()
self.started = True
self.pickConversation()
self.startNextClip()
def stopTask(self):
self.started = False
if not self.llPlayer is None:
self.llPlayer.terminate()
self.llPlayer = None
#if self.player.playing:
#self.player.next()
if self.condition == 'hard':
self.db.close()
def pickConversation(self):
cond = 0
if self.run == 0 and self.doPractice:
self.curShow = 'practice'
self.lastClip = 8
self.clip = -1
else:
if self.condition == 'hard':
cond = 1
if len(self.shows[cond]) > 0:
idx = random.sample(range(0,len(self.shows[cond])), 1)[0]
self.curShow = self.shows[cond][idx]
self.lastClip = self.numFragments[cond][idx]
del self.shows[cond][idx]
del self.numFragments[cond][idx]
self.clip = -1
else:
self.started = False
def startNextClip(self):
if self.started:
self.responseCountdown = 0.0
self.isi = 0.0
self.responded = False
self.playedLetter = False
self.phase = 0
if self.clip+1 >= self.lastClip:
#self.pickConversation()
self.clip = -1
self.stopTask()
else:
self.clip += 1
#self.currentClip = pyglet.media.load('radio/'+self.convglobals.db[self.conversation]+'_'+str(self.clip+1)+'.mp3', streaming=False)
if self.condition == 'hard':
currentClip = 'radio/'+self.curShow+'/'+self.curShow+'frag'+str(self.clip+1)+'.mp3'
self.playAudio(currentClip)
else:
currentClip = 'radio/'+self.curShow+'/'+self.curShow+'.mp3'
self.playAudio(currentClip)
#audio_file = os.getcwd()+'/radio/show1/show1frag1q.mp3'
#print audio_file
#self.playAudio(audio_file)
#self.llPlayer = subprocess.Popen(["afplay", audio_file], shell=False)
#pid = p.pid()
#p.terminate()
def draw(self):
return 0
def update(self, dt):
if self.started:
if self.condition == 'hard':
#print str(self.player.time)+' - '+str(self.currentClip.duration)
if not self.audioIsPlaying() and not self.responded:
if self.phase == 0: # clip finished, play question
currentClip = 'radio/'+self.curShow+'/'+self.curShow+'frag'+str(self.clip+1)+'q.mp3'
self.playAudio(currentClip)
self.questionOrder = [0, 1, 2]
random.shuffle(self.questionOrder)
#print self.questionOrder
self.questionIdx = 0
self.correctAnswer = self.questionOrder.index(0)
self.phase = 1
elif self.phase == 1: # questions finished, play options
if self.questionIdx > 2:
# repeat question after answers
if self.playedRepeat:
currentClip = 'radio/'+self.curShow+'/'+self.curShow+'frag'+str(self.clip+1)+'q.mp3'
self.playAudio(currentClip)
self.playedRepeat = False
self.phase = 2 # options finished, go to response phase
else: # say 'repeat'
currentClip = 'radio/herhaal.mp3'
self.playAudio(currentClip)
self.playedRepeat = True
else:
letters = ['a', 'b', 'c']
number = self.questionOrder[self.questionIdx]
letter = letters[number]
if self.playedLetter:
currentClip = 'radio/'+self.curShow+'/'+self.curShow+'frag'+str(self.clip+1)+letter+'.mp3'
self.playAudio(currentClip)
self.playedLetter = False
self.questionIdx += 1
else: # play the answer letter clip first
currentClip = 'radio/'+letters[self.questionIdx]+'.mp3'
self.playAudio(currentClip)
self.playedLetter = True
elif self.phase == 2:
self.responseCountdown += dt
#print self.responseCountdown
if self.responseCountdown >= self.responseLimit:
self.processResponse(-1)
elif self.phase == 3: # feedback
self.isi += dt
if self.isi >= self.clipInterval:
self.startNextClip()
#else: # easy
#a = 1
#if not self.player.playing: # get next clip
#print 'finished'
#self.startNextClip()
def processResponse(self, button):
if self.started and not self.responded and self.phase == 2:
#self.player.pause()
#self.player.next()
correct = 0
let = ['A', 'B', 'C']
if button >= 0 and button < 4:
if self.audioIsPlaying():
if self.llPlayer is not None:
self.llPlayer.terminate()
self.llPlayer = None
print 'Answer '+let[button]+' was selected. Correct answer was '+let[self.correctAnswer]
if button == self.correctAnswer:
correct = 1
else:
print 'No answer was selected. Correct answer was '+let[self.correctAnswer]
if correct:
correctSound = pyglet.resource.media('correct.wav')
correctSound.play()
globals.bonusCounter += globals.RADIO_QUIZ_BONUS
#self.correctSound.play()
else:
incorrectSound = pyglet.resource.media('incorrect.wav')
incorrectSound.play()
#self.incorrectSound.play()
self.phase = 3
self.responded = True
self.db.addData(['pp', globals.participant, 'cond', self.fullCondition, 'correct', correct,
'answer', button, 'conversation', self.conversation, 'question', self.clip,
'condtime', r3(globals.conditionTime()), 'time', r3(globals.currentTime())], True)
def checkInput(self):
if self.started:
if self.condition == 'hard':
if not self.responded and self.phase > 0:
if globals.hasWheel:
if globals.joystick.buttons[3]: #or globals.joystick.buttons[3]:
self.processResponse(0)
elif globals.joystick.buttons[2]: #or globals.joystick.buttons[5]:
self.processResponse(1)
elif globals.joystick.buttons[0]: #or globals.joystick.buttons[7]:
self.processResponse(2)
if globals.findKey('1') >= 0:
self.processResponse(0)
elif globals.findKey('2') >= 0:
self.processResponse(1)
elif globals.findKey('3') >= 0:
self.processResponse(2)
def setCondition(self, cond):
self.condition = cond
def playAudio(self, filename):
#filename2 = 'radio/'+self.curShow+'/'+self.curShow+'frag'+str(self.clip+1)+'.ogg'
clip = pyglet.media.load(filename, streaming=False)
self.clipDuration = clip.duration
#print 'clip duration'
#print clip.duration
#audio = MP3(filename2)
#print 'mutagen'
#print audio.info.length
#txt = ''
#try:
#subprocess.check_output(["ffmpeg", "-i", filename])
#except subprocess.CalledProcessError, e:
##print "Ping stdout output:\n", e.output
#txt = e.output
#ffmpeg -i show1frag1.mp3 2>&1|sed -n "s/.*Duration: \([^,]*\).*/\1/p"
#print txt
self.clipStartTime = time.time()
#print 'clip start time'
#print self.clipStartTime
clip = None
if globals.onOSX:
# using command prompt because pyglet media player is really buggy...
self.llPlayer = subprocess.Popen(["afplay", filename], shell=False)
if self.clipDuration > 5.0:
print 'Audio PID: '+str(self.llPlayer.pid)+' (kill with "kill <PID>" in bash)'
else:
# windows requires mplayer to be installed
self.llPlayer = subprocess.Popen(["mplayer", filename, "-ss", "30"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def audioIsPlaying(self):
if not self.llPlayer is None:
tm = time.time()
#print str(tm-self.clipStartTime)+' - '+str(tm)+' - '+str(self.clipStartTime+self.clipDuration)
if tm-self.clipStartTime >= self.clipDuration:
#print 'end time'
#print tm
if self.llPlayer is not None:
if not globals.onOSX:
self.llPlayer.stdin.write("q")
self.llPlayer.terminate()
self.llPlayer = None
return False
return True