annotate core.js @ 1180:3303a667548a

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