annotate core.js @ 1110:f53b1098795f

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