-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.js
590 lines (516 loc) · 15.9 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
const { remote, shell } = require('electron')
const {Menu, MenuItem} = remote
const path = require('path')
const csvsync = require('csvsync')
const fs = require('fs')
const $ = require('jQuery')
const {app} = require('electron').remote;
app.setName('PNT')
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 mkdirp = require('mkdirp');
var ipcRenderer = require('electron').ipcRenderer;
var moment = require('moment')
var content = document.getElementById("contentDiv")
var picNum = document.getElementById("picNumID")
var localMediaStream
var sys = {
modelID: 'unknown',
isMacBook: false // need to detect if macbook for ffmpeg recording framerate value
}
var instructions = ["<h1>I'm going to ask you to name some pictures. <br>" +
"Each picture will appear on the computer screen. <br>" +
"Your job is to name the picture using only one word. <br>" +
"We'll practice several pictures before we begin </h1>"]
var beepSound = path.join(__dirname, 'assets', 'beep.wav')
var exp = new experiment('pnt')
// construct a new ffmpeg recording object
var rec = new ff()
var timeoutTime = 30 // in seconds
exp.getRootPath()
exp.getMediaPath()
var stimfile = path.resolve(exp.mediapath, 'stim.csv')
//console.log(stimfile)
var trials = readCSV(stimfile)
var maxTrials = trials.length
var trialTimeoutID
var t = Number(picNum.value.trim())-1
var tReal = t-1
//lowLag.init(); // 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
}
function makeSureUserDataFolderIsThere() {
if (!fs.existsSync(userDataPath)) {
fs.mkdirSync(userDataPath)
}
}
//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 libx264
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.datestamp = getDateStamp(),
this.makeOutputFolder = function () {
outpath = path.join(savePath, app.getName(), getSubjID(), getSessID())
console.log(outpath)
if (!fs.existsSync(outpath)) {
mkdirp.sync(outpath)
}
return outpath
}
this.outputFilename = function() {
return path.join(this.makeOutputFolder(), this.getSubjID()+'_'+this.getSessID()+'_'+app.getName()+'_'+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() +
' -async 1' +
' -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)
}
// play audio file using lowLag API
function playAudio(fileToPlay) {
lowLag.load(fileToPlay);
lowLag.play(fileToPlay);
}
// 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)
}
function clearAllTimeouts() {
clearTimeout(trialTimeoutID)
}
// show text instructions on screen
function showInstructions(txt) {
clearScreen()
rec.startRec()
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 btnDiv = document.createElement("div")
var startBtn = document.createElement("button")
var startBtnTxt = document.createTextNode("Start")
startBtn.appendChild(startBtnTxt)
startBtn.className = 'startBtn'
startBtn.onclick = showNextTrial
btnDiv.appendChild(startBtn)
content.appendChild(textDiv)
content.appendChild(lineBreak)
content.appendChild(btnDiv)
return getTime()
}
// show single image on screen
function showImage(imgPath) {
clearScreen()
var imageEl = document.createElement("img")
imageEl.src = imgPath
content.appendChild(imageEl)
return getTime()
}
// 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')
}
}
// 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'],
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
}
}
// update keys object when a keydown event is detected
function updateKeys() {
// 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 === 'ArrowRight') {
showNextTrial()
}
if (keys.key === 'ArrowLeft') {
showPreviousTrial()
}
}
// store state of navigation pane
var nav = {
hidden: false
}
// open navigation pane
function openNav() {
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()
rec.stopRec()
clearTimeout(trialTimeoutID)
}
}
function getStarted() {
var subjID = document.getElementById("subjID").value.trim()
var sessID = document.getElementById("sessID").value.trim()
if (subjID === '' || sessID === '') {
console.log ('subject and/or session is blank')
alert('Participant field or session field is blank!')
} else {
console.log ('subject is: ', subjID)
console.log('session is: ', sessID)
stopWebCamPreview()
t = Number(picNum.value.trim())-1
tReal = t-1
if (Number(picNum.value.trim()) > maxTrials+1) {
alert("Invalid picture number " + picNum.value.trim() )
} else {
closeNav()
showInstructions(instructions)
}
}
}
function showNextTrial() {
clearTimeout(trialTimeoutID)
closeNav()
clearScreen()
t += 1
tReal = t-1
picNum.value = t
if (tReal >= maxTrials) {
clearScreen()
rec.stopRec()
clearAllTimeouts()
openNav()
return false
}
var img = document.createElement("img")
img.src = path.join(exp.mediapath, 'pics', trials[tReal].PictureName.trim() + '.png')
//playAudio(path.join(exp.mediapath, 'beep.wav'))
content.appendChild(img)
trialTimeoutID = setTimeout(showNextTrial, 1000 * timeoutTime)
return getTime()
}
function showPreviousTrial() {
clearTimeout(trialTimeoutID)
closeNav()
clearScreen()
t -= 1
tReal = t-1
picNum.value = t
if (tReal < 0) {
t=1
tReal = t-1
}
var img = document.createElement("img")
img.src = path.join(exp.mediapath, 'pics', trials[tReal].PictureName.trim() + '.png')
//playAudio(path.join(exp.mediapath, 'beep.wav'))
content.appendChild(img)
trialTimeoutID = setTimeout(showNextTrial, 1000 * timeoutTime)
return getTime()
}
// event listeners that are active for the life of the application
document.addEventListener('keyup', checkForEscape)
document.addEventListener('keyup', updateKeys)