forked from hanayik/Phonological-Video-Treatment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
879 lines (784 loc) · 25.5 KB
/
core.js
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
const {remote, shell} = require('electron')
const {Menu, MenuItem} = remote
const {dialog} = require('electron').remote
const path = require('path')
const csvsync = require('csvsync')
const fs = require('fs')
const os = require("os");
const $ = require('jQuery')
const {app} = require('electron').remote;
const appRootDir = require('app-root-dir').get() //get the path of the application bundle
const ffmpeg = appRootDir+'/ffmpeg/ffmpeg'
const exec = require( 'child_process' ).exec
const si = require('systeminformation');
const naturalSort = require('node-natural-sort')
const mkdirp = require('mkdirp');
var ipcRenderer = require('electron').ipcRenderer;
var moment = require('moment')
var content = document.getElementById("contentDiv")
var localMediaStream
var sys = {
modelID: 'unknown',
isMacBook: false // need to detect if macbook for ffmpeg recording framerate value
}
var exp = new experiment('Phonological-Video-Treatment')
var trialTimeoutID
var trialTimeoutTime = 1000*5 // 5 seconds
exp.getRootPath()
exp.getMediaPath()
var mediaPath = path.resolve(exp.mediapath, 'video')
var level1Trials = readCSV(path.resolve(exp.mediapath, 'level1.csv'))
var level2Trials = readCSV(path.resolve(exp.mediapath, 'level2.csv'))
var level3Trials = readCSV(path.resolve(exp.mediapath, 'level3.csv'))
var level4Trials = readCSV(path.resolve(exp.mediapath, 'level4.csv'))
var level5Trials = readCSV(path.resolve(exp.mediapath, 'level5.csv'))
var level6Trials = readCSV(path.resolve(exp.mediapath, 'level6.csv'))
var level7Trials = readCSV(path.resolve(exp.mediapath, 'level7.csv'))
var level8Trials = readCSV(path.resolve(exp.mediapath, 'level8.csv'))
var level9Trials = readCSV(path.resolve(exp.mediapath, 'level9.csv'))
var level10Trials = readCSV(path.resolve(exp.mediapath, 'level10.csv'))
var trials
var maxTrials = 20
var maxPracticeTrials = 2
var fileToSave
var fileHeader = ['subj', 'session', 'assessment', 'level', 'stim1', 'stim2', 'correctResp', 'keyPressed', 'reactionTime', 'accuracy', os.EOL]
var level = ''
var subjID
var sessID
var stimOnset
var accuracy
var totalAccArray = []
var rt
var nextTrialTimeoutID
//var trialNum = document.getElementById("trialNumID")
//var trialNumber = 1
var t = -1
var tReal = t-1
var level1Instructions = ["<h1>In this task you will see two videos at a time. <br>" +
"Each video is of a person's mouth saying a single word. <br>" +
"Listen closely to both words. <br>" +
"Press <span style='color:green'>GREEN</span> if they have the same number of syllables. <br>" +
"Press <span style='color:red'>RED</span> if they do not. </h1>"]
var level2Instructions = ["<h1>In this task you will see two videos at a time. <br>" +
"Each video is of a person's mouth saying a single word. <br>" +
"Listen closely to both words. <br>" +
"Press <span style='color:green'>GREEN</span> if the FIRST word has more syllables. <br>" +
"Press <span style='color:red'>RED</span> if the SECOND word has more syllables. </h1>"]
var level3Instructions = ["<h1>In this task you will see two videos at a time. <br>" +
"Each video is of a person's mouth saying a single word. <br>" +
"Listen closely to both words. <br>" +
"Press <span style='color:green'>GREEN</span> if they START with the same sound. <br>" +
"Press <span style='color:red'>RED</span> if they do not. </h1>"]
var level4Instructions = level3Instructions
var level5Instructions = level3Instructions
var level6Instructions = ["<h1>In this task you will see two videos at a time. <br>" +
"Each video is of a person's mouth saying a single word. <br>" +
"Listen closely to both words. <br>" +
"Press <span style='color:green'>GREEN</span> if they END with the same sound. <br>" +
"Press <span style='color:red'>RED</span> if they do not. </h1>"]
var level7Instructions = level6Instructions
var level8Instructions = level6Instructions
var level9Instructions = ["<h1>In this task you will see two videos at a time. <br>" +
"Each video is of a person's mouth saying a single word. <br>" +
"Listen closely to both words. <br>" +
"Press <span style='color:green'>GREEN</span> if they rhyme. <br>" +
"Press <span style='color:red'>RED</span> if they do not. </h1>"]
var level10Instructions = level9Instructions
var randomArray = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
var accCutOff = 0.8
var trialOrder = []
var userDataPath = path.join(app.getPath('userData'),'Data')
makeSureUserDataFolderIsThere()
var savePath
var summaryIsOnScreen = false
isPractice = false
var firstVideoNotFinished = true
var iti = 1500 // milliseconds
var fbTime = 750
function checkForUpdateFromRender() {
ipcRenderer.send('user-requests-update')
//alert('checked for update')
}
ipcRenderer.on('showSpinner', function () {
//<div class="loader">Loading...</div>
spinnerDiv = document.createElement('div')
spinnerDiv.className = 'loader'
spinnerDiv.style.zIndex = "1000";
content.appendChild(spinnerDiv)
console.log("added spinner!")
})
function getSubjID() {
var subjID = document.getElementById("subjID").value.trim()
if (subjID === '') {
subjID = '0'
}
return subjID
}
function getSessID() {
var sessID = document.getElementById("sessID").value.trim()
if (sessID === '') {
sessID = '0'
}
return sessID
}
function shuffle(array) {
//https://bost.ocks.org/mike/shuffle/
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
//camera preview on
function startWebCamPreview() {
clearScreen()
var vidPrevEl = document.createElement("video")
vidPrevEl.autoplay = true
vidPrevEl.id = "webcampreview"
content.appendChild(vidPrevEl)
navigator.webkitGetUserMedia({video: true, audio: false},
function(stream) {
localMediaStream = stream
vidPrevEl.src = URL.createObjectURL(stream)
},
function() {
alert('Could not connect to webcam')
}
)
}
// camera preview off
function stopWebCamPreview () {
if(typeof localMediaStream !== "undefined")
{
localMediaStream.getVideoTracks()[0].stop()
clearScreen()
}
}
// get date and time for appending to filenames
function getDateStamp() {
ts = moment().format('MMMM Do YYYY, h:mm:ss a')
ts = ts.replace(/ /g, '-') // replace spaces with dash
ts = ts.replace(/,/g, '') // replace comma with nothing
ts = ts.replace(/:/g, '-') // replace colon with dash
console.log('recording date stamp: ', ts)
return ts
}
// runs when called by systeminformation
function updateSys(ID) {
sys.modelID = ID
if (ID.includes("MacBook") == true) {
sys.isMacBook = true
}
//console.log("updateSys has updated!")
//console.log(ID.includes("MacBook"))
//console.log(sys.isMacBook)
} // end updateSys
si.system(function(data) {
console.log(data['model']);
updateSys(data['model'])
})
// open data folder in finder
function openDataFolder() {
dataFolder = savePath
if (!fs.existsSync(dataFolder)) {
mkdirp.sync(dataFolder)
}
shell.showItemInFolder(dataFolder)
}
function makeSureUserDataFolderIsThere(){
if (!fs.existsSync(userDataPath)) {
fs.mkdirSync(userDataPath)
}
}
function chooseFile() {
console.log("Analyze a file!")
dialog.showOpenDialog(
{title: "Video Treatment Analysis",
defaultPath: savePath,
properties: ["openFile"]},
analyzeSelectedFile)
}
function analyzeSelectedFile(theChosenOne) {
filePath = theChosenOne[0]
console.log("file chosen: ", filePath)
data = readCSV(filePath)
len = data.length
console.log('analyzing...')
accSum = 0
for (i = 0; i < len; i++) {
if (Number(data[i]['accuracy']) === 1) {
// if sounds were the same and accuracy was 1
accSum += 1 // increase by 1
}
}
clearScreen()
var textDiv = document.createElement("div")
textDiv.style.textAlign = 'center'
if (20 !== len) {
var warn_p = document.createElement("p")
var warn_txt = document.createTextNode("Warning! The number of trials in the data file does not match the total number of trials for this test")
warn_p.appendChild(warn_txt)
textDiv.appendChild(warn_p)
}
accMsg = 'Accuracy: ' + accSum/len
var acc_p = document.createElement("p")
var acc_txtNode = document.createTextNode(accMsg)
acc_p.appendChild(acc_txtNode)
textDiv.appendChild(acc_p)
content.appendChild(textDiv)
}
// get timestamp (milliseconds since file loaded)
function getTime() {
return performance.now()
}
// read csv file. This is how experiments will be controlled, query files to show, etc.
function readCSV(filename){
var csv = fs.readFileSync(filename)
var stim = csvsync.parse(csv, {
skipHeader: false,
returnObject: true
})
//var stim = csvReader(filename)
console.log(stim)
return stim
//stim = readCSV(myfile)
//console.log(stim)
//var myfile = __dirname+'/experiments/pnt/assets/txt/pntstim.csv'
}
// remove all child elements from a div, here the convention will be to
// remove the elements from "contentDiv" after a trial
function clearScreen() {
while (content.hasChildNodes()) {
content.removeChild(content.lastChild)
}
console.log("cleared the screen!")
}
// show text instructions on screen
function showInstructions(txt) {
trialOrder = shuffle(randomArray)
totalAccArray = []
dir = path.join(savePath, 'Phon', getSubjID(), getSessID())
if (!fs.existsSync(dir)) {
mkdirp.sync(dir)
}
fileToSave = path.join(dir,subjID+'_'+sessID+'_Phon_level_'+level+'_'+getDateStamp()+'.csv')
clearScreen()
var textDiv = document.createElement("div")
textDiv.style.textAlign = 'center'
var p = document.createElement("p")
//var txtNode = document.createTextNode(txt)
//p.appendChild(txtNode)
p.innerHTML = txt
textDiv.appendChild(p)
var lineBreak = document.createElement("br")
var startBtnDiv = document.createElement("div")
var startBtn = document.createElement("button")
var startBtnTxt = document.createTextNode("Start")
startBtn.appendChild(startBtnTxt)
startBtn.className = 'startBtn'
startBtn.onclick = function() {
showNextTrial(level)
}
startBtnDiv.appendChild(startBtn)
var practiceBtnDiv = document.createElement("div")
var practiceBtn = document.createElement("button")
var practiceBtnTxt = document.createTextNode("Practice")
practiceBtn.appendChild(practiceBtnTxt)
practiceBtn.className = 'startBtn'
practiceBtn.onclick = function() {
showNextPracticeTrial(level)
}
practiceBtnDiv.appendChild(practiceBtn)
content.appendChild(textDiv)
content.appendChild(lineBreak)
content.appendChild(startBtnDiv)
content.appendChild(lineBreak)
content.appendChild(practiceBtnDiv)
return getTime()
}
function showImg(imgPath, imgDurationMS) {
clearScreen()
var imageEl = document.createElement("img")
imageEl.src = imgPath
content.appendChild(imageEl)
clearTimeout(imgTimeoutID)
imgTimeoutID = setTimeout(clearScreen, imgDurationMS)
return getTime()
}
function stopRecordingAndShowNav() {
clearScreen()
openNav()
}
function clearScreenAndStopRecording() {
clearScreen()
openNav()
}
// load experiment module js file. All experiments are written in js, no separate html file
function loadJS (ID) {
if (!document.getElementById(ID +'JS')) {
expDir = path.join(__dirname, '/experiments/', ID, path.sep)
scrElement = document.createElement("script")
scrElement.type = "application/javascript"
scrElement.src = expDir + ID + '.js'
scrElement.id = ID + 'JS'
document.body.appendChild(scrElement)
console.log('loaded: ', scrElement.src)
//might need to wait for scrElement.onload event -- test this
//http://stackoverflow.com/a/38834971/3280952
}
}
// unload js at the end of experiment run
function unloadJS (ID) {
if (document.getElementById(ID +'JS')) {
scrElement = document.getElementById(ID +'JS')
document.body.removeChild(scrElement)
console.log('removed: ', ID +'JS')
}
}
function waitSecs(secs) {
var start = performance.now()
console.log("waitSecs started at: ", start)
var end = start
while(end < (start + (secs*1000))) {
end = performance.now()
}
console.log("waitSecs waited: ", end-start)
}
// wait for time (in ms) and then run the supplied function.
// for now, the supplied function can only have one input variable.
// this WILL HANG the gui
function waitThenDoSync(ms, doneWaitingCallback, arg){
var start = performance.now()
var end = start;
while(end < start + ms) {
end = performance.now()
}
if (arg !== undefined) {
doneWaitingCallback(arg)
} else {
doneWaitingCallback()
}
}
// wait for time (in ms) and then run the supplied function.
// for now, the supplied function can only have one input variable. (this does not hang gui)
function waitThenDoAsync (ms, doneWaitingCallback, arg) {
start = performance.now()
setTimeout(function () {
if (arg !== undefined) {
doneWaitingCallback(arg)
} else {
doneWaitingCallback()
}
end = performance.now()
console.log('Actual waitThenDo() time: ', end - start)
}, ms)
}
// keys object for storing keypress information
var keys = {
key : '',
time : 0,
rt: 0,
specialKeys: [' ', 'Enter', 'ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown', 'Shift', 'Tab', 'BackSpace'],
letterKeys: 'abcdefghijklmnopqrstuvwxyz'.split(''),
alphaNumericKeys: 'abcdefghijklmnopqrstuvwxyz1234567890'.split(''), // inspired by: http://stackoverflow.com/a/31755504/3280952
whiteList: function () {
return this.alphaNumericKeys.concat(this.specialKeys)
},
blackList: [],
isAllowed: function () {
idx = this.whiteList().indexOf(this.key)
var val = false
if (idx > 0) {
val = true
} else {
val = false
}
return val
}
}
// experiment object for storing session parameters, etc.
function experiment(name) {
this.beginTime= 0,
this.endTime= 0,
this.duration= 0,
this.name= name,
this.rootpath= '',
this.mediapath= '',
this.getDuration = function () {
return this.endTime - this.beginTime
},
this.setBeginTime = function() {
this.beginTime = performance.now()
},
this.setEndTime = function () {
this.endTime = performance.now()
},
this.getMediaPath = function () {
this.mediapath = path.join(__dirname, '/assets/')
return this.mediapath
},
this.getRootPath = function () {
this.rootpath = path.join(__dirname,'/')
return this.rootpath
}
}
function getRT() {
return keys.time - stimOnset
}
function checkAccuracy() {
if (keys.key === trials[trialOrder[t]].correctResp.trim()) {
acc = 1
} else {
acc = 0
}
return acc
}
function appendTrialDataToFile(fileToAppend, dataArray) {
dataArray.push(os.EOL)
dataString = csvsync.stringify(dataArray)
if (!fs.existsSync(fileToAppend)) {
fs.appendFileSync(fileToAppend, fileHeader)
fs.appendFileSync(fileToAppend, dataArray)
} else {
fs.appendFileSync(fileToAppend, dataArray)
}
console.log("appended file: ", fileToAppend)
}
// update keys object when a keydown event is detected
function updateKeys() {
if (summaryIsOnScreen) {
return
}
if (firstVideoNotFinished) {
return
}
// gets called from: document.addEventListener('keydown', updateKeys);
keys.key = event.key
keys.time = performance.now() // gives ms
keys.rt = 0
console.log("key: " + keys.key)
if (keys.key === '1' || keys.key === '2') {
if (!isPractice) {
clearTimeout(nextTrialTimeoutID)
clearTimeout(trialTimeoutID)
clearScreen()
accuracy = checkAccuracy()
totalAccArray.push(accuracy)
totalAcc = mean(totalAccArray)
console.log('total acc: ', totalAcc)
console.log("accuracy: ", accuracy)
keys.rt = getRT()
console.log("RT: ", keys.rt)
showFeedback(accuracy)
setTimeout(clearScreen, fbTime)
//['subj', 'session', 'assessment', 'level', 'stim1', 'stim2', 'correctResp', 'keyPressed', 'reactionTime', 'accuracy', os.EOL]
appendTrialDataToFile(fileToSave, [subjID, sessID, 'PhonTx', level, trials[trialOrder[t]].stim1.trim(), trials[trialOrder[t]].stim2.trim(), trials[trialOrder[t]].correctResp.trim(), keys.key, keys.rt, accuracy])
//waitSecs(1.5)
setTimeout(function() {showNextTrial(level)}, iti + fbTime)
} else if (isPractice) {
clearTimeout(nextTrialTimeoutID)
clearTimeout(trialTimeoutID)
clearScreen()
accuracy = checkAccuracy()
//totalAccArray.push(accuracy)
//totalAcc = mean(totalAccArray)
console.log("accuracy: ", accuracy)
keys.rt = getRT()
console.log("RT: ", keys.rt)
showFeedback(accuracy)
setTimeout(clearScreen, fbTime)
setTimeout(function() {showNextPracticeTrial(level)}, iti + fbTime)
}
firstVideoNotFinished = true
} else if (keys.key === 'ArrowLeft') {
}
}
function showFeedback (trialAcc) {
if (trialAcc == true) {
fbImg = path.join(exp.mediapath, 'correct' + '.png')
} else if (trialAcc == false) {
fbImg = path.join(exp.mediapath, 'incorrect' + '.png')
}
var img = document.createElement("img")
img.src = fbImg
img.style.height = '10%'
content.appendChild(img)
}
function mean(arrayToAvg) {
var sum = arrayToAvg.reduce((previous, current) => current += previous);
var avg = sum / arrayToAvg.length;
return avg
}
// store state of navigation pane
var nav = {
hidden: false
}
function clearAllTimeouts() {
clearTimeout(trialTimeoutID)
}
// open navigation pane
function openNav() {
clearAllTimeouts()
document.getElementById("navPanel").style.width = "150px"
document.getElementById("contentDiv").style.marginLeft = "150px"
document.body.style.backgroundColor = "rgba(0,0,0,0.3)"
if (document.getElementById("imageElement")) {
document.getElementById("imageElement").style.opacity = "0.1";
}
document.getElementById("closeNavBtn").innerHTML = "×"
}
// close navigation pane
function closeNav() {
document.getElementById("navPanel").style.width = "0px";
document.getElementById("contentDiv").style.marginLeft= "0px";
document.getElementById("contentDiv").style.width= "100%";
document.body.style.backgroundColor = "white";
//document.getElementById("menuBtn").innerHTML = "☰"
if (document.getElementById("imageElement")) {
document.getElementById("imageElement").style.opacity = "1";
}
}
// toggle navigation pane, detect if hidden or not
function toggleNav() {
if (nav.hidden) {
openNav()
nav.hidden = false
} else {
closeNav()
nav.hidden = true
}
}
// check if key that was pressed was the escape key or q. Quits experiment immediately
function checkForEscape() {
key = event.key
if (key === "Escape" || key=== "q") {
console.log("Escape was pressed")
openNav()
nav.hidden = false
// unloadJS(exp.name)
clearScreen()
resetVars()
}
}
function resetVars() {
t = -1
accuracy = null
totalAccArray = []
rt = null
trialOrder = []
trials = null
level = null
}
function getStarted() {
summaryIsOnScreen = false
resetVars()
subjID = document.getElementById("subjID").value.trim()
sessID = document.getElementById("sessID").value.trim()
level = document.getElementById("levelID").value.trim()
console.log("level chosen: ", level)
if (subjID === '' || sessID === '' || level === '') {
console.log ('subject, session, or level is blank')
alert('subject, session, or level is blank')
} else {
console.log ('subject is: ', subjID)
console.log('session is: ', sessID)
stopWebCamPreview()
closeNav()
resetTrialNumber()
if (level === 'level1') {
showInstructions(level1Instructions)
} else if (level === 'level2') {
showInstructions(level2Instructions)
} else if (level === 'level3') {
showInstructions(level3Instructions)
} else if (level === 'level4') {
showInstructions(level4Instructions)
} else if (level === 'level5') {
showInstructions(level5Instructions)
} else if (level === 'level6') {
showInstructions(level6Instructions)
} else if (level === 'level7') {
showInstructions(level7Instructions)
} else if (level === 'level8') {
showInstructions(level8Instructions)
} else if (level === 'level9') {
showInstructions(level9Instructions)
} else if (level === 'level10') {
showInstructions(level10Instructions)
}
}
}
function showNextTrial(level) {
isPractice = false
firstVideoNotFinished = true
if (level === 'level1') {
trials = level1Trials
} else if (level === 'level2') {
trials = level2Trials
} else if (level === 'level3') {
trials = level3Trials
} else if (level === 'level4') {
trials = level4Trials
} else if (level === 'level5') {
trials = level5Trials
} else if (level === 'level6') {
trials = level6Trials
} else if (level === 'level7') {
trials = level7Trials
} else if (level === 'level8') {
trials = level8Trials
} else if (level === 'level9') {
trials = level9Trials
} else if (level === 'level10') {
trials = level10Trials
}
clearTimeout(trialTimeoutID)
closeNav()
clearScreen()
t += 1
if (t > maxTrials-1) {
clearScreen()
clearAllTimeouts()
showSummary()
openNav()
t = maxTrials+1
return false
}
var vid = document.createElement("video")
vid.src = path.join(mediaPath, trials[trialOrder[t]].stim1.trim() + '.mp4')
vid.autoplay = true
vid.controls = false
content.appendChild(vid)
vid.onended = function() {
clearScreen()
firstVideoNotFinished = false
var vid2 = document.createElement("video")
vid2.src = path.join(mediaPath, trials[trialOrder[t]].stim2.trim() + '.mp4')
vid2.autoplay = true
vid2.controls = false
vid2.onplay = function() {
stimOnset = getTime()
}
vid2.onended = function() {
clearScreen()
trialTimeoutID = setTimeout(function() {
keys.rt = 0
keys.key = ''
accuracy = checkAccuracy()
totalAccArray.push(accuracy)
totalAcc = mean(totalAccArray)
console.log('total acc: ', totalAcc)
console.log("accuracy: ", accuracy)
showFeedback(accuracy)
setTimeout(clearScreen, fbTime)
//['subj', 'session', 'assessment', 'level', 'stim1', 'stim2', 'correctResp', 'keyPressed', 'reactionTime', 'accuracy', os.EOL]
appendTrialDataToFile(fileToSave, [subjID, sessID, 'PhonTx', level, trials[trialOrder[t]].stim1.trim(), trials[trialOrder[t]].stim2.trim(), trials[trialOrder[t]].correctResp.trim(), keys.key, keys.rt, accuracy])
nextTrialTimeoutID = setTimeout(function() {showNextTrial(level)}, iti + fbTime)
firstVideoNotFinished = true
}, trialTimeoutTime)
}
content.appendChild(vid2)
}
return getTime()
}
function showNextPracticeTrial(level) {
console.log ('Doing practice trial')
isPractice = true
firstVideoNotFinished = true
if (level === 'level1') {
trials = level1Trials
} else if (level === 'level2') {
trials = level2Trials
} else if (level === 'level3') {
trials = level3Trials
} else if (level === 'level4') {
trials = level4Trials
} else if (level === 'level5') {
trials = level5Trials
} else if (level === 'level6') {
trials = level6Trials
} else if (level === 'level7') {
trials = level7Trials
} else if (level === 'level8') {
trials = level8Trials
} else if (level === 'level9') {
trials = level9Trials
} else if (level === 'level10') {
trials = level10Trials
}
clearTimeout(trialTimeoutID)
closeNav()
clearScreen()
t += 1
if (t > maxPracticeTrials-1) {
clearScreen()
isPractice = false
clearAllTimeouts()
resetTrialNumber()
getStarted()
return false
}
var vid = document.createElement("video")
vid.src = path.join(mediaPath, trials[trialOrder[t]].stim1.trim() + '.mp4')
vid.autoplay = true
vid.controls = false
content.appendChild(vid)
vid.onended = function() {
clearScreen()
firstVideoNotFinished = false
var vid2 = document.createElement("video")
vid2.src = path.join(mediaPath, trials[trialOrder[t]].stim2.trim() + '.mp4')
vid2.autoplay = true
vid2.controls = false
vid2.onended = function() {
clearScreen()
trialTimeoutID = setTimeout(function() {
clearScreen()
keys.rt = 0
keys.key = ''
accuracy = checkAccuracy()
console.log("accuracy: ", accuracy)
showFeedback(accuracy)
setTimeout(clearScreen, fbTime)
setTimeout(function() {showNextPracticeTrial(level)}, iti + fbTime)
firstVideoNotFinished = true
}, trialTimeoutTime)
}
content.appendChild(vid2)
}
return getTime()
}
function showSummary() {
summaryIsOnScreen = true
accMin = 0.6
if (totalAcc < accCutOff && totalAcc >= accMin) {
accMsg = 'Accuracy: ' + totalAcc + '. Recommendation: Repeat current level (' + level + ')'
} else if (totalAcc < accMin) {
accMsg = 'Accuracy: ' + totalAcc + '. Recommendation: Move down one level'
} else if (totalAcc >= accCutOff) {
if (level === 'level10') {
accMsg = 'Accuracy: ' + totalAcc + '. Recommendation: Already at the highest level. Try for a higher accuracy.'
} else {
accMsg = 'Accuracy: ' + totalAcc + '. Recommendation: Move up one level'
}
}
var textDiv = document.createElement("div")
textDiv.style.textAlign = 'center'
var p = document.createElement("p")
var txtNode = document.createTextNode(accMsg)
p.appendChild(txtNode)
textDiv.appendChild(p)
content.appendChild(textDiv)
}
function resetTrialNumber() {
t = -1
}
// event listeners that are active for the life of the application
document.addEventListener('keyup', checkForEscape)
document.addEventListener('keyup', updateKeys)