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