Mercurial > hg > webaudioevaluationtool
comparison core.js @ 755:c73996a0fb21
Bug #1486: Fixed rogue '+' appearing in move slider alert. Unlabelled axis have default of 'Axis ' and their index.
author | Nicholas Jillings <nicholas.jillings@eecs.qmul.ac.uk> |
---|---|
date | Thu, 17 Dec 2015 13:03:39 +0000 |
parents | |
children | 46acb0963059 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 755:c73996a0fb21 |
---|---|
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 // Add a prototype to the bufferNode to hold the desired LINEAR gain | |
23 AudioBuffer.prototype.gain = undefined; | |
24 // Add a prototype to the bufferNode to hold the computed LUFS loudness | |
25 AudioBuffer.prototype.lufs = undefined; | |
26 | |
27 window.onload = function() { | |
28 // Function called once the browser has loaded all files. | |
29 // This should perform any initial commands such as structure / loading documents | |
30 | |
31 // Create a web audio API context | |
32 // Fixed for cross-browser support | |
33 var AudioContext = window.AudioContext || window.webkitAudioContext; | |
34 audioContext = new AudioContext; | |
35 | |
36 // Create test state | |
37 testState = new stateMachine(); | |
38 | |
39 // Create the popup interface object | |
40 popup = new interfacePopup(); | |
41 | |
42 // Create the specification object | |
43 specification = new Specification(); | |
44 | |
45 // Create the interface object | |
46 interfaceContext = new Interface(specification); | |
47 // Define window callbacks for interface | |
48 window.onresize = function(event){interfaceContext.resizeWindow(event);}; | |
49 }; | |
50 | |
51 function loadProjectSpec(url) { | |
52 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data | |
53 // If url is null, request client to upload project XML document | |
54 var r = new XMLHttpRequest(); | |
55 r.open('GET',url,true); | |
56 r.onload = function() { | |
57 loadProjectSpecCallback(r.response); | |
58 }; | |
59 r.send(); | |
60 }; | |
61 | |
62 function loadProjectSpecCallback(response) { | |
63 // Function called after asynchronous download of XML project specification | |
64 //var decode = $.parseXML(response); | |
65 //projectXML = $(decode); | |
66 | |
67 var parse = new DOMParser(); | |
68 projectXML = parse.parseFromString(response,'text/xml'); | |
69 | |
70 // Build the specification | |
71 specification.decode(projectXML); | |
72 | |
73 // Detect the interface to use and load the relevant javascripts. | |
74 var interfaceJS = document.createElement('script'); | |
75 interfaceJS.setAttribute("type","text/javascript"); | |
76 if (specification.interfaceType == 'APE') { | |
77 interfaceJS.setAttribute("src","ape.js"); | |
78 | |
79 // APE comes with a css file | |
80 var css = document.createElement('link'); | |
81 css.rel = 'stylesheet'; | |
82 css.type = 'text/css'; | |
83 css.href = 'ape.css'; | |
84 | |
85 document.getElementsByTagName("head")[0].appendChild(css); | |
86 } else if (specification.interfaceType == "MUSHRA") | |
87 { | |
88 interfaceJS.setAttribute("src","mushra.js"); | |
89 | |
90 // MUSHRA comes with a css file | |
91 var css = document.createElement('link'); | |
92 css.rel = 'stylesheet'; | |
93 css.type = 'text/css'; | |
94 css.href = 'mushra.css'; | |
95 | |
96 document.getElementsByTagName("head")[0].appendChild(css); | |
97 } | |
98 document.getElementsByTagName("head")[0].appendChild(interfaceJS); | |
99 | |
100 // Create the audio engine object | |
101 audioEngineContext = new AudioEngine(specification); | |
102 | |
103 testState.stateMap.push(specification.preTest); | |
104 | |
105 $(specification.audioHolders).each(function(index,elem){ | |
106 testState.stateMap.push(elem); | |
107 $(elem.audioElements).each(function(i,audioElem){ | |
108 var URL = audioElem.parent.hostURL + audioElem.url; | |
109 var buffer = null; | |
110 for (var i=0; i<audioEngineContext.buffers.length; i++) | |
111 { | |
112 if (URL == audioEngineContext.buffers[i].url) | |
113 { | |
114 buffer = audioEngineContext.buffers[i]; | |
115 break; | |
116 } | |
117 } | |
118 if (buffer == null) | |
119 { | |
120 buffer = new audioEngineContext.bufferObj(); | |
121 buffer.getMedia(URL); | |
122 audioEngineContext.buffers.push(buffer); | |
123 } | |
124 }); | |
125 }); | |
126 | |
127 testState.stateMap.push(specification.postTest); | |
128 } | |
129 | |
130 function createProjectSave(destURL) { | |
131 // Save the data from interface into XML and send to destURL | |
132 // If destURL is null then download XML in client | |
133 // Now time to render file locally | |
134 var xmlDoc = interfaceXMLSave(); | |
135 var parent = document.createElement("div"); | |
136 parent.appendChild(xmlDoc); | |
137 var file = [parent.innerHTML]; | |
138 if (destURL == "null" || destURL == undefined) { | |
139 var bb = new Blob(file,{type : 'application/xml'}); | |
140 var dnlk = window.URL.createObjectURL(bb); | |
141 var a = document.createElement("a"); | |
142 a.hidden = ''; | |
143 a.href = dnlk; | |
144 a.download = "save.xml"; | |
145 a.textContent = "Save File"; | |
146 | |
147 popup.showPopup(); | |
148 popup.popupContent.innerHTML = null; | |
149 popup.popupContent.appendChild(a); | |
150 } else { | |
151 var xmlhttp = new XMLHttpRequest; | |
152 xmlhttp.open("POST",destURL,true); | |
153 xmlhttp.setRequestHeader('Content-Type', 'text/xml'); | |
154 xmlhttp.onerror = function(){ | |
155 console.log('Error saving file to server! Presenting download locally'); | |
156 createProjectSave(null); | |
157 }; | |
158 xmlhttp.onreadystatechange = function() { | |
159 console.log(xmlhttp.status); | |
160 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) { | |
161 createProjectSave(null); | |
162 } else { | |
163 if (xmlhttp.responseXML == null) | |
164 { | |
165 return createProjectSave(null); | |
166 } | |
167 var response = xmlhttp.responseXML.childNodes[0]; | |
168 if (response.getAttribute('state') == "OK") | |
169 { | |
170 var file = response.getElementsByTagName('file')[0]; | |
171 console.log('Save OK: Filename '+file.textContent+','+file.getAttribute('bytes')+'B'); | |
172 popup.showPopup(); | |
173 popup.popupContent.innerHTML = null; | |
174 popup.popupContent.textContent = "Thank you!"; | |
175 } else { | |
176 var message = response.getElementsByTagName('message')[0]; | |
177 errorSessionDump(message.textContent); | |
178 } | |
179 } | |
180 }; | |
181 xmlhttp.send(file); | |
182 } | |
183 } | |
184 | |
185 function errorSessionDump(msg){ | |
186 // Create the partial interface XML save | |
187 // Include error node with message on why the dump occured | |
188 var xmlDoc = interfaceXMLSave(); | |
189 var err = document.createElement('error'); | |
190 err.textContent = msg; | |
191 xmlDoc.appendChild(err); | |
192 var parent = document.createElement("div"); | |
193 parent.appendChild(xmlDoc); | |
194 var file = [parent.innerHTML]; | |
195 var bb = new Blob(file,{type : 'application/xml'}); | |
196 var dnlk = window.URL.createObjectURL(bb); | |
197 var a = document.createElement("a"); | |
198 a.hidden = ''; | |
199 a.href = dnlk; | |
200 a.download = "save.xml"; | |
201 a.textContent = "Save File"; | |
202 | |
203 popup.showPopup(); | |
204 popup.popupContent.innerHTML = "ERROR : "+msg; | |
205 popup.popupContent.appendChild(a); | |
206 } | |
207 | |
208 // Only other global function which must be defined in the interface class. Determines how to create the XML document. | |
209 function interfaceXMLSave(){ | |
210 // Create the XML string to be exported with results | |
211 var xmlDoc = document.createElement("BrowserEvaluationResult"); | |
212 var projectDocument = specification.projectXML; | |
213 projectDocument.setAttribute('file-name',url); | |
214 xmlDoc.appendChild(projectDocument); | |
215 xmlDoc.appendChild(returnDateNode()); | |
216 xmlDoc.appendChild(interfaceContext.returnNavigator()); | |
217 for (var i=0; i<testState.stateResults.length; i++) | |
218 { | |
219 xmlDoc.appendChild(testState.stateResults[i]); | |
220 } | |
221 | |
222 return xmlDoc; | |
223 } | |
224 | |
225 function linearToDecibel(gain) | |
226 { | |
227 return 20.0*Math.log10(gain); | |
228 } | |
229 | |
230 function decibelToLinear(gain) | |
231 { | |
232 return Math.pow(10,gain/20.0); | |
233 } | |
234 | |
235 function interfacePopup() { | |
236 // Creates an object to manage the popup | |
237 this.popup = null; | |
238 this.popupContent = null; | |
239 this.popupTitle = null; | |
240 this.popupResponse = null; | |
241 this.buttonProceed = null; | |
242 this.buttonPrevious = null; | |
243 this.popupOptions = null; | |
244 this.currentIndex = null; | |
245 this.responses = null; | |
246 $(window).keypress(function(e){ | |
247 if (e.keyCode == 13 && popup.popup.style.visibility == 'visible') | |
248 { | |
249 console.log(e); | |
250 popup.buttonProceed.onclick(); | |
251 e.preventDefault(); | |
252 } | |
253 }); | |
254 | |
255 this.createPopup = function(){ | |
256 // Create popup window interface | |
257 var insertPoint = document.getElementById("topLevelBody"); | |
258 var blank = document.createElement('div'); | |
259 blank.className = 'testHalt'; | |
260 | |
261 this.popup = document.createElement('div'); | |
262 this.popup.id = 'popupHolder'; | |
263 this.popup.className = 'popupHolder'; | |
264 this.popup.style.position = 'absolute'; | |
265 this.popup.style.left = (window.innerWidth/2)-250 + 'px'; | |
266 this.popup.style.top = (window.innerHeight/2)-125 + 'px'; | |
267 | |
268 this.popupContent = document.createElement('div'); | |
269 this.popupContent.id = 'popupContent'; | |
270 this.popupContent.style.marginTop = '20px'; | |
271 this.popupContent.style.marginBottom = '5px'; | |
272 this.popup.appendChild(this.popupContent); | |
273 | |
274 var titleHolder = document.createElement('div'); | |
275 titleHolder.id = 'popupTitleHolder'; | |
276 titleHolder.align = 'center'; | |
277 titleHolder.style.width = 'inherit'; | |
278 titleHolder.style.minHeight = '25px'; | |
279 titleHolder.style.maxHeight = '250px'; | |
280 titleHolder.style.overflow = 'auto'; | |
281 titleHolder.style.marginBottom = '5px'; | |
282 | |
283 this.popupTitle = document.createElement('span'); | |
284 this.popupTitle.id = 'popupTitle'; | |
285 titleHolder.appendChild(this.popupTitle); | |
286 this.popupContent.appendChild(titleHolder); | |
287 | |
288 this.popupResponse = document.createElement('div'); | |
289 this.popupResponse.id = 'popupResponse'; | |
290 this.popupResponse.align = 'center'; | |
291 this.popupResponse.style.width = 'inherit'; | |
292 this.popupResponse.style.minHeight = '50px'; | |
293 this.popupResponse.style.maxHeight = '320px'; | |
294 this.popupResponse.style.overflow = 'auto'; | |
295 this.popupContent.appendChild(this.popupResponse); | |
296 | |
297 this.buttonProceed = document.createElement('button'); | |
298 this.buttonProceed.className = 'popupButton'; | |
299 this.buttonProceed.position = 'relative'; | |
300 this.buttonProceed.style.left = '390px'; | |
301 this.buttonProceed.innerHTML = 'Next'; | |
302 this.buttonProceed.onclick = function(){popup.proceedClicked();}; | |
303 | |
304 this.buttonPrevious = document.createElement('button'); | |
305 this.buttonPrevious.className = 'popupButton'; | |
306 this.buttonPrevious.position = 'relative'; | |
307 this.buttonPrevious.style.left = '10px'; | |
308 this.buttonPrevious.innerHTML = 'Back'; | |
309 this.buttonPrevious.onclick = function(){popup.previousClick();}; | |
310 | |
311 this.popupContent.appendChild(this.buttonPrevious); | |
312 this.popupContent.appendChild(this.buttonProceed); | |
313 | |
314 this.popup.style.zIndex = -1; | |
315 this.popup.style.visibility = 'hidden'; | |
316 blank.style.zIndex = -2; | |
317 blank.style.visibility = 'hidden'; | |
318 insertPoint.appendChild(this.popup); | |
319 insertPoint.appendChild(blank); | |
320 }; | |
321 | |
322 this.showPopup = function(){ | |
323 if (this.popup == null) { | |
324 this.createPopup(); | |
325 } | |
326 this.popup.style.zIndex = 3; | |
327 this.popup.style.visibility = 'visible'; | |
328 var blank = document.getElementsByClassName('testHalt')[0]; | |
329 blank.style.zIndex = 2; | |
330 blank.style.visibility = 'visible'; | |
331 }; | |
332 | |
333 this.hidePopup = function(){ | |
334 this.popup.style.zIndex = -1; | |
335 this.popup.style.visibility = 'hidden'; | |
336 var blank = document.getElementsByClassName('testHalt')[0]; | |
337 blank.style.zIndex = -2; | |
338 blank.style.visibility = 'hidden'; | |
339 this.buttonPrevious.style.visibility = 'inherit'; | |
340 }; | |
341 | |
342 this.postNode = function() { | |
343 // This will take the node from the popupOptions and display it | |
344 var node = this.popupOptions[this.currentIndex]; | |
345 this.popupResponse.innerHTML = null; | |
346 if (node.type == 'statement') { | |
347 this.popupTitle.textContent = null; | |
348 var statement = document.createElement('span'); | |
349 statement.textContent = node.statement; | |
350 this.popupResponse.appendChild(statement); | |
351 } else if (node.type == 'question') { | |
352 this.popupTitle.textContent = node.question; | |
353 var textArea = document.createElement('textarea'); | |
354 switch (node.boxsize) { | |
355 case 'small': | |
356 textArea.cols = "20"; | |
357 textArea.rows = "1"; | |
358 break; | |
359 case 'normal': | |
360 textArea.cols = "30"; | |
361 textArea.rows = "2"; | |
362 break; | |
363 case 'large': | |
364 textArea.cols = "40"; | |
365 textArea.rows = "5"; | |
366 break; | |
367 case 'huge': | |
368 textArea.cols = "50"; | |
369 textArea.rows = "10"; | |
370 break; | |
371 } | |
372 this.popupResponse.appendChild(textArea); | |
373 textArea.focus(); | |
374 } else if (node.type == 'checkbox') { | |
375 this.popupTitle.textContent = node.statement; | |
376 var optHold = this.popupResponse; | |
377 for (var i=0; i<node.options.length; i++) { | |
378 var option = node.options[i]; | |
379 var input = document.createElement('input'); | |
380 input.id = option.id; | |
381 input.type = 'checkbox'; | |
382 var span = document.createElement('span'); | |
383 span.textContent = option.text; | |
384 var hold = document.createElement('div'); | |
385 hold.setAttribute('name','option'); | |
386 hold.style.padding = '4px'; | |
387 hold.appendChild(input); | |
388 hold.appendChild(span); | |
389 optHold.appendChild(hold); | |
390 } | |
391 } else if (node.type == 'radio') { | |
392 this.popupTitle.textContent = node.statement; | |
393 var optHold = this.popupResponse; | |
394 for (var i=0; i<node.options.length; i++) { | |
395 var option = node.options[i]; | |
396 var input = document.createElement('input'); | |
397 input.id = option.name; | |
398 input.type = 'radio'; | |
399 input.name = node.id; | |
400 var span = document.createElement('span'); | |
401 span.textContent = option.text; | |
402 var hold = document.createElement('div'); | |
403 hold.setAttribute('name','option'); | |
404 hold.style.padding = '4px'; | |
405 hold.appendChild(input); | |
406 hold.appendChild(span); | |
407 optHold.appendChild(hold); | |
408 } | |
409 } else if (node.type == 'number') { | |
410 this.popupTitle.textContent = node.statement; | |
411 var input = document.createElement('input'); | |
412 input.type = 'textarea'; | |
413 if (node.min != null) {input.min = node.min;} | |
414 if (node.max != null) {input.max = node.max;} | |
415 if (node.step != null) {input.step = node.step;} | |
416 this.popupResponse.appendChild(input); | |
417 } | |
418 var content_height = Number(this.popup.offsetHeight.toFixed()); | |
419 content_height -= Number(this.popupContent.offsetHeight.toFixed()); | |
420 content_height -=Number(this.buttonProceed.offsetHeight.toFixed()); | |
421 content_height = content_height + "px"; | |
422 this.buttonProceed.style.top = content_height; | |
423 this.buttonPrevious.style.top = content_height; | |
424 if(this.currentIndex+1 == this.popupOptions.length) { | |
425 if (this.responses.nodeName == "PRETEST") { | |
426 this.buttonProceed.textContent = 'Start'; | |
427 } else { | |
428 this.buttonProceed.textContent = 'Submit'; | |
429 } | |
430 } else { | |
431 this.buttonProceed.textContent = 'Next'; | |
432 } | |
433 if(this.currentIndex > 0) | |
434 this.buttonPrevious.style.visibility = 'visible'; | |
435 else | |
436 this.buttonPrevious.style.visibility = 'hidden'; | |
437 }; | |
438 | |
439 this.initState = function(node) { | |
440 //Call this with your preTest and postTest nodes when needed to | |
441 // initialise the popup procedure. | |
442 this.popupOptions = node.options; | |
443 if (this.popupOptions.length > 0) { | |
444 if (node.type == 'pretest') { | |
445 this.responses = document.createElement('PreTest'); | |
446 } else if (node.type == 'posttest') { | |
447 this.responses = document.createElement('PostTest'); | |
448 } else { | |
449 console.log ('WARNING - popup node neither pre or post!'); | |
450 this.responses = document.createElement('responses'); | |
451 } | |
452 this.currentIndex = 0; | |
453 this.showPopup(); | |
454 this.postNode(); | |
455 } else { | |
456 advanceState(); | |
457 } | |
458 }; | |
459 | |
460 this.proceedClicked = function() { | |
461 // Each time the popup button is clicked! | |
462 var node = this.popupOptions[this.currentIndex]; | |
463 if (node.type == 'question') { | |
464 // Must extract the question data | |
465 var textArea = $(popup.popupContent).find('textarea')[0]; | |
466 if (node.mandatory == true && textArea.value.length == 0) { | |
467 alert('This question is mandatory'); | |
468 return; | |
469 } else { | |
470 // Save the text content | |
471 var hold = document.createElement('comment'); | |
472 hold.id = node.id; | |
473 hold.innerHTML = textArea.value; | |
474 console.log("Question: "+ node.question); | |
475 console.log("Question Response: "+ textArea.value); | |
476 this.responses.appendChild(hold); | |
477 } | |
478 } else if (node.type == 'checkbox') { | |
479 // Must extract checkbox data | |
480 var optHold = this.popupResponse; | |
481 var hold = document.createElement('checkbox'); | |
482 console.log("Checkbox: "+ node.statement); | |
483 hold.id = node.id; | |
484 for (var i=0; i<optHold.childElementCount; i++) { | |
485 var input = optHold.childNodes[i].getElementsByTagName('input')[0]; | |
486 var statement = optHold.childNodes[i].getElementsByTagName('span')[0]; | |
487 var response = document.createElement('option'); | |
488 response.setAttribute('name',input.id); | |
489 response.textContent = input.checked; | |
490 hold.appendChild(response); | |
491 console.log(input.id +': '+ input.checked); | |
492 } | |
493 this.responses.appendChild(hold); | |
494 } else if (node.type == "radio") { | |
495 var optHold = this.popupResponse; | |
496 var hold = document.createElement('radio'); | |
497 var responseID = null; | |
498 var i=0; | |
499 while(responseID == null) { | |
500 var input = optHold.childNodes[i].getElementsByTagName('input')[0]; | |
501 if (input.checked == true) { | |
502 responseID = i; | |
503 } | |
504 i++; | |
505 } | |
506 hold.id = node.id; | |
507 hold.setAttribute('name',node.options[responseID].name); | |
508 hold.textContent = node.options[responseID].text; | |
509 this.responses.appendChild(hold); | |
510 } else if (node.type == "number") { | |
511 var input = this.popupContent.getElementsByTagName('input')[0]; | |
512 if (node.mandatory == true && input.value.length == 0) { | |
513 alert('This question is mandatory. Please enter a number'); | |
514 return; | |
515 } | |
516 var enteredNumber = Number(input.value); | |
517 if (isNaN(enteredNumber)) { | |
518 alert('Please enter a valid number'); | |
519 return; | |
520 } | |
521 if (enteredNumber < node.min && node.min != null) { | |
522 alert('Number is below the minimum value of '+node.min); | |
523 return; | |
524 } | |
525 if (enteredNumber > node.max && node.max != null) { | |
526 alert('Number is above the maximum value of '+node.max); | |
527 return; | |
528 } | |
529 var hold = document.createElement('number'); | |
530 hold.id = node.id; | |
531 hold.textContent = input.value; | |
532 this.responses.appendChild(hold); | |
533 } | |
534 this.currentIndex++; | |
535 if (this.currentIndex < this.popupOptions.length) { | |
536 this.postNode(); | |
537 } else { | |
538 // Reached the end of the popupOptions | |
539 this.hidePopup(); | |
540 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) { | |
541 testState.stateResults[testState.stateIndex] = this.responses; | |
542 } else { | |
543 testState.stateResults[testState.stateIndex].appendChild(this.responses); | |
544 } | |
545 advanceState(); | |
546 } | |
547 }; | |
548 | |
549 this.previousClick = function() { | |
550 // Triggered when the 'Back' button is clicked in the survey | |
551 if (this.currentIndex > 0) { | |
552 this.currentIndex--; | |
553 var node = this.popupOptions[this.currentIndex]; | |
554 if (node.type != 'statement') { | |
555 var prevResp = this.responses.childNodes[this.responses.childElementCount-1]; | |
556 this.responses.removeChild(prevResp); | |
557 } | |
558 this.postNode(); | |
559 if (node.type == 'question') { | |
560 this.popupContent.getElementsByTagName('textarea')[0].value = prevResp.textContent; | |
561 } else if (node.type == 'checkbox') { | |
562 var options = this.popupContent.getElementsByTagName('input'); | |
563 var savedOptions = prevResp.getElementsByTagName('option'); | |
564 for (var i=0; i<options.length; i++) { | |
565 var id = options[i].id; | |
566 for (var j=0; j<savedOptions.length; j++) { | |
567 if (savedOptions[j].getAttribute('name') == id) { | |
568 if (savedOptions[j].textContent == 'true') {options[i].checked = true;} | |
569 else {options[i].checked = false;} | |
570 break; | |
571 } | |
572 } | |
573 } | |
574 } else if (node.type == 'number') { | |
575 this.popupContent.getElementsByTagName('input')[0].value = prevResp.textContent; | |
576 } else if (node.type == 'radio') { | |
577 var options = this.popupContent.getElementsByTagName('input'); | |
578 var name = prevResp.getAttribute('name'); | |
579 for (var i=0; i<options.length; i++) { | |
580 if (options[i].id == name) { | |
581 options[i].checked = true; | |
582 break; | |
583 } | |
584 } | |
585 } | |
586 } | |
587 }; | |
588 | |
589 this.resize = function(event) | |
590 { | |
591 // Called on window resize; | |
592 this.popup.style.left = (window.innerWidth/2)-250 + 'px'; | |
593 this.popup.style.top = (window.innerHeight/2)-125 + 'px'; | |
594 var blank = document.getElementsByClassName('testHalt')[0]; | |
595 blank.style.width = window.innerWidth; | |
596 blank.style.height = window.innerHeight; | |
597 }; | |
598 } | |
599 | |
600 function advanceState() | |
601 { | |
602 // Just for complete clarity | |
603 testState.advanceState(); | |
604 } | |
605 | |
606 function stateMachine() | |
607 { | |
608 // Object prototype for tracking and managing the test state | |
609 this.stateMap = []; | |
610 this.stateIndex = null; | |
611 this.currentStateMap = []; | |
612 this.currentIndex = null; | |
613 this.currentTestId = 0; | |
614 this.stateResults = []; | |
615 this.timerCallBackHolders = null; | |
616 this.initialise = function(){ | |
617 if (this.stateMap.length > 0) { | |
618 if(this.stateIndex != null) { | |
619 console.log('NOTE - State already initialise'); | |
620 } | |
621 this.stateIndex = -1; | |
622 var that = this; | |
623 var aH_pId = 0; | |
624 for (var id=0; id<this.stateMap.length; id++){ | |
625 var name = this.stateMap[id].type; | |
626 var obj = document.createElement(name); | |
627 if (name == 'audioHolder') { | |
628 obj.id = this.stateMap[id].id; | |
629 obj.setAttribute('presentedid',aH_pId); | |
630 aH_pId+=1; | |
631 } | |
632 this.stateResults.push(obj); | |
633 } | |
634 } else { | |
635 console.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP'); | |
636 } | |
637 }; | |
638 this.advanceState = function(){ | |
639 if (this.stateIndex == null) { | |
640 this.initialise(); | |
641 } | |
642 if (this.stateIndex == -1) { | |
643 console.log('Starting test...'); | |
644 } | |
645 if (this.currentIndex == null){ | |
646 if (this.currentStateMap.type == "audioHolder") { | |
647 // Save current page | |
648 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId); | |
649 this.currentTestId++; | |
650 } | |
651 this.stateIndex++; | |
652 if (this.stateIndex >= this.stateMap.length) { | |
653 console.log('Test Completed'); | |
654 createProjectSave(specification.projectReturn); | |
655 } else { | |
656 this.currentStateMap = this.stateMap[this.stateIndex]; | |
657 if (this.currentStateMap.type == "audioHolder") { | |
658 console.log('Loading test page'); | |
659 interfaceContext.newPage(this.currentStateMap); | |
660 this.initialiseInnerState(this.currentStateMap); | |
661 } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") { | |
662 if (this.currentStateMap.options.length >= 1) { | |
663 popup.initState(this.currentStateMap); | |
664 } else { | |
665 this.advanceState(); | |
666 } | |
667 } else { | |
668 this.advanceState(); | |
669 } | |
670 } | |
671 } else { | |
672 this.advanceInnerState(); | |
673 } | |
674 }; | |
675 | |
676 this.testPageCompleted = function(store, testXML, testId) { | |
677 // Function called each time a test page has been completed | |
678 var metric = document.createElement('metric'); | |
679 if (audioEngineContext.metric.enableTestTimer) | |
680 { | |
681 var testTime = document.createElement('metricResult'); | |
682 testTime.id = 'testTime'; | |
683 testTime.textContent = audioEngineContext.timer.testDuration; | |
684 metric.appendChild(testTime); | |
685 } | |
686 store.appendChild(metric); | |
687 var audioObjects = audioEngineContext.audioObjects; | |
688 for (var i=0; i<audioObjects.length; i++) | |
689 { | |
690 var audioElement = audioEngineContext.audioObjects[i].exportXMLDOM(); | |
691 audioElement.setAttribute('presentedId',i); | |
692 store.appendChild(audioElement); | |
693 } | |
694 $(interfaceContext.commentQuestions).each(function(index,element){ | |
695 var node = element.exportXMLDOM(); | |
696 store.appendChild(node); | |
697 }); | |
698 pageXMLSave(store, testXML); | |
699 }; | |
700 | |
701 this.initialiseInnerState = function(node) { | |
702 // Parses the received testXML for pre and post test options | |
703 this.currentStateMap = []; | |
704 var preTest = node.preTest; | |
705 var postTest = node.postTest; | |
706 if (preTest == undefined) {preTest = document.createElement("preTest");} | |
707 if (postTest == undefined){postTest= document.createElement("postTest");} | |
708 this.currentStateMap.push(preTest); | |
709 this.currentStateMap.push(node); | |
710 this.currentStateMap.push(postTest); | |
711 this.currentIndex = -1; | |
712 this.advanceInnerState(); | |
713 }; | |
714 | |
715 this.advanceInnerState = function() { | |
716 this.currentIndex++; | |
717 if (this.currentIndex >= this.currentStateMap.length) { | |
718 this.currentIndex = null; | |
719 this.currentStateMap = this.stateMap[this.stateIndex]; | |
720 this.advanceState(); | |
721 } else { | |
722 if (this.currentStateMap[this.currentIndex].type == "audioHolder") { | |
723 console.log("Loading test page"+this.currentTestId); | |
724 } else if (this.currentStateMap[this.currentIndex].type == "pretest") { | |
725 popup.initState(this.currentStateMap[this.currentIndex]); | |
726 } else if (this.currentStateMap[this.currentIndex].type == "posttest") { | |
727 popup.initState(this.currentStateMap[this.currentIndex]); | |
728 } else { | |
729 this.advanceInnerState(); | |
730 } | |
731 } | |
732 }; | |
733 | |
734 this.previousState = function(){}; | |
735 } | |
736 | |
737 function AudioEngine(specification) { | |
738 | |
739 // Create two output paths, the main outputGain and fooGain. | |
740 // Output gain is default to 1 and any items for playback route here | |
741 // Foo gain is used for analysis to ensure paths get processed, but are not heard | |
742 // because web audio will optimise and any route which does not go to the destination gets ignored. | |
743 this.outputGain = audioContext.createGain(); | |
744 this.fooGain = audioContext.createGain(); | |
745 this.fooGain.gain = 0; | |
746 | |
747 // Use this to detect playback state: 0 - stopped, 1 - playing | |
748 this.status = 0; | |
749 | |
750 // Connect both gains to output | |
751 this.outputGain.connect(audioContext.destination); | |
752 this.fooGain.connect(audioContext.destination); | |
753 | |
754 // Create the timer Object | |
755 this.timer = new timer(); | |
756 // Create session metrics | |
757 this.metric = new sessionMetrics(this,specification); | |
758 | |
759 this.loopPlayback = false; | |
760 | |
761 // Create store for new audioObjects | |
762 this.audioObjects = []; | |
763 | |
764 this.buffers = []; | |
765 this.bufferObj = function() | |
766 { | |
767 this.url = null; | |
768 this.buffer = null; | |
769 this.xmlRequest = new XMLHttpRequest(); | |
770 this.users = []; | |
771 this.getMedia = function(url) { | |
772 this.url = url; | |
773 this.xmlRequest.open('GET',this.url,true); | |
774 this.xmlRequest.responseType = 'arraybuffer'; | |
775 | |
776 var bufferObj = this; | |
777 | |
778 // Create callback to decode the data asynchronously | |
779 this.xmlRequest.onloadend = function() { | |
780 audioContext.decodeAudioData(bufferObj.xmlRequest.response, function(decodedData) { | |
781 bufferObj.buffer = decodedData; | |
782 for (var i=0; i<bufferObj.users.length; i++) | |
783 { | |
784 bufferObj.users[i].state = 1; | |
785 if (bufferObj.users[i].interfaceDOM != null) | |
786 { | |
787 bufferObj.users[i].bufferLoaded(bufferObj); | |
788 } | |
789 } | |
790 calculateLoudness(bufferObj.buffer,"I"); | |
791 }, function(){ | |
792 // Should only be called if there was an error, but sometimes gets called continuously | |
793 // Check here if the error is genuine | |
794 if (bufferObj.buffer == undefined) { | |
795 // Genuine error | |
796 console.log('FATAL - Error loading buffer on '+audioObj.id); | |
797 if (request.status == 404) | |
798 { | |
799 console.log('FATAL - Fragment '+audioObj.id+' 404 error'); | |
800 console.log('URL: '+audioObj.url); | |
801 errorSessionDump('Fragment '+audioObj.id+' 404 error'); | |
802 } | |
803 } | |
804 }); | |
805 }; | |
806 this.progress = 0; | |
807 this.progressCallback = function(event){ | |
808 if (event.lengthComputable) | |
809 { | |
810 this.progress = event.loaded / event.total; | |
811 } | |
812 }; | |
813 this.xmlRequest.addEventListener("progress", this.progressCallback); | |
814 this.xmlRequest.send(); | |
815 }; | |
816 }; | |
817 | |
818 this.play = function(id) { | |
819 // Start the timer and set the audioEngine state to playing (1) | |
820 if (this.status == 0 && this.loopPlayback) { | |
821 // Check if all audioObjects are ready | |
822 if(this.checkAllReady()) | |
823 { | |
824 this.status = 1; | |
825 this.setSynchronousLoop(); | |
826 } | |
827 } | |
828 else | |
829 { | |
830 this.status = 1; | |
831 } | |
832 if (this.status== 1) { | |
833 this.timer.startTest(); | |
834 if (id == undefined) { | |
835 id = -1; | |
836 console.log('FATAL - Passed id was undefined - AudioEngineContext.play(id)'); | |
837 return; | |
838 } else { | |
839 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]); | |
840 } | |
841 if (this.loopPlayback) { | |
842 for (var i=0; i<this.audioObjects.length; i++) | |
843 { | |
844 this.audioObjects[i].play(this.timer.getTestTime()+1); | |
845 if (id == i) { | |
846 this.audioObjects[i].loopStart(); | |
847 } else { | |
848 this.audioObjects[i].loopStop(); | |
849 } | |
850 } | |
851 } else { | |
852 for (var i=0; i<this.audioObjects.length; i++) | |
853 { | |
854 if (i != id) { | |
855 this.audioObjects[i].outputGain.gain.value = 0.0; | |
856 this.audioObjects[i].stop(); | |
857 } else if (i == id) { | |
858 this.audioObjects[id].outputGain.gain.value = this.audioObjects[id].specification.gain*this.audioObjects[id].buffer.buffer.gain; | |
859 this.audioObjects[id].play(audioContext.currentTime+0.01); | |
860 } | |
861 } | |
862 } | |
863 interfaceContext.playhead.start(); | |
864 } | |
865 }; | |
866 | |
867 this.stop = function() { | |
868 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1) | |
869 if (this.status == 1) { | |
870 for (var i=0; i<this.audioObjects.length; i++) | |
871 { | |
872 this.audioObjects[i].stop(); | |
873 } | |
874 interfaceContext.playhead.stop(); | |
875 this.status = 0; | |
876 } | |
877 }; | |
878 | |
879 this.newTrack = function(element) { | |
880 // Pull data from given URL into new audio buffer | |
881 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin' | |
882 | |
883 // Create the audioObject with ID of the new track length; | |
884 audioObjectId = this.audioObjects.length; | |
885 this.audioObjects[audioObjectId] = new audioObject(audioObjectId); | |
886 | |
887 // Check if audioObject buffer is currently stored by full URL | |
888 var URL = element.parent.hostURL + element.url; | |
889 var buffer = null; | |
890 for (var i=0; i<this.buffers.length; i++) | |
891 { | |
892 if (URL == this.buffers[i].url) | |
893 { | |
894 buffer = this.buffers[i]; | |
895 break; | |
896 } | |
897 } | |
898 if (buffer == null) | |
899 { | |
900 console.log("[WARN]: Buffer was not loaded in pre-test! "+URL); | |
901 buffer = new this.bufferObj(); | |
902 buffer.getMedia(URL); | |
903 this.buffers.push(buffer); | |
904 } | |
905 this.audioObjects[audioObjectId].specification = element; | |
906 this.audioObjects[audioObjectId].url = URL; | |
907 buffer.users.push(this.audioObjects[audioObjectId]); | |
908 if (buffer.buffer != null) | |
909 { | |
910 this.audioObjects[audioObjectId].bufferLoaded(buffer); | |
911 } | |
912 return this.audioObjects[audioObjectId]; | |
913 }; | |
914 | |
915 this.newTestPage = function() { | |
916 this.state = 0; | |
917 this.audioObjectsReady = false; | |
918 this.metric.reset(); | |
919 for (var i=0; i < this.buffers.length; i++) | |
920 { | |
921 this.buffers[i].users = []; | |
922 } | |
923 this.audioObjects = []; | |
924 }; | |
925 | |
926 this.checkAllPlayed = function() { | |
927 arr = []; | |
928 for (var id=0; id<this.audioObjects.length; id++) { | |
929 if (this.audioObjects[id].metric.wasListenedTo == false) { | |
930 arr.push(this.audioObjects[id].id); | |
931 } | |
932 } | |
933 return arr; | |
934 }; | |
935 | |
936 this.checkAllReady = function() { | |
937 var ready = true; | |
938 for (var i=0; i<this.audioObjects.length; i++) { | |
939 if (this.audioObjects[i].state == 0) { | |
940 // Track not ready | |
941 console.log('WAIT -- audioObject '+i+' not ready yet!'); | |
942 ready = false; | |
943 }; | |
944 } | |
945 return ready; | |
946 }; | |
947 | |
948 this.setSynchronousLoop = function() { | |
949 // Pads the signals so they are all exactly the same length | |
950 var length = 0; | |
951 var maxId; | |
952 for (var i=0; i<this.audioObjects.length; i++) | |
953 { | |
954 if (length < this.audioObjects[i].buffer.buffer.length) | |
955 { | |
956 length = this.audioObjects[i].buffer.buffer.length; | |
957 maxId = i; | |
958 } | |
959 } | |
960 // Extract the audio and zero-pad | |
961 for (var i=0; i<this.audioObjects.length; i++) | |
962 { | |
963 var orig = this.audioObjects[i].buffer.buffer; | |
964 var hold = audioContext.createBuffer(orig.numberOfChannels,length,orig.sampleRate); | |
965 for (var c=0; c<orig.numberOfChannels; c++) | |
966 { | |
967 var inData = hold.getChannelData(c); | |
968 var outData = orig.getChannelData(c); | |
969 for (var n=0; n<orig.length; n++) | |
970 {inData[n] = outData[n];} | |
971 } | |
972 hold.gain = orig.gain; | |
973 hold.lufs = orig.lufs; | |
974 this.audioObjects[i].buffer.buffer = hold; | |
975 } | |
976 }; | |
977 | |
978 } | |
979 | |
980 function audioObject(id) { | |
981 // The main buffer object with common control nodes to the AudioEngine | |
982 | |
983 this.specification; | |
984 this.id = id; | |
985 this.state = 0; // 0 - no data, 1 - ready | |
986 this.url = null; // Hold the URL given for the output back to the results. | |
987 this.metric = new metricTracker(this); | |
988 | |
989 // Bindings for GUI | |
990 this.interfaceDOM = null; | |
991 this.commentDOM = null; | |
992 | |
993 // Create a buffer and external gain control to allow internal patching of effects and volume leveling. | |
994 this.bufferNode = undefined; | |
995 this.outputGain = audioContext.createGain(); | |
996 | |
997 // Default output gain to be zero | |
998 this.outputGain.gain.value = 0.0; | |
999 | |
1000 // Connect buffer to the audio graph | |
1001 this.outputGain.connect(audioEngineContext.outputGain); | |
1002 | |
1003 // the audiobuffer is not designed for multi-start playback | |
1004 // When stopeed, the buffer node is deleted and recreated with the stored buffer. | |
1005 this.buffer; | |
1006 | |
1007 this.bufferLoaded = function(callee) | |
1008 { | |
1009 // Called by the associated buffer when it has finished loading, will then 'bind' the buffer to the | |
1010 // audioObject and trigger the interfaceDOM.enable() function for user feedback | |
1011 if (audioEngineContext.loopPlayback){ | |
1012 // First copy the buffer into this.buffer | |
1013 this.buffer = new audioEngineContext.bufferObj(); | |
1014 this.buffer.url = callee.url; | |
1015 this.buffer.buffer = audioContext.createBuffer(callee.buffer.numberOfChannels, callee.buffer.length, callee.buffer.sampleRate); | |
1016 for (var c=0; c<callee.buffer.numberOfChannels; c++) | |
1017 { | |
1018 var src = callee.buffer.getChannelData(c); | |
1019 var dst = this.buffer.buffer.getChannelData(c); | |
1020 for (var n=0; n<src.length; n++) | |
1021 { | |
1022 dst[n] = src[n]; | |
1023 } | |
1024 } | |
1025 } else { | |
1026 this.buffer = callee; | |
1027 } | |
1028 this.state = 1; | |
1029 this.buffer.buffer.gain = callee.buffer.gain; | |
1030 this.buffer.buffer.lufs = callee.buffer.lufs; | |
1031 var targetLUFS = this.specification.parent.loudness; | |
1032 if (typeof targetLUFS === "number") | |
1033 { | |
1034 this.buffer.buffer.gain = decibelToLinear(targetLUFS - this.buffer.buffer.lufs); | |
1035 } else { | |
1036 this.buffer.buffer.gain = 1.0; | |
1037 } | |
1038 if (this.interfaceDOM != null) { | |
1039 this.interfaceDOM.enable(); | |
1040 } | |
1041 }; | |
1042 | |
1043 this.loopStart = function() { | |
1044 this.outputGain.gain.value = this.specification.gain*this.buffer.buffer.gain; | |
1045 this.metric.startListening(audioEngineContext.timer.getTestTime()); | |
1046 }; | |
1047 | |
1048 this.loopStop = function() { | |
1049 if (this.outputGain.gain.value != 0.0) { | |
1050 this.outputGain.gain.value = 0.0; | |
1051 this.metric.stopListening(audioEngineContext.timer.getTestTime()); | |
1052 } | |
1053 }; | |
1054 | |
1055 this.play = function(startTime) { | |
1056 if (this.bufferNode == undefined && this.buffer.buffer != undefined) { | |
1057 this.bufferNode = audioContext.createBufferSource(); | |
1058 this.bufferNode.owner = this; | |
1059 this.bufferNode.connect(this.outputGain); | |
1060 this.bufferNode.buffer = this.buffer.buffer; | |
1061 this.bufferNode.loop = audioEngineContext.loopPlayback; | |
1062 this.bufferNode.onended = function(event) { | |
1063 // Safari does not like using 'this' to reference the calling object! | |
1064 //event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition()); | |
1065 event.currentTarget.owner.stop(); | |
1066 }; | |
1067 if (this.bufferNode.loop == false) { | |
1068 this.metric.startListening(audioEngineContext.timer.getTestTime()); | |
1069 } | |
1070 this.bufferNode.start(startTime); | |
1071 } | |
1072 }; | |
1073 | |
1074 this.stop = function() { | |
1075 if (this.bufferNode != undefined) | |
1076 { | |
1077 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition()); | |
1078 this.bufferNode.stop(0); | |
1079 this.bufferNode = undefined; | |
1080 } | |
1081 }; | |
1082 | |
1083 this.getCurrentPosition = function() { | |
1084 var time = audioEngineContext.timer.getTestTime(); | |
1085 if (this.bufferNode != undefined) { | |
1086 if (this.bufferNode.loop == true) { | |
1087 if (audioEngineContext.status == 1) { | |
1088 return (time-this.metric.listenStart)%this.buffer.buffer.duration; | |
1089 } else { | |
1090 return 0; | |
1091 } | |
1092 } else { | |
1093 if (this.metric.listenHold) { | |
1094 return time - this.metric.listenStart; | |
1095 } else { | |
1096 return 0; | |
1097 } | |
1098 } | |
1099 } else { | |
1100 return 0; | |
1101 } | |
1102 }; | |
1103 | |
1104 this.exportXMLDOM = function() { | |
1105 var root = document.createElement('audioElement'); | |
1106 root.id = this.specification.id; | |
1107 root.setAttribute('url',this.specification.url); | |
1108 var file = document.createElement('file'); | |
1109 file.setAttribute('sampleRate',this.buffer.buffer.sampleRate); | |
1110 file.setAttribute('channels',this.buffer.buffer.numberOfChannels); | |
1111 file.setAttribute('sampleCount',this.buffer.buffer.length); | |
1112 file.setAttribute('duration',this.buffer.buffer.duration); | |
1113 root.appendChild(file); | |
1114 if (this.specification.type != 'outsidereference') { | |
1115 var interfaceXML = this.interfaceDOM.exportXMLDOM(this); | |
1116 if (interfaceXML.length == undefined) { | |
1117 root.appendChild(); | |
1118 } else { | |
1119 for (var i=0; i<interfaceXML.length; i++) | |
1120 { | |
1121 root.appendChild(interfaceXML[i]); | |
1122 } | |
1123 } | |
1124 root.appendChild(this.commentDOM.exportXMLDOM(this)); | |
1125 if(this.specification.type == 'anchor') { | |
1126 root.setAttribute('anchor',true); | |
1127 } else if(this.specification.type == 'reference') { | |
1128 root.setAttribute('reference',true); | |
1129 } | |
1130 } | |
1131 root.appendChild(this.metric.exportXMLDOM()); | |
1132 return root; | |
1133 }; | |
1134 } | |
1135 | |
1136 function timer() | |
1137 { | |
1138 /* Timer object used in audioEngine to keep track of session timings | |
1139 * Uses the timer of the web audio API, so sample resolution | |
1140 */ | |
1141 this.testStarted = false; | |
1142 this.testStartTime = 0; | |
1143 this.testDuration = 0; | |
1144 this.minimumTestTime = 0; // No minimum test time | |
1145 this.startTest = function() | |
1146 { | |
1147 if (this.testStarted == false) | |
1148 { | |
1149 this.testStartTime = audioContext.currentTime; | |
1150 this.testStarted = true; | |
1151 this.updateTestTime(); | |
1152 audioEngineContext.metric.initialiseTest(); | |
1153 } | |
1154 }; | |
1155 this.stopTest = function() | |
1156 { | |
1157 if (this.testStarted) | |
1158 { | |
1159 this.testDuration = this.getTestTime(); | |
1160 this.testStarted = false; | |
1161 } else { | |
1162 console.log('ERR: Test tried to end before beginning'); | |
1163 } | |
1164 }; | |
1165 this.updateTestTime = function() | |
1166 { | |
1167 if (this.testStarted) | |
1168 { | |
1169 this.testDuration = audioContext.currentTime - this.testStartTime; | |
1170 } | |
1171 }; | |
1172 this.getTestTime = function() | |
1173 { | |
1174 this.updateTestTime(); | |
1175 return this.testDuration; | |
1176 }; | |
1177 } | |
1178 | |
1179 function sessionMetrics(engine,specification) | |
1180 { | |
1181 /* Used by audioEngine to link to audioObjects to minimise the timer call timers; | |
1182 */ | |
1183 this.engine = engine; | |
1184 this.lastClicked = -1; | |
1185 this.data = -1; | |
1186 this.reset = function() { | |
1187 this.lastClicked = -1; | |
1188 this.data = -1; | |
1189 }; | |
1190 | |
1191 this.enableElementInitialPosition = false; | |
1192 this.enableElementListenTracker = false; | |
1193 this.enableElementTimer = false; | |
1194 this.enableElementTracker = false; | |
1195 this.enableFlagListenedTo = false; | |
1196 this.enableFlagMoved = false; | |
1197 this.enableTestTimer = false; | |
1198 // Obtain the metrics enabled | |
1199 for (var i=0; i<specification.metrics.length; i++) | |
1200 { | |
1201 var node = specification.metrics[i]; | |
1202 switch(node.enabled) | |
1203 { | |
1204 case 'testTimer': | |
1205 this.enableTestTimer = true; | |
1206 break; | |
1207 case 'elementTimer': | |
1208 this.enableElementTimer = true; | |
1209 break; | |
1210 case 'elementTracker': | |
1211 this.enableElementTracker = true; | |
1212 break; | |
1213 case 'elementListenTracker': | |
1214 this.enableElementListenTracker = true; | |
1215 break; | |
1216 case 'elementInitialPosition': | |
1217 this.enableElementInitialPosition = true; | |
1218 break; | |
1219 case 'elementFlagListenedTo': | |
1220 this.enableFlagListenedTo = true; | |
1221 break; | |
1222 case 'elementFlagMoved': | |
1223 this.enableFlagMoved = true; | |
1224 break; | |
1225 case 'elementFlagComments': | |
1226 this.enableFlagComments = true; | |
1227 break; | |
1228 } | |
1229 } | |
1230 this.initialiseTest = function(){}; | |
1231 } | |
1232 | |
1233 function metricTracker(caller) | |
1234 { | |
1235 /* Custom object to track and collect metric data | |
1236 * Used only inside the audioObjects object. | |
1237 */ | |
1238 | |
1239 this.listenedTimer = 0; | |
1240 this.listenStart = 0; | |
1241 this.listenHold = false; | |
1242 this.initialPosition = -1; | |
1243 this.movementTracker = []; | |
1244 this.listenTracker =[]; | |
1245 this.wasListenedTo = false; | |
1246 this.wasMoved = false; | |
1247 this.hasComments = false; | |
1248 this.parent = caller; | |
1249 | |
1250 this.initialised = function(position) | |
1251 { | |
1252 if (this.initialPosition == -1) { | |
1253 this.initialPosition = position; | |
1254 } | |
1255 }; | |
1256 | |
1257 this.moved = function(time,position) | |
1258 { | |
1259 this.wasMoved = true; | |
1260 this.movementTracker[this.movementTracker.length] = [time, position]; | |
1261 }; | |
1262 | |
1263 this.startListening = function(time) | |
1264 { | |
1265 if (this.listenHold == false) | |
1266 { | |
1267 this.wasListenedTo = true; | |
1268 this.listenStart = time; | |
1269 this.listenHold = true; | |
1270 | |
1271 var evnt = document.createElement('event'); | |
1272 var testTime = document.createElement('testTime'); | |
1273 testTime.setAttribute('start',time); | |
1274 var bufferTime = document.createElement('bufferTime'); | |
1275 bufferTime.setAttribute('start',this.parent.getCurrentPosition()); | |
1276 evnt.appendChild(testTime); | |
1277 evnt.appendChild(bufferTime); | |
1278 this.listenTracker.push(evnt); | |
1279 | |
1280 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id | |
1281 } | |
1282 }; | |
1283 | |
1284 this.stopListening = function(time,bufferStopTime) | |
1285 { | |
1286 if (this.listenHold == true) | |
1287 { | |
1288 var diff = time - this.listenStart; | |
1289 this.listenedTimer += (diff); | |
1290 this.listenStart = 0; | |
1291 this.listenHold = false; | |
1292 | |
1293 var evnt = this.listenTracker[this.listenTracker.length-1]; | |
1294 var testTime = evnt.getElementsByTagName('testTime')[0]; | |
1295 var bufferTime = evnt.getElementsByTagName('bufferTime')[0]; | |
1296 testTime.setAttribute('stop',time); | |
1297 if (bufferStopTime == undefined) { | |
1298 bufferTime.setAttribute('stop',this.parent.getCurrentPosition()); | |
1299 } else { | |
1300 bufferTime.setAttribute('stop',bufferStopTime); | |
1301 } | |
1302 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id | |
1303 } | |
1304 }; | |
1305 | |
1306 this.exportXMLDOM = function() { | |
1307 var root = document.createElement('metric'); | |
1308 if (audioEngineContext.metric.enableElementTimer) { | |
1309 var mElementTimer = document.createElement('metricresult'); | |
1310 mElementTimer.setAttribute('name','enableElementTimer'); | |
1311 mElementTimer.textContent = this.listenedTimer; | |
1312 root.appendChild(mElementTimer); | |
1313 } | |
1314 if (audioEngineContext.metric.enableElementTracker) { | |
1315 var elementTrackerFull = document.createElement('metricResult'); | |
1316 elementTrackerFull.setAttribute('name','elementTrackerFull'); | |
1317 for (var k=0; k<this.movementTracker.length; k++) | |
1318 { | |
1319 var timePos = document.createElement('timePos'); | |
1320 timePos.id = k; | |
1321 var time = document.createElement('time'); | |
1322 time.textContent = this.movementTracker[k][0]; | |
1323 var position = document.createElement('position'); | |
1324 position.textContent = this.movementTracker[k][1]; | |
1325 timePos.appendChild(time); | |
1326 timePos.appendChild(position); | |
1327 elementTrackerFull.appendChild(timePos); | |
1328 } | |
1329 root.appendChild(elementTrackerFull); | |
1330 } | |
1331 if (audioEngineContext.metric.enableElementListenTracker) { | |
1332 var elementListenTracker = document.createElement('metricResult'); | |
1333 elementListenTracker.setAttribute('name','elementListenTracker'); | |
1334 for (var k=0; k<this.listenTracker.length; k++) { | |
1335 elementListenTracker.appendChild(this.listenTracker[k]); | |
1336 } | |
1337 root.appendChild(elementListenTracker); | |
1338 } | |
1339 if (audioEngineContext.metric.enableElementInitialPosition) { | |
1340 var elementInitial = document.createElement('metricResult'); | |
1341 elementInitial.setAttribute('name','elementInitialPosition'); | |
1342 elementInitial.textContent = this.initialPosition; | |
1343 root.appendChild(elementInitial); | |
1344 } | |
1345 if (audioEngineContext.metric.enableFlagListenedTo) { | |
1346 var flagListenedTo = document.createElement('metricResult'); | |
1347 flagListenedTo.setAttribute('name','elementFlagListenedTo'); | |
1348 flagListenedTo.textContent = this.wasListenedTo; | |
1349 root.appendChild(flagListenedTo); | |
1350 } | |
1351 if (audioEngineContext.metric.enableFlagMoved) { | |
1352 var flagMoved = document.createElement('metricResult'); | |
1353 flagMoved.setAttribute('name','elementFlagMoved'); | |
1354 flagMoved.textContent = this.wasMoved; | |
1355 root.appendChild(flagMoved); | |
1356 } | |
1357 if (audioEngineContext.metric.enableFlagComments) { | |
1358 var flagComments = document.createElement('metricResult'); | |
1359 flagComments.setAttribute('name','elementFlagComments'); | |
1360 if (this.parent.commentDOM == null) | |
1361 {flag.textContent = 'false';} | |
1362 else if (this.parent.commentDOM.textContent.length == 0) | |
1363 {flag.textContent = 'false';} | |
1364 else | |
1365 {flag.textContet = 'true';} | |
1366 root.appendChild(flagComments); | |
1367 } | |
1368 | |
1369 return root; | |
1370 }; | |
1371 } | |
1372 | |
1373 function randomiseOrder(input) | |
1374 { | |
1375 // This takes an array of information and randomises the order | |
1376 var N = input.length; | |
1377 | |
1378 var inputSequence = []; // For safety purposes: keep track of randomisation | |
1379 for (var counter = 0; counter < N; ++counter) | |
1380 inputSequence.push(counter) // Fill array | |
1381 var inputSequenceClone = inputSequence.slice(0); | |
1382 | |
1383 var holdArr = []; | |
1384 var outputSequence = []; | |
1385 for (var n=0; n<N; n++) | |
1386 { | |
1387 // First pick a random number | |
1388 var r = Math.random(); | |
1389 // Multiply and floor by the number of elements left | |
1390 r = Math.floor(r*input.length); | |
1391 // Pick out that element and delete from the array | |
1392 holdArr.push(input.splice(r,1)[0]); | |
1393 // Do the same with sequence | |
1394 outputSequence.push(inputSequence.splice(r,1)[0]); | |
1395 } | |
1396 console.log(inputSequenceClone.toString()); // print original array to console | |
1397 console.log(outputSequence.toString()); // print randomised array to console | |
1398 return holdArr; | |
1399 } | |
1400 | |
1401 function returnDateNode() | |
1402 { | |
1403 // Create an XML Node for the Date and Time a test was conducted | |
1404 // Structure is | |
1405 // <datetime> | |
1406 // <date year="##" month="##" day="##">DD/MM/YY</date> | |
1407 // <time hour="##" minute="##" sec="##">HH:MM:SS</time> | |
1408 // </datetime> | |
1409 var dateTime = new Date(); | |
1410 var year = document.createAttribute('year'); | |
1411 var month = document.createAttribute('month'); | |
1412 var day = document.createAttribute('day'); | |
1413 var hour = document.createAttribute('hour'); | |
1414 var minute = document.createAttribute('minute'); | |
1415 var secs = document.createAttribute('secs'); | |
1416 | |
1417 year.nodeValue = dateTime.getFullYear(); | |
1418 month.nodeValue = dateTime.getMonth()+1; | |
1419 day.nodeValue = dateTime.getDate(); | |
1420 hour.nodeValue = dateTime.getHours(); | |
1421 minute.nodeValue = dateTime.getMinutes(); | |
1422 secs.nodeValue = dateTime.getSeconds(); | |
1423 | |
1424 var hold = document.createElement("datetime"); | |
1425 var date = document.createElement("date"); | |
1426 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue; | |
1427 var time = document.createElement("time"); | |
1428 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue; | |
1429 | |
1430 date.setAttributeNode(year); | |
1431 date.setAttributeNode(month); | |
1432 date.setAttributeNode(day); | |
1433 time.setAttributeNode(hour); | |
1434 time.setAttributeNode(minute); | |
1435 time.setAttributeNode(secs); | |
1436 | |
1437 hold.appendChild(date); | |
1438 hold.appendChild(time); | |
1439 return hold; | |
1440 | |
1441 } | |
1442 | |
1443 function Specification() { | |
1444 // Handles the decoding of the project specification XML into a simple JavaScript Object. | |
1445 | |
1446 this.interfaceType = null; | |
1447 this.commonInterface = new function() | |
1448 { | |
1449 this.options = []; | |
1450 this.optionNode = function(input) | |
1451 { | |
1452 var name = input.getAttribute('name'); | |
1453 this.type = name; | |
1454 if(this.type == "option") | |
1455 { | |
1456 this.name = input.id; | |
1457 } else if (this.type == "check") | |
1458 { | |
1459 this.check = input.id; | |
1460 } | |
1461 }; | |
1462 }; | |
1463 | |
1464 this.randomiseOrder = function(input) | |
1465 { | |
1466 // This takes an array of information and randomises the order | |
1467 var N = input.length; | |
1468 | |
1469 var inputSequence = []; // For safety purposes: keep track of randomisation | |
1470 for (var counter = 0; counter < N; ++counter) | |
1471 inputSequence.push(counter) // Fill array | |
1472 var inputSequenceClone = inputSequence.slice(0); | |
1473 | |
1474 var holdArr = []; | |
1475 var outputSequence = []; | |
1476 for (var n=0; n<N; n++) | |
1477 { | |
1478 // First pick a random number | |
1479 var r = Math.random(); | |
1480 // Multiply and floor by the number of elements left | |
1481 r = Math.floor(r*input.length); | |
1482 // Pick out that element and delete from the array | |
1483 holdArr.push(input.splice(r,1)[0]); | |
1484 // Do the same with sequence | |
1485 outputSequence.push(inputSequence.splice(r,1)[0]); | |
1486 } | |
1487 console.log(inputSequenceClone.toString()); // print original array to console | |
1488 console.log(outputSequence.toString()); // print randomised array to console | |
1489 return holdArr; | |
1490 }; | |
1491 this.projectReturn = null; | |
1492 this.randomiseOrder = null; | |
1493 this.collectMetrics = null; | |
1494 this.testPages = null; | |
1495 this.audioHolders = []; | |
1496 this.metrics = []; | |
1497 this.loudness = null; | |
1498 | |
1499 this.decode = function(projectXML) { | |
1500 // projectXML - DOM Parsed document | |
1501 this.projectXML = projectXML.childNodes[0]; | |
1502 var setupNode = projectXML.getElementsByTagName('setup')[0]; | |
1503 this.interfaceType = setupNode.getAttribute('interface'); | |
1504 this.projectReturn = setupNode.getAttribute('projectReturn'); | |
1505 this.testPages = setupNode.getAttribute('testPages'); | |
1506 if (setupNode.getAttribute('randomiseOrder') == "true") { | |
1507 this.randomiseOrder = true; | |
1508 } else {this.randomiseOrder = false;} | |
1509 if (setupNode.getAttribute('collectMetrics') == "true") { | |
1510 this.collectMetrics = true; | |
1511 } else {this.collectMetrics = false;} | |
1512 if (isNaN(Number(this.testPages)) || this.testPages == undefined) | |
1513 { | |
1514 this.testPages = null; | |
1515 } else { | |
1516 this.testPages = Number(this.testPages); | |
1517 if (this.testPages == 0) {this.testPages = null;} | |
1518 } | |
1519 if (setupNode.getAttribute('loudness') != null) | |
1520 { | |
1521 var XMLloudness = setupNode.getAttribute('loudness'); | |
1522 if (isNaN(Number(XMLloudness)) == false) | |
1523 { | |
1524 this.loudness = Number(XMLloudness); | |
1525 } | |
1526 } | |
1527 var metricCollection = setupNode.getElementsByTagName('Metric'); | |
1528 | |
1529 var setupPreTestNode = setupNode.getElementsByTagName('PreTest'); | |
1530 if (setupPreTestNode.length != 0) | |
1531 { | |
1532 setupPreTestNode = setupPreTestNode[0]; | |
1533 this.preTest.construct(setupPreTestNode); | |
1534 } | |
1535 | |
1536 var setupPostTestNode = setupNode.getElementsByTagName('PostTest'); | |
1537 if (setupPostTestNode.length != 0) | |
1538 { | |
1539 setupPostTestNode = setupPostTestNode[0]; | |
1540 this.postTest.construct(setupPostTestNode); | |
1541 } | |
1542 | |
1543 if (metricCollection.length > 0) { | |
1544 metricCollection = metricCollection[0].getElementsByTagName('metricEnable'); | |
1545 for (var i=0; i<metricCollection.length; i++) { | |
1546 this.metrics.push(new this.metricNode(metricCollection[i].textContent)); | |
1547 } | |
1548 } | |
1549 | |
1550 var commonInterfaceNode = setupNode.getElementsByTagName('interface'); | |
1551 if (commonInterfaceNode.length > 0) { | |
1552 commonInterfaceNode = commonInterfaceNode[0]; | |
1553 } else { | |
1554 commonInterfaceNode = undefined; | |
1555 } | |
1556 | |
1557 this.commonInterface = new function() { | |
1558 this.OptionNode = function(child) { | |
1559 this.type = child.nodeName; | |
1560 if (this.type == 'option') | |
1561 { | |
1562 this.name = child.getAttribute('name'); | |
1563 } | |
1564 else if (this.type == 'check') { | |
1565 this.check = child.getAttribute('name'); | |
1566 if (this.check == 'scalerange') { | |
1567 this.min = child.getAttribute('min'); | |
1568 this.max = child.getAttribute('max'); | |
1569 if (this.min == null) {this.min = 1;} | |
1570 else if (Number(this.min) > 1 && this.min != null) { | |
1571 this.min = Number(this.min)/100; | |
1572 } else { | |
1573 this.min = Number(this.min); | |
1574 } | |
1575 if (this.max == null) {this.max = 0;} | |
1576 else if (Number(this.max) > 1 && this.max != null) { | |
1577 this.max = Number(this.max)/100; | |
1578 } else { | |
1579 this.max = Number(this.max); | |
1580 } | |
1581 } | |
1582 } else if (this.type == 'anchor' || this.type == 'reference') { | |
1583 this.value = Number(child.textContent); | |
1584 this.enforce = child.getAttribute('enforce'); | |
1585 if (this.enforce == 'true') {this.enforce = true;} | |
1586 else {this.enforce = false;} | |
1587 } | |
1588 }; | |
1589 this.options = []; | |
1590 if (commonInterfaceNode != undefined) { | |
1591 var child = commonInterfaceNode.firstElementChild; | |
1592 while (child != undefined) { | |
1593 this.options.push(new this.OptionNode(child)); | |
1594 child = child.nextElementSibling; | |
1595 } | |
1596 } | |
1597 }; | |
1598 | |
1599 var audioHolders = projectXML.getElementsByTagName('audioHolder'); | |
1600 for (var i=0; i<audioHolders.length; i++) { | |
1601 var node = new this.audioHolderNode(this); | |
1602 node.decode(this,audioHolders[i]); | |
1603 this.audioHolders.push(node); | |
1604 } | |
1605 | |
1606 // New check if we need to randomise the test order | |
1607 if (this.randomiseOrder && typeof randomiseOrder === "function") | |
1608 { | |
1609 this.audioHolders = randomiseOrder(this.audioHolders); | |
1610 for (var i=0; i<this.audioHolders.length; i++) | |
1611 { | |
1612 this.audioHolders[i].presentedId = i; | |
1613 } | |
1614 } | |
1615 | |
1616 if (this.testPages != null || this.testPages != undefined) | |
1617 { | |
1618 if (this.testPages > audioHolders.length) | |
1619 { | |
1620 console.log('Warning: You have specified '+audioHolders.length+' tests but requested '+this.testPages+' be completed!'); | |
1621 this.testPages = audioHolders.length; | |
1622 } | |
1623 var aH = this.audioHolders; | |
1624 this.audioHolders = []; | |
1625 for (var i=0; i<this.testPages; i++) | |
1626 { | |
1627 this.audioHolders.push(aH[i]); | |
1628 } | |
1629 } | |
1630 }; | |
1631 | |
1632 this.encode = function() | |
1633 { | |
1634 var root = document.implementation.createDocument(null,"BrowserEvalProjectDocument"); | |
1635 // First get all the <setup> tag compiled | |
1636 var setupNode = root.createElement("setup"); | |
1637 setupNode.setAttribute('interface',this.interfaceType); | |
1638 setupNode.setAttribute('projectReturn',this.projectReturn); | |
1639 setupNode.setAttribute('randomiseOrder',this.randomiseOrder); | |
1640 setupNode.setAttribute('collectMetrics',this.collectMetrics); | |
1641 setupNode.setAttribute('testPages',this.testPages); | |
1642 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);} | |
1643 | |
1644 var setupPreTest = root.createElement("PreTest"); | |
1645 for (var i=0; i<this.preTest.options.length; i++) | |
1646 { | |
1647 setupPreTest.appendChild(this.preTest.options[i].exportXML(root)); | |
1648 } | |
1649 | |
1650 var setupPostTest = root.createElement("PostTest"); | |
1651 for (var i=0; i<this.postTest.options.length; i++) | |
1652 { | |
1653 setupPostTest.appendChild(this.postTest.options[i].exportXML(root)); | |
1654 } | |
1655 | |
1656 setupNode.appendChild(setupPreTest); | |
1657 setupNode.appendChild(setupPostTest); | |
1658 | |
1659 // <Metric> tag | |
1660 var Metric = root.createElement("Metric"); | |
1661 for (var i=0; i<this.metrics.length; i++) | |
1662 { | |
1663 var metricEnable = root.createElement("metricEnable"); | |
1664 metricEnable.textContent = this.metrics[i].enabled; | |
1665 Metric.appendChild(metricEnable); | |
1666 } | |
1667 setupNode.appendChild(Metric); | |
1668 | |
1669 // <interface> tag | |
1670 var CommonInterface = root.createElement("interface"); | |
1671 for (var i=0; i<this.commonInterface.options.length; i++) | |
1672 { | |
1673 var CIObj = this.commonInterface.options[i]; | |
1674 var CINode = root.createElement(CIObj.type); | |
1675 if (CIObj.type == "check") {CINode.setAttribute("name",CIObj.check);} | |
1676 else {CINode.setAttribute("name",CIObj.name);} | |
1677 CommonInterface.appendChild(CINode); | |
1678 } | |
1679 setupNode.appendChild(CommonInterface); | |
1680 | |
1681 root.getElementsByTagName("BrowserEvalProjectDocument")[0].appendChild(setupNode); | |
1682 // Time for the <audioHolder> tags | |
1683 for (var ahIndex = 0; ahIndex < this.audioHolders.length; ahIndex++) | |
1684 { | |
1685 var node = this.audioHolders[ahIndex].encode(root); | |
1686 root.getElementsByTagName("BrowserEvalProjectDocument")[0].appendChild(node); | |
1687 } | |
1688 return root; | |
1689 }; | |
1690 | |
1691 this.prepostNode = function(type) { | |
1692 this.type = type; | |
1693 this.options = []; | |
1694 | |
1695 this.OptionNode = function() { | |
1696 | |
1697 this.childOption = function() { | |
1698 this.type = 'option'; | |
1699 this.id = null; | |
1700 this.name = undefined; | |
1701 this.text = null; | |
1702 }; | |
1703 | |
1704 this.type = undefined; | |
1705 this.id = undefined; | |
1706 this.mandatory = undefined; | |
1707 this.question = undefined; | |
1708 this.statement = undefined; | |
1709 this.boxsize = undefined; | |
1710 this.options = []; | |
1711 this.min = undefined; | |
1712 this.max = undefined; | |
1713 this.step = undefined; | |
1714 | |
1715 this.decode = function(child) | |
1716 { | |
1717 this.type = child.nodeName; | |
1718 if (child.nodeName == "question") { | |
1719 this.id = child.id; | |
1720 this.mandatory; | |
1721 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;} | |
1722 else {this.mandatory = false;} | |
1723 this.question = child.textContent; | |
1724 if (child.getAttribute('boxsize') == null) { | |
1725 this.boxsize = 'normal'; | |
1726 } else { | |
1727 this.boxsize = child.getAttribute('boxsize'); | |
1728 } | |
1729 } else if (child.nodeName == "statement") { | |
1730 this.statement = child.textContent; | |
1731 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") { | |
1732 var element = child.firstElementChild; | |
1733 this.id = child.id; | |
1734 if (element == null) { | |
1735 console.log('Malformed' +child.nodeName+ 'entry'); | |
1736 this.statement = 'Malformed' +child.nodeName+ 'entry'; | |
1737 this.type = 'statement'; | |
1738 } else { | |
1739 this.options = []; | |
1740 while (element != null) { | |
1741 if (element.nodeName == 'statement' && this.statement == undefined){ | |
1742 this.statement = element.textContent; | |
1743 } else if (element.nodeName == 'option') { | |
1744 var node = new this.childOption(); | |
1745 node.id = element.id; | |
1746 node.name = element.getAttribute('name'); | |
1747 node.text = element.textContent; | |
1748 this.options.push(node); | |
1749 } | |
1750 element = element.nextElementSibling; | |
1751 } | |
1752 } | |
1753 } else if (child.nodeName == "number") { | |
1754 this.statement = child.textContent; | |
1755 this.id = child.id; | |
1756 this.min = child.getAttribute('min'); | |
1757 this.max = child.getAttribute('max'); | |
1758 this.step = child.getAttribute('step'); | |
1759 } | |
1760 }; | |
1761 | |
1762 this.exportXML = function(root) | |
1763 { | |
1764 var node = root.createElement(this.type); | |
1765 switch(this.type) | |
1766 { | |
1767 case "statement": | |
1768 node.textContent = this.statement; | |
1769 break; | |
1770 case "question": | |
1771 node.id = this.id; | |
1772 node.setAttribute("mandatory",this.mandatory); | |
1773 node.setAttribute("boxsize",this.boxsize); | |
1774 node.textContent = this.question; | |
1775 break; | |
1776 case "number": | |
1777 node.id = this.id; | |
1778 node.setAttribute("mandatory",this.mandatory); | |
1779 node.setAttribute("min", this.min); | |
1780 node.setAttribute("max", this.max); | |
1781 node.setAttribute("step", this.step); | |
1782 node.textContent = this.statement; | |
1783 break; | |
1784 case "checkbox": | |
1785 node.id = this.id; | |
1786 var statement = root.createElement("statement"); | |
1787 statement.textContent = this.statement; | |
1788 node.appendChild(statement); | |
1789 for (var i=0; i<this.options.length; i++) | |
1790 { | |
1791 var option = this.options[i]; | |
1792 var optionNode = root.createElement("option"); | |
1793 optionNode.id = option.id; | |
1794 optionNode.textContent = option.text; | |
1795 node.appendChild(optionNode); | |
1796 } | |
1797 break; | |
1798 case "radio": | |
1799 node.id = this.id; | |
1800 var statement = root.createElement("statement"); | |
1801 statement.textContent = this.statement; | |
1802 node.appendChild(statement); | |
1803 for (var i=0; i<this.options.length; i++) | |
1804 { | |
1805 var option = this.options[i]; | |
1806 var optionNode = root.createElement("option"); | |
1807 optionNode.setAttribute("name",option.name); | |
1808 optionNode.textContent = option.text; | |
1809 node.appendChild(optionNode); | |
1810 } | |
1811 break; | |
1812 } | |
1813 return node; | |
1814 }; | |
1815 }; | |
1816 this.construct = function(Collection) | |
1817 { | |
1818 if (Collection.childElementCount != 0) { | |
1819 var child = Collection.firstElementChild; | |
1820 var node = new this.OptionNode(); | |
1821 node.decode(child); | |
1822 this.options.push(node); | |
1823 while (child.nextElementSibling != null) { | |
1824 child = child.nextElementSibling; | |
1825 node = new this.OptionNode(); | |
1826 node.decode(child); | |
1827 this.options.push(node); | |
1828 } | |
1829 } | |
1830 }; | |
1831 }; | |
1832 this.preTest = new this.prepostNode("pretest"); | |
1833 this.postTest = new this.prepostNode("posttest"); | |
1834 | |
1835 this.metricNode = function(name) { | |
1836 this.enabled = name; | |
1837 }; | |
1838 | |
1839 this.audioHolderNode = function(parent) { | |
1840 this.type = 'audioHolder'; | |
1841 this.presentedId = undefined; | |
1842 this.id = undefined; | |
1843 this.hostURL = undefined; | |
1844 this.sampleRate = undefined; | |
1845 this.randomiseOrder = undefined; | |
1846 this.loop = undefined; | |
1847 this.elementComments = undefined; | |
1848 this.outsideReference = null; | |
1849 this.loudness = null; | |
1850 this.preTest = new parent.prepostNode("pretest"); | |
1851 this.postTest = new parent.prepostNode("pretest"); | |
1852 this.interfaces = []; | |
1853 this.commentBoxPrefix = "Comment on track"; | |
1854 this.audioElements = []; | |
1855 this.commentQuestions = []; | |
1856 | |
1857 this.decode = function(parent,xml) | |
1858 { | |
1859 this.presentedId = parent.audioHolders.length; | |
1860 this.id = xml.id; | |
1861 this.hostURL = xml.getAttribute('hostURL'); | |
1862 this.sampleRate = xml.getAttribute('sampleRate'); | |
1863 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;} | |
1864 else {this.randomiseOrder = false;} | |
1865 this.repeatCount = xml.getAttribute('repeatCount'); | |
1866 if (xml.getAttribute('loop') == 'true') {this.loop = true;} | |
1867 else {this.loop == false;} | |
1868 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;} | |
1869 else {this.elementComments = false;} | |
1870 if (typeof parent.loudness === "number") | |
1871 { | |
1872 this.loudness = parent.loudness; | |
1873 } | |
1874 if (xml.getAttribute('loudness') != null) | |
1875 { | |
1876 var XMLloudness = xml.getAttribute('loudness'); | |
1877 if (isNaN(Number(XMLloudness)) == false) | |
1878 { | |
1879 this.loudness = Number(XMLloudness); | |
1880 } | |
1881 } | |
1882 var setupPreTestNode = xml.getElementsByTagName('PreTest'); | |
1883 if (setupPreTestNode.length != 0) | |
1884 { | |
1885 setupPreTestNode = setupPreTestNode[0]; | |
1886 this.preTest.construct(setupPreTestNode); | |
1887 } | |
1888 | |
1889 var setupPostTestNode = xml.getElementsByTagName('PostTest'); | |
1890 if (setupPostTestNode.length != 0) | |
1891 { | |
1892 setupPostTestNode = setupPostTestNode[0]; | |
1893 this.postTest.construct(setupPostTestNode); | |
1894 } | |
1895 | |
1896 var interfaceDOM = xml.getElementsByTagName('interface'); | |
1897 for (var i=0; i<interfaceDOM.length; i++) { | |
1898 var node = new this.interfaceNode(); | |
1899 node.decode(interfaceDOM[i]); | |
1900 this.interfaces.push(node); | |
1901 } | |
1902 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix'); | |
1903 if (this.commentBoxPrefix.length != 0) { | |
1904 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent; | |
1905 } else { | |
1906 this.commentBoxPrefix = "Comment on track"; | |
1907 } | |
1908 var audioElementsDOM = xml.getElementsByTagName('audioElements'); | |
1909 for (var i=0; i<audioElementsDOM.length; i++) { | |
1910 var node = new this.audioElementNode(); | |
1911 node.decode(this,audioElementsDOM[i]); | |
1912 if (audioElementsDOM[i].getAttribute('type') == 'outsidereference') { | |
1913 if (this.outsideReference == null) { | |
1914 this.outsideReference = node; | |
1915 } else { | |
1916 console.log('Error only one audioelement can be of type outsidereference per audioholder'); | |
1917 this.audioElements.push(node); | |
1918 console.log('Element id '+audioElementsDOM[i].id+' made into normal node'); | |
1919 } | |
1920 } else { | |
1921 this.audioElements.push(node); | |
1922 } | |
1923 } | |
1924 | |
1925 if (this.randomiseOrder == true && typeof randomiseOrder === "function") | |
1926 { | |
1927 this.audioElements = randomiseOrder(this.audioElements); | |
1928 } | |
1929 | |
1930 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion'); | |
1931 for (var i=0; i<commentQuestionsDOM.length; i++) { | |
1932 var node = new this.commentQuestionNode(); | |
1933 node.decode(commentQuestionsDOM[i]); | |
1934 this.commentQuestions.push(node); | |
1935 } | |
1936 }; | |
1937 | |
1938 this.encode = function(root) | |
1939 { | |
1940 var AHNode = root.createElement("audioHolder"); | |
1941 AHNode.id = this.id; | |
1942 AHNode.setAttribute("hostURL",this.hostURL); | |
1943 AHNode.setAttribute("sampleRate",this.sampleRate); | |
1944 AHNode.setAttribute("randomiseOrder",this.randomiseOrder); | |
1945 AHNode.setAttribute("repeatCount",this.repeatCount); | |
1946 AHNode.setAttribute("loop",this.loop); | |
1947 AHNode.setAttribute("elementComments",this.elementComments); | |
1948 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);} | |
1949 | |
1950 for (var i=0; i<this.interfaces.length; i++) | |
1951 { | |
1952 AHNode.appendChild(this.interfaces[i].encode(root)); | |
1953 } | |
1954 | |
1955 for (var i=0; i<this.audioElements.length; i++) { | |
1956 AHNode.appendChild(this.audioElements[i].encode(root)); | |
1957 } | |
1958 // Create <CommentQuestion> | |
1959 for (var i=0; i<this.commentQuestions.length; i++) | |
1960 { | |
1961 AHNode.appendChild(this.commentQuestions[i].exportXML(root)); | |
1962 } | |
1963 | |
1964 // Create <PreTest> | |
1965 var AHPreTest = root.createElement("PreTest"); | |
1966 for (var i=0; i<this.preTest.options.length; i++) | |
1967 { | |
1968 AHPreTest.appendChild(this.preTest.options[i].exportXML(root)); | |
1969 } | |
1970 | |
1971 var AHPostTest = root.createElement("PostTest"); | |
1972 for (var i=0; i<this.postTest.options.length; i++) | |
1973 { | |
1974 AHPostTest.appendChild(this.postTest.options[i].exportXML(root)); | |
1975 } | |
1976 AHNode.appendChild(AHPreTest); | |
1977 AHNode.appendChild(AHPostTest); | |
1978 return AHNode; | |
1979 }; | |
1980 | |
1981 this.interfaceNode = function() { | |
1982 this.title = undefined; | |
1983 this.options = []; | |
1984 this.scale = []; | |
1985 this.name = undefined; | |
1986 this.decode = function(DOM) | |
1987 { | |
1988 var title = DOM.getElementsByTagName('title'); | |
1989 if (title.length == 0) {this.title = null;} | |
1990 else {this.title = title[0].textContent;} | |
1991 var name = DOM.getAttribute("name"); | |
1992 if (name != undefined) {this.name = name;} | |
1993 this.options = parent.commonInterface.options; | |
1994 var scale = DOM.getElementsByTagName('scale'); | |
1995 this.scale = []; | |
1996 for (var i=0; i<scale.length; i++) { | |
1997 var arr = [null, null]; | |
1998 arr[0] = scale[i].getAttribute('position'); | |
1999 arr[1] = scale[i].textContent; | |
2000 this.scale.push(arr); | |
2001 } | |
2002 }; | |
2003 this.encode = function(root) | |
2004 { | |
2005 var node = root.createElement("interface"); | |
2006 if (this.title != undefined) | |
2007 { | |
2008 var title = root.createElement("title"); | |
2009 title.textContent = this.title; | |
2010 node.appendChild(title); | |
2011 } | |
2012 for (var i=0; i<this.options.length; i++) | |
2013 { | |
2014 var optionNode = root.createElement(this.options[i].type); | |
2015 if (this.options[i].type == "option") | |
2016 { | |
2017 optionNode.setAttribute("name",this.options[i].name); | |
2018 } else if (this.options[i].type == "check") { | |
2019 optionNode.setAttribute("check",this.options[i].check); | |
2020 } else if (this.options[i].type == "scalerange") { | |
2021 optionNode.setAttribute("min",this.options[i].min*100); | |
2022 optionNode.setAttribute("max",this.options[i].max*100); | |
2023 } | |
2024 node.appendChild(optionNode); | |
2025 } | |
2026 for (var i=0; i<this.scale.length; i++) { | |
2027 var scale = root.createElement("scale"); | |
2028 scale.setAttribute("position",this.scale[i][0]); | |
2029 scale.textContent = this.scale[i][1]; | |
2030 node.appendChild(scale); | |
2031 } | |
2032 return node; | |
2033 }; | |
2034 }; | |
2035 | |
2036 this.audioElementNode = function() { | |
2037 this.url = null; | |
2038 this.id = null; | |
2039 this.parent = null; | |
2040 this.type = "normal"; | |
2041 this.marker = false; | |
2042 this.enforce = false; | |
2043 this.gain = 1.0; | |
2044 this.decode = function(parent,xml) | |
2045 { | |
2046 this.url = xml.getAttribute('url'); | |
2047 this.id = xml.id; | |
2048 this.parent = parent; | |
2049 this.type = xml.getAttribute('type'); | |
2050 var gain = xml.getAttribute('gain'); | |
2051 if (isNaN(gain) == false && gain != null) | |
2052 { | |
2053 this.gain = decibelToLinear(Number(gain)); | |
2054 } | |
2055 if (this.type == null) {this.type = "normal";} | |
2056 if (this.type == 'anchor') {this.anchor = true;} | |
2057 else {this.anchor = false;} | |
2058 if (this.type == 'reference') {this.reference = true;} | |
2059 else {this.reference = false;} | |
2060 if (this.anchor == true || this.reference == true) | |
2061 { | |
2062 this.marker = xml.getAttribute('marker'); | |
2063 if (this.marker != undefined) | |
2064 { | |
2065 this.marker = Number(this.marker); | |
2066 if (isNaN(this.marker) == false) | |
2067 { | |
2068 if (this.marker > 1) | |
2069 { this.marker /= 100.0;} | |
2070 if (this.marker >= 0 && this.marker <= 1) | |
2071 { | |
2072 this.enforce = true; | |
2073 return; | |
2074 } else { | |
2075 console.log("ERROR - Marker of audioElement "+this.id+" is not between 0 and 1 (float) or 0 and 100 (integer)!"); | |
2076 console.log("ERROR - Marker not enforced!"); | |
2077 } | |
2078 } else { | |
2079 console.log("ERROR - Marker of audioElement "+this.id+" is not a number!"); | |
2080 console.log("ERROR - Marker not enforced!"); | |
2081 } | |
2082 } | |
2083 } | |
2084 }; | |
2085 this.encode = function(root) | |
2086 { | |
2087 var AENode = root.createElement("audioElements"); | |
2088 AENode.id = this.id; | |
2089 AENode.setAttribute("url",this.url); | |
2090 AENode.setAttribute("type",this.type); | |
2091 AENode.setAttribute("gain",linearToDecibel(this.gain)); | |
2092 if (this.marker != false) | |
2093 { | |
2094 AENode.setAttribute("marker",this.marker*100); | |
2095 } | |
2096 return AENode; | |
2097 }; | |
2098 }; | |
2099 | |
2100 this.commentQuestionNode = function(xml) { | |
2101 this.id = null; | |
2102 this.type = undefined; | |
2103 this.question = undefined; | |
2104 this.options = []; | |
2105 this.statement = undefined; | |
2106 | |
2107 this.childOption = function() { | |
2108 this.type = 'option'; | |
2109 this.name = null; | |
2110 this.text = null; | |
2111 }; | |
2112 this.exportXML = function(root) | |
2113 { | |
2114 var CQNode = root.createElement("CommentQuestion"); | |
2115 CQNode.id = this.id; | |
2116 CQNode.setAttribute("type",this.type); | |
2117 switch(this.type) | |
2118 { | |
2119 case "text": | |
2120 CQNode.textContent = this.question; | |
2121 break; | |
2122 case "radio": | |
2123 var statement = root.createElement("statement"); | |
2124 statement.textContent = this.statement; | |
2125 CQNode.appendChild(statement); | |
2126 for (var i=0; i<this.options.length; i++) | |
2127 { | |
2128 var optionNode = root.createElement("option"); | |
2129 optionNode.setAttribute("name",this.options[i].name); | |
2130 optionNode.textContent = this.options[i].text; | |
2131 CQNode.appendChild(optionNode); | |
2132 } | |
2133 break; | |
2134 case "checkbox": | |
2135 var statement = root.createElement("statement"); | |
2136 statement.textContent = this.statement; | |
2137 CQNode.appendChild(statement); | |
2138 for (var i=0; i<this.options.length; i++) | |
2139 { | |
2140 var optionNode = root.createElement("option"); | |
2141 optionNode.setAttribute("name",this.options[i].name); | |
2142 optionNode.textContent = this.options[i].text; | |
2143 CQNode.appendChild(optionNode); | |
2144 } | |
2145 break; | |
2146 } | |
2147 return CQNode; | |
2148 }; | |
2149 this.decode = function(xml) { | |
2150 this.id = xml.id; | |
2151 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;} | |
2152 else {this.mandatory = false;} | |
2153 this.type = xml.getAttribute('type'); | |
2154 if (this.type == undefined) {this.type = 'text';} | |
2155 switch (this.type) { | |
2156 case 'text': | |
2157 this.question = xml.textContent; | |
2158 break; | |
2159 case 'radio': | |
2160 var child = xml.firstElementChild; | |
2161 this.options = []; | |
2162 while (child != undefined) { | |
2163 if (child.nodeName == 'statement' && this.statement == undefined) { | |
2164 this.statement = child.textContent; | |
2165 } else if (child.nodeName == 'option') { | |
2166 var node = new this.childOption(); | |
2167 node.name = child.getAttribute('name'); | |
2168 node.text = child.textContent; | |
2169 this.options.push(node); | |
2170 } | |
2171 child = child.nextElementSibling; | |
2172 } | |
2173 break; | |
2174 case 'checkbox': | |
2175 var child = xml.firstElementChild; | |
2176 this.options = []; | |
2177 while (child != undefined) { | |
2178 if (child.nodeName == 'statement' && this.statement == undefined) { | |
2179 this.statement = child.textContent; | |
2180 } else if (child.nodeName == 'option') { | |
2181 var node = new this.childOption(); | |
2182 node.name = child.getAttribute('name'); | |
2183 node.text = child.textContent; | |
2184 this.options.push(node); | |
2185 } | |
2186 child = child.nextElementSibling; | |
2187 } | |
2188 break; | |
2189 } | |
2190 }; | |
2191 }; | |
2192 }; | |
2193 } | |
2194 | |
2195 function Interface(specificationObject) { | |
2196 // This handles the bindings between the interface and the audioEngineContext; | |
2197 this.specification = specificationObject; | |
2198 this.insertPoint = document.getElementById("topLevelBody"); | |
2199 | |
2200 this.newPage = function(audioHolderObject) | |
2201 { | |
2202 audioEngineContext.newTestPage(); | |
2203 /// CHECK FOR SAMPLE RATE COMPATIBILITY | |
2204 if (audioHolderObject.sampleRate != undefined) { | |
2205 if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) { | |
2206 var errStr = 'Sample rates do not match! Requested '+Number(audioHolderObject.sampleRate)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.'; | |
2207 alert(errStr); | |
2208 return; | |
2209 } | |
2210 } | |
2211 | |
2212 audioEngineContext.loopPlayback = audioHolderObject.loop; | |
2213 // Delete any previous audioObjects associated with the audioEngine | |
2214 audioEngineContext.audioObjects = []; | |
2215 interfaceContext.deleteCommentBoxes(); | |
2216 interfaceContext.deleteCommentQuestions(); | |
2217 loadTest(audioHolderObject); | |
2218 }; | |
2219 | |
2220 // Bounded by interface!! | |
2221 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels | |
2222 // For example, APE returns the slider position normalised in a <value> tag. | |
2223 this.interfaceObjects = []; | |
2224 this.interfaceObject = function(){}; | |
2225 | |
2226 this.resizeWindow = function(event) | |
2227 { | |
2228 popup.resize(event); | |
2229 for(var i=0; i<this.commentBoxes.length; i++) | |
2230 {this.commentBoxes[i].resize();} | |
2231 for(var i=0; i<this.commentQuestions.length; i++) | |
2232 {this.commentQuestions[i].resize();} | |
2233 try | |
2234 { | |
2235 resizeWindow(event); | |
2236 } | |
2237 catch(err) | |
2238 { | |
2239 console.log("Warning - Interface does not have Resize option"); | |
2240 console.log(err); | |
2241 } | |
2242 }; | |
2243 | |
2244 this.returnNavigator = function() | |
2245 { | |
2246 var node = document.createElement("navigator"); | |
2247 var platform = document.createElement("platform"); | |
2248 platform.textContent = navigator.platform; | |
2249 var vendor = document.createElement("vendor"); | |
2250 vendor.textContent = navigator.vendor; | |
2251 var userAgent = document.createElement("uagent"); | |
2252 userAgent.textContent = navigator.userAgent; | |
2253 node.appendChild(platform); | |
2254 node.appendChild(vendor); | |
2255 node.appendChild(userAgent); | |
2256 return node; | |
2257 }; | |
2258 | |
2259 this.commentBoxes = []; | |
2260 this.elementCommentBox = function(audioObject) { | |
2261 var element = audioObject.specification; | |
2262 this.audioObject = audioObject; | |
2263 this.id = audioObject.id; | |
2264 var audioHolderObject = audioObject.specification.parent; | |
2265 // Create document objects to hold the comment boxes | |
2266 this.trackComment = document.createElement('div'); | |
2267 this.trackComment.className = 'comment-div'; | |
2268 this.trackComment.id = 'comment-div-'+audioObject.id; | |
2269 // Create a string next to each comment asking for a comment | |
2270 this.trackString = document.createElement('span'); | |
2271 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id; | |
2272 // Create the HTML5 comment box 'textarea' | |
2273 this.trackCommentBox = document.createElement('textarea'); | |
2274 this.trackCommentBox.rows = '4'; | |
2275 this.trackCommentBox.cols = '100'; | |
2276 this.trackCommentBox.name = 'trackComment'+audioObject.id; | |
2277 this.trackCommentBox.className = 'trackComment'; | |
2278 var br = document.createElement('br'); | |
2279 // Add to the holder. | |
2280 this.trackComment.appendChild(this.trackString); | |
2281 this.trackComment.appendChild(br); | |
2282 this.trackComment.appendChild(this.trackCommentBox); | |
2283 | |
2284 this.exportXMLDOM = function() { | |
2285 var root = document.createElement('comment'); | |
2286 if (this.audioObject.specification.parent.elementComments) { | |
2287 var question = document.createElement('question'); | |
2288 question.textContent = this.trackString.textContent; | |
2289 var response = document.createElement('response'); | |
2290 response.textContent = this.trackCommentBox.value; | |
2291 console.log("Comment frag-"+this.id+": "+response.textContent); | |
2292 root.appendChild(question); | |
2293 root.appendChild(response); | |
2294 } | |
2295 return root; | |
2296 }; | |
2297 this.resize = function() | |
2298 { | |
2299 var boxwidth = (window.innerWidth-100)/2; | |
2300 if (boxwidth >= 600) | |
2301 { | |
2302 boxwidth = 600; | |
2303 } | |
2304 else if (boxwidth < 400) | |
2305 { | |
2306 boxwidth = 400; | |
2307 } | |
2308 this.trackComment.style.width = boxwidth+"px"; | |
2309 this.trackCommentBox.style.width = boxwidth-6+"px"; | |
2310 }; | |
2311 this.resize(); | |
2312 }; | |
2313 | |
2314 this.commentQuestions = []; | |
2315 | |
2316 this.commentBox = function(commentQuestion) { | |
2317 this.specification = commentQuestion; | |
2318 // Create document objects to hold the comment boxes | |
2319 this.holder = document.createElement('div'); | |
2320 this.holder.className = 'comment-div'; | |
2321 // Create a string next to each comment asking for a comment | |
2322 this.string = document.createElement('span'); | |
2323 this.string.innerHTML = commentQuestion.question; | |
2324 // Create the HTML5 comment box 'textarea' | |
2325 this.textArea = document.createElement('textarea'); | |
2326 this.textArea.rows = '4'; | |
2327 this.textArea.cols = '100'; | |
2328 this.textArea.className = 'trackComment'; | |
2329 var br = document.createElement('br'); | |
2330 // Add to the holder. | |
2331 this.holder.appendChild(this.string); | |
2332 this.holder.appendChild(br); | |
2333 this.holder.appendChild(this.textArea); | |
2334 | |
2335 this.exportXMLDOM = function() { | |
2336 var root = document.createElement('comment'); | |
2337 root.id = this.specification.id; | |
2338 root.setAttribute('type',this.specification.type); | |
2339 root.textContent = this.textArea.value; | |
2340 console.log("Question: "+this.string.textContent); | |
2341 console.log("Response: "+root.textContent); | |
2342 return root; | |
2343 }; | |
2344 this.resize = function() | |
2345 { | |
2346 var boxwidth = (window.innerWidth-100)/2; | |
2347 if (boxwidth >= 600) | |
2348 { | |
2349 boxwidth = 600; | |
2350 } | |
2351 else if (boxwidth < 400) | |
2352 { | |
2353 boxwidth = 400; | |
2354 } | |
2355 this.holder.style.width = boxwidth+"px"; | |
2356 this.textArea.style.width = boxwidth-6+"px"; | |
2357 }; | |
2358 this.resize(); | |
2359 }; | |
2360 | |
2361 this.radioBox = function(commentQuestion) { | |
2362 this.specification = commentQuestion; | |
2363 // Create document objects to hold the comment boxes | |
2364 this.holder = document.createElement('div'); | |
2365 this.holder.className = 'comment-div'; | |
2366 // Create a string next to each comment asking for a comment | |
2367 this.string = document.createElement('span'); | |
2368 this.string.innerHTML = commentQuestion.statement; | |
2369 var br = document.createElement('br'); | |
2370 // Add to the holder. | |
2371 this.holder.appendChild(this.string); | |
2372 this.holder.appendChild(br); | |
2373 this.options = []; | |
2374 this.inputs = document.createElement('div'); | |
2375 this.span = document.createElement('div'); | |
2376 this.inputs.align = 'center'; | |
2377 this.inputs.style.marginLeft = '12px'; | |
2378 this.span.style.marginLeft = '12px'; | |
2379 this.span.align = 'center'; | |
2380 this.span.style.marginTop = '15px'; | |
2381 | |
2382 var optCount = commentQuestion.options.length; | |
2383 for (var i=0; i<optCount; i++) | |
2384 { | |
2385 var div = document.createElement('div'); | |
2386 div.style.width = '80px'; | |
2387 div.style.float = 'left'; | |
2388 var input = document.createElement('input'); | |
2389 input.type = 'radio'; | |
2390 input.name = commentQuestion.id; | |
2391 input.setAttribute('setvalue',commentQuestion.options[i].name); | |
2392 input.className = 'comment-radio'; | |
2393 div.appendChild(input); | |
2394 this.inputs.appendChild(div); | |
2395 | |
2396 | |
2397 div = document.createElement('div'); | |
2398 div.style.width = '80px'; | |
2399 div.style.float = 'left'; | |
2400 div.align = 'center'; | |
2401 var span = document.createElement('span'); | |
2402 span.textContent = commentQuestion.options[i].text; | |
2403 span.className = 'comment-radio-span'; | |
2404 div.appendChild(span); | |
2405 this.span.appendChild(div); | |
2406 this.options.push(input); | |
2407 } | |
2408 this.holder.appendChild(this.span); | |
2409 this.holder.appendChild(this.inputs); | |
2410 | |
2411 this.exportXMLDOM = function() { | |
2412 var root = document.createElement('comment'); | |
2413 root.id = this.specification.id; | |
2414 root.setAttribute('type',this.specification.type); | |
2415 var question = document.createElement('question'); | |
2416 question.textContent = this.string.textContent; | |
2417 var response = document.createElement('response'); | |
2418 var i=0; | |
2419 while(this.options[i].checked == false) { | |
2420 i++; | |
2421 if (i >= this.options.length) { | |
2422 break; | |
2423 } | |
2424 } | |
2425 if (i >= this.options.length) { | |
2426 response.textContent = 'null'; | |
2427 } else { | |
2428 response.textContent = this.options[i].getAttribute('setvalue'); | |
2429 response.setAttribute('number',i); | |
2430 } | |
2431 console.log('Comment: '+question.textContent); | |
2432 console.log('Response: '+response.textContent); | |
2433 root.appendChild(question); | |
2434 root.appendChild(response); | |
2435 return root; | |
2436 }; | |
2437 this.resize = function() | |
2438 { | |
2439 var boxwidth = (window.innerWidth-100)/2; | |
2440 if (boxwidth >= 600) | |
2441 { | |
2442 boxwidth = 600; | |
2443 } | |
2444 else if (boxwidth < 400) | |
2445 { | |
2446 boxwidth = 400; | |
2447 } | |
2448 this.holder.style.width = boxwidth+"px"; | |
2449 var text = this.holder.children[2]; | |
2450 var options = this.holder.children[3]; | |
2451 var optCount = options.children.length; | |
2452 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px'; | |
2453 var options = options.firstChild; | |
2454 var text = text.firstChild; | |
2455 options.style.marginRight = spanMargin; | |
2456 options.style.marginLeft = spanMargin; | |
2457 text.style.marginRight = spanMargin; | |
2458 text.style.marginLeft = spanMargin; | |
2459 while(options.nextSibling != undefined) | |
2460 { | |
2461 options = options.nextSibling; | |
2462 text = text.nextSibling; | |
2463 options.style.marginRight = spanMargin; | |
2464 options.style.marginLeft = spanMargin; | |
2465 text.style.marginRight = spanMargin; | |
2466 text.style.marginLeft = spanMargin; | |
2467 } | |
2468 }; | |
2469 this.resize(); | |
2470 }; | |
2471 | |
2472 this.checkboxBox = function(commentQuestion) { | |
2473 this.specification = commentQuestion; | |
2474 // Create document objects to hold the comment boxes | |
2475 this.holder = document.createElement('div'); | |
2476 this.holder.className = 'comment-div'; | |
2477 // Create a string next to each comment asking for a comment | |
2478 this.string = document.createElement('span'); | |
2479 this.string.innerHTML = commentQuestion.statement; | |
2480 var br = document.createElement('br'); | |
2481 // Add to the holder. | |
2482 this.holder.appendChild(this.string); | |
2483 this.holder.appendChild(br); | |
2484 this.options = []; | |
2485 this.inputs = document.createElement('div'); | |
2486 this.span = document.createElement('div'); | |
2487 this.inputs.align = 'center'; | |
2488 this.inputs.style.marginLeft = '12px'; | |
2489 this.span.style.marginLeft = '12px'; | |
2490 this.span.align = 'center'; | |
2491 this.span.style.marginTop = '15px'; | |
2492 | |
2493 var optCount = commentQuestion.options.length; | |
2494 for (var i=0; i<optCount; i++) | |
2495 { | |
2496 var div = document.createElement('div'); | |
2497 div.style.width = '80px'; | |
2498 div.style.float = 'left'; | |
2499 var input = document.createElement('input'); | |
2500 input.type = 'checkbox'; | |
2501 input.name = commentQuestion.id; | |
2502 input.setAttribute('setvalue',commentQuestion.options[i].name); | |
2503 input.className = 'comment-radio'; | |
2504 div.appendChild(input); | |
2505 this.inputs.appendChild(div); | |
2506 | |
2507 | |
2508 div = document.createElement('div'); | |
2509 div.style.width = '80px'; | |
2510 div.style.float = 'left'; | |
2511 div.align = 'center'; | |
2512 var span = document.createElement('span'); | |
2513 span.textContent = commentQuestion.options[i].text; | |
2514 span.className = 'comment-radio-span'; | |
2515 div.appendChild(span); | |
2516 this.span.appendChild(div); | |
2517 this.options.push(input); | |
2518 } | |
2519 this.holder.appendChild(this.span); | |
2520 this.holder.appendChild(this.inputs); | |
2521 | |
2522 this.exportXMLDOM = function() { | |
2523 var root = document.createElement('comment'); | |
2524 root.id = this.specification.id; | |
2525 root.setAttribute('type',this.specification.type); | |
2526 var question = document.createElement('question'); | |
2527 question.textContent = this.string.textContent; | |
2528 root.appendChild(question); | |
2529 console.log('Comment: '+question.textContent); | |
2530 for (var i=0; i<this.options.length; i++) { | |
2531 var response = document.createElement('response'); | |
2532 response.textContent = this.options[i].checked; | |
2533 response.setAttribute('name',this.options[i].getAttribute('setvalue')); | |
2534 root.appendChild(response); | |
2535 console.log('Response '+response.getAttribute('name') +': '+response.textContent); | |
2536 } | |
2537 return root; | |
2538 }; | |
2539 this.resize = function() | |
2540 { | |
2541 var boxwidth = (window.innerWidth-100)/2; | |
2542 if (boxwidth >= 600) | |
2543 { | |
2544 boxwidth = 600; | |
2545 } | |
2546 else if (boxwidth < 400) | |
2547 { | |
2548 boxwidth = 400; | |
2549 } | |
2550 this.holder.style.width = boxwidth+"px"; | |
2551 var text = this.holder.children[2]; | |
2552 var options = this.holder.children[3]; | |
2553 var optCount = options.children.length; | |
2554 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px'; | |
2555 var options = options.firstChild; | |
2556 var text = text.firstChild; | |
2557 options.style.marginRight = spanMargin; | |
2558 options.style.marginLeft = spanMargin; | |
2559 text.style.marginRight = spanMargin; | |
2560 text.style.marginLeft = spanMargin; | |
2561 while(options.nextSibling != undefined) | |
2562 { | |
2563 options = options.nextSibling; | |
2564 text = text.nextSibling; | |
2565 options.style.marginRight = spanMargin; | |
2566 options.style.marginLeft = spanMargin; | |
2567 text.style.marginRight = spanMargin; | |
2568 text.style.marginLeft = spanMargin; | |
2569 } | |
2570 }; | |
2571 this.resize(); | |
2572 }; | |
2573 | |
2574 this.createCommentBox = function(audioObject) { | |
2575 var node = new this.elementCommentBox(audioObject); | |
2576 this.commentBoxes.push(node); | |
2577 audioObject.commentDOM = node; | |
2578 return node; | |
2579 }; | |
2580 | |
2581 this.sortCommentBoxes = function() { | |
2582 var holder = []; | |
2583 while (this.commentBoxes.length > 0) { | |
2584 var node = this.commentBoxes.pop(0); | |
2585 holder[node.id] = node; | |
2586 } | |
2587 this.commentBoxes = holder; | |
2588 }; | |
2589 | |
2590 this.showCommentBoxes = function(inject, sort) { | |
2591 if (sort) {interfaceContext.sortCommentBoxes();} | |
2592 for (var i=0; i<interfaceContext.commentBoxes.length; i++) { | |
2593 inject.appendChild(this.commentBoxes[i].trackComment); | |
2594 } | |
2595 }; | |
2596 | |
2597 this.deleteCommentBoxes = function() { | |
2598 this.commentBoxes = []; | |
2599 }; | |
2600 | |
2601 this.createCommentQuestion = function(element) { | |
2602 var node; | |
2603 if (element.type == 'text') { | |
2604 node = new this.commentBox(element); | |
2605 } else if (element.type == 'radio') { | |
2606 node = new this.radioBox(element); | |
2607 } else if (element.type == 'checkbox') { | |
2608 node = new this.checkboxBox(element); | |
2609 } | |
2610 this.commentQuestions.push(node); | |
2611 return node; | |
2612 }; | |
2613 | |
2614 this.deleteCommentQuestions = function() | |
2615 { | |
2616 this.commentQuestions = []; | |
2617 }; | |
2618 | |
2619 this.playhead = new function() | |
2620 { | |
2621 this.object = document.createElement('div'); | |
2622 this.object.className = 'playhead'; | |
2623 this.object.align = 'left'; | |
2624 var curTime = document.createElement('div'); | |
2625 curTime.style.width = '50px'; | |
2626 this.curTimeSpan = document.createElement('span'); | |
2627 this.curTimeSpan.textContent = '00:00'; | |
2628 curTime.appendChild(this.curTimeSpan); | |
2629 this.object.appendChild(curTime); | |
2630 this.scrubberTrack = document.createElement('div'); | |
2631 this.scrubberTrack.className = 'playhead-scrub-track'; | |
2632 | |
2633 this.scrubberHead = document.createElement('div'); | |
2634 this.scrubberHead.id = 'playhead-scrubber'; | |
2635 this.scrubberTrack.appendChild(this.scrubberHead); | |
2636 this.object.appendChild(this.scrubberTrack); | |
2637 | |
2638 this.timePerPixel = 0; | |
2639 this.maxTime = 0; | |
2640 | |
2641 this.playbackObject; | |
2642 | |
2643 this.setTimePerPixel = function(audioObject) { | |
2644 //maxTime must be in seconds | |
2645 this.playbackObject = audioObject; | |
2646 this.maxTime = audioObject.buffer.buffer.duration; | |
2647 var width = 490; //500 - 10, 5 each side of the tracker head | |
2648 this.timePerPixel = this.maxTime/490; | |
2649 if (this.maxTime < 60) { | |
2650 this.curTimeSpan.textContent = '0.00'; | |
2651 } else { | |
2652 this.curTimeSpan.textContent = '00:00'; | |
2653 } | |
2654 }; | |
2655 | |
2656 this.update = function() { | |
2657 // Update the playhead position, startPlay must be called | |
2658 if (this.timePerPixel > 0) { | |
2659 var time = this.playbackObject.getCurrentPosition(); | |
2660 if (time > 0) { | |
2661 var width = 490; | |
2662 var pix = Math.floor(time/this.timePerPixel); | |
2663 this.scrubberHead.style.left = pix+'px'; | |
2664 if (this.maxTime > 60.0) { | |
2665 var secs = time%60; | |
2666 var mins = Math.floor((time-secs)/60); | |
2667 secs = secs.toString(); | |
2668 secs = secs.substr(0,2); | |
2669 mins = mins.toString(); | |
2670 this.curTimeSpan.textContent = mins+':'+secs; | |
2671 } else { | |
2672 time = time.toString(); | |
2673 this.curTimeSpan.textContent = time.substr(0,4); | |
2674 } | |
2675 } else { | |
2676 this.scrubberHead.style.left = '0px'; | |
2677 if (this.maxTime < 60) { | |
2678 this.curTimeSpan.textContent = '0.00'; | |
2679 } else { | |
2680 this.curTimeSpan.textContent = '00:00'; | |
2681 } | |
2682 } | |
2683 } | |
2684 }; | |
2685 | |
2686 this.interval = undefined; | |
2687 | |
2688 this.start = function() { | |
2689 if (this.playbackObject != undefined && this.interval == undefined) { | |
2690 if (this.maxTime < 60) { | |
2691 this.interval = setInterval(function(){interfaceContext.playhead.update();},10); | |
2692 } else { | |
2693 this.interval = setInterval(function(){interfaceContext.playhead.update();},100); | |
2694 } | |
2695 } | |
2696 }; | |
2697 this.stop = function() { | |
2698 clearInterval(this.interval); | |
2699 this.interval = undefined; | |
2700 if (this.maxTime < 60) { | |
2701 this.curTimeSpan.textContent = '0.00'; | |
2702 } else { | |
2703 this.curTimeSpan.textContent = '00:00'; | |
2704 } | |
2705 }; | |
2706 }; | |
2707 | |
2708 // Global Checkers | |
2709 // These functions will help enforce the checkers | |
2710 this.checkHiddenAnchor = function() | |
2711 { | |
2712 var audioHolder = testState.currentStateMap[testState.currentIndex]; | |
2713 if (audioHolder.anchorId != null) | |
2714 { | |
2715 var audioObject = audioEngineContext.audioObjects[audioHolder.anchorId]; | |
2716 if (audioObject.interfaceDOM.getValue() > audioObject.specification.marker && audioObject.interfaceDOM.enforce == true) | |
2717 { | |
2718 // Anchor is not set below | |
2719 console.log('Anchor node not below marker value'); | |
2720 alert('Please keep listening'); | |
2721 return false; | |
2722 } | |
2723 } | |
2724 return true; | |
2725 }; | |
2726 | |
2727 this.checkHiddenReference = function() | |
2728 { | |
2729 var audioHolder = testState.currentStateMap[testState.currentIndex]; | |
2730 if (audioHolder.referenceId != null) | |
2731 { | |
2732 var audioObject = audioEngineContext.audioObjects[audioHolder.referenceId]; | |
2733 if (audioObject.interfaceDOM.getValue() < audioObject.specification.marker && audioObject.interfaceDOM.enforce == true) | |
2734 { | |
2735 // Anchor is not set below | |
2736 console.log('Reference node not above marker value'); | |
2737 alert('Please keep listening'); | |
2738 return false; | |
2739 } | |
2740 } | |
2741 return true; | |
2742 }; | |
2743 | |
2744 this.checkFragmentsFullyPlayed = function () | |
2745 { | |
2746 // Checks the entire file has been played back | |
2747 // NOTE ! This will return true IF playback is Looped!!! | |
2748 if (audioEngineContext.loopPlayback) | |
2749 { | |
2750 console.log("WARNING - Looped source: Cannot check fragments are fully played"); | |
2751 return true; | |
2752 } | |
2753 var check_pass = true; | |
2754 var error_obj = []; | |
2755 for (var i = 0; i<audioEngineContext.audioObjects.length; i++) | |
2756 { | |
2757 var object = audioEngineContext.audioObjects[i]; | |
2758 var time = object.buffer.duration; | |
2759 var metric = object.metric; | |
2760 var passed = false; | |
2761 for (var j=0; j<metric.listenTracker.length; j++) | |
2762 { | |
2763 var bt = metric.listenTracker[j].getElementsByTagName('buffertime'); | |
2764 var start_time = Number(bt[0].getAttribute('start')); | |
2765 var stop_time = Number(bt[0].getAttribute('stop')); | |
2766 var delta = stop_time - start_time; | |
2767 if (delta >= time) | |
2768 { | |
2769 passed = true; | |
2770 break; | |
2771 } | |
2772 } | |
2773 if (passed == false) | |
2774 { | |
2775 check_pass = false; | |
2776 console.log("Continue listening to track-"+i); | |
2777 error_obj.push(i); | |
2778 } | |
2779 } | |
2780 if (check_pass == false) | |
2781 { | |
2782 var str_start = "You have not listened to fragments "; | |
2783 for (var i=0; i<error_obj.length; i++) | |
2784 { | |
2785 str_start += error_obj[i]; | |
2786 if (i != error_obj.length-1) | |
2787 { | |
2788 str_start += ', '; | |
2789 } | |
2790 } | |
2791 str_start += ". Please keep listening"; | |
2792 console.log("[ALERT]: "+str_start); | |
2793 alert(str_start); | |
2794 } | |
2795 }; | |
2796 } |