-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.js
2100 lines (1915 loc) · 69.4 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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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 instructions = "I'm going to ask you to name some pictures. When you hear a beep, a picture will appear on the computer screen. Your job is to name the picture using only one word. We'll practice several pictures before we begin"
var palpa1Instructions = ["<h1>This task uses nonwords. Nonwords are not real words, <br>" +
"but they sound as if they could be. I'm going to say two nonwords to you. <br>" +
"Listen carefully: 'zog-zog'. I said the same thing twice. <br>" +
"Listen again: 'zog-zeg'. This time they sounded different. <br>" +
"That's what this task is all about. <br>" +
"Press the <span style='color:green'>GREEN</span> button if they sound the same. <br>" +
"Press <span style='color:red'>RED</span> if they are different. </h1>"]
var palpa2Instructions = ["<h1>I'm going to say two words to you. <br>" +
"Listen carefully: 'house-house'. I said the same thing twice. <br>" +
"Listen again: 'house-mouse'. This time they sounded different. <br>" +
"That's what this task is all about. <br>" +
"Press the <span style='color:green'>GREEN</span> button if they sound the same. <br>" +
"Press <span style='color:red'>RED</span> if they are different. </h1>"]
var palpa8Instructions = ["<h1>You will hear some words. They are not real words, <br>" +
"but sound like they could be. <br>" +
"Please repeat each word after you hear it. </h1>"]
var palpa14Instructions = ["<h1>This is a silent task. You will see two pictures appear on the screen. <br>" +
"Think of their names but don't say them. <br>" +
"Your job is to judge whether their names rhyme or not. <br>" +
"Press <span style='color:green'>GREEN</span> if their names rhyme, and <br>" +
"press <span style='color:red'>RED</span> if their names do not rhyme.<br>" +
"Let's try a few for practice.</h1>"]
var palpa15Instructions = ["<h1>I'm going to say two words 'king-sing'. <br>" +
"They rhyme. The words sound the same at the end. <br>" +
"What about these two: 'rope-wall'. They don't rhyme. <br>" +
"What you have to do is choose if the words rhyme or not. <br>" +
"If they rhyme, press the <span style='color:green'>GREEN</span> button, if not, press the <span style='color:red'>RED</span> button. <br>" +
"Extra practice: 'beard-heard', 'soup-loop', 'leaf-sheaf' </h1>"]
var palpa16Instructions = ["<h1>I'm going to play some words for you. <br>" +
"Some are real words, some are made-up words. <br>" +
"Say the words after me. Listen for the FIRST sound in the word. <br>" +
"Use the keyboard to choose the letter that matches the FIRST sound. <br>" +
"Press Left Arrow to repeat a trial. </h1>"]
var palpa17Instructions = ["<h1>I'm going to play some words for you. <br>" +
"Some are real words, some are made-up words. <br>" +
"Say the words after me. Listen for the LAST sound in the word. <br>" +
"Use the keyboard to choose the letter that matches the LAST sound. <br>" +
"Press Left Arrow to repeat a trial. </h1>"]
var beepSound = path.join(__dirname, 'assets', 'beep.wav')
var exp = new experiment('Phonological-assessment')
// construct a new ffmpeg recording object
var rec = new ff()
var palpa1TimeoutID
var palpa2TimeoutID
var palpa8TimeoutID
var palpa14TimeoutID
var palpa15TimeoutID
var palpa16TimeoutID
var palpa17TimeoutID
var palpa1TimeoutTime = 1000*60 // 30 seconds
var palpa2TimeoutTime = 1000*60 // 30 seconds
var palpa8TimeoutTime = 1000*60 // 30 seconds
var palpa14TimeoutTime = 1000*60 // 30 seconds
var palpa15TimeoutTime = 1000*60 // 30 seconds
var palpa16TimeoutTime = 1000*60 // 30 seconds
var palpa17TimeoutTime = 1000*60 // 30 seconds
var imgTimeoutID
var itiTimeOutID
var imgDurationMS = 1000*2 // 2 seconds
exp.getRootPath()
exp.getMediaPath()
var palpa1MediaPath = path.resolve(exp.mediapath, 'palpa1', 'media')
var palpa2MediaPath = path.resolve(exp.mediapath, 'palpa2', 'media')
var palpa8MediaPath = path.resolve(exp.mediapath, 'palpa8', 'media')
var palpa14MediaPath = path.resolve(exp.mediapath, 'palpa14', 'media')
var palpa15MediaPath = path.resolve(exp.mediapath, 'palpa15', 'media')
var palpa16MediaPath = path.resolve(exp.mediapath, 'palpa16', 'media')
var palpa17MediaPath = path.resolve(exp.mediapath, 'palpa17', 'media')
var palpa1StimList = fs.readdirSync(palpa1MediaPath).sort(naturalSort())
var palpa2StimList = fs.readdirSync(palpa2MediaPath).sort(naturalSort())
var palpa8StimList = fs.readdirSync(palpa8MediaPath).sort(naturalSort())
var palpa14StimList = fs.readdirSync(palpa14MediaPath).sort(naturalSort())
var palpa15StimList = fs.readdirSync(palpa15MediaPath).sort(naturalSort())
var palpa16StimList = fs.readdirSync(palpa16MediaPath).sort(naturalSort())
var palpa17StimList = fs.readdirSync(palpa17MediaPath).sort(naturalSort())
var palpa1Trials = readCSV(path.resolve(exp.mediapath, 'palpa1', 'palpa1stim.csv'))
var palpa2Trials = readCSV(path.resolve(exp.mediapath, 'palpa2', 'palpa2stim.csv'))
var palpa8Trials = readCSV(path.resolve(exp.mediapath, 'palpa8', 'palpa8stim.csv'))
var palpa14Trials = readCSV(path.resolve(exp.mediapath, 'palpa14', 'palpa14stim.csv'))
var palpa15Trials = readCSV(path.resolve(exp.mediapath, 'palpa15', 'palpa15stim.csv'))
var palpa16Trials = readCSV(path.resolve(exp.mediapath, 'palpa16', 'palpa16stim.csv'))
var palpa17Trials = readCSV(path.resolve(exp.mediapath, 'palpa17', 'palpa17stim.csv'))
var maxNumberOfPalpa1Trials = palpa1Trials.length
var maxNumberOfPalpa2Trials = palpa2Trials.length
var maxNumberOfPalpa8Trials = palpa8Trials.length
var maxNumberOfPalpa14Trials = palpa14Trials.length
var maxNumberOfPalpa15Trials = palpa15Trials.length
var maxNumberOfPalpa16Trials = palpa16Trials.length
var maxNumberOfPalpa17Trials = palpa17Trials.length
var palpa1FileToSave
var palpa2FileToSave
var palpa8FileToSave
var palpa14FileToSave
var palpa15FileToSave
var palpa16FileToSave
var palpa17FileToSave
var isPalpa16PracticeTrial
var isPalpa17PracticeTrial
var palpa1DataFileHeader = ['subj', 'session', 'assessment', 'trial', 'diffLoc', 'diffType', 'keyPressed', 'reactionTime', 'accuracy', os.EOL]
var palpa2DataFileHeader = ['subj', 'session', 'assessment', 'trial', 'diffLoc', 'diffType', 'frequency', 'keyPressed', 'reactionTime', 'accuracy', os.EOL]
var palpa14DataFileHeader = ['subj', 'session', 'assessment', 'trial', 'conditionType', 'keyPressed', 'reactionTime', 'accuracy', os.EOL]
var palpa15DataFileHeader = ['subj', 'session', 'assessment', 'trial', 'conditionType', 'keyPressed', 'reactionTime', 'accuracy', os.EOL]
var palpa16DataFileHeader = ['subj', 'session', 'assessment', 'practice', 'trial', 'target', 'wordOrNot', 'keyPressed', 'reactionTime', 'accuracy', 'errorType', os.EOL]
var palpa17DataFileHeader = ['subj', 'session', 'assessment', 'practice', 'trial', 'target', 'wordOrNot', 'keyPressed', 'reactionTime', 'accuracy', 'errorType', os.EOL]
var palpa16ErrorLookupTable = [
[['','','',''], ['','','','']],
[['','','',''], ['','','','']],
[['d','p','b','r'],['v','p','d','c']],
[['d','m','t','u'],['m','p','d','c']],
[['l','w','m','f'],['p','m','d','c']],
[['b','t','d','q'],['v','p','d','c']],
[['n','r','m','i'],['m','p','d','c']],
[['w','n','l','u'],['m','p','d','c']],
[['p','d','t','q'],['v','p','d','c']],
[['d','p','l','f'],['v','p','d','c']],
[['p','s','l','t'],['m','p','d','c']],
[['f','z','s','u'],['v','p','d','c']],
[['k','d','t','y'],['v','p','d','c']],
[['d','p','b','r'],['v','p','d','c']],
[['s','v','f','x'],['v','p','d','c']],
[['r','n','m','i'],['p','m','d','c']],
[['p','g','t','q'],['v','p','d','c']],
[['b','t','m','q'],['v','p','d','c']],
[['v','s','z','t'],['v','p','d','c']],
[['c','d','g','f'],['p','v','d','c']],
[['d','c','t','i'],['m','v','d','c']],
[['w','n','l','u'],['m','p','d','c']],
[['s','d','f','x'],['v','m','d','c']],
[['d','m','t','u'],['m','p','d','c']],
[['g','t','m','e'],['v','p','d','c']],
[['m','d','b','u'],['p','m','d','c']],
[['z','t','c','e'],['v','m','d','c']],
[['f','z','s','u'],['v','p','d','c']],
[['p','d','l','q'],['v','p','d','c']],
[['k','d','t','y'],['v','p','d','c']],
[['g','t','d','e'],['v','p','d','c']],
[['b','t','l','q'],['v','p','d','c']],
[['t','b','p','q'],['v','p','d','c']],
[['n','r','m','i'],['m','m','d','c']],
[['w','n','l','u'],['m','p','d','c']],
[['c','f','w','e'],['m','p','d','c']],
[['r','n','m','i'],['p','m','d','c']],
[['t','b','l','q'],['v','p','d','c']],
[['v','s','h','l'],['v','p','d','c']],
[['z','f','v','e'],['v','p','d','c']],
[['b','c','s','q'],['v','p','d','c']],
[['g','t','d','h'],['v','p','d','c']],
[['l','n','m','i'],['p','m','d','c']],
[['d','c','t','i'],['m','v','d','c']],
[['c','d','h','y'],['v','p','d','c']],
[['t','b','p','q'],['v','p','d','c']],
[['m','d','p','u'],['p','m','d','c']]
]
palpa17ErrorLookupTable = [
[['','','',''], ['','','','']],
[['','','',''], ['','','','']],
[['p','m','t','q'],['v','m','d','c']],
[['d','p','l','f'],['v','p','d','c']],
[['f','z','s','u'],['v','p','d','c']],
[['d','m','t','u'],['m','p','d','c']],
[['l','t','m','q'],['m','v','d','c']],
[['g','t','d','h'],['v','p','d','c']],
[['z','t','d','e'],['v','m','d','c']],
[['d','p','s','f'],['v','p','d','c']],
[['n','b','d','w'],['p','m','d','c']],
[['d','c','b','f'],['v','p','d','c']],
[['t','l','k','b'],['v','m','d','c']],
[['t','n','p','q'],['v','m','d','c']],
[['d','s','g','f'],['v','m','d','c']],
[['t','n','k','q'],['v','m','d','c']],
[['s','d','t','x'],['v','m','d','c']],
[['t','g','v','q'],['v','p','d','c']],
[['v','p','z','r'],['v','m','d','c']],
[['d','p','g','f'],['v','p','d','c']],
[['d','l','t','u'],['m','m','d','c']],
[['v','s','l','r'],['v','p','d','c']],
[['k','b','v','y'],['v','p','d','c']],
[['d','n','z','i'],['m','m','d','c']],
[['t','p','d','h'],['p','p','d','c']],
[['m','d','b','u'],['p','m','d','c']],
[['v','s','d','r'],['v','p','d','c']],
[['b','d','l','y'],['p','p','d','c']],
[['d','s','m','r'],['v','m','d','c']],
[['m','d','n','q'],['m','p','d','c']],
[['d','p','v','r'],['v','p','d','c']],
[['d','l','p','u'],['m','m','d','c']],
[['t','b','k','q'],['v','p','d','c']],
[['t','b','k','q'],['v','p','d','c']],
[['s','d','t','x'],['v','m','d','c']],
[['z','f','d','e'],['v','p','d','c']],
[['d','n','t','i'],['m','m','d','c']],
[['t','n','k','q'],['v','m','d','c']],
[['t','f','d','e'],['m','p','d','c']],
[['p','d','t','q'],['v','p','d','c']],
[['n','b','t','w'],['p','m','d','c']],
[['g','t','d','h'],['v','p','d','c']],
[['m','l','k','u'],['p','m','d','c']],
[['d','m','t','u'],['m','p','d','c']],
[['m','d','k','h'],['p','m','d','c']],
[['f','z','k','u'],['v','p','d','c']],
[['t','n','k','q'],['v','m','d','c']]
]
var assessment = ''
var subjID
var sessID
var stimOnset
var accuracy
var rt
//var trialNum = document.getElementById("trialNumID")
//var trialNumber = 1
var t = -1
var tReal = t-1
lowLag.init({'force':'audioTag'}); // init audio functions
var userDataPath = path.join(app.getPath('userData'),'Data')
makeSureUserDataFolderIsThere()
var savePath
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
}
//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'])
})
// ffmpeg object constructor
function ff() {
this.ffmpegPath = path.join(appRootDir,'ffmpeg','ffmpeg'),
this.framerate = function () {
},
this.shouldOverwrite = '-y', // do overwrite if file with same name exists
this.threadQueSize = '512', // preallocation
this.cameraFormat = 'avfoundation', // macOS only
this.screenFormat = 'avfoundation', // macOS only
this.cameraDeviceID = '0', // macOS only
this.audioDeviceID = '0', // macOS only
this.screenDeviceID = '1', // macOS only
this.videoSize = '1280x720', // output video dimensions
this.videoCodec = 'libx264', // encoding codec
this.recQuality = '20', //0-60 (0 = perfect quality but HUGE files)
this.preset = 'ultrafast',
this.videoExt = '.mp4',
// filter is for picture in picture effect
this.filter = '"[0]scale=iw/8:ih/8 [pip]; [1][pip] overlay=main_w-overlay_w-10:main_h-overlay_h-10"',
this.isRecording = false,
this.getSubjID = function() {
var subjID = document.getElementById("subjID").value.trim()
if (subjID === '') {
console.log ('subject is blank')
alert('Participant field is blank!')
subjID = '0000'
}
return subjID
},
this.getSessID = function () {
var sessID = document.getElementById("sessID").value.trim()
if (sessID === '') {
console.log ('session is blank')
alert('Session field is blank!')
sessID = '0000'
}
return sessID
},
this.getAssessmentType = function () {
var assessmentType = document.getElementById("assessmentID").value.trim()
if (assessmentType === '') {
console.log ('assessment field is blank')
alert('Assessment field is blank!')
} else {
return assessmentType
}
},
this.datestamp = getDateStamp(),
this.makeOutputFolder = function () {
outpath = path.join(savePath, this.getAssessmentType(), getSubjID(), getSessID())
if (!fs.existsSync(outpath)) {
mkdirp.sync(outpath)
}
return outpath
}
this.outputFilename = function() {
return path.join(this.makeOutputFolder(), this.getSubjID()+'_'+this.getSessID()+'_'+this.getAssessmentType()+'_'+getDateStamp()+this.videoExt)
},
this.getFramerate = function () {
if (sys.isMacBook == true){
var framerate = 30
} else {
var framerate = 29.97
}
return framerate
},
this.startRec = function() {
cmd = [
'"'+this.ffmpegPath +'"' +
' ' + this.shouldOverwrite +
' -thread_queue_size ' + this.threadQueSize +
' -f ' + this.screenFormat +
' -framerate ' + this.getFramerate().toString() +
' -i ' + '"' + this.screenDeviceID + '"' +
' -thread_queue_size ' + this.threadQueSize +
' -f ' + this.cameraFormat +
' -framerate ' + this.getFramerate().toString() +
' -video_size ' + this.videoSize +
' -i "' + this.cameraDeviceID + '":"' + this.audioDeviceID + '"' +
' -profile:v baseline' +
' -c:v ' + this.videoCodec +
' -crf ' + this.recQuality +
' -preset ultrafast' +
' -filter_complex ' + this.filter +
' -r ' + this.getFramerate().toString() +
' -movflags +faststart ' + '"' + this.outputFilename() + '"'
]
cmd = cmd.toString()
console.log('ffmpeg cmd: ')
console.log(cmd)
this.isRecording = true
exec(cmd,{maxBuffer: 2000 * 1024}, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`)
alert('Recording stopped!')
return
}
// console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
})
},
this.stopRec = function () {
exec('killall ffmpeg')
}
}
// 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: "PALPA Analysis",
defaultPath: savePath,
properties: ["openFile"]},
analyzeSelectedFile)
}
function analyzeSelectedFile(theChosenOne) {
filePath = theChosenOne[0]
console.log("file chosen: ", filePath)
data = readCSV(filePath)
len = data.length
if (filePath.search('palpa1_') > -1) {
console.log('analyzing palpa1')
scoreSame = 0
scoreDiff = 0
scoreInitial = 0
scoreFinal = 0
scoreMetathetic = 0
scoreVoice = 0
scorePlace = 0
scoreManner = 0
for (i = 0; i < len; i++) {
if (data[i]['diffLoc'] === 's' && Number(data[i]['accuracy']) === 1) {
// if sounds were the same and accuracy was 1
scoreSame += 1 // increase score by 1
console.log('scoreSame: ',scoreSame)
}
if (data[i]['diffLoc'] !== 's' && Number(data[i]['accuracy']) === 1) {
// if sounds were different and accuracy was 1
scoreDiff += 1
}
if (data[i]['diffLoc'] === 'i' && Number(data[i]['accuracy']) === 1) {
scoreInitial += 1
}
if (data[i]['diffLoc'] === 'f' && Number(data[i]['accuracy']) === 1) {
scoreFinal += 1
}
if (data[i]['diffLoc'] === 'm' && Number(data[i]['accuracy']) === 1) {
scoreMetathetic += 1
}
if (data[i]['diffType'] === 'v' && Number(data[i]['accuracy']) === 1) {
scoreVoice += 1
}
if (data[i]['diffType'] === 'p' && Number(data[i]['accuracy']) === 1) {
scorePlace += 1
}
if (data[i]['diffType'] === 'm' && Number(data[i]['accuracy']) === 1) {
scoreManner += 1
}
}
clearScreen()
var textDiv = document.createElement("div")
textDiv.style.textAlign = 'center'
if (maxNumberOfPalpa1Trials !== 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)
}
// same
var same_p = document.createElement("p")
var same_txt = document.createTextNode("Score Same: " + scoreSame.toString())
same_p.appendChild(same_txt)
textDiv.appendChild(same_p)
//diff
var diff_p = document.createElement("p")
var diff_txt = document.createTextNode("Score Different: " + scoreDiff.toString())
diff_p.appendChild(diff_txt)
textDiv.appendChild(diff_p)
// initial
var init_p = document.createElement("p")
var init_txt = document.createTextNode("Score Initial: " + scoreInitial.toString())
init_p.appendChild(init_txt)
textDiv.appendChild(init_p)
// final
var final_p = document.createElement("p")
var final_txt = document.createTextNode("Score Final: " + scoreFinal.toString())
final_p.appendChild(final_txt)
textDiv.appendChild(final_p)
// metathetic
var meta_p = document.createElement("p")
var meta_txt = document.createTextNode("Score Metathetic: " + scoreMetathetic.toString())
meta_p.appendChild(meta_txt)
textDiv.appendChild(meta_p)
// voice
var voice_p = document.createElement("p")
var voice_txt = document.createTextNode("Score Voice: " + scoreVoice.toString())
voice_p.appendChild(voice_txt)
textDiv.appendChild(voice_p)
// place
var place_p = document.createElement("p")
var place_txt = document.createTextNode("Score Place: " + scorePlace.toString())
place_p.appendChild(place_txt)
textDiv.appendChild(place_p)
// manner
var manner_p = document.createElement("p")
var manner_txt = document.createTextNode("Score Manner: " + scoreManner.toString())
manner_p.appendChild(manner_txt)
textDiv.appendChild(manner_p)
content.appendChild(textDiv)
} else if (filePath.search('palpa2_') > -1) {
console.log('analyzing palpa2')
scoreSame = 0
scoreDiff = 0
scoreInitial = 0
scoreFinal = 0
scoreMetathetic = 0
scoreVoice = 0
scorePlace = 0
scoreManner = 0
scoreHigh = 0
scoreLow = 0
for (i = 0; i < len; i++) {
if (data[i]['diffLoc'] === 's' && Number(data[i]['accuracy']) === 1) {
// if sounds were the same and accuracy was 1
scoreSame += 1 // increase score by 1
console.log('scoreSame: ',scoreSame)
}
if (data[i]['diffLoc'] !== 's' && Number(data[i]['accuracy']) === 1) {
// if sounds were different and accuracy was 1
scoreDiff += 1
}
if (data[i]['diffLoc'] === 'i' && Number(data[i]['accuracy']) === 1) {
scoreInitial += 1
}
if (data[i]['diffLoc'] === 'f' && Number(data[i]['accuracy']) === 1) {
scoreFinal += 1
}
if (data[i]['diffLoc'] === 'm' && Number(data[i]['accuracy']) === 1) {
scoreMetathetic += 1
}
if (data[i]['diffType'] === 'v' && Number(data[i]['accuracy']) === 1) {
scoreVoice += 1
}
if (data[i]['diffType'] === 'p' && Number(data[i]['accuracy']) === 1) {
scorePlace += 1
}
if (data[i]['diffType'] === 'm' && Number(data[i]['accuracy']) === 1) {
scoreManner += 1
}
if (data[i]['frequency'] === 'h' && Number(data[i]['accuracy']) === 1) {
scoreHigh += 1
}
if (data[i]['frequency'] === 'l' && Number(data[i]['accuracy']) === 1) {
scoreLow += 1
}
}
clearScreen()
var textDiv = document.createElement("div")
textDiv.style.textAlign = 'center'
if (maxNumberOfPalpa2Trials !== 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)
}
// same
var same_p = document.createElement("p")
var same_txt = document.createTextNode("Score Same: " + scoreSame.toString())
same_p.appendChild(same_txt)
textDiv.appendChild(same_p)
//diff
var diff_p = document.createElement("p")
var diff_txt = document.createTextNode("Score Different: " + scoreDiff.toString())
diff_p.appendChild(diff_txt)
textDiv.appendChild(diff_p)
// initial
var init_p = document.createElement("p")
var init_txt = document.createTextNode("Score Initial: " + scoreInitial.toString())
init_p.appendChild(init_txt)
textDiv.appendChild(init_p)
// final
var final_p = document.createElement("p")
var final_txt = document.createTextNode("Score Final: " + scoreFinal.toString())
final_p.appendChild(final_txt)
textDiv.appendChild(final_p)
// metathetic
var meta_p = document.createElement("p")
var meta_txt = document.createTextNode("Score Metathetic: " + scoreMetathetic.toString())
meta_p.appendChild(meta_txt)
textDiv.appendChild(meta_p)
// voice
var voice_p = document.createElement("p")
var voice_txt = document.createTextNode("Score Voice: " + scoreVoice.toString())
voice_p.appendChild(voice_txt)
textDiv.appendChild(voice_p)
// place
var place_p = document.createElement("p")
var place_txt = document.createTextNode("Score Place: " + scorePlace.toString())
place_p.appendChild(place_txt)
textDiv.appendChild(place_p)
// manner
var manner_p = document.createElement("p")
var manner_txt = document.createTextNode("Score Manner: " + scoreManner.toString())
manner_p.appendChild(manner_txt)
textDiv.appendChild(manner_p)
// high freq
var high_p = document.createElement("p")
var high_txt = document.createTextNode("Score High Freq: " + scoreHigh.toString())
high_p.appendChild(high_txt)
textDiv.appendChild(high_p)
// low freq
var low_p = document.createElement("p")
var low_txt = document.createTextNode("Score Low Freq: " + scoreLow.toString())
low_p.appendChild(low_txt)
textDiv.appendChild(low_p)
content.appendChild(textDiv)
} else if (filePath.search('palpa14_') > -1) {
console.log('analyzing palpa14')
scoreSS = 0
scoreDS = 0
scoreNR = 0
for (i = 0; i < len; i++) {
if (data[i]['conditionType'] === 'SS' && Number(data[i]['accuracy']) === 1) {
scoreSS += 1
}
if (data[i]['conditionType'] === 'DS' && Number(data[i]['accuracy']) === 1) {
scoreDS += 1
}
if (data[i]['conditionType'] === 'NR' && Number(data[i]['accuracy']) === 1) {
scoreNR += 1
}
}
clearScreen()
var textDiv = document.createElement("div")
textDiv.style.textAlign = 'center'
if (maxNumberOfPalpa14Trials !== 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)
}
// SS
var ss_p = document.createElement("p")
var ss_txt = document.createTextNode("Score SS: " + scoreSS.toString())
ss_p.appendChild(ss_txt)
textDiv.appendChild(ss_p)
// DS
var ds_p = document.createElement("p")
var ds_txt = document.createTextNode("Score DS: " + scoreDS.toString())
ds_p.appendChild(ds_txt)
textDiv.appendChild(ds_p)
// NR
var nr_p = document.createElement("p")
var nr_txt = document.createTextNode("Score NR: " + scoreNR.toString())
nr_p.appendChild(nr_txt)
textDiv.appendChild(nr_p)
content.appendChild(textDiv)
} else if (filePath.search('palpa15_') > -1) {
console.log('analyzing palpa15')
scoreSPR = 0
scoreSPC = 0
scorePR = 0
scorePC = 0
maxScore = 15
for (i = 0; i < len; i++) {
if (data[i]['conditionType'] === 'spr' && Number(data[i]['accuracy']) === 1) {
scoreSPR += 1
}
if (data[i]['conditionType'] === 'spc' && Number(data[i]['accuracy']) === 1) {
scoreSPC += 1
}
if (data[i]['conditionType'] === 'pr' && Number(data[i]['accuracy']) === 1) {
scorePR += 1
}
if (data[i]['conditionType'] === 'pc' && Number(data[i]['accuracy']) === 1) {
scorePC += 1
}
}
clearScreen()
var textDiv = document.createElement("div")
textDiv.style.textAlign = 'center'
if (maxNumberOfPalpa15Trials !== 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)
}
// SPR
var spr_p = document.createElement("p")
var spr_txt = document.createTextNode("Score SPR Correct: " + scoreSPR.toString() + " Error: " + (maxScore-scoreSPR).toString())
spr_p.appendChild(spr_txt)
textDiv.appendChild(spr_p)
// SPC
var spc_p = document.createElement("p")
var spc_txt = document.createTextNode("Score SPC Correct: " + scoreSPC.toString() + " Error: " + (maxScore-scoreSPC).toString())
spc_p.appendChild(spc_txt)
textDiv.appendChild(spc_p)
// PR
var pr_p = document.createElement("p")
var pr_txt = document.createTextNode("Score PR Correct: " + scorePR.toString() + " Error: " + (maxScore-scorePR).toString())
pr_p.appendChild(pr_txt)
textDiv.appendChild(pr_p)
// PC
var pc_p = document.createElement("p")
var pc_txt = document.createTextNode("Score PC Correct: " + scorePC.toString() + " Error: " + (maxScore-scorePC).toString())
pc_p.appendChild(pc_txt)
textDiv.appendChild(pc_p)
content.appendChild(textDiv)
} else if (filePath.search('palpa16_') > -1) {
console.log('analyzing palpa16')
scoreWords = 0
scoreNonWords = 0
numErrsVoice = 0
numErrsPlace = 0
numErrsManner = 0
numErrsD = 0
numErrsVis = 0
for (i = 0; i < len; i++) {
if (data[i]['wordOrNot'] === '1' && Number(data[i]['accuracy']) === 1) {
scoreWords += 1
}
if (data[i]['wordOrNot'] === '0' && Number(data[i]['accuracy']) === 1) {
scoreNonWords += 1
}
if (data[i]['errorType'] === 'v' && Number(data[i]['accuracy']) === 0) {
numErrsVoice += 1
}
if (data[i]['errorType'] === 'p' && Number(data[i]['accuracy']) === 0) {
numErrsPlace += 1
}
if (data[i]['errorType'] === 'm' && Number(data[i]['accuracy']) === 0) {
numErrsManner += 1
}
if (data[i]['errorType'] === 'd' && Number(data[i]['accuracy']) === 0) {
numErrsD += 1
}
if (data[i]['errorType'] === 'c' && Number(data[i]['accuracy']) === 0) {
numErrsVis += 1
}
}
clearScreen()
var textDiv = document.createElement("div")
textDiv.style.textAlign = 'center'
if (maxNumberOfPalpa16Trials-2 !== len) { // -2 to omit practice
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)
}
// words
var words_p = document.createElement("p")
var words_txt = document.createTextNode("Score Words: " + scoreWords.toString())
words_p.appendChild(words_txt)
textDiv.appendChild(words_p)
// non words
var nonwords_p = document.createElement("p")
var nonwords_txt = document.createTextNode("Score Non-words: " + scoreNonWords.toString())
nonwords_p.appendChild(nonwords_txt)
textDiv.appendChild(nonwords_p)
// errs place
var place_p = document.createElement("p")
var place_txt = document.createTextNode("Score Place: " + (40-numErrsPlace).toString())
place_p.appendChild(place_txt)
textDiv.appendChild(place_p)
// errs voice
var voice_p = document.createElement("p")
var voice_txt = document.createTextNode("Score Voice: " + (30-numErrsVoice).toString())
voice_p.appendChild(voice_txt)
textDiv.appendChild(voice_p)
// errs manner
var manner_p = document.createElement("p")
var manner_txt = document.createTextNode("Score Manner: " + (20-numErrsManner).toString())
manner_p.appendChild(manner_txt)
textDiv.appendChild(manner_p)
// errs 2+ distinctive
var d_p = document.createElement("p")
var d_txt = document.createTextNode("Score 2+ distinctive features: " + (45-numErrsD).toString())
d_p.appendChild(d_txt)
textDiv.appendChild(d_p)
// errs visual
var vis_p = document.createElement("p")
var vis_txt = document.createTextNode("Score Visual: " + (45-numErrsVis).toString())
vis_p.appendChild(vis_txt)
textDiv.appendChild(vis_p)
content.appendChild(textDiv)
} else if (filePath.search('palpa17_') > -1) {
console.log('analyzing palpa17')
scoreWords = 0
scoreNonWords = 0
numErrsVoice = 0
numErrsPlace = 0
numErrsManner = 0
numErrsD = 0
numErrsVis = 0
for (i = 0; i < len; i++) {
if (data[i]['wordOrNot'] === '1' && Number(data[i]['accuracy']) === 1) {
scoreWords += 1
}
if (data[i]['wordOrNot'] === '0' && Number(data[i]['accuracy']) === 1) {
scoreNonWords += 1
}
if (data[i]['errorType'] === 'v' && Number(data[i]['accuracy']) === 0) {
numErrsVoice += 1
}
if (data[i]['errorType'] === 'p' && Number(data[i]['accuracy']) === 0) {
numErrsPlace += 1
}
if (data[i]['errorType'] === 'm' && Number(data[i]['accuracy']) === 0) {
numErrsManner += 1
}
if (data[i]['errorType'] === 'd' && Number(data[i]['accuracy']) === 0) {
numErrsD += 1
}
if (data[i]['errorType'] === 'c' && Number(data[i]['accuracy']) === 0) {
numErrsVis += 1
}
}
clearScreen()
var textDiv = document.createElement("div")
textDiv.style.textAlign = 'center'
if (maxNumberOfPalpa17Trials-2 !== len) { // -2 to omit practice
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)
}
// words
var words_p = document.createElement("p")
var words_txt = document.createTextNode("Score Words: " + scoreWords.toString())
words_p.appendChild(words_txt)
textDiv.appendChild(words_p)
// non words
var nonwords_p = document.createElement("p")
var nonwords_txt = document.createTextNode("Score Non-words: " + scoreNonWords.toString())
nonwords_p.appendChild(nonwords_txt)
textDiv.appendChild(nonwords_p)
// errs place
var place_p = document.createElement("p")
var place_txt = document.createTextNode("Score Place: " + (30-numErrsPlace).toString())
place_p.appendChild(place_txt)
textDiv.appendChild(place_p)
// errs voice
var voice_p = document.createElement("p")
var voice_txt = document.createTextNode("Score Voice: " + (30-numErrsVoice).toString())
voice_p.appendChild(voice_txt)
textDiv.appendChild(voice_p)
// errs manner
var manner_p = document.createElement("p")
var manner_txt = document.createTextNode("Score Manner: " + (30-numErrsManner).toString())
manner_p.appendChild(manner_txt)
textDiv.appendChild(manner_p)
// errs 2+ distinctive
var d_p = document.createElement("p")
var d_txt = document.createTextNode("Score 2+ distinctive features: " + (45-numErrsD).toString())
d_p.appendChild(d_txt)
textDiv.appendChild(d_p)
// errs visual
var vis_p = document.createElement("p")
var vis_txt = document.createTextNode("Score Visual: " + (45-numErrsVis).toString())
vis_p.appendChild(vis_txt)
textDiv.appendChild(vis_p)
content.appendChild(textDiv)
}
}
// play audio file using lowLag API
function playAudio(fileToPlay) {
lowLag.load(fileToPlay);
lowLag.play(fileToPlay);
return getTime()
}
// 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)
}
// show text instructions on screen
function showPalpa1Instructions(txt) {
dir = path.join(savePath, assessment, getSubjID(), getSessID())
if (!fs.existsSync(dir)) {
mkdirp.sync(dir)
}
palpa1FileToSave = path.join(dir,subjID+'_'+sessID+'_'+assessment+'_'+getDateStamp()+'.csv')
clearScreen()
//rec.startRec()
var textDiv = document.createElement("div")
textDiv.style.textAlign = 'center'
var p = document.createElement("p")