Mercurial > hg > webaudioevaluationtool
comparison core.js @ 841:71fd099e9fd2
Started some user guide
author | Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk> |
---|---|
date | Sat, 08 Aug 2015 09:00:41 +0100 |
parents | |
children | f86d75be0d56 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 841:71fd099e9fd2 |
---|---|
1 /** | |
2 * core.js | |
3 * | |
4 * Main script to run, calls all other core functions and manages loading/store to backend. | |
5 * Also contains all global variables. | |
6 */ | |
7 | |
8 /* create the web audio API context and store in audioContext*/ | |
9 var audioContext; // Hold the browser web audio API | |
10 var projectXML; // Hold the parsed setup XML | |
11 var specification; | |
12 var interfaceContext; | |
13 var popup; // Hold the interfacePopup object | |
14 var testState; | |
15 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order | |
16 var audioEngineContext; // The custome AudioEngine object | |
17 var projectReturn; // Hold the URL for the return | |
18 | |
19 | |
20 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it | |
21 AudioBufferSourceNode.prototype.owner = undefined; | |
22 | |
23 window.onload = function() { | |
24 // Function called once the browser has loaded all files. | |
25 // This should perform any initial commands such as structure / loading documents | |
26 | |
27 // Create a web audio API context | |
28 // Fixed for cross-browser support | |
29 var AudioContext = window.AudioContext || window.webkitAudioContext; | |
30 audioContext = new AudioContext; | |
31 | |
32 // Create test state | |
33 testState = new stateMachine(); | |
34 | |
35 // Create the audio engine object | |
36 audioEngineContext = new AudioEngine(); | |
37 | |
38 // Create the popup interface object | |
39 popup = new interfacePopup(); | |
40 | |
41 // Create the specification object | |
42 specification = new Specification(); | |
43 | |
44 // Create the interface object | |
45 interfaceContext = new Interface(specification); | |
46 }; | |
47 | |
48 function interfacePopup() { | |
49 // Creates an object to manage the popup | |
50 this.popup = null; | |
51 this.popupContent = null; | |
52 this.buttonProceed = null; | |
53 this.buttonPrevious = null; | |
54 this.popupOptions = null; | |
55 this.currentIndex = null; | |
56 this.responses = null; | |
57 | |
58 this.createPopup = function(){ | |
59 // Create popup window interface | |
60 var insertPoint = document.getElementById("topLevelBody"); | |
61 var blank = document.createElement('div'); | |
62 blank.className = 'testHalt'; | |
63 | |
64 this.popup = document.createElement('div'); | |
65 this.popup.id = 'popupHolder'; | |
66 this.popup.className = 'popupHolder'; | |
67 this.popup.style.position = 'absolute'; | |
68 this.popup.style.left = (window.innerWidth/2)-250 + 'px'; | |
69 this.popup.style.top = (window.innerHeight/2)-125 + 'px'; | |
70 | |
71 this.popupContent = document.createElement('div'); | |
72 this.popupContent.id = 'popupContent'; | |
73 this.popupContent.style.marginTop = '25px'; | |
74 this.popupContent.align = 'center'; | |
75 this.popup.appendChild(this.popupContent); | |
76 | |
77 this.buttonProceed = document.createElement('button'); | |
78 this.buttonProceed.className = 'popupButton'; | |
79 this.buttonProceed.style.left = '440px'; | |
80 this.buttonProceed.style.top = '215px'; | |
81 this.buttonProceed.innerHTML = 'Next'; | |
82 this.buttonProceed.onclick = function(){popup.proceedClicked();}; | |
83 | |
84 this.buttonPrevious = document.createElement('button'); | |
85 this.buttonPrevious.className = 'popupButton'; | |
86 this.buttonPrevious.style.left = '10px'; | |
87 this.buttonPrevious.style.top = '215px'; | |
88 this.buttonPrevious.innerHTML = 'Back'; | |
89 this.buttonPrevious.onclick = function(){popup.previousClick();}; | |
90 | |
91 this.popup.style.zIndex = -1; | |
92 this.popup.style.visibility = 'hidden'; | |
93 blank.style.zIndex = -2; | |
94 blank.style.visibility = 'hidden'; | |
95 insertPoint.appendChild(this.popup); | |
96 insertPoint.appendChild(blank); | |
97 }; | |
98 | |
99 this.showPopup = function(){ | |
100 if (this.popup == null) { | |
101 this.createPopup(); | |
102 } | |
103 this.popup.style.zIndex = 3; | |
104 this.popup.style.visibility = 'visible'; | |
105 var blank = document.getElementsByClassName('testHalt')[0]; | |
106 blank.style.zIndex = 2; | |
107 blank.style.visibility = 'visible'; | |
108 $(window).keypress(function(e){ | |
109 if (e.keyCode == 13 && popup.popup.style.visibility == 'visible') | |
110 { | |
111 // Enter key pressed | |
112 var textarea = $(popup.popupContent).find('textarea'); | |
113 if (textarea.length != 0) | |
114 { | |
115 if (textarea[0] == document.activeElement) | |
116 {return;} | |
117 } | |
118 popup.buttonProceed.onclick(); | |
119 } | |
120 }); | |
121 }; | |
122 | |
123 this.hidePopup = function(){ | |
124 this.popup.style.zIndex = -1; | |
125 this.popup.style.visibility = 'hidden'; | |
126 var blank = document.getElementsByClassName('testHalt')[0]; | |
127 blank.style.zIndex = -2; | |
128 blank.style.visibility = 'hidden'; | |
129 }; | |
130 | |
131 this.postNode = function() { | |
132 // This will take the node from the popupOptions and display it | |
133 var node = this.popupOptions[this.currentIndex]; | |
134 this.popupContent.innerHTML = null; | |
135 if (node.type == 'statement') { | |
136 var span = document.createElement('span'); | |
137 span.textContent = node.statement; | |
138 this.popupContent.appendChild(span); | |
139 } else if (node.type == 'question') { | |
140 var span = document.createElement('span'); | |
141 span.textContent = node.question; | |
142 var textArea = document.createElement('textarea'); | |
143 switch (node.boxsize) { | |
144 case 'small': | |
145 textArea.cols = "20"; | |
146 textArea.rows = "1"; | |
147 break; | |
148 case 'normal': | |
149 textArea.cols = "30"; | |
150 textArea.rows = "2"; | |
151 break; | |
152 case 'large': | |
153 textArea.cols = "40"; | |
154 textArea.rows = "5"; | |
155 break; | |
156 case 'huge': | |
157 textArea.cols = "50"; | |
158 textArea.rows = "10"; | |
159 break; | |
160 } | |
161 var br = document.createElement('br'); | |
162 this.popupContent.appendChild(span); | |
163 this.popupContent.appendChild(br); | |
164 this.popupContent.appendChild(textArea); | |
165 this.popupContent.childNodes[2].focus(); | |
166 } else if (node.type == 'checkbox') { | |
167 var span = document.createElement('span'); | |
168 span.textContent = node.statement; | |
169 this.popupContent.appendChild(span); | |
170 var optHold = document.createElement('div'); | |
171 optHold.id = 'option-holder'; | |
172 optHold.align = 'left'; | |
173 for (var i=0; i<node.options.length; i++) { | |
174 var option = node.options[i]; | |
175 var input = document.createElement('input'); | |
176 input.id = option.id; | |
177 input.type = 'checkbox'; | |
178 var span = document.createElement('span'); | |
179 span.textContent = option.text; | |
180 var hold = document.createElement('div'); | |
181 hold.setAttribute('name','option'); | |
182 hold.style.float = 'left'; | |
183 hold.style.padding = '4px'; | |
184 hold.appendChild(input); | |
185 hold.appendChild(span); | |
186 optHold.appendChild(hold); | |
187 } | |
188 this.popupContent.appendChild(optHold); | |
189 } else if (node.type == 'radio') { | |
190 var span = document.createElement('span'); | |
191 span.textContent = node.statement; | |
192 this.popupContent.appendChild(span); | |
193 var optHold = document.createElement('div'); | |
194 optHold.id = 'option-holder'; | |
195 optHold.align = 'none'; | |
196 optHold.style.float = 'left'; | |
197 optHold.style.width = "100%"; | |
198 for (var i=0; i<node.options.length; i++) { | |
199 var option = node.options[i]; | |
200 var input = document.createElement('input'); | |
201 input.id = option.name; | |
202 input.type = 'radio'; | |
203 input.name = node.id; | |
204 var span = document.createElement('span'); | |
205 span.textContent = option.text; | |
206 var hold = document.createElement('div'); | |
207 hold.setAttribute('name','option'); | |
208 hold.style.padding = '4px'; | |
209 hold.appendChild(input); | |
210 hold.appendChild(span); | |
211 optHold.appendChild(hold); | |
212 } | |
213 this.popupContent.appendChild(optHold); | |
214 } else if (node.type == 'number') { | |
215 var span = document.createElement('span'); | |
216 span.textContent = node.statement; | |
217 this.popupContent.appendChild(span); | |
218 this.popupContent.appendChild(document.createElement('br')); | |
219 var input = document.createElement('input'); | |
220 input.type = 'textarea'; | |
221 if (node.min != null) {input.min = node.min;} | |
222 if (node.max != null) {input.max = node.max;} | |
223 if (node.step != null) {input.step = node.step;} | |
224 this.popupContent.appendChild(input); | |
225 } | |
226 this.popupContent.appendChild(this.buttonProceed); | |
227 if(this.currentIndex+1 == this.popupOptions.length) { | |
228 if (this.responses.nodeName == "PRETEST") { | |
229 this.buttonProceed.textContent = 'Start'; | |
230 } else { | |
231 this.buttonProceed.textContent = 'Submit'; | |
232 } | |
233 } else { | |
234 this.buttonProceed.textContent = 'Next'; | |
235 } | |
236 if(this.currentIndex > 0) | |
237 this.popupContent.appendChild(this.buttonPrevious); | |
238 }; | |
239 | |
240 this.initState = function(node) { | |
241 //Call this with your preTest and postTest nodes when needed to | |
242 // initialise the popup procedure. | |
243 this.popupOptions = node.options; | |
244 if (this.popupOptions.length > 0) { | |
245 if (node.type == 'pretest') { | |
246 this.responses = document.createElement('PreTest'); | |
247 } else if (node.type == 'posttest') { | |
248 this.responses = document.createElement('PostTest'); | |
249 } else { | |
250 console.log ('WARNING - popup node neither pre or post!'); | |
251 this.responses = document.createElement('responses'); | |
252 } | |
253 this.currentIndex = 0; | |
254 this.showPopup(); | |
255 this.postNode(); | |
256 } else { | |
257 advanceState(); | |
258 } | |
259 }; | |
260 | |
261 this.proceedClicked = function() { | |
262 // Each time the popup button is clicked! | |
263 var node = this.popupOptions[this.currentIndex]; | |
264 if (node.type == 'question') { | |
265 // Must extract the question data | |
266 var textArea = $(popup.popupContent).find('textarea')[0]; | |
267 if (node.mandatory == true && textArea.value.length == 0) { | |
268 alert('This question is mandatory'); | |
269 return; | |
270 } else { | |
271 // Save the text content | |
272 var hold = document.createElement('comment'); | |
273 hold.id = node.id; | |
274 hold.innerHTML = textArea.value; | |
275 console.log("Question: "+ node.question); | |
276 console.log("Question Response: "+ textArea.value); | |
277 this.responses.appendChild(hold); | |
278 } | |
279 } else if (node.type == 'checkbox') { | |
280 // Must extract checkbox data | |
281 var optHold = document.getElementById('option-holder'); | |
282 var hold = document.createElement('checkbox'); | |
283 console.log("Checkbox: "+ node.statement); | |
284 hold.id = node.id; | |
285 for (var i=0; i<optHold.childElementCount; i++) { | |
286 var input = optHold.childNodes[i].getElementsByTagName('input')[0]; | |
287 var statement = optHold.childNodes[i].getElementsByTagName('span')[0]; | |
288 var response = document.createElement('option'); | |
289 response.setAttribute('name',input.id); | |
290 response.textContent = input.checked; | |
291 hold.appendChild(response); | |
292 console.log(input.id +': '+ input.checked); | |
293 } | |
294 this.responses.appendChild(hold); | |
295 } else if (node.type == "radio") { | |
296 var optHold = document.getElementById('option-holder'); | |
297 var hold = document.createElement('radio'); | |
298 var responseID = null; | |
299 var i=0; | |
300 while(responseID == null) { | |
301 var input = optHold.childNodes[i].getElementsByTagName('input')[0]; | |
302 if (input.checked == true) { | |
303 responseID = i; | |
304 } | |
305 i++; | |
306 } | |
307 hold.id = node.id; | |
308 hold.setAttribute('name',node.options[responseID].name); | |
309 hold.textContent = node.options[responseID].text; | |
310 this.responses.appendChild(hold); | |
311 } else if (node.type == "number") { | |
312 var input = this.popupContent.getElementsByTagName('input')[0]; | |
313 if (node.mandatory == true && input.value.length == 0) { | |
314 alert('This question is mandatory. Please enter a number'); | |
315 return; | |
316 } | |
317 var enteredNumber = Number(input.value); | |
318 if (isNaN(enteredNumber)) { | |
319 alert('Please enter a valid number'); | |
320 return; | |
321 } | |
322 if (enteredNumber < node.min && node.min != null) { | |
323 alert('Number is below the minimum value of '+node.min); | |
324 return; | |
325 } | |
326 if (enteredNumber > node.max && node.max != null) { | |
327 alert('Number is above the maximum value of '+node.max); | |
328 return; | |
329 } | |
330 var hold = document.createElement('number'); | |
331 hold.id = node.id; | |
332 hold.textContent = input.value; | |
333 this.responses.appendChild(hold); | |
334 } | |
335 this.currentIndex++; | |
336 if (this.currentIndex < this.popupOptions.length) { | |
337 this.postNode(); | |
338 } else { | |
339 // Reached the end of the popupOptions | |
340 this.hidePopup(); | |
341 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) { | |
342 testState.stateResults[testState.stateIndex] = this.responses; | |
343 } else { | |
344 testState.stateResults[testState.stateIndex].appendChild(this.responses); | |
345 } | |
346 advanceState(); | |
347 } | |
348 }; | |
349 | |
350 this.previousClick = function() { | |
351 // Triggered when the 'Back' button is clicked in the survey | |
352 if (this.currentIndex > 0) { | |
353 this.currentIndex--; | |
354 var node = this.popupOptions[this.currentIndex]; | |
355 if (node.type != 'statement') { | |
356 var prevResp = this.responses.childNodes[this.responses.childElementCount-1]; | |
357 this.responses.removeChild(prevResp); | |
358 } | |
359 this.postNode(); | |
360 if (node.type == 'question') { | |
361 this.popupContent.getElementsByTagName('textarea')[0].value = prevResp.textContent; | |
362 } else if (node.type == 'checkbox') { | |
363 var options = this.popupContent.getElementsByTagName('input'); | |
364 var savedOptions = prevResp.getElementsByTagName('option'); | |
365 for (var i=0; i<options.length; i++) { | |
366 var id = options[i].id; | |
367 for (var j=0; j<savedOptions.length; j++) { | |
368 if (savedOptions[j].getAttribute('name') == id) { | |
369 if (savedOptions[j].textContent == 'true') {options[i].checked = true;} | |
370 else {options[i].checked = false;} | |
371 break; | |
372 } | |
373 } | |
374 } | |
375 } else if (node.type == 'number') { | |
376 this.popupContent.getElementsByTagName('input')[0].value = prevResp.textContent; | |
377 } else if (node.type == 'radio') { | |
378 var options = this.popupContent.getElementsByTagName('input'); | |
379 var name = prevResp.getAttribute('name'); | |
380 for (var i=0; i<options.length; i++) { | |
381 if (options[i].id == name) { | |
382 options[i].checked = true; | |
383 break; | |
384 } | |
385 } | |
386 } | |
387 } | |
388 }; | |
389 } | |
390 | |
391 function advanceState() | |
392 { | |
393 // Just for complete clarity | |
394 testState.advanceState(); | |
395 } | |
396 | |
397 function stateMachine() | |
398 { | |
399 // Object prototype for tracking and managing the test state | |
400 this.stateMap = []; | |
401 this.stateIndex = null; | |
402 this.currentStateMap = []; | |
403 this.currentIndex = null; | |
404 this.currentTestId = 0; | |
405 this.stateResults = []; | |
406 this.timerCallBackHolders = null; | |
407 this.initialise = function(){ | |
408 if (this.stateMap.length > 0) { | |
409 if(this.stateIndex != null) { | |
410 console.log('NOTE - State already initialise'); | |
411 } | |
412 this.stateIndex = -1; | |
413 var that = this; | |
414 var aH_pId = 0; | |
415 for (var id=0; id<this.stateMap.length; id++){ | |
416 var name = this.stateMap[id].type; | |
417 var obj = document.createElement(name); | |
418 if (name == 'audioHolder') { | |
419 obj.id = this.stateMap[id].id; | |
420 obj.setAttribute('presentedid',aH_pId); | |
421 aH_pId+=1; | |
422 } | |
423 this.stateResults.push(obj); | |
424 } | |
425 } else { | |
426 console.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP'); | |
427 } | |
428 }; | |
429 this.advanceState = function(){ | |
430 if (this.stateIndex == null) { | |
431 this.initialise(); | |
432 } | |
433 if (this.stateIndex == -1) { | |
434 console.log('Starting test...'); | |
435 } | |
436 if (this.currentIndex == null){ | |
437 if (this.currentStateMap.type == "audioHolder") { | |
438 // Save current page | |
439 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId); | |
440 this.currentTestId++; | |
441 } | |
442 this.stateIndex++; | |
443 if (this.stateIndex >= this.stateMap.length) { | |
444 console.log('Test Completed'); | |
445 createProjectSave(specification.projectReturn); | |
446 } else { | |
447 this.currentStateMap = this.stateMap[this.stateIndex]; | |
448 if (this.currentStateMap.type == "audioHolder") { | |
449 console.log('Loading test page'); | |
450 loadTest(this.currentStateMap); | |
451 this.initialiseInnerState(this.currentStateMap); | |
452 } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") { | |
453 if (this.currentStateMap.options.length >= 1) { | |
454 popup.initState(this.currentStateMap); | |
455 } else { | |
456 this.advanceState(); | |
457 } | |
458 } else { | |
459 this.advanceState(); | |
460 } | |
461 } | |
462 } else { | |
463 this.advanceInnerState(); | |
464 } | |
465 }; | |
466 | |
467 this.testPageCompleted = function(store, testXML, testId) { | |
468 // Function called each time a test page has been completed | |
469 // Can be used to over-rule default behaviour | |
470 | |
471 pageXMLSave(store, testXML); | |
472 }; | |
473 | |
474 this.initialiseInnerState = function(node) { | |
475 // Parses the received testXML for pre and post test options | |
476 this.currentStateMap = []; | |
477 var preTest = node.preTest; | |
478 var postTest = node.postTest; | |
479 if (preTest == undefined) {preTest = document.createElement("preTest");} | |
480 if (postTest == undefined){postTest= document.createElement("postTest");} | |
481 this.currentStateMap.push(preTest); | |
482 this.currentStateMap.push(node); | |
483 this.currentStateMap.push(postTest); | |
484 this.currentIndex = -1; | |
485 this.advanceInnerState(); | |
486 }; | |
487 | |
488 this.advanceInnerState = function() { | |
489 this.currentIndex++; | |
490 if (this.currentIndex >= this.currentStateMap.length) { | |
491 this.currentIndex = null; | |
492 this.currentStateMap = this.stateMap[this.stateIndex]; | |
493 this.advanceState(); | |
494 } else { | |
495 if (this.currentStateMap[this.currentIndex].type == "audioHolder") { | |
496 console.log("Loading test page"+this.currentTestId); | |
497 } else if (this.currentStateMap[this.currentIndex].type == "pretest") { | |
498 popup.initState(this.currentStateMap[this.currentIndex]); | |
499 } else if (this.currentStateMap[this.currentIndex].type == "posttest") { | |
500 popup.initState(this.currentStateMap[this.currentIndex]); | |
501 } else { | |
502 this.advanceInnerState(); | |
503 } | |
504 } | |
505 }; | |
506 | |
507 this.previousState = function(){}; | |
508 } | |
509 | |
510 function testEnded(testId) | |
511 { | |
512 pageXMLSave(testId); | |
513 if (testXMLSetups.length-1 > testId) | |
514 { | |
515 // Yes we have another test to perform | |
516 testId = (Number(testId)+1); | |
517 currentState = 'testRun-'+testId; | |
518 loadTest(testId); | |
519 } else { | |
520 console.log('Testing Completed!'); | |
521 currentState = 'postTest'; | |
522 // Check for any post tests | |
523 var xmlSetup = projectXML.find('setup'); | |
524 var postTest = xmlSetup.find('PostTest')[0]; | |
525 popup.initState(postTest); | |
526 } | |
527 } | |
528 | |
529 function loadProjectSpec(url) { | |
530 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data | |
531 // If url is null, request client to upload project XML document | |
532 var r = new XMLHttpRequest(); | |
533 r.open('GET',url,true); | |
534 r.onload = function() { | |
535 loadProjectSpecCallback(r.response); | |
536 }; | |
537 r.send(); | |
538 }; | |
539 | |
540 function loadProjectSpecCallback(response) { | |
541 // Function called after asynchronous download of XML project specification | |
542 //var decode = $.parseXML(response); | |
543 //projectXML = $(decode); | |
544 | |
545 var parse = new DOMParser(); | |
546 projectXML = parse.parseFromString(response,'text/xml'); | |
547 | |
548 // Build the specification | |
549 specification.decode(); | |
550 | |
551 testState.stateMap.push(specification.preTest); | |
552 | |
553 // New check if we need to randomise the test order | |
554 if (specification.randomiseOrder) | |
555 { | |
556 specification.audioHolders = randomiseOrder(specification.audioHolders); | |
557 for (var i=0; i<specification.audioHolders.length; i++) | |
558 { | |
559 specification.audioHolders[i].presentedId = i; | |
560 } | |
561 } | |
562 | |
563 $(specification.audioHolders).each(function(index,elem){ | |
564 testState.stateMap.push(elem); | |
565 }); | |
566 | |
567 testState.stateMap.push(specification.postTest); | |
568 | |
569 // Obtain the metrics enabled | |
570 $(specification.metrics).each(function(index,node){ | |
571 var enabled = node.textContent; | |
572 switch(node.enabled) | |
573 { | |
574 case 'testTimer': | |
575 sessionMetrics.prototype.enableTestTimer = true; | |
576 break; | |
577 case 'elementTimer': | |
578 sessionMetrics.prototype.enableElementTimer = true; | |
579 break; | |
580 case 'elementTracker': | |
581 sessionMetrics.prototype.enableElementTracker = true; | |
582 break; | |
583 case 'elementListenTracker': | |
584 sessionMetrics.prototype.enableElementListenTracker = true; | |
585 break; | |
586 case 'elementInitialPosition': | |
587 sessionMetrics.prototype.enableElementInitialPosition = true; | |
588 break; | |
589 case 'elementFlagListenedTo': | |
590 sessionMetrics.prototype.enableFlagListenedTo = true; | |
591 break; | |
592 case 'elementFlagMoved': | |
593 sessionMetrics.prototype.enableFlagMoved = true; | |
594 break; | |
595 case 'elementFlagComments': | |
596 sessionMetrics.prototype.enableFlagComments = true; | |
597 break; | |
598 } | |
599 }); | |
600 | |
601 | |
602 | |
603 // Detect the interface to use and load the relevant javascripts. | |
604 var interfaceJS = document.createElement('script'); | |
605 interfaceJS.setAttribute("type","text/javascript"); | |
606 if (specification.interfaceType == 'APE') { | |
607 interfaceJS.setAttribute("src","ape.js"); | |
608 | |
609 // APE comes with a css file | |
610 var css = document.createElement('link'); | |
611 css.rel = 'stylesheet'; | |
612 css.type = 'text/css'; | |
613 css.href = 'ape.css'; | |
614 | |
615 document.getElementsByTagName("head")[0].appendChild(css); | |
616 } else if (specification.interfaceType == "MUSHRA") | |
617 { | |
618 interfaceJS.setAttribute("src","mushra.js"); | |
619 | |
620 // MUSHRA comes with a css file | |
621 var css = document.createElement('link'); | |
622 css.rel = 'stylesheet'; | |
623 css.type = 'text/css'; | |
624 css.href = 'mushra.css'; | |
625 | |
626 document.getElementsByTagName("head")[0].appendChild(css); | |
627 } | |
628 document.getElementsByTagName("head")[0].appendChild(interfaceJS); | |
629 | |
630 // Define window callbacks for interface | |
631 window.onresize = function(event){resizeWindow(event);}; | |
632 } | |
633 | |
634 function createProjectSave(destURL) { | |
635 // Save the data from interface into XML and send to destURL | |
636 // If destURL is null then download XML in client | |
637 // Now time to render file locally | |
638 var xmlDoc = interfaceXMLSave(); | |
639 var parent = document.createElement("div"); | |
640 parent.appendChild(xmlDoc); | |
641 var file = [parent.innerHTML]; | |
642 if (destURL == "null" || destURL == undefined) { | |
643 var bb = new Blob(file,{type : 'application/xml'}); | |
644 var dnlk = window.URL.createObjectURL(bb); | |
645 var a = document.createElement("a"); | |
646 a.hidden = ''; | |
647 a.href = dnlk; | |
648 a.download = "save.xml"; | |
649 a.textContent = "Save File"; | |
650 | |
651 popup.showPopup(); | |
652 popup.popupContent.innerHTML = null; | |
653 popup.popupContent.appendChild(a); | |
654 } else { | |
655 var xmlhttp = new XMLHttpRequest; | |
656 xmlhttp.open("POST",destURL,true); | |
657 xmlhttp.setRequestHeader('Content-Type', 'text/xml'); | |
658 xmlhttp.onerror = function(){ | |
659 console.log('Error saving file to server! Presenting download locally'); | |
660 createProjectSave(null); | |
661 }; | |
662 xmlhttp.onreadystatechange = function() { | |
663 console.log(xmlhttp.status); | |
664 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) { | |
665 createProjectSave(null); | |
666 } else { | |
667 popup.showPopup(); | |
668 popup.popupContent.innerHTML = null; | |
669 popup.popupContent.textContent = "Thank you for performing this listening test"; | |
670 } | |
671 }; | |
672 xmlhttp.send(file); | |
673 } | |
674 } | |
675 | |
676 function errorSessionDump(msg){ | |
677 // Create the partial interface XML save | |
678 // Include error node with message on why the dump occured | |
679 var xmlDoc = interfaceXMLSave(); | |
680 var err = document.createElement('error'); | |
681 err.textContent = msg; | |
682 xmlDoc.appendChild(err); | |
683 var parent = document.createElement("div"); | |
684 parent.appendChild(xmlDoc); | |
685 var file = [parent.innerHTML]; | |
686 var bb = new Blob(file,{type : 'application/xml'}); | |
687 var dnlk = window.URL.createObjectURL(bb); | |
688 var a = document.createElement("a"); | |
689 a.hidden = ''; | |
690 a.href = dnlk; | |
691 a.download = "save.xml"; | |
692 a.textContent = "Save File"; | |
693 | |
694 popup.showPopup(); | |
695 popup.popupContent.innerHTML = "ERROR : "+msg; | |
696 popup.popupContent.appendChild(a); | |
697 } | |
698 | |
699 // Only other global function which must be defined in the interface class. Determines how to create the XML document. | |
700 function interfaceXMLSave(){ | |
701 // Create the XML string to be exported with results | |
702 var xmlDoc = document.createElement("BrowserEvaluationResult"); | |
703 var projectDocument = specification.projectXML; | |
704 projectDocument.setAttribute('file-name',url); | |
705 xmlDoc.appendChild(projectDocument); | |
706 xmlDoc.appendChild(returnDateNode()); | |
707 for (var i=0; i<testState.stateResults.length; i++) | |
708 { | |
709 xmlDoc.appendChild(testState.stateResults[i]); | |
710 } | |
711 | |
712 return xmlDoc; | |
713 } | |
714 | |
715 function AudioEngine() { | |
716 | |
717 // Create two output paths, the main outputGain and fooGain. | |
718 // Output gain is default to 1 and any items for playback route here | |
719 // Foo gain is used for analysis to ensure paths get processed, but are not heard | |
720 // because web audio will optimise and any route which does not go to the destination gets ignored. | |
721 this.outputGain = audioContext.createGain(); | |
722 this.fooGain = audioContext.createGain(); | |
723 this.fooGain.gain = 0; | |
724 | |
725 // Use this to detect playback state: 0 - stopped, 1 - playing | |
726 this.status = 0; | |
727 this.audioObjectsReady = false; | |
728 | |
729 // Connect both gains to output | |
730 this.outputGain.connect(audioContext.destination); | |
731 this.fooGain.connect(audioContext.destination); | |
732 | |
733 // Create the timer Object | |
734 this.timer = new timer(); | |
735 // Create session metrics | |
736 this.metric = new sessionMetrics(this); | |
737 | |
738 this.loopPlayback = false; | |
739 | |
740 // Create store for new audioObjects | |
741 this.audioObjects = []; | |
742 | |
743 this.play = function(id) { | |
744 // Start the timer and set the audioEngine state to playing (1) | |
745 if (this.status == 0) { | |
746 // Check if all audioObjects are ready | |
747 if (this.audioObjectsReady == false) { | |
748 this.audioObjectsReady = this.checkAllReady(); | |
749 } | |
750 if (this.audioObjectsReady == true) { | |
751 this.timer.startTest(); | |
752 if (this.loopPlayback) | |
753 {this.setSynchronousLoop();} | |
754 this.status = 1; | |
755 } | |
756 } | |
757 if (this.status== 1) { | |
758 if (id == undefined) { | |
759 id = -1; | |
760 } else { | |
761 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]); | |
762 } | |
763 if (this.loopPlayback) { | |
764 for (var i=0; i<this.audioObjects.length; i++) | |
765 { | |
766 this.audioObjects[i].play(this.timer.getTestTime()+1); | |
767 if (id == i) { | |
768 this.audioObjects[i].loopStart(); | |
769 } else { | |
770 this.audioObjects[i].loopStop(); | |
771 } | |
772 } | |
773 } else { | |
774 for (var i=0; i<this.audioObjects.length; i++) | |
775 { | |
776 if (i != id) { | |
777 this.audioObjects[i].outputGain.gain.value = 0.0; | |
778 this.audioObjects[i].stop(); | |
779 } else if (i == id) { | |
780 this.audioObjects[id].outputGain.gain.value = 1.0; | |
781 this.audioObjects[id].play(audioContext.currentTime+0.01); | |
782 } | |
783 } | |
784 } | |
785 interfaceContext.playhead.start(); | |
786 } | |
787 }; | |
788 | |
789 this.stop = function() { | |
790 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1) | |
791 if (this.status == 1) { | |
792 for (var i=0; i<this.audioObjects.length; i++) | |
793 { | |
794 this.audioObjects[i].stop(); | |
795 } | |
796 interfaceContext.playhead.stop(); | |
797 this.status = 0; | |
798 } | |
799 }; | |
800 | |
801 this.newTrack = function(element) { | |
802 // Pull data from given URL into new audio buffer | |
803 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin' | |
804 | |
805 // Create the audioObject with ID of the new track length; | |
806 audioObjectId = this.audioObjects.length; | |
807 this.audioObjects[audioObjectId] = new audioObject(audioObjectId); | |
808 | |
809 // AudioObject will get track itself. | |
810 this.audioObjects[audioObjectId].specification = element; | |
811 this.audioObjects[audioObjectId].constructTrack(element.parent.hostURL + element.url); | |
812 return this.audioObjects[audioObjectId]; | |
813 }; | |
814 | |
815 this.newTestPage = function() { | |
816 this.state = 0; | |
817 this.audioObjectsReady = false; | |
818 this.metric.reset(); | |
819 this.audioObjects = []; | |
820 }; | |
821 | |
822 this.checkAllPlayed = function() { | |
823 arr = []; | |
824 for (var id=0; id<this.audioObjects.length; id++) { | |
825 if (this.audioObjects[id].metric.wasListenedTo == false) { | |
826 arr.push(this.audioObjects[id].id); | |
827 } | |
828 } | |
829 return arr; | |
830 }; | |
831 | |
832 this.checkAllReady = function() { | |
833 var ready = true; | |
834 for (var i=0; i<this.audioObjects.length; i++) { | |
835 if (this.audioObjects[i].state == 0) { | |
836 // Track not ready | |
837 console.log('WAIT -- audioObject '+i+' not ready yet!'); | |
838 ready = false; | |
839 }; | |
840 } | |
841 return ready; | |
842 }; | |
843 | |
844 this.setSynchronousLoop = function() { | |
845 // Pads the signals so they are all exactly the same length | |
846 if (this.audioObjectsReady) | |
847 { | |
848 var length = 0; | |
849 var lens = []; | |
850 var maxId; | |
851 for (var i=0; i<this.audioObjects.length; i++) | |
852 { | |
853 lens.push(this.audioObjects[i].buffer.length); | |
854 if (length < this.audioObjects[i].buffer.length) | |
855 { | |
856 length = this.audioObjects[i].buffer.length; | |
857 maxId = i; | |
858 } | |
859 } | |
860 // Perform difference | |
861 for (var i=0; i<lens.length; i++) | |
862 { | |
863 lens[i] = length - lens[i]; | |
864 } | |
865 // Extract the audio and zero-pad | |
866 for (var i=0; i<lens.length; i++) | |
867 { | |
868 var orig = this.audioObjects[i].buffer; | |
869 var hold = audioContext.createBuffer(orig.numberOfChannels,length,orig.sampleRate); | |
870 for (var c=0; c<orig.numberOfChannels; c++) | |
871 { | |
872 var inData = hold.getChannelData(c); | |
873 var outData = orig.getChannelData(c); | |
874 for (var n=0; n<orig.length; n++) | |
875 {inData[n] = outData[n];} | |
876 } | |
877 this.audioObjects[i].buffer = hold; | |
878 delete orig; | |
879 } | |
880 } | |
881 }; | |
882 | |
883 } | |
884 | |
885 function audioObject(id) { | |
886 // The main buffer object with common control nodes to the AudioEngine | |
887 | |
888 this.specification; | |
889 this.id = id; | |
890 this.state = 0; // 0 - no data, 1 - ready | |
891 this.url = null; // Hold the URL given for the output back to the results. | |
892 this.metric = new metricTracker(this); | |
893 | |
894 // Bindings for GUI | |
895 this.interfaceDOM = null; | |
896 this.commentDOM = null; | |
897 | |
898 // Create a buffer and external gain control to allow internal patching of effects and volume leveling. | |
899 this.bufferNode = undefined; | |
900 this.outputGain = audioContext.createGain(); | |
901 | |
902 // Default output gain to be zero | |
903 this.outputGain.gain.value = 0.0; | |
904 | |
905 // Connect buffer to the audio graph | |
906 this.outputGain.connect(audioEngineContext.outputGain); | |
907 | |
908 // the audiobuffer is not designed for multi-start playback | |
909 // When stopeed, the buffer node is deleted and recreated with the stored buffer. | |
910 this.buffer; | |
911 | |
912 this.loopStart = function() { | |
913 this.outputGain.gain.value = 1.0; | |
914 this.metric.startListening(audioEngineContext.timer.getTestTime()); | |
915 }; | |
916 | |
917 this.loopStop = function() { | |
918 if (this.outputGain.gain.value != 0.0) { | |
919 this.outputGain.gain.value = 0.0; | |
920 this.metric.stopListening(audioEngineContext.timer.getTestTime()); | |
921 } | |
922 }; | |
923 | |
924 this.play = function(startTime) { | |
925 if (this.bufferNode == undefined) { | |
926 this.bufferNode = audioContext.createBufferSource(); | |
927 this.bufferNode.owner = this; | |
928 this.bufferNode.connect(this.outputGain); | |
929 this.bufferNode.buffer = this.buffer; | |
930 this.bufferNode.loop = audioEngineContext.loopPlayback; | |
931 this.bufferNode.onended = function() { | |
932 // Safari does not like using 'this' to reference the calling object! | |
933 event.srcElement.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.srcElement.owner.getCurrentPosition()); | |
934 }; | |
935 if (this.bufferNode.loop == false) { | |
936 this.metric.startListening(audioEngineContext.timer.getTestTime()); | |
937 } | |
938 this.bufferNode.start(startTime); | |
939 } | |
940 }; | |
941 | |
942 this.stop = function() { | |
943 if (this.bufferNode != undefined) | |
944 { | |
945 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition()); | |
946 this.bufferNode.stop(0); | |
947 this.bufferNode = undefined; | |
948 } | |
949 }; | |
950 | |
951 this.getCurrentPosition = function() { | |
952 var time = audioEngineContext.timer.getTestTime(); | |
953 if (this.bufferNode != undefined) { | |
954 if (this.bufferNode.loop == true) { | |
955 if (audioEngineContext.status == 1) { | |
956 return time%this.buffer.duration; | |
957 } else { | |
958 return 0; | |
959 } | |
960 } else { | |
961 if (this.metric.listenHold) { | |
962 return time - this.metric.listenStart; | |
963 } else { | |
964 return 0; | |
965 } | |
966 } | |
967 } else { | |
968 return 0; | |
969 } | |
970 }; | |
971 | |
972 this.constructTrack = function(url) { | |
973 var request = new XMLHttpRequest(); | |
974 this.url = url; | |
975 request.open('GET',url,true); | |
976 request.responseType = 'arraybuffer'; | |
977 | |
978 var audioObj = this; | |
979 | |
980 // Create callback to decode the data asynchronously | |
981 request.onloadend = function() { | |
982 audioContext.decodeAudioData(request.response, function(decodedData) { | |
983 audioObj.buffer = decodedData; | |
984 audioObj.state = 1; | |
985 if (audioObj.specification.type != 'outsidereference') | |
986 {audioObj.interfaceDOM.enable();} | |
987 }, function(){ | |
988 // Should only be called if there was an error, but sometimes gets called continuously | |
989 // Check here if the error is genuine | |
990 if (audioObj.state == 0 || audioObj.buffer == undefined) { | |
991 // Genuine error | |
992 console.log('FATAL - Error loading buffer on '+audioObj.id); | |
993 if (request.status == 404) | |
994 { | |
995 console.log('FATAL - Fragment '+audioObj.id+' 404 error'); | |
996 console.log('URL: '+audioObj.url); | |
997 errorSessionDump('Fragment '+audioObj.id+' 404 error'); | |
998 } | |
999 } | |
1000 }); | |
1001 }; | |
1002 request.send(); | |
1003 }; | |
1004 | |
1005 this.exportXMLDOM = function() { | |
1006 var root = document.createElement('audioElement'); | |
1007 root.id = this.specification.id; | |
1008 root.setAttribute('url',this.url); | |
1009 var file = document.createElement('file'); | |
1010 file.setAttribute('sampleRate',this.buffer.sampleRate); | |
1011 file.setAttribute('channels',this.buffer.numberOfChannels); | |
1012 file.setAttribute('sampleCount',this.buffer.length); | |
1013 file.setAttribute('duration',this.buffer.duration); | |
1014 root.appendChild(file); | |
1015 if (this.specification.type != 'outsidereference') { | |
1016 root.appendChild(this.interfaceDOM.exportXMLDOM(this)); | |
1017 root.appendChild(this.commentDOM.exportXMLDOM(this)); | |
1018 if(this.specification.type == 'anchor') { | |
1019 root.setAttribute('anchor',true); | |
1020 } else if(this.specification.type == 'reference') { | |
1021 root.setAttribute('reference',true); | |
1022 } | |
1023 } | |
1024 root.appendChild(this.metric.exportXMLDOM()); | |
1025 return root; | |
1026 }; | |
1027 } | |
1028 | |
1029 function timer() | |
1030 { | |
1031 /* Timer object used in audioEngine to keep track of session timings | |
1032 * Uses the timer of the web audio API, so sample resolution | |
1033 */ | |
1034 this.testStarted = false; | |
1035 this.testStartTime = 0; | |
1036 this.testDuration = 0; | |
1037 this.minimumTestTime = 0; // No minimum test time | |
1038 this.startTest = function() | |
1039 { | |
1040 if (this.testStarted == false) | |
1041 { | |
1042 this.testStartTime = audioContext.currentTime; | |
1043 this.testStarted = true; | |
1044 this.updateTestTime(); | |
1045 audioEngineContext.metric.initialiseTest(); | |
1046 } | |
1047 }; | |
1048 this.stopTest = function() | |
1049 { | |
1050 if (this.testStarted) | |
1051 { | |
1052 this.testDuration = this.getTestTime(); | |
1053 this.testStarted = false; | |
1054 } else { | |
1055 console.log('ERR: Test tried to end before beginning'); | |
1056 } | |
1057 }; | |
1058 this.updateTestTime = function() | |
1059 { | |
1060 if (this.testStarted) | |
1061 { | |
1062 this.testDuration = audioContext.currentTime - this.testStartTime; | |
1063 } | |
1064 }; | |
1065 this.getTestTime = function() | |
1066 { | |
1067 this.updateTestTime(); | |
1068 return this.testDuration; | |
1069 }; | |
1070 } | |
1071 | |
1072 function sessionMetrics(engine) | |
1073 { | |
1074 /* Used by audioEngine to link to audioObjects to minimise the timer call timers; | |
1075 */ | |
1076 this.engine = engine; | |
1077 this.lastClicked = -1; | |
1078 this.data = -1; | |
1079 this.reset = function() { | |
1080 this.lastClicked = -1; | |
1081 this.data = -1; | |
1082 }; | |
1083 this.initialiseTest = function(){}; | |
1084 } | |
1085 | |
1086 function metricTracker(caller) | |
1087 { | |
1088 /* Custom object to track and collect metric data | |
1089 * Used only inside the audioObjects object. | |
1090 */ | |
1091 | |
1092 this.listenedTimer = 0; | |
1093 this.listenStart = 0; | |
1094 this.listenHold = false; | |
1095 this.initialPosition = -1; | |
1096 this.movementTracker = []; | |
1097 this.listenTracker =[]; | |
1098 this.wasListenedTo = false; | |
1099 this.wasMoved = false; | |
1100 this.hasComments = false; | |
1101 this.parent = caller; | |
1102 | |
1103 this.initialised = function(position) | |
1104 { | |
1105 if (this.initialPosition == -1) { | |
1106 this.initialPosition = position; | |
1107 } | |
1108 }; | |
1109 | |
1110 this.moved = function(time,position) | |
1111 { | |
1112 this.wasMoved = true; | |
1113 this.movementTracker[this.movementTracker.length] = [time, position]; | |
1114 }; | |
1115 | |
1116 this.startListening = function(time) | |
1117 { | |
1118 if (this.listenHold == false) | |
1119 { | |
1120 this.wasListenedTo = true; | |
1121 this.listenStart = time; | |
1122 this.listenHold = true; | |
1123 | |
1124 var evnt = document.createElement('event'); | |
1125 var testTime = document.createElement('testTime'); | |
1126 testTime.setAttribute('start',time); | |
1127 var bufferTime = document.createElement('bufferTime'); | |
1128 bufferTime.setAttribute('start',this.parent.getCurrentPosition()); | |
1129 evnt.appendChild(testTime); | |
1130 evnt.appendChild(bufferTime); | |
1131 this.listenTracker.push(evnt); | |
1132 | |
1133 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id | |
1134 } | |
1135 }; | |
1136 | |
1137 this.stopListening = function(time,bufferStopTime) | |
1138 { | |
1139 if (this.listenHold == true) | |
1140 { | |
1141 var diff = time - this.listenStart; | |
1142 this.listenedTimer += (diff); | |
1143 this.listenStart = 0; | |
1144 this.listenHold = false; | |
1145 | |
1146 var evnt = this.listenTracker[this.listenTracker.length-1]; | |
1147 var testTime = evnt.getElementsByTagName('testTime')[0]; | |
1148 var bufferTime = evnt.getElementsByTagName('bufferTime')[0]; | |
1149 testTime.setAttribute('stop',time); | |
1150 if (bufferStopTime == undefined) { | |
1151 bufferTime.setAttribute('stop',this.parent.getCurrentPosition()); | |
1152 } else { | |
1153 bufferTime.setAttribute('stop',bufferStopTime); | |
1154 } | |
1155 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id | |
1156 } | |
1157 }; | |
1158 | |
1159 this.exportXMLDOM = function() { | |
1160 var root = document.createElement('metric'); | |
1161 if (audioEngineContext.metric.enableElementTimer) { | |
1162 var mElementTimer = document.createElement('metricresult'); | |
1163 mElementTimer.setAttribute('name','enableElementTimer'); | |
1164 mElementTimer.textContent = this.listenedTimer; | |
1165 root.appendChild(mElementTimer); | |
1166 } | |
1167 if (audioEngineContext.metric.enableElementTracker) { | |
1168 var elementTrackerFull = document.createElement('metricResult'); | |
1169 elementTrackerFull.setAttribute('name','elementTrackerFull'); | |
1170 for (var k=0; k<this.movementTracker.length; k++) | |
1171 { | |
1172 var timePos = document.createElement('timePos'); | |
1173 timePos.id = k; | |
1174 var time = document.createElement('time'); | |
1175 time.textContent = this.movementTracker[k][0]; | |
1176 var position = document.createElement('position'); | |
1177 position.textContent = this.movementTracker[k][1]; | |
1178 timePos.appendChild(time); | |
1179 timePos.appendChild(position); | |
1180 elementTrackerFull.appendChild(timePos); | |
1181 } | |
1182 root.appendChild(elementTrackerFull); | |
1183 } | |
1184 if (audioEngineContext.metric.enableElementListenTracker) { | |
1185 var elementListenTracker = document.createElement('metricResult'); | |
1186 elementListenTracker.setAttribute('name','elementListenTracker'); | |
1187 for (var k=0; k<this.listenTracker.length; k++) { | |
1188 elementListenTracker.appendChild(this.listenTracker[k]); | |
1189 } | |
1190 root.appendChild(elementListenTracker); | |
1191 } | |
1192 if (audioEngineContext.metric.enableElementInitialPosition) { | |
1193 var elementInitial = document.createElement('metricResult'); | |
1194 elementInitial.setAttribute('name','elementInitialPosition'); | |
1195 elementInitial.textContent = this.initialPosition; | |
1196 root.appendChild(elementInitial); | |
1197 } | |
1198 if (audioEngineContext.metric.enableFlagListenedTo) { | |
1199 var flagListenedTo = document.createElement('metricResult'); | |
1200 flagListenedTo.setAttribute('name','elementFlagListenedTo'); | |
1201 flagListenedTo.textContent = this.wasListenedTo; | |
1202 root.appendChild(flagListenedTo); | |
1203 } | |
1204 if (audioEngineContext.metric.enableFlagMoved) { | |
1205 var flagMoved = document.createElement('metricResult'); | |
1206 flagMoved.setAttribute('name','elementFlagMoved'); | |
1207 flagMoved.textContent = this.wasMoved; | |
1208 root.appendChild(flagMoved); | |
1209 } | |
1210 if (audioEngineContext.metric.enableFlagComments) { | |
1211 var flagComments = document.createElement('metricResult'); | |
1212 flagComments.setAttribute('name','elementFlagComments'); | |
1213 if (this.parent.commentDOM == null) | |
1214 {flag.textContent = 'false';} | |
1215 else if (this.parent.commentDOM.textContent.length == 0) | |
1216 {flag.textContent = 'false';} | |
1217 else | |
1218 {flag.textContet = 'true';} | |
1219 root.appendChild(flagComments); | |
1220 } | |
1221 | |
1222 return root; | |
1223 }; | |
1224 } | |
1225 | |
1226 function randomiseOrder(input) | |
1227 { | |
1228 // This takes an array of information and randomises the order | |
1229 var N = input.length; | |
1230 | |
1231 var inputSequence = []; // For safety purposes: keep track of randomisation | |
1232 for (var counter = 0; counter < N; ++counter) | |
1233 inputSequence.push(counter) // Fill array | |
1234 var inputSequenceClone = inputSequence.slice(0); | |
1235 | |
1236 var holdArr = []; | |
1237 var outputSequence = []; | |
1238 for (var n=0; n<N; n++) | |
1239 { | |
1240 // First pick a random number | |
1241 var r = Math.random(); | |
1242 // Multiply and floor by the number of elements left | |
1243 r = Math.floor(r*input.length); | |
1244 // Pick out that element and delete from the array | |
1245 holdArr.push(input.splice(r,1)[0]); | |
1246 // Do the same with sequence | |
1247 outputSequence.push(inputSequence.splice(r,1)[0]); | |
1248 } | |
1249 console.log(inputSequenceClone.toString()); // print original array to console | |
1250 console.log(outputSequence.toString()); // print randomised array to console | |
1251 return holdArr; | |
1252 } | |
1253 | |
1254 function returnDateNode() | |
1255 { | |
1256 // Create an XML Node for the Date and Time a test was conducted | |
1257 // Structure is | |
1258 // <datetime> | |
1259 // <date year="##" month="##" day="##">DD/MM/YY</date> | |
1260 // <time hour="##" minute="##" sec="##">HH:MM:SS</time> | |
1261 // </datetime> | |
1262 var dateTime = new Date(); | |
1263 var year = document.createAttribute('year'); | |
1264 var month = document.createAttribute('month'); | |
1265 var day = document.createAttribute('day'); | |
1266 var hour = document.createAttribute('hour'); | |
1267 var minute = document.createAttribute('minute'); | |
1268 var secs = document.createAttribute('secs'); | |
1269 | |
1270 year.nodeValue = dateTime.getFullYear(); | |
1271 month.nodeValue = dateTime.getMonth()+1; | |
1272 day.nodeValue = dateTime.getDate(); | |
1273 hour.nodeValue = dateTime.getHours(); | |
1274 minute.nodeValue = dateTime.getMinutes(); | |
1275 secs.nodeValue = dateTime.getSeconds(); | |
1276 | |
1277 var hold = document.createElement("datetime"); | |
1278 var date = document.createElement("date"); | |
1279 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue; | |
1280 var time = document.createElement("time"); | |
1281 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue; | |
1282 | |
1283 date.setAttributeNode(year); | |
1284 date.setAttributeNode(month); | |
1285 date.setAttributeNode(day); | |
1286 time.setAttributeNode(hour); | |
1287 time.setAttributeNode(minute); | |
1288 time.setAttributeNode(secs); | |
1289 | |
1290 hold.appendChild(date); | |
1291 hold.appendChild(time); | |
1292 return hold | |
1293 | |
1294 } | |
1295 | |
1296 function testWaitIndicator() { | |
1297 if (audioEngineContext.checkAllReady() == false) { | |
1298 var hold = document.createElement("div"); | |
1299 hold.id = "testWaitIndicator"; | |
1300 hold.className = "indicator-box"; | |
1301 hold.style.zIndex = 3; | |
1302 var span = document.createElement("span"); | |
1303 span.textContent = "Please wait! Elements still loading"; | |
1304 hold.appendChild(span); | |
1305 var blank = document.createElement('div'); | |
1306 blank.className = 'testHalt'; | |
1307 blank.id = "testHaltBlank"; | |
1308 var body = document.getElementsByTagName('body')[0]; | |
1309 body.appendChild(hold); | |
1310 body.appendChild(blank); | |
1311 testWaitTimerIntervalHolder = setInterval(function(){ | |
1312 var ready = audioEngineContext.checkAllReady(); | |
1313 if (ready) { | |
1314 var elem = document.getElementById('testWaitIndicator'); | |
1315 var blank = document.getElementById('testHaltBlank'); | |
1316 var body = document.getElementsByTagName('body')[0]; | |
1317 body.removeChild(elem); | |
1318 body.removeChild(blank); | |
1319 clearInterval(testWaitTimerIntervalHolder); | |
1320 } | |
1321 },500,false); | |
1322 } | |
1323 } | |
1324 | |
1325 var testWaitTimerIntervalHolder = null; | |
1326 | |
1327 function Specification() { | |
1328 // Handles the decoding of the project specification XML into a simple JavaScript Object. | |
1329 | |
1330 this.interfaceType; | |
1331 this.commonInterface; | |
1332 this.projectReturn; | |
1333 this.randomiseOrder; | |
1334 this.collectMetrics; | |
1335 this.preTest; | |
1336 this.postTest; | |
1337 this.metrics =[]; | |
1338 | |
1339 this.audioHolders = []; | |
1340 | |
1341 this.decode = function() { | |
1342 // projectXML - DOM Parsed document | |
1343 this.projectXML = projectXML.childNodes[0]; | |
1344 var setupNode = projectXML.getElementsByTagName('setup')[0]; | |
1345 this.interfaceType = setupNode.getAttribute('interface'); | |
1346 this.projectReturn = setupNode.getAttribute('projectReturn'); | |
1347 if (setupNode.getAttribute('randomiseOrder') == "true") { | |
1348 this.randomiseOrder = true; | |
1349 } else {this.randomiseOrder = false;} | |
1350 if (setupNode.getAttribute('collectMetrics') == "true") { | |
1351 this.collectMetrics = true; | |
1352 } else {this.collectMetrics = false;} | |
1353 var metricCollection = setupNode.getElementsByTagName('Metric'); | |
1354 | |
1355 this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest')); | |
1356 this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest')); | |
1357 | |
1358 if (metricCollection.length > 0) { | |
1359 metricCollection = metricCollection[0].getElementsByTagName('metricEnable'); | |
1360 for (var i=0; i<metricCollection.length; i++) { | |
1361 this.metrics.push(new this.metricNode(metricCollection[i].textContent)); | |
1362 } | |
1363 } | |
1364 | |
1365 var commonInterfaceNode = setupNode.getElementsByTagName('interface'); | |
1366 if (commonInterfaceNode.length > 0) { | |
1367 commonInterfaceNode = commonInterfaceNode[0]; | |
1368 } else { | |
1369 commonInterfaceNode = undefined; | |
1370 } | |
1371 | |
1372 this.commonInterface = new function() { | |
1373 this.OptionNode = function(child) { | |
1374 this.type = child.nodeName; | |
1375 if (this.type == 'option') | |
1376 { | |
1377 this.name = child.getAttribute('name'); | |
1378 } | |
1379 else if (this.type == 'check') { | |
1380 this.check = child.getAttribute('name'); | |
1381 if (this.check == 'scalerange') { | |
1382 this.min = child.getAttribute('min'); | |
1383 this.max = child.getAttribute('max'); | |
1384 if (this.min == null) {this.min = 1;} | |
1385 else if (Number(this.min) > 1 && this.min != null) { | |
1386 this.min = Number(this.min)/100; | |
1387 } else { | |
1388 this.min = Number(this.min); | |
1389 } | |
1390 if (this.max == null) {this.max = 0;} | |
1391 else if (Number(this.max) > 1 && this.max != null) { | |
1392 this.max = Number(this.max)/100; | |
1393 } else { | |
1394 this.max = Number(this.max); | |
1395 } | |
1396 } | |
1397 } else if (this.type == 'anchor' || this.type == 'reference') { | |
1398 this.value = Number(child.textContent); | |
1399 this.enforce = child.getAttribute('enforce'); | |
1400 if (this.enforce == 'true') {this.enforce = true;} | |
1401 else {this.enforce = false;} | |
1402 } | |
1403 }; | |
1404 this.options = []; | |
1405 if (commonInterfaceNode != undefined) { | |
1406 var child = commonInterfaceNode.firstElementChild; | |
1407 while (child != undefined) { | |
1408 this.options.push(new this.OptionNode(child)); | |
1409 child = child.nextElementSibling; | |
1410 } | |
1411 } | |
1412 }; | |
1413 | |
1414 var audioHolders = projectXML.getElementsByTagName('audioHolder'); | |
1415 for (var i=0; i<audioHolders.length; i++) { | |
1416 this.audioHolders.push(new this.audioHolderNode(this,audioHolders[i])); | |
1417 } | |
1418 | |
1419 }; | |
1420 | |
1421 this.prepostNode = function(type,Collection) { | |
1422 this.type = type; | |
1423 this.options = []; | |
1424 | |
1425 this.OptionNode = function(child) { | |
1426 | |
1427 this.childOption = function(element) { | |
1428 this.type = 'option'; | |
1429 this.id = element.id; | |
1430 this.name = element.getAttribute('name'); | |
1431 this.text = element.textContent; | |
1432 }; | |
1433 | |
1434 this.type = child.nodeName; | |
1435 if (child.nodeName == "question") { | |
1436 this.id = child.id; | |
1437 this.mandatory; | |
1438 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;} | |
1439 else {this.mandatory = false;} | |
1440 this.question = child.textContent; | |
1441 if (child.getAttribute('boxsize') == null) { | |
1442 this.boxsize = 'normal'; | |
1443 } else { | |
1444 this.boxsize = child.getAttribute('boxsize'); | |
1445 } | |
1446 } else if (child.nodeName == "statement") { | |
1447 this.statement = child.textContent; | |
1448 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") { | |
1449 var element = child.firstElementChild; | |
1450 this.id = child.id; | |
1451 if (element == null) { | |
1452 console.log('Malformed' +child.nodeName+ 'entry'); | |
1453 this.statement = 'Malformed' +child.nodeName+ 'entry'; | |
1454 this.type = 'statement'; | |
1455 } else { | |
1456 this.options = []; | |
1457 while (element != null) { | |
1458 if (element.nodeName == 'statement' && this.statement == undefined){ | |
1459 this.statement = element.textContent; | |
1460 } else if (element.nodeName == 'option') { | |
1461 this.options.push(new this.childOption(element)); | |
1462 } | |
1463 element = element.nextElementSibling; | |
1464 } | |
1465 } | |
1466 } else if (child.nodeName == "number") { | |
1467 this.statement = child.textContent; | |
1468 this.id = child.id; | |
1469 this.min = child.getAttribute('min'); | |
1470 this.max = child.getAttribute('max'); | |
1471 this.step = child.getAttribute('step'); | |
1472 } | |
1473 }; | |
1474 | |
1475 // On construction: | |
1476 if (Collection.length != 0) { | |
1477 Collection = Collection[0]; | |
1478 if (Collection.childElementCount != 0) { | |
1479 var child = Collection.firstElementChild; | |
1480 this.options.push(new this.OptionNode(child)); | |
1481 while (child.nextElementSibling != null) { | |
1482 child = child.nextElementSibling; | |
1483 this.options.push(new this.OptionNode(child)); | |
1484 } | |
1485 } | |
1486 } | |
1487 }; | |
1488 | |
1489 this.metricNode = function(name) { | |
1490 this.enabled = name; | |
1491 }; | |
1492 | |
1493 this.audioHolderNode = function(parent,xml) { | |
1494 this.type = 'audioHolder'; | |
1495 this.presentedId = parent.audioHolders.length; | |
1496 this.interfaceNode = function(DOM) { | |
1497 var title = DOM.getElementsByTagName('title'); | |
1498 if (title.length == 0) {this.title = null;} | |
1499 else {this.title = title[0].textContent;} | |
1500 this.options = parent.commonInterface.options; | |
1501 var scale = DOM.getElementsByTagName('scale'); | |
1502 this.scale = []; | |
1503 for (var i=0; i<scale.length; i++) { | |
1504 var arr = [null, null]; | |
1505 arr[0] = scale[i].getAttribute('position'); | |
1506 arr[1] = scale[i].textContent; | |
1507 this.scale.push(arr); | |
1508 } | |
1509 }; | |
1510 | |
1511 this.audioElementNode = function(parent,xml) { | |
1512 this.url = xml.getAttribute('url'); | |
1513 this.id = xml.id; | |
1514 this.parent = parent; | |
1515 this.type = xml.getAttribute('type'); | |
1516 if (this.type == null) {this.type = "normal";} | |
1517 if (this.type == 'anchor') {this.anchor = true;} | |
1518 else {this.anchor = false;} | |
1519 if (this.type == 'reference') {this.reference = true;} | |
1520 else {this.reference = false;} | |
1521 | |
1522 this.marker = xml.getAttribute('marker'); | |
1523 if (this.marker == null) {this.marker = undefined;} | |
1524 | |
1525 if (this.anchor == true) { | |
1526 if (this.marker != undefined) {this.enforce = true;} | |
1527 else {this.enforce = enforceAnchor;} | |
1528 this.marker = anchor; | |
1529 } | |
1530 else if (this.reference == true) { | |
1531 if (this.marker != undefined) {this.enforce = true;} | |
1532 else {this.enforce = enforceReference;} | |
1533 this.marker = reference; | |
1534 } | |
1535 | |
1536 if (this.marker != undefined) { | |
1537 this.marker = Number(this.marker); | |
1538 if (this.marker > 1) {this.marker /= 100;} | |
1539 } | |
1540 }; | |
1541 | |
1542 this.commentQuestionNode = function(xml) { | |
1543 this.childOption = function(element) { | |
1544 this.type = 'option'; | |
1545 this.name = element.getAttribute('name'); | |
1546 this.text = element.textContent; | |
1547 }; | |
1548 this.id = xml.id; | |
1549 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;} | |
1550 else {this.mandatory = false;} | |
1551 this.type = xml.getAttribute('type'); | |
1552 if (this.type == undefined) {this.type = 'text';} | |
1553 switch (this.type) { | |
1554 case 'text': | |
1555 this.question = xml.textContent; | |
1556 break; | |
1557 case 'radio': | |
1558 var child = xml.firstElementChild; | |
1559 this.options = []; | |
1560 while (child != undefined) { | |
1561 if (child.nodeName == 'statement' && this.statement == undefined) { | |
1562 this.statement = child.textContent; | |
1563 } else if (child.nodeName == 'option') { | |
1564 this.options.push(new this.childOption(child)); | |
1565 } | |
1566 child = child.nextElementSibling; | |
1567 } | |
1568 break; | |
1569 case 'checkbox': | |
1570 var child = xml.firstElementChild; | |
1571 this.options = []; | |
1572 while (child != undefined) { | |
1573 if (child.nodeName == 'statement' && this.statement == undefined) { | |
1574 this.statement = child.textContent; | |
1575 } else if (child.nodeName == 'option') { | |
1576 this.options.push(new this.childOption(child)); | |
1577 } | |
1578 child = child.nextElementSibling; | |
1579 } | |
1580 break; | |
1581 } | |
1582 }; | |
1583 | |
1584 this.id = xml.id; | |
1585 this.hostURL = xml.getAttribute('hostURL'); | |
1586 this.sampleRate = xml.getAttribute('sampleRate'); | |
1587 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;} | |
1588 else {this.randomiseOrder = false;} | |
1589 this.repeatCount = xml.getAttribute('repeatCount'); | |
1590 if (xml.getAttribute('loop') == 'true') {this.loop = true;} | |
1591 else {this.loop == false;} | |
1592 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;} | |
1593 else {this.elementComments = false;} | |
1594 | |
1595 var anchor = xml.getElementsByTagName('anchor'); | |
1596 var enforceAnchor = false; | |
1597 if (anchor.length == 0) { | |
1598 // Find anchor in commonInterface; | |
1599 for (var i=0; i<parent.commonInterface.options.length; i++) { | |
1600 if(parent.commonInterface.options[i].type == 'anchor') { | |
1601 anchor = parent.commonInterface.options[i].value; | |
1602 enforceAnchor = parent.commonInterface.options[i].enforce; | |
1603 break; | |
1604 } | |
1605 } | |
1606 if (typeof(anchor) == "object") { | |
1607 anchor = null; | |
1608 } | |
1609 } else { | |
1610 anchor = anchor[0].textContent; | |
1611 } | |
1612 | |
1613 var reference = xml.getElementsByTagName('anchor'); | |
1614 var enforceReference = false; | |
1615 if (reference.length == 0) { | |
1616 // Find anchor in commonInterface; | |
1617 for (var i=0; i<parent.commonInterface.options.length; i++) { | |
1618 if(parent.commonInterface.options[i].type == 'reference') { | |
1619 reference = parent.commonInterface.options[i].value; | |
1620 enforceReference = parent.commonInterface.options[i].enforce; | |
1621 break; | |
1622 } | |
1623 } | |
1624 if (typeof(reference) == "object") { | |
1625 reference = null; | |
1626 } | |
1627 } else { | |
1628 reference = reference[0].textContent; | |
1629 } | |
1630 | |
1631 if (typeof(anchor) == 'number') { | |
1632 if (anchor > 1 && anchor < 100) {anchor /= 100.0;} | |
1633 } | |
1634 | |
1635 if (typeof(reference) == 'number') { | |
1636 if (reference > 1 && reference < 100) {reference /= 100.0;} | |
1637 } | |
1638 | |
1639 this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest')); | |
1640 this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest')); | |
1641 | |
1642 this.interfaces = []; | |
1643 var interfaceDOM = xml.getElementsByTagName('interface'); | |
1644 for (var i=0; i<interfaceDOM.length; i++) { | |
1645 this.interfaces.push(new this.interfaceNode(interfaceDOM[i])); | |
1646 } | |
1647 | |
1648 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix'); | |
1649 if (this.commentBoxPrefix.length != 0) { | |
1650 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent; | |
1651 } else { | |
1652 this.commentBoxPrefix = "Comment on track"; | |
1653 } | |
1654 | |
1655 this.audioElements =[]; | |
1656 var audioElementsDOM = xml.getElementsByTagName('audioElements'); | |
1657 this.outsideReference = null; | |
1658 for (var i=0; i<audioElementsDOM.length; i++) { | |
1659 if (audioElementsDOM[i].getAttribute('type') == 'outsidereference') { | |
1660 if (this.outsideReference == null) { | |
1661 this.outsideReference = new this.audioElementNode(this,audioElementsDOM[i]); | |
1662 } else { | |
1663 console.log('Error only one audioelement can be of type outsidereference per audioholder'); | |
1664 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i])); | |
1665 console.log('Element id '+audioElementsDOM[i].id+' made into normal node'); | |
1666 } | |
1667 } else { | |
1668 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i])); | |
1669 } | |
1670 } | |
1671 | |
1672 if (this.randomiseOrder) { | |
1673 this.audioElements = randomiseOrder(this.audioElements); | |
1674 } | |
1675 | |
1676 // Check only one anchor and one reference per audioNode | |
1677 var anchor = []; | |
1678 var reference = []; | |
1679 this.anchorId = null; | |
1680 this.referenceId = null; | |
1681 for (var i=0; i<this.audioElements.length; i++) | |
1682 { | |
1683 if (this.audioElements[i].anchor == true) {anchor.push(i);} | |
1684 if (this.audioElements[i].reference == true) {reference.push(i);} | |
1685 } | |
1686 | |
1687 if (anchor.length > 1) { | |
1688 console.log('Error - cannot have more than one anchor!'); | |
1689 console.log('Each anchor node will be a normal mode to continue the test'); | |
1690 for (var i=0; i<anchor.length; i++) | |
1691 { | |
1692 this.audioElements[anchor[i]].anchor = false; | |
1693 this.audioElements[anchor[i]].value = undefined; | |
1694 } | |
1695 } else {this.anchorId = anchor[0];} | |
1696 if (reference.length > 1) { | |
1697 console.log('Error - cannot have more than one anchor!'); | |
1698 console.log('Each anchor node will be a normal mode to continue the test'); | |
1699 for (var i=0; i<reference.length; i++) | |
1700 { | |
1701 this.audioElements[reference[i]].reference = false; | |
1702 this.audioElements[reference[i]].value = undefined; | |
1703 } | |
1704 } else {this.referenceId = reference[0];} | |
1705 | |
1706 this.commentQuestions = []; | |
1707 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion'); | |
1708 for (var i=0; i<commentQuestionsDOM.length; i++) { | |
1709 this.commentQuestions.push(new this.commentQuestionNode(commentQuestionsDOM[i])); | |
1710 } | |
1711 }; | |
1712 } | |
1713 | |
1714 function Interface(specificationObject) { | |
1715 // This handles the bindings between the interface and the audioEngineContext; | |
1716 this.specification = specificationObject; | |
1717 this.insertPoint = document.getElementById("topLevelBody"); | |
1718 | |
1719 // Bounded by interface!! | |
1720 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels | |
1721 // For example, APE returns the slider position normalised in a <value> tag. | |
1722 this.interfaceObjects = []; | |
1723 this.interfaceObject = function(){}; | |
1724 | |
1725 this.commentBoxes = []; | |
1726 this.elementCommentBox = function(audioObject) { | |
1727 var element = audioObject.specification; | |
1728 this.audioObject = audioObject; | |
1729 this.id = audioObject.id; | |
1730 var audioHolderObject = audioObject.specification.parent; | |
1731 // Create document objects to hold the comment boxes | |
1732 this.trackComment = document.createElement('div'); | |
1733 this.trackComment.className = 'comment-div'; | |
1734 this.trackComment.id = 'comment-div-'+audioObject.id; | |
1735 // Create a string next to each comment asking for a comment | |
1736 this.trackString = document.createElement('span'); | |
1737 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id; | |
1738 // Create the HTML5 comment box 'textarea' | |
1739 this.trackCommentBox = document.createElement('textarea'); | |
1740 this.trackCommentBox.rows = '4'; | |
1741 this.trackCommentBox.cols = '100'; | |
1742 this.trackCommentBox.name = 'trackComment'+audioObject.id; | |
1743 this.trackCommentBox.className = 'trackComment'; | |
1744 var br = document.createElement('br'); | |
1745 // Add to the holder. | |
1746 this.trackComment.appendChild(this.trackString); | |
1747 this.trackComment.appendChild(br); | |
1748 this.trackComment.appendChild(this.trackCommentBox); | |
1749 | |
1750 this.exportXMLDOM = function() { | |
1751 var root = document.createElement('comment'); | |
1752 if (this.audioObject.specification.parent.elementComments) { | |
1753 var question = document.createElement('question'); | |
1754 question.textContent = this.trackString.textContent; | |
1755 var response = document.createElement('response'); | |
1756 response.textContent = this.trackCommentBox.value; | |
1757 console.log("Comment frag-"+this.id+": "+response.textContent); | |
1758 root.appendChild(question); | |
1759 root.appendChild(response); | |
1760 } | |
1761 return root; | |
1762 }; | |
1763 }; | |
1764 | |
1765 this.commentQuestions = []; | |
1766 | |
1767 this.commentBox = function(commentQuestion) { | |
1768 this.specification = commentQuestion; | |
1769 // Create document objects to hold the comment boxes | |
1770 this.holder = document.createElement('div'); | |
1771 this.holder.className = 'comment-div'; | |
1772 // Create a string next to each comment asking for a comment | |
1773 this.string = document.createElement('span'); | |
1774 this.string.innerHTML = commentQuestion.question; | |
1775 // Create the HTML5 comment box 'textarea' | |
1776 this.textArea = document.createElement('textarea'); | |
1777 this.textArea.rows = '4'; | |
1778 this.textArea.cols = '100'; | |
1779 this.textArea.className = 'trackComment'; | |
1780 var br = document.createElement('br'); | |
1781 // Add to the holder. | |
1782 this.holder.appendChild(this.string); | |
1783 this.holder.appendChild(br); | |
1784 this.holder.appendChild(this.textArea); | |
1785 | |
1786 this.exportXMLDOM = function() { | |
1787 var root = document.createElement('comment'); | |
1788 root.id = this.specification.id; | |
1789 root.setAttribute('type',this.specification.type); | |
1790 root.textContent = this.textArea.value; | |
1791 console.log("Question: "+this.string.textContent); | |
1792 console.log("Response: "+root.textContent); | |
1793 return root; | |
1794 }; | |
1795 }; | |
1796 | |
1797 this.radioBox = function(commentQuestion) { | |
1798 this.specification = commentQuestion; | |
1799 // Create document objects to hold the comment boxes | |
1800 this.holder = document.createElement('div'); | |
1801 this.holder.className = 'comment-div'; | |
1802 // Create a string next to each comment asking for a comment | |
1803 this.string = document.createElement('span'); | |
1804 this.string.innerHTML = commentQuestion.statement; | |
1805 var br = document.createElement('br'); | |
1806 // Add to the holder. | |
1807 this.holder.appendChild(this.string); | |
1808 this.holder.appendChild(br); | |
1809 this.options = []; | |
1810 this.inputs = document.createElement('div'); | |
1811 this.span = document.createElement('div'); | |
1812 this.inputs.align = 'center'; | |
1813 this.inputs.style.marginLeft = '12px'; | |
1814 this.span.style.marginLeft = '12px'; | |
1815 this.span.align = 'center'; | |
1816 this.span.style.marginTop = '15px'; | |
1817 | |
1818 var optCount = commentQuestion.options.length; | |
1819 var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px'; | |
1820 console.log(spanMargin); | |
1821 for (var i=0; i<optCount; i++) | |
1822 { | |
1823 var div = document.createElement('div'); | |
1824 div.style.width = '100px'; | |
1825 div.style.float = 'left'; | |
1826 div.style.marginRight = spanMargin; | |
1827 div.style.marginLeft = spanMargin; | |
1828 var input = document.createElement('input'); | |
1829 input.type = 'radio'; | |
1830 input.name = commentQuestion.id; | |
1831 input.setAttribute('setvalue',commentQuestion.options[i].name); | |
1832 input.className = 'comment-radio'; | |
1833 div.appendChild(input); | |
1834 this.inputs.appendChild(div); | |
1835 | |
1836 | |
1837 div = document.createElement('div'); | |
1838 div.style.width = '100px'; | |
1839 div.style.float = 'left'; | |
1840 div.style.marginRight = spanMargin; | |
1841 div.style.marginLeft = spanMargin; | |
1842 div.align = 'center'; | |
1843 var span = document.createElement('span'); | |
1844 span.textContent = commentQuestion.options[i].text; | |
1845 span.className = 'comment-radio-span'; | |
1846 div.appendChild(span); | |
1847 this.span.appendChild(div); | |
1848 this.options.push(input); | |
1849 } | |
1850 this.holder.appendChild(this.span); | |
1851 this.holder.appendChild(this.inputs); | |
1852 | |
1853 this.exportXMLDOM = function() { | |
1854 var root = document.createElement('comment'); | |
1855 root.id = this.specification.id; | |
1856 root.setAttribute('type',this.specification.type); | |
1857 var question = document.createElement('question'); | |
1858 question.textContent = this.string.textContent; | |
1859 var response = document.createElement('response'); | |
1860 var i=0; | |
1861 while(this.options[i].checked == false) { | |
1862 i++; | |
1863 if (i >= this.options.length) { | |
1864 break; | |
1865 } | |
1866 } | |
1867 if (i >= this.options.length) { | |
1868 response.textContent = 'null'; | |
1869 } else { | |
1870 response.textContent = this.options[i].getAttribute('setvalue'); | |
1871 response.setAttribute('number',i); | |
1872 } | |
1873 console.log('Comment: '+question.textContent); | |
1874 console.log('Response: '+response.textContent); | |
1875 root.appendChild(question); | |
1876 root.appendChild(response); | |
1877 return root; | |
1878 }; | |
1879 }; | |
1880 | |
1881 this.checkboxBox = function(commentQuestion) { | |
1882 this.specification = commentQuestion; | |
1883 // Create document objects to hold the comment boxes | |
1884 this.holder = document.createElement('div'); | |
1885 this.holder.className = 'comment-div'; | |
1886 // Create a string next to each comment asking for a comment | |
1887 this.string = document.createElement('span'); | |
1888 this.string.innerHTML = commentQuestion.statement; | |
1889 var br = document.createElement('br'); | |
1890 // Add to the holder. | |
1891 this.holder.appendChild(this.string); | |
1892 this.holder.appendChild(br); | |
1893 this.options = []; | |
1894 this.inputs = document.createElement('div'); | |
1895 this.span = document.createElement('div'); | |
1896 this.inputs.align = 'center'; | |
1897 this.inputs.style.marginLeft = '12px'; | |
1898 this.span.style.marginLeft = '12px'; | |
1899 this.span.align = 'center'; | |
1900 this.span.style.marginTop = '15px'; | |
1901 | |
1902 var optCount = commentQuestion.options.length; | |
1903 var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px'; | |
1904 console.log(spanMargin); | |
1905 for (var i=0; i<optCount; i++) | |
1906 { | |
1907 var div = document.createElement('div'); | |
1908 div.style.width = '100px'; | |
1909 div.style.float = 'left'; | |
1910 div.style.marginRight = spanMargin; | |
1911 div.style.marginLeft = spanMargin; | |
1912 var input = document.createElement('input'); | |
1913 input.type = 'checkbox'; | |
1914 input.name = commentQuestion.id; | |
1915 input.setAttribute('setvalue',commentQuestion.options[i].name); | |
1916 input.className = 'comment-radio'; | |
1917 div.appendChild(input); | |
1918 this.inputs.appendChild(div); | |
1919 | |
1920 | |
1921 div = document.createElement('div'); | |
1922 div.style.width = '100px'; | |
1923 div.style.float = 'left'; | |
1924 div.style.marginRight = spanMargin; | |
1925 div.style.marginLeft = spanMargin; | |
1926 div.align = 'center'; | |
1927 var span = document.createElement('span'); | |
1928 span.textContent = commentQuestion.options[i].text; | |
1929 span.className = 'comment-radio-span'; | |
1930 div.appendChild(span); | |
1931 this.span.appendChild(div); | |
1932 this.options.push(input); | |
1933 } | |
1934 this.holder.appendChild(this.span); | |
1935 this.holder.appendChild(this.inputs); | |
1936 | |
1937 this.exportXMLDOM = function() { | |
1938 var root = document.createElement('comment'); | |
1939 root.id = this.specification.id; | |
1940 root.setAttribute('type',this.specification.type); | |
1941 var question = document.createElement('question'); | |
1942 question.textContent = this.string.textContent; | |
1943 root.appendChild(question); | |
1944 console.log('Comment: '+question.textContent); | |
1945 for (var i=0; i<this.options.length; i++) { | |
1946 var response = document.createElement('response'); | |
1947 response.textContent = this.options[i].checked; | |
1948 response.setAttribute('name',this.options[i].getAttribute('setvalue')); | |
1949 root.appendChild(response); | |
1950 console.log('Response '+response.getAttribute('name') +': '+response.textContent); | |
1951 } | |
1952 return root; | |
1953 }; | |
1954 }; | |
1955 | |
1956 this.createCommentBox = function(audioObject) { | |
1957 var node = new this.elementCommentBox(audioObject); | |
1958 this.commentBoxes.push(node); | |
1959 audioObject.commentDOM = node; | |
1960 return node; | |
1961 }; | |
1962 | |
1963 this.sortCommentBoxes = function() { | |
1964 var holder = []; | |
1965 while (this.commentBoxes.length > 0) { | |
1966 var node = this.commentBoxes.pop(0); | |
1967 holder[node.id] = node; | |
1968 } | |
1969 this.commentBoxes = holder; | |
1970 }; | |
1971 | |
1972 this.showCommentBoxes = function(inject, sort) { | |
1973 if (sort) {interfaceContext.sortCommentBoxes();} | |
1974 for (var i=0; i<interfaceContext.commentBoxes.length; i++) { | |
1975 inject.appendChild(this.commentBoxes[i].trackComment); | |
1976 } | |
1977 }; | |
1978 | |
1979 this.deleteCommentBoxes = function() { | |
1980 this.commentBoxes = []; | |
1981 }; | |
1982 | |
1983 this.createCommentQuestion = function(element) { | |
1984 var node; | |
1985 if (element.type == 'text') { | |
1986 node = new this.commentBox(element); | |
1987 } else if (element.type == 'radio') { | |
1988 node = new this.radioBox(element); | |
1989 } else if (element.type == 'checkbox') { | |
1990 node = new this.checkboxBox(element); | |
1991 } | |
1992 this.commentQuestions.push(node); | |
1993 return node; | |
1994 }; | |
1995 | |
1996 this.deleteCommentQuestions = function() | |
1997 { | |
1998 this.commentQuestions = []; | |
1999 }; | |
2000 | |
2001 this.playhead = new function() | |
2002 { | |
2003 this.object = document.createElement('div'); | |
2004 this.object.className = 'playhead'; | |
2005 this.object.align = 'left'; | |
2006 var curTime = document.createElement('div'); | |
2007 curTime.style.width = '50px'; | |
2008 this.curTimeSpan = document.createElement('span'); | |
2009 this.curTimeSpan.textContent = '00:00'; | |
2010 curTime.appendChild(this.curTimeSpan); | |
2011 this.object.appendChild(curTime); | |
2012 this.scrubberTrack = document.createElement('div'); | |
2013 this.scrubberTrack.className = 'playhead-scrub-track'; | |
2014 | |
2015 this.scrubberHead = document.createElement('div'); | |
2016 this.scrubberHead.id = 'playhead-scrubber'; | |
2017 this.scrubberTrack.appendChild(this.scrubberHead); | |
2018 this.object.appendChild(this.scrubberTrack); | |
2019 | |
2020 this.timePerPixel = 0; | |
2021 this.maxTime = 0; | |
2022 | |
2023 this.playbackObject; | |
2024 | |
2025 this.setTimePerPixel = function(audioObject) { | |
2026 //maxTime must be in seconds | |
2027 this.playbackObject = audioObject; | |
2028 this.maxTime = audioObject.buffer.duration; | |
2029 var width = 490; //500 - 10, 5 each side of the tracker head | |
2030 this.timePerPixel = this.maxTime/490; | |
2031 if (this.maxTime < 60) { | |
2032 this.curTimeSpan.textContent = '0.00'; | |
2033 } else { | |
2034 this.curTimeSpan.textContent = '00:00'; | |
2035 } | |
2036 }; | |
2037 | |
2038 this.update = function() { | |
2039 // Update the playhead position, startPlay must be called | |
2040 if (this.timePerPixel > 0) { | |
2041 var time = this.playbackObject.getCurrentPosition(); | |
2042 if (time > 0) { | |
2043 var width = 490; | |
2044 var pix = Math.floor(time/this.timePerPixel); | |
2045 this.scrubberHead.style.left = pix+'px'; | |
2046 if (this.maxTime > 60.0) { | |
2047 var secs = time%60; | |
2048 var mins = Math.floor((time-secs)/60); | |
2049 secs = secs.toString(); | |
2050 secs = secs.substr(0,2); | |
2051 mins = mins.toString(); | |
2052 this.curTimeSpan.textContent = mins+':'+secs; | |
2053 } else { | |
2054 time = time.toString(); | |
2055 this.curTimeSpan.textContent = time.substr(0,4); | |
2056 } | |
2057 } else { | |
2058 this.scrubberHead.style.left = '0px'; | |
2059 if (this.maxTime < 60) { | |
2060 this.curTimeSpan.textContent = '0.00'; | |
2061 } else { | |
2062 this.curTimeSpan.textContent = '00:00'; | |
2063 } | |
2064 } | |
2065 } | |
2066 }; | |
2067 | |
2068 this.interval = undefined; | |
2069 | |
2070 this.start = function() { | |
2071 if (this.playbackObject != undefined && this.interval == undefined) { | |
2072 if (this.maxTime < 60) { | |
2073 this.interval = setInterval(function(){interfaceContext.playhead.update();},10); | |
2074 } else { | |
2075 this.interval = setInterval(function(){interfaceContext.playhead.update();},100); | |
2076 } | |
2077 } | |
2078 }; | |
2079 this.stop = function() { | |
2080 clearInterval(this.interval); | |
2081 this.interval = undefined; | |
2082 if (this.maxTime < 60) { | |
2083 this.curTimeSpan.textContent = '0.00'; | |
2084 } else { | |
2085 this.curTimeSpan.textContent = '00:00'; | |
2086 } | |
2087 }; | |
2088 }; | |
2089 | |
2090 // Global Checkers | |
2091 // These functions will help enforce the checkers | |
2092 this.checkHiddenAnchor = function() | |
2093 { | |
2094 var audioHolder = testState.currentStateMap[testState.currentIndex]; | |
2095 if (audioHolder.anchorId != null) | |
2096 { | |
2097 var audioObject = audioEngineContext.audioObjects[audioHolder.anchorId]; | |
2098 if (audioObject.interfaceDOM.getValue() > audioObject.specification.marker && audioObject.interfaceDOM.enforce == true) | |
2099 { | |
2100 // Anchor is not set below | |
2101 console.log('Anchor node not below marker value'); | |
2102 alert('Please keep listening'); | |
2103 return false; | |
2104 } | |
2105 } | |
2106 return true; | |
2107 }; | |
2108 | |
2109 this.checkHiddenReference = function() | |
2110 { | |
2111 var audioHolder = testState.currentStateMap[testState.currentIndex]; | |
2112 if (audioHolder.referenceId != null) | |
2113 { | |
2114 var audioObject = audioEngineContext.audioObjects[audioHolder.referenceId]; | |
2115 if (audioObject.interfaceDOM.getValue() < audioObject.specification.marker && audioObject.interfaceDOM.enforce == true) | |
2116 { | |
2117 // Anchor is not set below | |
2118 console.log('Reference node not above marker value'); | |
2119 alert('Please keep listening'); | |
2120 return false; | |
2121 } | |
2122 } | |
2123 return true; | |
2124 }; | |
2125 } |