annotate core.js @ 1179:c2e19bc54c3c

Bug #1564: index.html is blank, the links are included in demo.html. Popup now created in index.html rather than generated.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Thu, 11 Feb 2016 16:54:33 +0000
parents 9131490d80a3
children 3303a667548a
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@1124 741 pageHolder.push(page);
n@1124 742 }
n@1124 743 if (specification.randomiseOrder)
n@1124 744 {
n@1124 745 pageHolder = randomiseOrder(pageHolder);
n@1124 746 }
n@1124 747 for (var i=0; i<pageHolder.length; i++)
n@1124 748 {
n@1124 749 pageHolder[i].presentedId = i;
n@1124 750 }
n@1124 751 for (var i=0; i<specification.pages.length; i++)
n@1124 752 {
n@1124 753 if (specification.testPages < i && specification.testPages != 0) {break;}
n@1124 754 this.stateMap.push(pageHolder[i]);
n@1124 755
n@1124 756 }
n@1124 757 if (specification.preTest != null) {this.preTestSurvey = specification.preTest;}
n@1124 758 if (specification.postTest != null) {this.postTestSurvey = specification.postTest;}
n@1124 759
n@1118 760 if (this.stateMap.length > 0) {
n@1118 761 if(this.stateIndex != null) {
n@1118 762 console.log('NOTE - State already initialise');
n@1118 763 }
n@1118 764 this.stateIndex = -1;
n@1118 765 } else {
n@1118 766 console.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
n@1118 767 }
n@1118 768 };
n@1118 769 this.advanceState = function(){
n@1118 770 if (this.stateIndex == null) {
n@1118 771 this.initialise();
n@1118 772 }
n@1118 773 if (this.stateIndex == -1) {
n@1142 774 this.stateIndex++;
n@1118 775 console.log('Starting test...');
n@1124 776 if (this.preTestSurvey != null)
n@1124 777 {
n@1124 778 popup.initState(this.preTestSurvey,storage.globalPreTest);
n@1142 779 } else {
n@1142 780 this.advanceState();
n@1118 781 }
n@1124 782 } else if (this.stateIndex == this.stateMap.length)
n@1124 783 {
n@1124 784 // All test pages complete, post test
n@1124 785 console.log('Ending test ...');
n@1124 786 this.stateIndex++;
n@1124 787 if (this.postTestSurvey == null) {
n@1124 788 this.advanceState();
n@1118 789 } else {
n@1124 790 popup.initState(this.postTestSurvey,storage.globalPostTest);
n@1124 791 }
n@1124 792 } else if (this.stateIndex > this.stateMap.length)
n@1124 793 {
n@1124 794 createProjectSave(specification.projectReturn);
n@1124 795 }
n@1124 796 else
n@1124 797 {
n@1124 798 if (this.currentStateMap == null)
n@1124 799 {
n@1118 800 this.currentStateMap = this.stateMap[this.stateIndex];
n@1134 801 if (this.currentStateMap.randomiseOrder)
n@1134 802 {
n@1134 803 this.currentStateMap.audioElements = randomiseOrder(this.currentStateMap.audioElements);
n@1134 804 }
n@1154 805 this.currentStore = storage.createTestPageStore(this.currentStateMap);
n@1124 806 if (this.currentStateMap.preTest != null)
n@1124 807 {
n@1124 808 this.currentStatePosition = 'pre';
n@1124 809 popup.initState(this.currentStateMap.preTest,storage.testPages[this.stateIndex].preTest);
n@1118 810 } else {
n@1124 811 this.currentStatePosition = 'test';
n@1124 812 }
n@1124 813 interfaceContext.newPage(this.currentStateMap,storage.testPages[this.stateIndex]);
n@1124 814 return;
n@1124 815 }
n@1124 816 switch(this.currentStatePosition)
n@1124 817 {
n@1124 818 case 'pre':
n@1124 819 this.currentStatePosition = 'test';
n@1124 820 break;
n@1124 821 case 'test':
n@1124 822 this.currentStatePosition = 'post';
n@1124 823 // Save the data
n@1124 824 this.testPageCompleted();
n@1124 825 if (this.currentStateMap.postTest == null)
n@1124 826 {
n@1118 827 this.advanceState();
n@1124 828 return;
n@1124 829 } else {
n@1124 830 popup.initState(this.currentStateMap.postTest,storage.testPages[this.stateIndex].postTest);
n@1118 831 }
n@1124 832 break;
n@1124 833 case 'post':
n@1124 834 this.stateIndex++;
n@1124 835 this.currentStateMap = null;
n@1124 836 this.advanceState();
n@1124 837 break;
n@1124 838 };
n@1118 839 }
n@1118 840 };
n@1118 841
n@1124 842 this.testPageCompleted = function() {
n@1118 843 // Function called each time a test page has been completed
n@1124 844 var storePoint = storage.testPages[this.stateIndex];
n@1124 845 // First get the test metric
n@1124 846
n@1124 847 var metric = storePoint.XMLDOM.getElementsByTagName('metric')[0];
n@1118 848 if (audioEngineContext.metric.enableTestTimer)
n@1118 849 {
n@1124 850 var testTime = storePoint.parent.document.createElement('metricresult');
n@1118 851 testTime.id = 'testTime';
n@1118 852 testTime.textContent = audioEngineContext.timer.testDuration;
n@1118 853 metric.appendChild(testTime);
n@1118 854 }
n@1124 855
n@1118 856 var audioObjects = audioEngineContext.audioObjects;
n@1124 857 for (var ao of audioEngineContext.audioObjects)
n@1118 858 {
n@1124 859 ao.exportXMLDOM();
n@1118 860 }
n@1124 861 for (var element of interfaceContext.commentQuestions)
n@1124 862 {
n@1124 863 element.exportXMLDOM(storePoint);
n@1124 864 }
n@1124 865 pageXMLSave(storePoint.XMLDOM, this.currentStateMap);
n@1118 866 };
n@1118 867 }
n@1118 868
n@1118 869 function AudioEngine(specification) {
n@1118 870
n@1118 871 // Create two output paths, the main outputGain and fooGain.
n@1118 872 // Output gain is default to 1 and any items for playback route here
n@1118 873 // Foo gain is used for analysis to ensure paths get processed, but are not heard
n@1118 874 // because web audio will optimise and any route which does not go to the destination gets ignored.
n@1118 875 this.outputGain = audioContext.createGain();
n@1118 876 this.fooGain = audioContext.createGain();
n@1118 877 this.fooGain.gain = 0;
n@1118 878
n@1118 879 // Use this to detect playback state: 0 - stopped, 1 - playing
n@1118 880 this.status = 0;
n@1118 881
n@1118 882 // Connect both gains to output
n@1118 883 this.outputGain.connect(audioContext.destination);
n@1118 884 this.fooGain.connect(audioContext.destination);
n@1118 885
n@1118 886 // Create the timer Object
n@1118 887 this.timer = new timer();
n@1118 888 // Create session metrics
n@1118 889 this.metric = new sessionMetrics(this,specification);
n@1118 890
n@1118 891 this.loopPlayback = false;
n@1118 892
n@1124 893 this.pageStore = null;
n@1124 894
n@1118 895 // Create store for new audioObjects
n@1118 896 this.audioObjects = [];
n@1118 897
n@1118 898 this.buffers = [];
n@1118 899 this.bufferObj = function()
n@1118 900 {
n@1118 901 this.url = null;
n@1118 902 this.buffer = null;
n@1118 903 this.xmlRequest = new XMLHttpRequest();
n@1118 904 this.xmlRequest.parent = this;
n@1118 905 this.users = [];
n@1116 906 this.progress = 0;
n@1116 907 this.status = 0;
n@1142 908 this.ready = function()
n@1142 909 {
n@1116 910 if (this.status >= 2)
n@1116 911 {
n@1116 912 this.status = 3;
n@1116 913 }
n@1142 914 for (var i=0; i<this.users.length; i++)
n@1142 915 {
n@1142 916 this.users[i].state = 1;
n@1142 917 if (this.users[i].interfaceDOM != null)
n@1142 918 {
n@1142 919 this.users[i].bufferLoaded(this);
n@1142 920 }
n@1142 921 }
n@1142 922 };
n@1118 923 this.getMedia = function(url) {
n@1118 924 this.url = url;
n@1118 925 this.xmlRequest.open('GET',this.url,true);
n@1118 926 this.xmlRequest.responseType = 'arraybuffer';
n@1118 927
n@1118 928 var bufferObj = this;
n@1118 929
n@1118 930 // Create callback to decode the data asynchronously
n@1118 931 this.xmlRequest.onloadend = function() {
n@1153 932 // Use inbuilt WAVE decoder first
n@1153 933 var waveObj = new WAVE();
n@1153 934 if (waveObj.open(bufferObj.xmlRequest.response) == 0)
n@1153 935 {
n@1153 936 bufferObj.buffer = audioContext.createBuffer(waveObj.num_channels,waveObj.num_samples,waveObj.sample_rate);
n@1153 937 for (var c=0; c<waveObj.num_channels; c++)
n@1153 938 {
n@1153 939 var buffer_ptr = bufferObj.buffer.getChannelData(c);
n@1153 940 for (var n=0; n<waveObj.num_samples; n++)
n@1153 941 {
n@1153 942 buffer_ptr[n] = waveObj.decoded_data[c][n];
n@1153 943 }
n@1153 944 }
n@1116 945
n@1153 946 delete waveObj;
n@1153 947 } else {
n@1153 948 audioContext.decodeAudioData(bufferObj.xmlRequest.response, function(decodedData) {
n@1153 949 bufferObj.buffer = decodedData;
n@1153 950 }, function(e){
n@1153 951 // Should only be called if there was an error, but sometimes gets called continuously
n@1153 952 // Check here if the error is genuine
n@1153 953 if (bufferObj.xmlRequest.response == undefined) {
n@1153 954 // Genuine error
n@1153 955 console.log('FATAL - Error loading buffer on '+audioObj.id);
n@1153 956 if (request.status == 404)
n@1153 957 {
n@1153 958 console.log('FATAL - Fragment '+audioObj.id+' 404 error');
n@1153 959 console.log('URL: '+audioObj.url);
n@1153 960 errorSessionDump('Fragment '+audioObj.id+' 404 error');
n@1153 961 }
n@1116 962 this.status = -1;
n@1153 963 }
n@1153 964 });
n@1153 965 }
n@1153 966 if (bufferObj.buffer != undefined)
n@1153 967 {
n@1116 968 bufferObj.status = 2;
n@1153 969 calculateLoudness(bufferObj,"I");
n@1153 970 }
n@1118 971 };
n@1118 972 this.progress = 0;
n@1118 973 this.progressCallback = function(event){
n@1118 974 if (event.lengthComputable)
n@1118 975 {
n@1118 976 this.parent.progress = event.loaded / event.total;
n@1118 977 for (var i=0; i<this.parent.users.length; i++)
n@1118 978 {
n@1118 979 if(this.parent.users[i].interfaceDOM != null)
n@1118 980 {
n@1118 981 if (typeof this.parent.users[i].interfaceDOM.updateLoading === "function")
n@1118 982 {
n@1118 983 this.parent.users[i].interfaceDOM.updateLoading(this.parent.progress*100);
n@1118 984 }
n@1118 985 }
n@1118 986 }
n@1118 987 }
n@1118 988 };
n@1118 989 this.xmlRequest.addEventListener("progress", this.progressCallback);
n@1116 990 this.status = 1;
n@1118 991 this.xmlRequest.send();
n@1118 992 };
n@1116 993
n@1116 994 this.registerAudioObject = function(audioObject)
n@1116 995 {
n@1116 996 // Called by an audioObject to register to the buffer for use
n@1116 997 // First check if already in the register pool
n@1116 998 for (var objects of this.users)
n@1116 999 {
n@1116 1000 if (audioObject.id == objects.id){return 0;}
n@1116 1001 }
n@1116 1002 this.users.push(audioObject);
n@1116 1003 if (this.status == 3)
n@1116 1004 {
n@1116 1005 // The buffer is already ready, trigger bufferLoaded
n@1116 1006 audioObject.bufferLoaded(this);
n@1116 1007 }
n@1116 1008 }
n@1118 1009 };
n@1118 1010
n@1118 1011 this.play = function(id) {
n@1118 1012 // Start the timer and set the audioEngine state to playing (1)
n@1118 1013 if (this.status == 0 && this.loopPlayback) {
n@1118 1014 // Check if all audioObjects are ready
n@1118 1015 if(this.checkAllReady())
n@1118 1016 {
n@1118 1017 this.status = 1;
n@1118 1018 this.setSynchronousLoop();
n@1118 1019 }
n@1118 1020 }
n@1118 1021 else
n@1118 1022 {
n@1118 1023 this.status = 1;
n@1118 1024 }
n@1118 1025 if (this.status== 1) {
n@1118 1026 this.timer.startTest();
n@1118 1027 if (id == undefined) {
n@1118 1028 id = -1;
n@1118 1029 console.log('FATAL - Passed id was undefined - AudioEngineContext.play(id)');
n@1118 1030 return;
n@1118 1031 } else {
n@1118 1032 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]);
n@1118 1033 }
n@1118 1034 if (this.loopPlayback) {
n@1160 1035 var setTime = audioContext.currentTime+2;
n@1118 1036 for (var i=0; i<this.audioObjects.length; i++)
n@1118 1037 {
n@1160 1038 this.audioObjects[i].play(setTime-2);
n@1118 1039 if (id == i) {
n@1160 1040 this.audioObjects[i].loopStart(setTime);
n@1118 1041 } else {
n@1160 1042 this.audioObjects[i].loopStop(setTime);
n@1118 1043 }
n@1118 1044 }
n@1118 1045 } else {
n@1160 1046 var setTime = audioContext.currentTime+0.1;
n@1118 1047 for (var i=0; i<this.audioObjects.length; i++)
n@1118 1048 {
n@1118 1049 if (i != id) {
n@1160 1050 this.audioObjects[i].stop(setTime);
n@1118 1051 } else if (i == id) {
n@1160 1052 this.audioObjects[id].play(setTime);
n@1118 1053 }
n@1118 1054 }
n@1118 1055 }
n@1118 1056 interfaceContext.playhead.start();
n@1118 1057 }
n@1118 1058 };
n@1118 1059
n@1118 1060 this.stop = function() {
n@1178 1061 // Send stop and reset command to all playback buffers
n@1118 1062 if (this.status == 1) {
n@1160 1063 var setTime = audioContext.currentTime+0.1;
n@1118 1064 for (var i=0; i<this.audioObjects.length; i++)
n@1118 1065 {
n@1160 1066 this.audioObjects[i].stop(setTime);
n@1118 1067 }
n@1118 1068 interfaceContext.playhead.stop();
n@1118 1069 }
n@1118 1070 };
n@1118 1071
n@1118 1072 this.newTrack = function(element) {
n@1118 1073 // Pull data from given URL into new audio buffer
n@1118 1074 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
n@1118 1075
n@1118 1076 // Create the audioObject with ID of the new track length;
n@1118 1077 audioObjectId = this.audioObjects.length;
n@1118 1078 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
n@1118 1079
n@1118 1080 // Check if audioObject buffer is currently stored by full URL
n@1124 1081 var URL = testState.currentStateMap.hostURL + element.url;
n@1118 1082 var buffer = null;
n@1118 1083 for (var i=0; i<this.buffers.length; i++)
n@1118 1084 {
n@1118 1085 if (URL == this.buffers[i].url)
n@1118 1086 {
n@1118 1087 buffer = this.buffers[i];
n@1118 1088 break;
n@1118 1089 }
n@1118 1090 }
n@1118 1091 if (buffer == null)
n@1118 1092 {
n@1118 1093 console.log("[WARN]: Buffer was not loaded in pre-test! "+URL);
n@1118 1094 buffer = new this.bufferObj();
n@1116 1095 this.buffers.push(buffer);
n@1118 1096 buffer.getMedia(URL);
n@1118 1097 }
n@1118 1098 this.audioObjects[audioObjectId].specification = element;
n@1118 1099 this.audioObjects[audioObjectId].url = URL;
n@1124 1100 // Obtain store node
n@1124 1101 var aeNodes = this.pageStore.XMLDOM.getElementsByTagName('audioelement');
n@1124 1102 for (var i=0; i<aeNodes.length; i++)
n@1124 1103 {
n@1124 1104 if(aeNodes[i].id == element.id)
n@1124 1105 {
n@1124 1106 this.audioObjects[audioObjectId].storeDOM = aeNodes[i];
n@1124 1107 break;
n@1124 1108 }
n@1124 1109 }
n@1116 1110 buffer.registerAudioObject(this.audioObjects[audioObjectId]);
n@1118 1111 return this.audioObjects[audioObjectId];
n@1118 1112 };
n@1118 1113
n@1169 1114 this.newTestPage = function(audioHolderObject,store) {
n@1124 1115 this.pageStore = store;
n@1118 1116 this.state = 0;
n@1118 1117 this.audioObjectsReady = false;
n@1118 1118 this.metric.reset();
n@1118 1119 for (var i=0; i < this.buffers.length; i++)
n@1118 1120 {
n@1118 1121 this.buffers[i].users = [];
n@1118 1122 }
n@1118 1123 this.audioObjects = [];
n@1169 1124 this.timer = new timer();
n@1169 1125 this.loopPlayback = audioHolderObject.loop;
n@1118 1126 };
n@1118 1127
n@1118 1128 this.checkAllPlayed = function() {
n@1118 1129 arr = [];
n@1118 1130 for (var id=0; id<this.audioObjects.length; id++) {
n@1118 1131 if (this.audioObjects[id].metric.wasListenedTo == false) {
n@1118 1132 arr.push(this.audioObjects[id].id);
n@1118 1133 }
n@1118 1134 }
n@1118 1135 return arr;
n@1118 1136 };
n@1118 1137
n@1118 1138 this.checkAllReady = function() {
n@1118 1139 var ready = true;
n@1118 1140 for (var i=0; i<this.audioObjects.length; i++) {
n@1118 1141 if (this.audioObjects[i].state == 0) {
n@1118 1142 // Track not ready
n@1118 1143 console.log('WAIT -- audioObject '+i+' not ready yet!');
n@1118 1144 ready = false;
n@1118 1145 };
n@1118 1146 }
n@1118 1147 return ready;
n@1118 1148 };
n@1118 1149
n@1118 1150 this.setSynchronousLoop = function() {
n@1118 1151 // Pads the signals so they are all exactly the same length
n@1118 1152 var length = 0;
n@1118 1153 var maxId;
n@1118 1154 for (var i=0; i<this.audioObjects.length; i++)
n@1118 1155 {
n@1118 1156 if (length < this.audioObjects[i].buffer.buffer.length)
n@1118 1157 {
n@1118 1158 length = this.audioObjects[i].buffer.buffer.length;
n@1118 1159 maxId = i;
n@1118 1160 }
n@1118 1161 }
n@1118 1162 // Extract the audio and zero-pad
n@1118 1163 for (var i=0; i<this.audioObjects.length; i++)
n@1118 1164 {
n@1118 1165 var orig = this.audioObjects[i].buffer.buffer;
n@1118 1166 var hold = audioContext.createBuffer(orig.numberOfChannels,length,orig.sampleRate);
n@1118 1167 for (var c=0; c<orig.numberOfChannels; c++)
n@1118 1168 {
n@1118 1169 var inData = hold.getChannelData(c);
n@1118 1170 var outData = orig.getChannelData(c);
n@1118 1171 for (var n=0; n<orig.length; n++)
n@1118 1172 {inData[n] = outData[n];}
n@1118 1173 }
n@1120 1174 hold.playbackGain = orig.playbackGain;
n@1118 1175 hold.lufs = orig.lufs;
n@1118 1176 this.audioObjects[i].buffer.buffer = hold;
n@1118 1177 }
n@1118 1178 };
n@1170 1179
n@1170 1180 this.exportXML = function()
n@1170 1181 {
n@1170 1182
n@1170 1183 };
n@1118 1184
n@1118 1185 }
n@1118 1186
n@1118 1187 function audioObject(id) {
n@1118 1188 // The main buffer object with common control nodes to the AudioEngine
n@1118 1189
n@1118 1190 this.specification;
n@1118 1191 this.id = id;
n@1118 1192 this.state = 0; // 0 - no data, 1 - ready
n@1118 1193 this.url = null; // Hold the URL given for the output back to the results.
n@1118 1194 this.metric = new metricTracker(this);
n@1124 1195 this.storeDOM = null;
n@1118 1196
n@1118 1197 // Bindings for GUI
n@1118 1198 this.interfaceDOM = null;
n@1118 1199 this.commentDOM = null;
n@1118 1200
n@1118 1201 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
n@1118 1202 this.bufferNode = undefined;
n@1118 1203 this.outputGain = audioContext.createGain();
n@1118 1204
n@1124 1205 this.onplayGain = 1.0;
n@1118 1206
n@1118 1207 // Connect buffer to the audio graph
n@1118 1208 this.outputGain.connect(audioEngineContext.outputGain);
n@1118 1209
n@1118 1210 // the audiobuffer is not designed for multi-start playback
n@1118 1211 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
n@1118 1212 this.buffer;
n@1118 1213
n@1118 1214 this.bufferLoaded = function(callee)
n@1118 1215 {
n@1118 1216 // Called by the associated buffer when it has finished loading, will then 'bind' the buffer to the
n@1118 1217 // audioObject and trigger the interfaceDOM.enable() function for user feedback
n@1118 1218 if (audioEngineContext.loopPlayback){
n@1118 1219 // First copy the buffer into this.buffer
n@1118 1220 this.buffer = new audioEngineContext.bufferObj();
n@1118 1221 this.buffer.url = callee.url;
n@1118 1222 this.buffer.buffer = audioContext.createBuffer(callee.buffer.numberOfChannels, callee.buffer.length, callee.buffer.sampleRate);
n@1118 1223 for (var c=0; c<callee.buffer.numberOfChannels; c++)
n@1118 1224 {
n@1118 1225 var src = callee.buffer.getChannelData(c);
n@1118 1226 var dst = this.buffer.buffer.getChannelData(c);
n@1118 1227 for (var n=0; n<src.length; n++)
n@1118 1228 {
n@1118 1229 dst[n] = src[n];
n@1118 1230 }
n@1118 1231 }
n@1118 1232 } else {
n@1118 1233 this.buffer = callee;
n@1118 1234 }
n@1118 1235 this.state = 1;
n@1120 1236 this.buffer.buffer.playbackGain = callee.buffer.playbackGain;
n@1118 1237 this.buffer.buffer.lufs = callee.buffer.lufs;
n@1148 1238 var targetLUFS = this.specification.parent.loudness || specification.loudness;
n@1118 1239 if (typeof targetLUFS === "number")
n@1118 1240 {
n@1120 1241 this.buffer.buffer.playbackGain = decibelToLinear(targetLUFS - this.buffer.buffer.lufs);
n@1118 1242 } else {
n@1120 1243 this.buffer.buffer.playbackGain = 1.0;
n@1118 1244 }
n@1118 1245 if (this.interfaceDOM != null) {
n@1118 1246 this.interfaceDOM.enable();
n@1118 1247 }
n@1124 1248 this.onplayGain = decibelToLinear(this.specification.gain)*this.buffer.buffer.playbackGain;
n@1124 1249 this.storeDOM.setAttribute('playGain',linearToDecibel(this.onplayGain));
n@1118 1250 };
n@1125 1251
n@1125 1252 this.bindInterface = function(interfaceObject)
n@1125 1253 {
n@1125 1254 this.interfaceDOM = interfaceObject;
n@1125 1255 this.metric.initialise(interfaceObject.getValue());
n@1125 1256 if (this.state == 1)
n@1125 1257 {
n@1125 1258 this.interfaceDOM.enable();
n@1125 1259 }
n@1138 1260 this.storeDOM.setAttribute('presentedId',interfaceObject.getPresentedId());
n@1125 1261 };
n@1118 1262
n@1160 1263 this.loopStart = function(setTime) {
n@1160 1264 this.outputGain.gain.linearRampToValueAtTime(this.onplayGain,setTime);
n@1118 1265 this.metric.startListening(audioEngineContext.timer.getTestTime());
n@1160 1266 this.interfaceDOM.startPlayback();
n@1118 1267 };
n@1118 1268
n@1160 1269 this.loopStop = function(setTime) {
n@1118 1270 if (this.outputGain.gain.value != 0.0) {
n@1160 1271 this.outputGain.gain.linearRampToValueAtTime(0.0,setTime);
n@1118 1272 this.metric.stopListening(audioEngineContext.timer.getTestTime());
n@1118 1273 }
n@1160 1274 this.interfaceDOM.stopPlayback();
n@1118 1275 };
n@1118 1276
n@1118 1277 this.play = function(startTime) {
n@1118 1278 if (this.bufferNode == undefined && this.buffer.buffer != undefined) {
n@1118 1279 this.bufferNode = audioContext.createBufferSource();
n@1118 1280 this.bufferNode.owner = this;
n@1118 1281 this.bufferNode.connect(this.outputGain);
n@1118 1282 this.bufferNode.buffer = this.buffer.buffer;
n@1118 1283 this.bufferNode.loop = audioEngineContext.loopPlayback;
n@1118 1284 this.bufferNode.onended = function(event) {
n@1118 1285 // Safari does not like using 'this' to reference the calling object!
n@1118 1286 //event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition());
n@1160 1287 event.currentTarget.owner.stop(audioContext.currentTime+1);
n@1118 1288 };
n@1118 1289 if (this.bufferNode.loop == false) {
n@1118 1290 this.metric.startListening(audioEngineContext.timer.getTestTime());
n@1160 1291 this.outputGain.gain.setValueAtTime(this.onplayGain,startTime);
n@1160 1292 this.interfaceDOM.startPlayback();
n@1160 1293 } else {
n@1160 1294 this.outputGain.gain.setValueAtTime(0.0,startTime);
n@1160 1295 }
n@1118 1296 this.bufferNode.start(startTime);
n@1118 1297 }
n@1118 1298 };
n@1118 1299
n@1160 1300 this.stop = function(stopTime) {
n@1160 1301 this.outputGain.gain.cancelScheduledValues(audioContext.currentTime);
n@1118 1302 if (this.bufferNode != undefined)
n@1118 1303 {
n@1118 1304 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition());
n@1160 1305 this.bufferNode.stop(stopTime);
n@1118 1306 this.bufferNode = undefined;
n@1118 1307 }
n@1160 1308 this.outputGain.gain.value = 0.0;
n@1160 1309 this.interfaceDOM.stopPlayback();
n@1118 1310 };
n@1118 1311
n@1118 1312 this.getCurrentPosition = function() {
n@1118 1313 var time = audioEngineContext.timer.getTestTime();
n@1118 1314 if (this.bufferNode != undefined) {
n@1118 1315 if (this.bufferNode.loop == true) {
n@1118 1316 if (audioEngineContext.status == 1) {
n@1118 1317 return (time-this.metric.listenStart)%this.buffer.buffer.duration;
n@1118 1318 } else {
n@1118 1319 return 0;
n@1118 1320 }
n@1118 1321 } else {
n@1118 1322 if (this.metric.listenHold) {
n@1118 1323 return time - this.metric.listenStart;
n@1118 1324 } else {
n@1118 1325 return 0;
n@1118 1326 }
n@1118 1327 }
n@1118 1328 } else {
n@1118 1329 return 0;
n@1118 1330 }
n@1118 1331 };
n@1118 1332
n@1118 1333 this.exportXMLDOM = function() {
n@1124 1334 var file = storage.document.createElement('file');
n@1118 1335 file.setAttribute('sampleRate',this.buffer.buffer.sampleRate);
n@1118 1336 file.setAttribute('channels',this.buffer.buffer.numberOfChannels);
n@1118 1337 file.setAttribute('sampleCount',this.buffer.buffer.length);
n@1118 1338 file.setAttribute('duration',this.buffer.buffer.duration);
n@1124 1339 this.storeDOM.appendChild(file);
n@1124 1340 if (this.specification.type != 'outside-reference') {
n@1118 1341 var interfaceXML = this.interfaceDOM.exportXMLDOM(this);
n@1140 1342 if (interfaceXML != null)
n@1140 1343 {
n@1140 1344 if (interfaceXML.length == undefined) {
n@1140 1345 this.storeDOM.appendChild(interfaceXML);
n@1140 1346 } else {
n@1140 1347 for (var i=0; i<interfaceXML.length; i++)
n@1140 1348 {
n@1140 1349 this.storeDOM.appendChild(interfaceXML[i]);
n@1140 1350 }
n@1118 1351 }
n@1118 1352 }
n@1130 1353 if (this.commentDOM != null) {
n@1130 1354 this.storeDOM.appendChild(this.commentDOM.exportXMLDOM(this));
n@1130 1355 }
n@1118 1356 }
n@1124 1357 var nodes = this.metric.exportXMLDOM();
n@1124 1358 var mroot = this.storeDOM.getElementsByTagName('metric')[0];
n@1124 1359 for (var i=0; i<nodes.length; i++)
n@1124 1360 {
n@1124 1361 mroot.appendChild(nodes[i]);
n@1124 1362 }
n@1118 1363 };
n@1118 1364 }
n@1118 1365
n@1118 1366 function timer()
n@1118 1367 {
n@1118 1368 /* Timer object used in audioEngine to keep track of session timings
n@1118 1369 * Uses the timer of the web audio API, so sample resolution
n@1118 1370 */
n@1118 1371 this.testStarted = false;
n@1118 1372 this.testStartTime = 0;
n@1118 1373 this.testDuration = 0;
n@1118 1374 this.minimumTestTime = 0; // No minimum test time
n@1118 1375 this.startTest = function()
n@1118 1376 {
n@1118 1377 if (this.testStarted == false)
n@1118 1378 {
n@1118 1379 this.testStartTime = audioContext.currentTime;
n@1118 1380 this.testStarted = true;
n@1118 1381 this.updateTestTime();
n@1118 1382 audioEngineContext.metric.initialiseTest();
n@1118 1383 }
n@1118 1384 };
n@1118 1385 this.stopTest = function()
n@1118 1386 {
n@1118 1387 if (this.testStarted)
n@1118 1388 {
n@1118 1389 this.testDuration = this.getTestTime();
n@1118 1390 this.testStarted = false;
n@1118 1391 } else {
n@1118 1392 console.log('ERR: Test tried to end before beginning');
n@1118 1393 }
n@1118 1394 };
n@1118 1395 this.updateTestTime = function()
n@1118 1396 {
n@1118 1397 if (this.testStarted)
n@1118 1398 {
n@1118 1399 this.testDuration = audioContext.currentTime - this.testStartTime;
n@1118 1400 }
n@1118 1401 };
n@1118 1402 this.getTestTime = function()
n@1118 1403 {
n@1118 1404 this.updateTestTime();
n@1118 1405 return this.testDuration;
n@1118 1406 };
n@1118 1407 }
n@1118 1408
n@1118 1409 function sessionMetrics(engine,specification)
n@1118 1410 {
n@1118 1411 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
n@1118 1412 */
n@1118 1413 this.engine = engine;
n@1118 1414 this.lastClicked = -1;
n@1118 1415 this.data = -1;
n@1118 1416 this.reset = function() {
n@1118 1417 this.lastClicked = -1;
n@1118 1418 this.data = -1;
n@1118 1419 };
n@1118 1420
n@1118 1421 this.enableElementInitialPosition = false;
n@1118 1422 this.enableElementListenTracker = false;
n@1118 1423 this.enableElementTimer = false;
n@1118 1424 this.enableElementTracker = false;
n@1118 1425 this.enableFlagListenedTo = false;
n@1118 1426 this.enableFlagMoved = false;
n@1118 1427 this.enableTestTimer = false;
n@1118 1428 // Obtain the metrics enabled
n@1124 1429 for (var i=0; i<specification.metrics.enabled.length; i++)
n@1118 1430 {
n@1124 1431 var node = specification.metrics.enabled[i];
n@1124 1432 switch(node)
n@1118 1433 {
n@1118 1434 case 'testTimer':
n@1118 1435 this.enableTestTimer = true;
n@1118 1436 break;
n@1118 1437 case 'elementTimer':
n@1118 1438 this.enableElementTimer = true;
n@1118 1439 break;
n@1118 1440 case 'elementTracker':
n@1118 1441 this.enableElementTracker = true;
n@1118 1442 break;
n@1118 1443 case 'elementListenTracker':
n@1118 1444 this.enableElementListenTracker = true;
n@1118 1445 break;
n@1118 1446 case 'elementInitialPosition':
n@1118 1447 this.enableElementInitialPosition = true;
n@1118 1448 break;
n@1118 1449 case 'elementFlagListenedTo':
n@1118 1450 this.enableFlagListenedTo = true;
n@1118 1451 break;
n@1118 1452 case 'elementFlagMoved':
n@1118 1453 this.enableFlagMoved = true;
n@1118 1454 break;
n@1118 1455 case 'elementFlagComments':
n@1118 1456 this.enableFlagComments = true;
n@1118 1457 break;
n@1118 1458 }
n@1118 1459 }
n@1118 1460 this.initialiseTest = function(){};
n@1118 1461 }
n@1118 1462
n@1118 1463 function metricTracker(caller)
n@1118 1464 {
n@1118 1465 /* Custom object to track and collect metric data
n@1118 1466 * Used only inside the audioObjects object.
n@1118 1467 */
n@1118 1468
n@1118 1469 this.listenedTimer = 0;
n@1118 1470 this.listenStart = 0;
n@1118 1471 this.listenHold = false;
n@1118 1472 this.initialPosition = -1;
n@1118 1473 this.movementTracker = [];
n@1118 1474 this.listenTracker =[];
n@1118 1475 this.wasListenedTo = false;
n@1118 1476 this.wasMoved = false;
n@1118 1477 this.hasComments = false;
n@1118 1478 this.parent = caller;
n@1118 1479
n@1124 1480 this.initialise = function(position)
n@1118 1481 {
n@1118 1482 if (this.initialPosition == -1) {
n@1118 1483 this.initialPosition = position;
n@1125 1484 this.moved(0,position);
n@1118 1485 }
n@1118 1486 };
n@1118 1487
n@1118 1488 this.moved = function(time,position)
n@1118 1489 {
n@1125 1490 if (time > 0) {this.wasMoved = true;}
n@1118 1491 this.movementTracker[this.movementTracker.length] = [time, position];
n@1118 1492 };
n@1118 1493
n@1118 1494 this.startListening = function(time)
n@1118 1495 {
n@1118 1496 if (this.listenHold == false)
n@1118 1497 {
n@1118 1498 this.wasListenedTo = true;
n@1118 1499 this.listenStart = time;
n@1118 1500 this.listenHold = true;
n@1118 1501
n@1118 1502 var evnt = document.createElement('event');
n@1118 1503 var testTime = document.createElement('testTime');
n@1118 1504 testTime.setAttribute('start',time);
n@1118 1505 var bufferTime = document.createElement('bufferTime');
n@1118 1506 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
n@1118 1507 evnt.appendChild(testTime);
n@1118 1508 evnt.appendChild(bufferTime);
n@1118 1509 this.listenTracker.push(evnt);
n@1118 1510
n@1118 1511 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
n@1118 1512 }
n@1118 1513 };
n@1118 1514
n@1118 1515 this.stopListening = function(time,bufferStopTime)
n@1118 1516 {
n@1118 1517 if (this.listenHold == true)
n@1118 1518 {
n@1118 1519 var diff = time - this.listenStart;
n@1118 1520 this.listenedTimer += (diff);
n@1118 1521 this.listenStart = 0;
n@1118 1522 this.listenHold = false;
n@1118 1523
n@1118 1524 var evnt = this.listenTracker[this.listenTracker.length-1];
n@1118 1525 var testTime = evnt.getElementsByTagName('testTime')[0];
n@1118 1526 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
n@1118 1527 testTime.setAttribute('stop',time);
n@1118 1528 if (bufferStopTime == undefined) {
n@1118 1529 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
n@1118 1530 } else {
n@1118 1531 bufferTime.setAttribute('stop',bufferStopTime);
n@1118 1532 }
n@1118 1533 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
n@1118 1534 }
n@1118 1535 };
n@1118 1536
n@1118 1537 this.exportXMLDOM = function() {
n@1124 1538 var storeDOM = [];
n@1118 1539 if (audioEngineContext.metric.enableElementTimer) {
n@1124 1540 var mElementTimer = storage.document.createElement('metricresult');
n@1118 1541 mElementTimer.setAttribute('name','enableElementTimer');
n@1118 1542 mElementTimer.textContent = this.listenedTimer;
n@1124 1543 storeDOM.push(mElementTimer);
n@1118 1544 }
n@1118 1545 if (audioEngineContext.metric.enableElementTracker) {
n@1124 1546 var elementTrackerFull = storage.document.createElement('metricResult');
n@1118 1547 elementTrackerFull.setAttribute('name','elementTrackerFull');
n@1118 1548 for (var k=0; k<this.movementTracker.length; k++)
n@1118 1549 {
n@1124 1550 var timePos = storage.document.createElement('timePos');
n@1118 1551 timePos.id = k;
n@1124 1552 var time = storage.document.createElement('time');
n@1118 1553 time.textContent = this.movementTracker[k][0];
n@1118 1554 var position = document.createElement('position');
n@1118 1555 position.textContent = this.movementTracker[k][1];
n@1118 1556 timePos.appendChild(time);
n@1118 1557 timePos.appendChild(position);
n@1118 1558 elementTrackerFull.appendChild(timePos);
n@1118 1559 }
n@1124 1560 storeDOM.push(elementTrackerFull);
n@1118 1561 }
n@1118 1562 if (audioEngineContext.metric.enableElementListenTracker) {
n@1124 1563 var elementListenTracker = storage.document.createElement('metricResult');
n@1118 1564 elementListenTracker.setAttribute('name','elementListenTracker');
n@1118 1565 for (var k=0; k<this.listenTracker.length; k++) {
n@1118 1566 elementListenTracker.appendChild(this.listenTracker[k]);
n@1118 1567 }
n@1124 1568 storeDOM.push(elementListenTracker);
n@1118 1569 }
n@1118 1570 if (audioEngineContext.metric.enableElementInitialPosition) {
n@1124 1571 var elementInitial = storage.document.createElement('metricResult');
n@1118 1572 elementInitial.setAttribute('name','elementInitialPosition');
n@1118 1573 elementInitial.textContent = this.initialPosition;
n@1124 1574 storeDOM.push(elementInitial);
n@1118 1575 }
n@1118 1576 if (audioEngineContext.metric.enableFlagListenedTo) {
n@1124 1577 var flagListenedTo = storage.document.createElement('metricResult');
n@1118 1578 flagListenedTo.setAttribute('name','elementFlagListenedTo');
n@1118 1579 flagListenedTo.textContent = this.wasListenedTo;
n@1124 1580 storeDOM.push(flagListenedTo);
n@1118 1581 }
n@1118 1582 if (audioEngineContext.metric.enableFlagMoved) {
n@1124 1583 var flagMoved = storage.document.createElement('metricResult');
n@1118 1584 flagMoved.setAttribute('name','elementFlagMoved');
n@1118 1585 flagMoved.textContent = this.wasMoved;
n@1124 1586 storeDOM.push(flagMoved);
n@1118 1587 }
n@1118 1588 if (audioEngineContext.metric.enableFlagComments) {
n@1124 1589 var flagComments = storage.document.createElement('metricResult');
n@1118 1590 flagComments.setAttribute('name','elementFlagComments');
n@1118 1591 if (this.parent.commentDOM == null)
n@1118 1592 {flag.textContent = 'false';}
n@1118 1593 else if (this.parent.commentDOM.textContent.length == 0)
n@1118 1594 {flag.textContent = 'false';}
n@1118 1595 else
n@1118 1596 {flag.textContet = 'true';}
n@1124 1597 storeDOM.push(flagComments);
n@1118 1598 }
n@1124 1599 return storeDOM;
n@1118 1600 };
n@1118 1601 }
n@1118 1602
n@1118 1603 function randomiseOrder(input)
n@1118 1604 {
n@1118 1605 // This takes an array of information and randomises the order
n@1118 1606 var N = input.length;
n@1118 1607
n@1118 1608 var inputSequence = []; // For safety purposes: keep track of randomisation
n@1118 1609 for (var counter = 0; counter < N; ++counter)
n@1118 1610 inputSequence.push(counter) // Fill array
n@1118 1611 var inputSequenceClone = inputSequence.slice(0);
n@1118 1612
n@1118 1613 var holdArr = [];
n@1118 1614 var outputSequence = [];
n@1118 1615 for (var n=0; n<N; n++)
n@1118 1616 {
n@1118 1617 // First pick a random number
n@1118 1618 var r = Math.random();
n@1118 1619 // Multiply and floor by the number of elements left
n@1118 1620 r = Math.floor(r*input.length);
n@1118 1621 // Pick out that element and delete from the array
n@1118 1622 holdArr.push(input.splice(r,1)[0]);
n@1118 1623 // Do the same with sequence
n@1118 1624 outputSequence.push(inputSequence.splice(r,1)[0]);
n@1118 1625 }
n@1118 1626 console.log(inputSequenceClone.toString()); // print original array to console
n@1118 1627 console.log(outputSequence.toString()); // print randomised array to console
n@1118 1628 return holdArr;
n@1118 1629 }
n@1118 1630
n@1118 1631 function returnDateNode()
n@1118 1632 {
n@1118 1633 // Create an XML Node for the Date and Time a test was conducted
n@1118 1634 // Structure is
n@1118 1635 // <datetime>
n@1118 1636 // <date year="##" month="##" day="##">DD/MM/YY</date>
n@1118 1637 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
n@1118 1638 // </datetime>
n@1118 1639 var dateTime = new Date();
n@1118 1640 var year = document.createAttribute('year');
n@1118 1641 var month = document.createAttribute('month');
n@1118 1642 var day = document.createAttribute('day');
n@1118 1643 var hour = document.createAttribute('hour');
n@1118 1644 var minute = document.createAttribute('minute');
n@1118 1645 var secs = document.createAttribute('secs');
n@1118 1646
n@1118 1647 year.nodeValue = dateTime.getFullYear();
n@1118 1648 month.nodeValue = dateTime.getMonth()+1;
n@1118 1649 day.nodeValue = dateTime.getDate();
n@1118 1650 hour.nodeValue = dateTime.getHours();
n@1118 1651 minute.nodeValue = dateTime.getMinutes();
n@1118 1652 secs.nodeValue = dateTime.getSeconds();
n@1118 1653
n@1118 1654 var hold = document.createElement("datetime");
n@1118 1655 var date = document.createElement("date");
n@1118 1656 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
n@1118 1657 var time = document.createElement("time");
n@1118 1658 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
n@1118 1659
n@1118 1660 date.setAttributeNode(year);
n@1118 1661 date.setAttributeNode(month);
n@1118 1662 date.setAttributeNode(day);
n@1118 1663 time.setAttributeNode(hour);
n@1118 1664 time.setAttributeNode(minute);
n@1118 1665 time.setAttributeNode(secs);
n@1118 1666
n@1118 1667 hold.appendChild(date);
n@1118 1668 hold.appendChild(time);
n@1118 1669 return hold;
n@1118 1670
n@1118 1671 }
n@1118 1672
n@1118 1673 function Specification() {
n@1118 1674 // Handles the decoding of the project specification XML into a simple JavaScript Object.
n@1118 1675
n@1124 1676 this.interface = null;
n@1173 1677 this.projectReturn = "null";
n@1124 1678 this.randomiseOrder = null;
n@1124 1679 this.testPages = null;
n@1124 1680 this.pages = [];
n@1124 1681 this.metrics = null;
n@1124 1682 this.interfaces = null;
n@1124 1683 this.loudness = null;
n@1124 1684 this.errors = [];
n@1124 1685 this.schema = null;
n@1118 1686
n@1124 1687 this.processAttribute = function(attribute,schema)
n@1124 1688 {
n@1124 1689 // attribute is the string returned from getAttribute on the XML
n@1124 1690 // schema is the <xs:attribute> node
n@1124 1691 if (schema.getAttribute('name') == undefined && schema.getAttribute('ref') != undefined)
n@1124 1692 {
n@1148 1693 schema = this.schema.getAllElementsByName(schema.getAttribute('ref'))[0];
n@1124 1694 }
n@1124 1695 var defaultOpt = schema.getAttribute('default');
n@1124 1696 if (attribute == null) {
n@1124 1697 attribute = defaultOpt;
n@1124 1698 }
n@1124 1699 var dataType = schema.getAttribute('type');
n@1124 1700 if (typeof dataType == "string") { dataType = dataType.substr(3);}
n@1124 1701 else {dataType = "string";}
n@1124 1702 if (attribute == null)
n@1124 1703 {
n@1124 1704 return attribute;
n@1124 1705 }
n@1124 1706 switch(dataType)
n@1124 1707 {
n@1124 1708 case "boolean":
n@1124 1709 if (attribute == 'true'){attribute = true;}else{attribute=false;}
n@1124 1710 break;
n@1124 1711 case "negativeInteger":
n@1124 1712 case "positiveInteger":
n@1124 1713 case "nonNegativeInteger":
n@1124 1714 case "nonPositiveInteger":
n@1124 1715 case "integer":
n@1124 1716 case "decimal":
n@1124 1717 case "short":
n@1124 1718 attribute = Number(attribute);
n@1124 1719 break;
n@1124 1720 case "string":
n@1124 1721 default:
n@1124 1722 attribute = String(attribute);
n@1124 1723 break;
n@1124 1724 }
n@1124 1725 return attribute;
n@1124 1726 };
n@1118 1727
n@1118 1728 this.decode = function(projectXML) {
n@1124 1729 this.errors = [];
n@1118 1730 // projectXML - DOM Parsed document
n@1118 1731 this.projectXML = projectXML.childNodes[0];
n@1118 1732 var setupNode = projectXML.getElementsByTagName('setup')[0];
n@1148 1733 var schemaSetup = this.schema.getAllElementsByName('setup')[0];
n@1124 1734 // First decode the attributes
n@1148 1735 var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
n@1124 1736 for (var i in attributes)
n@1118 1737 {
n@1124 1738 if (isNaN(Number(i)) == true){break;}
n@1124 1739 var attributeName = attributes[i].getAttribute('name');
n@1124 1740 var projectAttr = setupNode.getAttribute(attributeName);
n@1124 1741 projectAttr = this.processAttribute(projectAttr,attributes[i]);
n@1124 1742 switch(typeof projectAttr)
n@1118 1743 {
n@1124 1744 case "number":
n@1124 1745 case "boolean":
n@1124 1746 eval('this.'+attributeName+' = '+projectAttr);
n@1124 1747 break;
n@1124 1748 case "string":
n@1124 1749 eval('this.'+attributeName+' = "'+projectAttr+'"');
n@1124 1750 break;
n@1118 1751 }
n@1124 1752
n@1118 1753 }
n@1118 1754
n@1170 1755 this.metrics = new this.metricNode();
n@1118 1756
n@1124 1757 this.metrics.decode(this,setupNode.getElementsByTagName('metric')[0]);
n@1124 1758
n@1124 1759 // Now process the survey node options
n@1124 1760 var survey = setupNode.getElementsByTagName('survey');
n@1124 1761 for (var i in survey) {
n@1124 1762 if (isNaN(Number(i)) == true){break;}
n@1124 1763 var location = survey[i].getAttribute('location');
n@1124 1764 if (location == 'pre' || location == 'before')
n@1124 1765 {
n@1124 1766 if (this.preTest != null){this.errors.push("Already a pre/before test survey defined! Ignoring second!!");}
n@1124 1767 else {
n@1124 1768 this.preTest = new this.surveyNode();
n@1170 1769 this.preTest.decode(this,survey[i]);
n@1124 1770 }
n@1124 1771 } else if (location == 'post' || location == 'after') {
n@1124 1772 if (this.postTest != null){this.errors.push("Already a post/after test survey defined! Ignoring second!!");}
n@1124 1773 else {
n@1124 1774 this.postTest = new this.surveyNode();
n@1170 1775 this.postTest.decode(this,survey[i]);
n@1124 1776 }
n@1118 1777 }
n@1118 1778 }
n@1118 1779
n@1124 1780 var interfaceNode = setupNode.getElementsByTagName('interface');
n@1124 1781 if (interfaceNode.length > 1)
n@1124 1782 {
n@1124 1783 this.errors.push("Only one <interface> node in the <setup> node allowed! Others except first ingnored!");
n@1124 1784 }
n@1124 1785 this.interfaces = new this.interfaceNode();
n@1124 1786 if (interfaceNode.length != 0)
n@1124 1787 {
n@1124 1788 interfaceNode = interfaceNode[0];
n@1148 1789 this.interfaces.decode(this,interfaceNode,this.schema.getAllElementsByName('interface')[1]);
n@1118 1790 }
n@1118 1791
n@1124 1792 // Page tags
n@1124 1793 var pageTags = projectXML.getElementsByTagName('page');
n@1148 1794 var pageSchema = this.schema.getAllElementsByName('page')[0];
n@1124 1795 for (var i=0; i<pageTags.length; i++)
n@1118 1796 {
n@1124 1797 var node = new this.page();
n@1124 1798 node.decode(this,pageTags[i],pageSchema);
n@1124 1799 this.pages.push(node);
n@1118 1800 }
n@1118 1801 };
n@1118 1802
n@1118 1803 this.encode = function()
n@1118 1804 {
n@1172 1805 var RootDocument = document.implementation.createDocument(null,"waet");
n@1172 1806 var root = RootDocument.children[0];
n@1172 1807 root.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
n@1172 1808 root.setAttribute("xsi:noNamespaceSchemaLocation","test-schema.xsd");
n@1124 1809 // Build setup node
n@1172 1810 var setup = RootDocument.createElement("setup");
n@1172 1811 var schemaSetup = this.schema.getAllElementsByName('setup')[0];
n@1172 1812 // First decode the attributes
n@1172 1813 var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
n@1172 1814 for (var i=0; i<attributes.length; i++)
n@1172 1815 {
n@1172 1816 var name = attributes[i].getAttribute("name");
n@1172 1817 if (name == undefined) {
n@1172 1818 name = attributes[i].getAttribute("ref");
n@1172 1819 }
n@1172 1820 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
n@1172 1821 {
n@1172 1822 eval("setup.setAttribute('"+name+"',this."+name+")");
n@1172 1823 }
n@1172 1824 }
n@1172 1825 root.appendChild(setup);
n@1172 1826 // Survey node
n@1172 1827 setup.appendChild(this.preTest.encode(RootDocument));
n@1172 1828 setup.appendChild(this.postTest.encode(RootDocument));
n@1172 1829 setup.appendChild(this.metrics.encode(RootDocument));
n@1172 1830 setup.appendChild(this.interfaces.encode(RootDocument));
n@1172 1831 for (var page of this.pages)
n@1172 1832 {
n@1172 1833 root.appendChild(page.encode(RootDocument));
n@1172 1834 }
n@1172 1835 return RootDocument;
n@1118 1836 };
n@1118 1837
n@1124 1838 this.surveyNode = function() {
n@1124 1839 this.location = null;
n@1118 1840 this.options = [];
n@1170 1841 this.schema = specification.schema.getAllElementsByName('survey')[0];
n@1118 1842
n@1118 1843 this.OptionNode = function() {
n@1118 1844 this.type = undefined;
n@1170 1845 this.schema = specification.schema.getAllElementsByName('surveyentry')[0];
n@1118 1846 this.id = undefined;
n@1118 1847 this.mandatory = undefined;
n@1118 1848 this.statement = undefined;
n@1118 1849 this.boxsize = undefined;
n@1118 1850 this.options = [];
n@1118 1851 this.min = undefined;
n@1118 1852 this.max = undefined;
n@1118 1853 this.step = undefined;
n@1118 1854
n@1170 1855 this.decode = function(parent,child)
n@1118 1856 {
n@1170 1857 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
n@1124 1858 for (var i in attributeMap){
n@1124 1859 if(isNaN(Number(i)) == true){break;}
n@1124 1860 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
n@1124 1861 var projectAttr = child.getAttribute(attributeName);
n@1124 1862 projectAttr = parent.processAttribute(projectAttr,attributeMap[i]);
n@1124 1863 switch(typeof projectAttr)
n@1124 1864 {
n@1124 1865 case "number":
n@1124 1866 case "boolean":
n@1124 1867 eval('this.'+attributeName+' = '+projectAttr);
n@1124 1868 break;
n@1124 1869 case "string":
n@1124 1870 eval('this.'+attributeName+' = "'+projectAttr+'"');
n@1124 1871 break;
n@1118 1872 }
n@1124 1873 }
n@1124 1874 this.statement = child.getElementsByTagName('statement')[0].textContent;
n@1124 1875 if (this.type == "checkbox" || this.type == "radio") {
n@1124 1876 var children = child.getElementsByTagName('option');
n@1124 1877 if (children.length == null) {
n@1118 1878 console.log('Malformed' +child.nodeName+ 'entry');
n@1118 1879 this.statement = 'Malformed' +child.nodeName+ 'entry';
n@1118 1880 this.type = 'statement';
n@1118 1881 } else {
n@1118 1882 this.options = [];
n@1124 1883 for (var i in children)
n@1124 1884 {
n@1124 1885 if (isNaN(Number(i))==true){break;}
n@1124 1886 this.options.push({
n@1124 1887 name: children[i].getAttribute('name'),
n@1124 1888 text: children[i].textContent
n@1124 1889 });
n@1118 1890 }
n@1118 1891 }
n@1118 1892 }
n@1118 1893 };
n@1118 1894
n@1172 1895 this.exportXML = function(doc)
n@1118 1896 {
n@1172 1897 var node = doc.createElement('surveyelement');
n@1124 1898 node.setAttribute('type',this.type);
n@1172 1899 var statement = doc.createElement('statement');
n@1124 1900 statement.textContent = this.statement;
n@1124 1901 node.appendChild(statement);
n@1118 1902 switch(this.type)
n@1118 1903 {
n@1118 1904 case "statement":
n@1118 1905 break;
n@1118 1906 case "question":
n@1118 1907 node.id = this.id;
n@1118 1908 node.setAttribute("mandatory",this.mandatory);
n@1118 1909 node.setAttribute("boxsize",this.boxsize);
n@1118 1910 break;
n@1118 1911 case "number":
n@1118 1912 node.id = this.id;
n@1118 1913 node.setAttribute("mandatory",this.mandatory);
n@1118 1914 node.setAttribute("min", this.min);
n@1118 1915 node.setAttribute("max", this.max);
n@1118 1916 node.setAttribute("step", this.step);
n@1118 1917 break;
n@1118 1918 case "checkbox":
n@1118 1919 case "radio":
n@1118 1920 node.id = this.id;
n@1118 1921 for (var i=0; i<this.options.length; i++)
n@1118 1922 {
n@1118 1923 var option = this.options[i];
n@1172 1924 var optionNode = doc.createElement("option");
n@1118 1925 optionNode.setAttribute("name",option.name);
n@1118 1926 optionNode.textContent = option.text;
n@1118 1927 node.appendChild(optionNode);
n@1118 1928 }
n@1118 1929 break;
n@1118 1930 }
n@1118 1931 return node;
n@1118 1932 };
n@1118 1933 };
n@1170 1934 this.decode = function(parent,xml) {
n@1124 1935 this.location = xml.getAttribute('location');
n@1124 1936 if (this.location == 'before'){this.location = 'pre';}
n@1124 1937 else if (this.location == 'after'){this.location = 'post';}
n@1124 1938 for (var i in xml.children)
n@1124 1939 {
n@1124 1940 if(isNaN(Number(i))==true){break;}
n@1118 1941 var node = new this.OptionNode();
n@1170 1942 node.decode(parent,xml.children[i]);
n@1118 1943 this.options.push(node);
n@1124 1944 }
n@1124 1945 };
n@1172 1946 this.encode = function(doc) {
n@1172 1947 var node = doc.createElement('survey');
n@1124 1948 node.setAttribute('location',this.location);
n@1124 1949 for (var i=0; i<this.options.length; i++)
n@1124 1950 {
n@1172 1951 node.appendChild(this.options[i].exportXML(doc));
n@1124 1952 }
n@1124 1953 return node;
n@1124 1954 };
n@1124 1955 };
n@1124 1956
n@1124 1957 this.interfaceNode = function()
n@1124 1958 {
n@1124 1959 this.title = null;
n@1124 1960 this.name = null;
n@1124 1961 this.options = [];
n@1124 1962 this.scales = [];
n@1170 1963 this.schema = specification.schema.getAllElementsByName('interface')[1];
n@1124 1964
n@1170 1965 this.decode = function(parent,xml) {
n@1124 1966 this.name = xml.getAttribute('name');
n@1124 1967 var titleNode = xml.getElementsByTagName('title');
n@1124 1968 if (titleNode.length == 1)
n@1124 1969 {
n@1124 1970 this.title = titleNode[0].textContent;
n@1124 1971 }
n@1124 1972 var interfaceOptionNodes = xml.getElementsByTagName('interfaceoption');
n@1124 1973 // Extract interfaceoption node schema
n@1170 1974 var interfaceOptionNodeSchema = this.schema.getAllElementsByName('interfaceoption')[0];
n@1148 1975 var attributeMap = interfaceOptionNodeSchema.getAllElementsByTagName('xs:attribute');
n@1124 1976 for (var i=0; i<interfaceOptionNodes.length; i++)
n@1124 1977 {
n@1124 1978 var ioNode = interfaceOptionNodes[i];
n@1124 1979 var option = {};
n@1124 1980 for (var j=0; j<attributeMap.length; j++)
n@1124 1981 {
n@1124 1982 var attributeName = attributeMap[j].getAttribute('name') || attributeMap[j].getAttribute('ref');
n@1124 1983 var projectAttr = ioNode.getAttribute(attributeName);
n@1124 1984 projectAttr = parent.processAttribute(projectAttr,attributeMap[j]);
n@1124 1985 switch(typeof projectAttr)
n@1124 1986 {
n@1124 1987 case "number":
n@1124 1988 case "boolean":
n@1124 1989 eval('option.'+attributeName+' = '+projectAttr);
n@1124 1990 break;
n@1124 1991 case "string":
n@1124 1992 eval('option.'+attributeName+' = "'+projectAttr+'"');
n@1124 1993 break;
n@1124 1994 }
n@1124 1995 }
n@1124 1996 this.options.push(option);
n@1124 1997 }
n@1124 1998
n@1124 1999 // Now the scales nodes
n@1124 2000 var scaleParent = xml.getElementsByTagName('scales');
n@1124 2001 if (scaleParent.length == 1) {
n@1124 2002 scaleParent = scaleParent[0];
n@1124 2003 for (var i=0; i<scaleParent.children.length; i++) {
n@1124 2004 var child = scaleParent.children[i];
n@1124 2005 this.scales.push({
n@1124 2006 text: child.textContent,
n@1124 2007 position: Number(child.getAttribute('position'))
n@1124 2008 });
n@1118 2009 }
n@1118 2010 }
n@1118 2011 };
n@1124 2012
n@1172 2013 this.encode = function(doc) {
n@1172 2014 var node = doc.createElement("interface");
n@1172 2015 if (typeof name == "string")
n@1172 2016 node.setAttribute("name",this.name);
n@1172 2017 for (var option of this.options)
n@1172 2018 {
n@1172 2019 var child = doc.createElement("interfaceoption");
n@1172 2020 child.setAttribute("type",option.type);
n@1172 2021 child.setAttribute("name",option.name);
n@1172 2022 node.appendChild(child);
n@1172 2023 }
n@1172 2024 if (this.scales.length != 0) {
n@1172 2025 var scales = doc.createElement("scales");
n@1172 2026 for (var scale of this.scales)
n@1172 2027 {
n@1172 2028 var child = doc.createElement("scalelabel");
n@1172 2029 child.setAttribute("position",scale.position);
n@1172 2030 child.textContent = scale.text;
n@1172 2031 scales.appendChild(child);
n@1172 2032 }
n@1172 2033 node.appendChild(scales);
n@1172 2034 }
n@1172 2035 return node;
n@1124 2036 };
n@1118 2037 };
n@1118 2038
n@1170 2039 this.metricNode = function() {
n@1170 2040 this.enabled = [];
n@1170 2041 this.decode = function(parent, xml) {
n@1170 2042 var children = xml.getElementsByTagName('metricenable');
n@1170 2043 for (var i in children) {
n@1170 2044 if (isNaN(Number(i)) == true){break;}
n@1170 2045 this.enabled.push(children[i].textContent);
n@1170 2046 }
n@1170 2047 }
n@1172 2048 this.encode = function(doc) {
n@1172 2049 var node = doc.createElement('metric');
n@1170 2050 for (var i in this.enabled)
n@1170 2051 {
n@1170 2052 if (isNaN(Number(i)) == true){break;}
n@1172 2053 var child = doc.createElement('metricenable');
n@1170 2054 child.textContent = this.enabled[i];
n@1170 2055 node.appendChild(child);
n@1170 2056 }
n@1170 2057 return node;
n@1170 2058 }
n@1170 2059 }
n@1170 2060
n@1124 2061 this.page = function() {
n@1118 2062 this.presentedId = undefined;
n@1118 2063 this.id = undefined;
n@1118 2064 this.hostURL = undefined;
n@1118 2065 this.randomiseOrder = undefined;
n@1118 2066 this.loop = undefined;
n@1124 2067 this.showElementComments = undefined;
n@1118 2068 this.outsideReference = null;
n@1118 2069 this.loudness = null;
n@1124 2070 this.preTest = null;
n@1124 2071 this.postTest = null;
n@1118 2072 this.interfaces = [];
n@1118 2073 this.commentBoxPrefix = "Comment on track";
n@1118 2074 this.audioElements = [];
n@1118 2075 this.commentQuestions = [];
n@1170 2076 this.schema = specification.schema.getAllElementsByName("page")[0];
n@1118 2077
n@1170 2078 this.decode = function(parent,xml)
n@1118 2079 {
n@1148 2080 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
n@1124 2081 for (var i=0; i<attributeMap.length; i++)
n@1118 2082 {
n@1124 2083 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
n@1124 2084 var projectAttr = xml.getAttribute(attributeName);
n@1124 2085 projectAttr = parent.processAttribute(projectAttr,attributeMap[i]);
n@1124 2086 switch(typeof projectAttr)
n@1118 2087 {
n@1124 2088 case "number":
n@1124 2089 case "boolean":
n@1124 2090 eval('this.'+attributeName+' = '+projectAttr);
n@1124 2091 break;
n@1124 2092 case "string":
n@1124 2093 eval('this.'+attributeName+' = "'+projectAttr+'"');
n@1124 2094 break;
n@1118 2095 }
n@1118 2096 }
n@1118 2097
n@1124 2098 // Get the Comment Box Prefix
n@1124 2099 var CBP = xml.getElementsByTagName('commentboxprefix');
n@1124 2100 if (CBP.length != 0) {
n@1124 2101 this.commentBoxPrefix = CBP[0].textContent;
n@1118 2102 }
n@1118 2103
n@1124 2104 // Now decode the interfaces
n@1124 2105 var interfaceNode = xml.getElementsByTagName('interface');
n@1124 2106 for (var i=0; i<interfaceNode.length; i++)
n@1124 2107 {
n@1124 2108 var node = new parent.interfaceNode();
n@1148 2109 node.decode(this,interfaceNode[i],parent.schema.getAllElementsByName('interface')[1]);
n@1124 2110 this.interfaces.push(node);
n@1124 2111 }
n@1118 2112
n@1124 2113 // Now process the survey node options
n@1124 2114 var survey = xml.getElementsByTagName('survey');
n@1148 2115 var surveySchema = parent.schema.getAllElementsByName('survey')[0];
n@1124 2116 for (var i in survey) {
n@1124 2117 if (isNaN(Number(i)) == true){break;}
n@1124 2118 var location = survey[i].getAttribute('location');
n@1124 2119 if (location == 'pre' || location == 'before')
n@1124 2120 {
n@1124 2121 if (this.preTest != null){this.errors.push("Already a pre/before test survey defined! Ignoring second!!");}
n@1124 2122 else {
n@1124 2123 this.preTest = new parent.surveyNode();
n@1124 2124 this.preTest.decode(parent,survey[i],surveySchema);
n@1124 2125 }
n@1124 2126 } else if (location == 'post' || location == 'after') {
n@1124 2127 if (this.postTest != null){this.errors.push("Already a post/after test survey defined! Ignoring second!!");}
n@1124 2128 else {
n@1124 2129 this.postTest = new parent.surveyNode();
n@1124 2130 this.postTest.decode(parent,survey[i],surveySchema);
n@1124 2131 }
n@1124 2132 }
n@1124 2133 }
n@1124 2134
n@1124 2135 // Now process the audioelement tags
n@1124 2136 var audioElements = xml.getElementsByTagName('audioelement');
n@1124 2137 for (var i=0; i<audioElements.length; i++)
n@1124 2138 {
n@1124 2139 var node = new this.audioElementNode();
n@1170 2140 node.decode(this,audioElements[i]);
n@1124 2141 this.audioElements.push(node);
n@1124 2142 }
n@1124 2143
n@1124 2144 // Now decode the commentquestions
n@1124 2145 var commentQuestions = xml.getElementsByTagName('commentquestion');
n@1124 2146 for (var i=0; i<commentQuestions.length; i++)
n@1124 2147 {
n@1118 2148 var node = new this.commentQuestionNode();
n@1170 2149 node.decode(parent,commentQuestions[i]);
n@1118 2150 this.commentQuestions.push(node);
n@1118 2151 }
n@1118 2152 };
n@1118 2153
n@1118 2154 this.encode = function(root)
n@1118 2155 {
n@1172 2156 var AHNode = root.createElement("page");
n@1172 2157 // First decode the attributes
n@1172 2158 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
n@1172 2159 for (var i=0; i<attributes.length; i++)
n@1172 2160 {
n@1172 2161 var name = attributes[i].getAttribute("name");
n@1172 2162 if (name == undefined) {
n@1172 2163 name = attributes[i].getAttribute("ref");
n@1172 2164 }
n@1172 2165 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
n@1172 2166 {
n@1172 2167 eval("AHNode.setAttribute('"+name+"',this."+name+")");
n@1172 2168 }
n@1172 2169 }
n@1118 2170 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);}
n@1172 2171 // <commentboxprefix>
n@1172 2172 var commentboxprefix = root.createElement("commentboxprefix");
n@1172 2173 commentboxprefix.textContent = this.commentBoxPrefix;
n@1172 2174 AHNode.appendChild(commentboxprefix);
n@1172 2175
n@1118 2176 for (var i=0; i<this.interfaces.length; i++)
n@1118 2177 {
n@1118 2178 AHNode.appendChild(this.interfaces[i].encode(root));
n@1118 2179 }
n@1118 2180
n@1118 2181 for (var i=0; i<this.audioElements.length; i++) {
n@1118 2182 AHNode.appendChild(this.audioElements[i].encode(root));
n@1118 2183 }
n@1118 2184 // Create <CommentQuestion>
n@1118 2185 for (var i=0; i<this.commentQuestions.length; i++)
n@1118 2186 {
n@1172 2187 AHNode.appendChild(this.commentQuestions[i].encode(root));
n@1118 2188 }
n@1118 2189
n@1172 2190 AHNode.appendChild(this.preTest.encode(root));
n@1172 2191 AHNode.appendChild(this.postTest.encode(root));
n@1118 2192 return AHNode;
n@1118 2193 };
n@1118 2194
n@1124 2195 this.commentQuestionNode = function() {
n@1124 2196 this.id = null;
n@1124 2197 this.type = undefined;
n@1118 2198 this.options = [];
n@1124 2199 this.statement = undefined;
n@1170 2200 this.schema = specification.schema.getAllElementsByName('commentquestion')[0];
n@1170 2201 this.decode = function(parent,xml)
n@1118 2202 {
n@1124 2203 this.id = xml.id;
n@1124 2204 this.type = xml.getAttribute('type');
n@1124 2205 this.statement = xml.getElementsByTagName('statement')[0].textContent;
n@1124 2206 var optNodes = xml.getElementsByTagName('option');
n@1124 2207 for (var i=0; i<optNodes.length; i++)
n@1124 2208 {
n@1124 2209 var optNode = optNodes[i];
n@1124 2210 this.options.push({
n@1124 2211 name: optNode.getAttribute('name'),
n@1124 2212 text: optNode.textContent
n@1124 2213 });
n@1118 2214 }
n@1118 2215 };
n@1124 2216
n@1118 2217 this.encode = function(root)
n@1118 2218 {
n@1172 2219 var node = root.createElement("commentquestion");
n@1172 2220 node.id = this.id;
n@1172 2221 node.setAttribute("type",this.type);
n@1172 2222 var statement = root.createElement("statement");
n@1172 2223 statement.textContent = this.statement;
n@1172 2224 node.appendChild(statement);
n@1172 2225 for (var option of this.options)
n@1172 2226 {
n@1172 2227 var child = root.createElement("option");
n@1172 2228 child.setAttribute("name",option.name);
n@1172 2229 child.textContent = option.text;
n@1172 2230 node.appendChild(child);
n@1172 2231 }
n@1172 2232 return node;
n@1118 2233 };
n@1118 2234 };
n@1118 2235
n@1118 2236 this.audioElementNode = function() {
n@1118 2237 this.url = null;
n@1118 2238 this.id = null;
n@1118 2239 this.parent = null;
n@1124 2240 this.type = null;
n@1118 2241 this.marker = false;
n@1118 2242 this.enforce = false;
n@1118 2243 this.gain = 1.0;
n@1170 2244 this.schema = specification.schema.getAllElementsByName('audioelement')[0];;
n@1124 2245 this.parent = null;
n@1170 2246 this.decode = function(parent,xml)
n@1118 2247 {
n@1118 2248 this.parent = parent;
n@1148 2249 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
n@1124 2250 for (var i=0; i<attributeMap.length; i++)
n@1118 2251 {
n@1124 2252 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
n@1124 2253 var projectAttr = xml.getAttribute(attributeName);
n@1124 2254 projectAttr = specification.processAttribute(projectAttr,attributeMap[i]);
n@1124 2255 switch(typeof projectAttr)
n@1118 2256 {
n@1124 2257 case "number":
n@1124 2258 case "boolean":
n@1124 2259 eval('this.'+attributeName+' = '+projectAttr);
n@1124 2260 break;
n@1124 2261 case "string":
n@1124 2262 eval('this.'+attributeName+' = "'+projectAttr+'"');
n@1124 2263 break;
n@1118 2264 }
n@1118 2265 }
n@1124 2266
n@1118 2267 };
n@1118 2268 this.encode = function(root)
n@1118 2269 {
n@1172 2270 var AENode = root.createElement("audioelement");
n@1172 2271 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
n@1172 2272 for (var i=0; i<attributes.length; i++)
n@1172 2273 {
n@1172 2274 var name = attributes[i].getAttribute("name");
n@1172 2275 if (name == undefined) {
n@1172 2276 name = attributes[i].getAttribute("ref");
n@1172 2277 }
n@1172 2278 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
n@1172 2279 {
n@1172 2280 eval("AENode.setAttribute('"+name+"',this."+name+")");
n@1172 2281 }
n@1172 2282 }
n@1118 2283 return AENode;
n@1118 2284 };
n@1118 2285 };
n@1118 2286 };
n@1118 2287 }
n@1118 2288
n@1118 2289 function Interface(specificationObject) {
n@1118 2290 // This handles the bindings between the interface and the audioEngineContext;
n@1118 2291 this.specification = specificationObject;
n@1118 2292 this.insertPoint = document.getElementById("topLevelBody");
n@1118 2293
n@1124 2294 this.newPage = function(audioHolderObject,store)
n@1118 2295 {
n@1169 2296 audioEngineContext.newTestPage(audioHolderObject,store);
n@1118 2297 interfaceContext.deleteCommentBoxes();
n@1118 2298 interfaceContext.deleteCommentQuestions();
n@1124 2299 loadTest(audioHolderObject,store);
n@1118 2300 };
n@1118 2301
n@1118 2302 // Bounded by interface!!
n@1118 2303 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
n@1118 2304 // For example, APE returns the slider position normalised in a <value> tag.
n@1118 2305 this.interfaceObjects = [];
n@1118 2306 this.interfaceObject = function(){};
n@1118 2307
n@1118 2308 this.resizeWindow = function(event)
n@1118 2309 {
n@1118 2310 popup.resize(event);
n@1118 2311 for(var i=0; i<this.commentBoxes.length; i++)
n@1118 2312 {this.commentBoxes[i].resize();}
n@1118 2313 for(var i=0; i<this.commentQuestions.length; i++)
n@1118 2314 {this.commentQuestions[i].resize();}
n@1118 2315 try
n@1118 2316 {
n@1118 2317 resizeWindow(event);
n@1118 2318 }
n@1118 2319 catch(err)
n@1118 2320 {
n@1118 2321 console.log("Warning - Interface does not have Resize option");
n@1118 2322 console.log(err);
n@1118 2323 }
n@1118 2324 };
n@1118 2325
n@1118 2326 this.returnNavigator = function()
n@1118 2327 {
n@1162 2328 var node = storage.document.createElement("navigator");
n@1162 2329 var platform = storage.document.createElement("platform");
n@1118 2330 platform.textContent = navigator.platform;
n@1162 2331 var vendor = storage.document.createElement("vendor");
n@1118 2332 vendor.textContent = navigator.vendor;
n@1162 2333 var userAgent = storage.document.createElement("uagent");
n@1118 2334 userAgent.textContent = navigator.userAgent;
n@1162 2335 var screen = storage.document.createElement("window");
n@1162 2336 screen.setAttribute('innerWidth',window.innerWidth);
n@1162 2337 screen.setAttribute('innerHeight',window.innerHeight);
n@1118 2338 node.appendChild(platform);
n@1118 2339 node.appendChild(vendor);
n@1118 2340 node.appendChild(userAgent);
n@1162 2341 node.appendChild(screen);
n@1118 2342 return node;
n@1118 2343 };
n@1118 2344
n@1118 2345 this.commentBoxes = [];
n@1118 2346 this.elementCommentBox = function(audioObject) {
n@1118 2347 var element = audioObject.specification;
n@1118 2348 this.audioObject = audioObject;
n@1118 2349 this.id = audioObject.id;
n@1118 2350 var audioHolderObject = audioObject.specification.parent;
n@1118 2351 // Create document objects to hold the comment boxes
n@1118 2352 this.trackComment = document.createElement('div');
n@1118 2353 this.trackComment.className = 'comment-div';
n@1118 2354 this.trackComment.id = 'comment-div-'+audioObject.id;
n@1118 2355 // Create a string next to each comment asking for a comment
n@1118 2356 this.trackString = document.createElement('span');
n@1116 2357 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.interfaceDOM.getPresentedId();
n@1118 2358 // Create the HTML5 comment box 'textarea'
n@1118 2359 this.trackCommentBox = document.createElement('textarea');
n@1118 2360 this.trackCommentBox.rows = '4';
n@1118 2361 this.trackCommentBox.cols = '100';
n@1118 2362 this.trackCommentBox.name = 'trackComment'+audioObject.id;
n@1118 2363 this.trackCommentBox.className = 'trackComment';
n@1118 2364 var br = document.createElement('br');
n@1118 2365 // Add to the holder.
n@1118 2366 this.trackComment.appendChild(this.trackString);
n@1118 2367 this.trackComment.appendChild(br);
n@1118 2368 this.trackComment.appendChild(this.trackCommentBox);
n@1118 2369
n@1118 2370 this.exportXMLDOM = function() {
n@1118 2371 var root = document.createElement('comment');
n@1118 2372 if (this.audioObject.specification.parent.elementComments) {
n@1118 2373 var question = document.createElement('question');
n@1118 2374 question.textContent = this.trackString.textContent;
n@1118 2375 var response = document.createElement('response');
n@1118 2376 response.textContent = this.trackCommentBox.value;
n@1118 2377 console.log("Comment frag-"+this.id+": "+response.textContent);
n@1118 2378 root.appendChild(question);
n@1118 2379 root.appendChild(response);
n@1118 2380 }
n@1118 2381 return root;
n@1118 2382 };
n@1118 2383 this.resize = function()
n@1118 2384 {
n@1118 2385 var boxwidth = (window.innerWidth-100)/2;
n@1118 2386 if (boxwidth >= 600)
n@1118 2387 {
n@1118 2388 boxwidth = 600;
n@1118 2389 }
n@1118 2390 else if (boxwidth < 400)
n@1118 2391 {
n@1118 2392 boxwidth = 400;
n@1118 2393 }
n@1118 2394 this.trackComment.style.width = boxwidth+"px";
n@1118 2395 this.trackCommentBox.style.width = boxwidth-6+"px";
n@1118 2396 };
n@1118 2397 this.resize();
n@1118 2398 };
n@1118 2399
n@1118 2400 this.commentQuestions = [];
n@1118 2401
n@1118 2402 this.commentBox = function(commentQuestion) {
n@1118 2403 this.specification = commentQuestion;
n@1118 2404 // Create document objects to hold the comment boxes
n@1118 2405 this.holder = document.createElement('div');
n@1118 2406 this.holder.className = 'comment-div';
n@1118 2407 // Create a string next to each comment asking for a comment
n@1118 2408 this.string = document.createElement('span');
n@1124 2409 this.string.innerHTML = commentQuestion.statement;
n@1118 2410 // Create the HTML5 comment box 'textarea'
n@1118 2411 this.textArea = document.createElement('textarea');
n@1118 2412 this.textArea.rows = '4';
n@1118 2413 this.textArea.cols = '100';
n@1118 2414 this.textArea.className = 'trackComment';
n@1118 2415 var br = document.createElement('br');
n@1118 2416 // Add to the holder.
n@1118 2417 this.holder.appendChild(this.string);
n@1118 2418 this.holder.appendChild(br);
n@1118 2419 this.holder.appendChild(this.textArea);
n@1118 2420
n@1118 2421 this.exportXMLDOM = function() {
n@1118 2422 var root = document.createElement('comment');
n@1118 2423 root.id = this.specification.id;
n@1118 2424 root.setAttribute('type',this.specification.type);
n@1118 2425 root.textContent = this.textArea.value;
n@1118 2426 console.log("Question: "+this.string.textContent);
n@1118 2427 console.log("Response: "+root.textContent);
n@1118 2428 return root;
n@1118 2429 };
n@1118 2430 this.resize = function()
n@1118 2431 {
n@1118 2432 var boxwidth = (window.innerWidth-100)/2;
n@1118 2433 if (boxwidth >= 600)
n@1118 2434 {
n@1118 2435 boxwidth = 600;
n@1118 2436 }
n@1118 2437 else if (boxwidth < 400)
n@1118 2438 {
n@1118 2439 boxwidth = 400;
n@1118 2440 }
n@1118 2441 this.holder.style.width = boxwidth+"px";
n@1118 2442 this.textArea.style.width = boxwidth-6+"px";
n@1118 2443 };
n@1118 2444 this.resize();
n@1118 2445 };
n@1118 2446
n@1118 2447 this.radioBox = function(commentQuestion) {
n@1118 2448 this.specification = commentQuestion;
n@1118 2449 // Create document objects to hold the comment boxes
n@1118 2450 this.holder = document.createElement('div');
n@1118 2451 this.holder.className = 'comment-div';
n@1118 2452 // Create a string next to each comment asking for a comment
n@1118 2453 this.string = document.createElement('span');
n@1118 2454 this.string.innerHTML = commentQuestion.statement;
n@1118 2455 var br = document.createElement('br');
n@1118 2456 // Add to the holder.
n@1118 2457 this.holder.appendChild(this.string);
n@1118 2458 this.holder.appendChild(br);
n@1118 2459 this.options = [];
n@1118 2460 this.inputs = document.createElement('div');
n@1118 2461 this.span = document.createElement('div');
n@1118 2462 this.inputs.align = 'center';
n@1118 2463 this.inputs.style.marginLeft = '12px';
n@1118 2464 this.span.style.marginLeft = '12px';
n@1118 2465 this.span.align = 'center';
n@1118 2466 this.span.style.marginTop = '15px';
n@1118 2467
n@1118 2468 var optCount = commentQuestion.options.length;
n@1124 2469 for (var optNode of commentQuestion.options)
n@1118 2470 {
n@1118 2471 var div = document.createElement('div');
n@1118 2472 div.style.width = '80px';
n@1118 2473 div.style.float = 'left';
n@1118 2474 var input = document.createElement('input');
n@1118 2475 input.type = 'radio';
n@1118 2476 input.name = commentQuestion.id;
n@1124 2477 input.setAttribute('setvalue',optNode.name);
n@1118 2478 input.className = 'comment-radio';
n@1118 2479 div.appendChild(input);
n@1118 2480 this.inputs.appendChild(div);
n@1118 2481
n@1118 2482
n@1118 2483 div = document.createElement('div');
n@1118 2484 div.style.width = '80px';
n@1118 2485 div.style.float = 'left';
n@1118 2486 div.align = 'center';
n@1118 2487 var span = document.createElement('span');
n@1124 2488 span.textContent = optNode.text;
n@1118 2489 span.className = 'comment-radio-span';
n@1118 2490 div.appendChild(span);
n@1118 2491 this.span.appendChild(div);
n@1118 2492 this.options.push(input);
n@1118 2493 }
n@1118 2494 this.holder.appendChild(this.span);
n@1118 2495 this.holder.appendChild(this.inputs);
n@1118 2496
n@1118 2497 this.exportXMLDOM = function() {
n@1118 2498 var root = document.createElement('comment');
n@1118 2499 root.id = this.specification.id;
n@1118 2500 root.setAttribute('type',this.specification.type);
n@1118 2501 var question = document.createElement('question');
n@1118 2502 question.textContent = this.string.textContent;
n@1118 2503 var response = document.createElement('response');
n@1118 2504 var i=0;
n@1118 2505 while(this.options[i].checked == false) {
n@1118 2506 i++;
n@1118 2507 if (i >= this.options.length) {
n@1118 2508 break;
n@1118 2509 }
n@1118 2510 }
n@1118 2511 if (i >= this.options.length) {
n@1118 2512 response.textContent = 'null';
n@1118 2513 } else {
n@1118 2514 response.textContent = this.options[i].getAttribute('setvalue');
n@1118 2515 response.setAttribute('number',i);
n@1118 2516 }
n@1118 2517 console.log('Comment: '+question.textContent);
n@1118 2518 console.log('Response: '+response.textContent);
n@1118 2519 root.appendChild(question);
n@1118 2520 root.appendChild(response);
n@1118 2521 return root;
n@1118 2522 };
n@1118 2523 this.resize = function()
n@1118 2524 {
n@1118 2525 var boxwidth = (window.innerWidth-100)/2;
n@1118 2526 if (boxwidth >= 600)
n@1118 2527 {
n@1118 2528 boxwidth = 600;
n@1118 2529 }
n@1118 2530 else if (boxwidth < 400)
n@1118 2531 {
n@1118 2532 boxwidth = 400;
n@1118 2533 }
n@1118 2534 this.holder.style.width = boxwidth+"px";
n@1118 2535 var text = this.holder.children[2];
n@1118 2536 var options = this.holder.children[3];
n@1118 2537 var optCount = options.children.length;
n@1118 2538 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
n@1118 2539 var options = options.firstChild;
n@1118 2540 var text = text.firstChild;
n@1118 2541 options.style.marginRight = spanMargin;
n@1118 2542 options.style.marginLeft = spanMargin;
n@1118 2543 text.style.marginRight = spanMargin;
n@1118 2544 text.style.marginLeft = spanMargin;
n@1118 2545 while(options.nextSibling != undefined)
n@1118 2546 {
n@1118 2547 options = options.nextSibling;
n@1118 2548 text = text.nextSibling;
n@1118 2549 options.style.marginRight = spanMargin;
n@1118 2550 options.style.marginLeft = spanMargin;
n@1118 2551 text.style.marginRight = spanMargin;
n@1118 2552 text.style.marginLeft = spanMargin;
n@1118 2553 }
n@1118 2554 };
n@1118 2555 this.resize();
n@1118 2556 };
n@1118 2557
n@1118 2558 this.checkboxBox = function(commentQuestion) {
n@1118 2559 this.specification = commentQuestion;
n@1118 2560 // Create document objects to hold the comment boxes
n@1118 2561 this.holder = document.createElement('div');
n@1118 2562 this.holder.className = 'comment-div';
n@1118 2563 // Create a string next to each comment asking for a comment
n@1118 2564 this.string = document.createElement('span');
n@1118 2565 this.string.innerHTML = commentQuestion.statement;
n@1118 2566 var br = document.createElement('br');
n@1118 2567 // Add to the holder.
n@1118 2568 this.holder.appendChild(this.string);
n@1118 2569 this.holder.appendChild(br);
n@1118 2570 this.options = [];
n@1118 2571 this.inputs = document.createElement('div');
n@1118 2572 this.span = document.createElement('div');
n@1118 2573 this.inputs.align = 'center';
n@1118 2574 this.inputs.style.marginLeft = '12px';
n@1118 2575 this.span.style.marginLeft = '12px';
n@1118 2576 this.span.align = 'center';
n@1118 2577 this.span.style.marginTop = '15px';
n@1118 2578
n@1118 2579 var optCount = commentQuestion.options.length;
n@1118 2580 for (var i=0; i<optCount; i++)
n@1118 2581 {
n@1118 2582 var div = document.createElement('div');
n@1118 2583 div.style.width = '80px';
n@1118 2584 div.style.float = 'left';
n@1118 2585 var input = document.createElement('input');
n@1118 2586 input.type = 'checkbox';
n@1118 2587 input.name = commentQuestion.id;
n@1118 2588 input.setAttribute('setvalue',commentQuestion.options[i].name);
n@1118 2589 input.className = 'comment-radio';
n@1118 2590 div.appendChild(input);
n@1118 2591 this.inputs.appendChild(div);
n@1118 2592
n@1118 2593
n@1118 2594 div = document.createElement('div');
n@1118 2595 div.style.width = '80px';
n@1118 2596 div.style.float = 'left';
n@1118 2597 div.align = 'center';
n@1118 2598 var span = document.createElement('span');
n@1118 2599 span.textContent = commentQuestion.options[i].text;
n@1118 2600 span.className = 'comment-radio-span';
n@1118 2601 div.appendChild(span);
n@1118 2602 this.span.appendChild(div);
n@1118 2603 this.options.push(input);
n@1118 2604 }
n@1118 2605 this.holder.appendChild(this.span);
n@1118 2606 this.holder.appendChild(this.inputs);
n@1118 2607
n@1118 2608 this.exportXMLDOM = function() {
n@1118 2609 var root = document.createElement('comment');
n@1118 2610 root.id = this.specification.id;
n@1118 2611 root.setAttribute('type',this.specification.type);
n@1118 2612 var question = document.createElement('question');
n@1118 2613 question.textContent = this.string.textContent;
n@1118 2614 root.appendChild(question);
n@1118 2615 console.log('Comment: '+question.textContent);
n@1118 2616 for (var i=0; i<this.options.length; i++) {
n@1118 2617 var response = document.createElement('response');
n@1118 2618 response.textContent = this.options[i].checked;
n@1118 2619 response.setAttribute('name',this.options[i].getAttribute('setvalue'));
n@1118 2620 root.appendChild(response);
n@1118 2621 console.log('Response '+response.getAttribute('name') +': '+response.textContent);
n@1118 2622 }
n@1118 2623 return root;
n@1118 2624 };
n@1118 2625 this.resize = function()
n@1118 2626 {
n@1118 2627 var boxwidth = (window.innerWidth-100)/2;
n@1118 2628 if (boxwidth >= 600)
n@1118 2629 {
n@1118 2630 boxwidth = 600;
n@1118 2631 }
n@1118 2632 else if (boxwidth < 400)
n@1118 2633 {
n@1118 2634 boxwidth = 400;
n@1118 2635 }
n@1118 2636 this.holder.style.width = boxwidth+"px";
n@1118 2637 var text = this.holder.children[2];
n@1118 2638 var options = this.holder.children[3];
n@1118 2639 var optCount = options.children.length;
n@1118 2640 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
n@1118 2641 var options = options.firstChild;
n@1118 2642 var text = text.firstChild;
n@1118 2643 options.style.marginRight = spanMargin;
n@1118 2644 options.style.marginLeft = spanMargin;
n@1118 2645 text.style.marginRight = spanMargin;
n@1118 2646 text.style.marginLeft = spanMargin;
n@1118 2647 while(options.nextSibling != undefined)
n@1118 2648 {
n@1118 2649 options = options.nextSibling;
n@1118 2650 text = text.nextSibling;
n@1118 2651 options.style.marginRight = spanMargin;
n@1118 2652 options.style.marginLeft = spanMargin;
n@1118 2653 text.style.marginRight = spanMargin;
n@1118 2654 text.style.marginLeft = spanMargin;
n@1118 2655 }
n@1118 2656 };
n@1118 2657 this.resize();
n@1118 2658 };
n@1118 2659
n@1118 2660 this.createCommentBox = function(audioObject) {
n@1118 2661 var node = new this.elementCommentBox(audioObject);
n@1118 2662 this.commentBoxes.push(node);
n@1118 2663 audioObject.commentDOM = node;
n@1118 2664 return node;
n@1118 2665 };
n@1118 2666
n@1118 2667 this.sortCommentBoxes = function() {
n@1138 2668 this.commentBoxes.sort(function(a,b){return a.id - b.id;});
n@1118 2669 };
n@1118 2670
n@1118 2671 this.showCommentBoxes = function(inject, sort) {
n@1118 2672 if (sort) {interfaceContext.sortCommentBoxes();}
n@1138 2673 for (var box of interfaceContext.commentBoxes) {
n@1138 2674 inject.appendChild(box.trackComment);
n@1118 2675 }
n@1118 2676 };
n@1118 2677
n@1118 2678 this.deleteCommentBoxes = function() {
n@1118 2679 this.commentBoxes = [];
n@1118 2680 };
n@1118 2681
n@1118 2682 this.createCommentQuestion = function(element) {
n@1118 2683 var node;
n@1124 2684 if (element.type == 'question') {
n@1118 2685 node = new this.commentBox(element);
n@1118 2686 } else if (element.type == 'radio') {
n@1118 2687 node = new this.radioBox(element);
n@1118 2688 } else if (element.type == 'checkbox') {
n@1118 2689 node = new this.checkboxBox(element);
n@1118 2690 }
n@1118 2691 this.commentQuestions.push(node);
n@1118 2692 return node;
n@1118 2693 };
n@1118 2694
n@1118 2695 this.deleteCommentQuestions = function()
n@1118 2696 {
n@1118 2697 this.commentQuestions = [];
n@1118 2698 };
n@1118 2699
n@1118 2700 this.playhead = new function()
n@1118 2701 {
n@1118 2702 this.object = document.createElement('div');
n@1118 2703 this.object.className = 'playhead';
n@1118 2704 this.object.align = 'left';
n@1118 2705 var curTime = document.createElement('div');
n@1118 2706 curTime.style.width = '50px';
n@1118 2707 this.curTimeSpan = document.createElement('span');
n@1118 2708 this.curTimeSpan.textContent = '00:00';
n@1118 2709 curTime.appendChild(this.curTimeSpan);
n@1118 2710 this.object.appendChild(curTime);
n@1118 2711 this.scrubberTrack = document.createElement('div');
n@1118 2712 this.scrubberTrack.className = 'playhead-scrub-track';
n@1118 2713
n@1118 2714 this.scrubberHead = document.createElement('div');
n@1118 2715 this.scrubberHead.id = 'playhead-scrubber';
n@1118 2716 this.scrubberTrack.appendChild(this.scrubberHead);
n@1118 2717 this.object.appendChild(this.scrubberTrack);
n@1118 2718
n@1118 2719 this.timePerPixel = 0;
n@1118 2720 this.maxTime = 0;
n@1118 2721
n@1118 2722 this.playbackObject;
n@1118 2723
n@1118 2724 this.setTimePerPixel = function(audioObject) {
n@1118 2725 //maxTime must be in seconds
n@1118 2726 this.playbackObject = audioObject;
n@1118 2727 this.maxTime = audioObject.buffer.buffer.duration;
n@1118 2728 var width = 490; //500 - 10, 5 each side of the tracker head
n@1118 2729 this.timePerPixel = this.maxTime/490;
n@1118 2730 if (this.maxTime < 60) {
n@1118 2731 this.curTimeSpan.textContent = '0.00';
n@1118 2732 } else {
n@1118 2733 this.curTimeSpan.textContent = '00:00';
n@1118 2734 }
n@1118 2735 };
n@1118 2736
n@1118 2737 this.update = function() {
n@1118 2738 // Update the playhead position, startPlay must be called
n@1118 2739 if (this.timePerPixel > 0) {
n@1118 2740 var time = this.playbackObject.getCurrentPosition();
n@1167 2741 if (time > 0 && time < this.maxTime) {
n@1118 2742 var width = 490;
n@1118 2743 var pix = Math.floor(time/this.timePerPixel);
n@1118 2744 this.scrubberHead.style.left = pix+'px';
n@1118 2745 if (this.maxTime > 60.0) {
n@1118 2746 var secs = time%60;
n@1118 2747 var mins = Math.floor((time-secs)/60);
n@1118 2748 secs = secs.toString();
n@1118 2749 secs = secs.substr(0,2);
n@1118 2750 mins = mins.toString();
n@1118 2751 this.curTimeSpan.textContent = mins+':'+secs;
n@1118 2752 } else {
n@1118 2753 time = time.toString();
n@1118 2754 this.curTimeSpan.textContent = time.substr(0,4);
n@1118 2755 }
n@1118 2756 } else {
n@1118 2757 this.scrubberHead.style.left = '0px';
n@1118 2758 if (this.maxTime < 60) {
n@1118 2759 this.curTimeSpan.textContent = '0.00';
n@1118 2760 } else {
n@1118 2761 this.curTimeSpan.textContent = '00:00';
n@1118 2762 }
n@1118 2763 }
n@1118 2764 }
n@1118 2765 };
n@1118 2766
n@1118 2767 this.interval = undefined;
n@1118 2768
n@1118 2769 this.start = function() {
n@1118 2770 if (this.playbackObject != undefined && this.interval == undefined) {
n@1118 2771 if (this.maxTime < 60) {
n@1118 2772 this.interval = setInterval(function(){interfaceContext.playhead.update();},10);
n@1118 2773 } else {
n@1118 2774 this.interval = setInterval(function(){interfaceContext.playhead.update();},100);
n@1118 2775 }
n@1118 2776 }
n@1118 2777 };
n@1118 2778 this.stop = function() {
n@1118 2779 clearInterval(this.interval);
n@1118 2780 this.interval = undefined;
n@1118 2781 if (this.maxTime < 60) {
n@1118 2782 this.curTimeSpan.textContent = '0.00';
n@1118 2783 } else {
n@1118 2784 this.curTimeSpan.textContent = '00:00';
n@1118 2785 }
n@1118 2786 };
n@1118 2787 };
n@1154 2788
n@1154 2789 this.volume = new function()
n@1154 2790 {
n@1154 2791 // An in-built volume module which can be viewed on page
n@1154 2792 // Includes trackers on page-by-page data
n@1154 2793 // Volume does NOT reset to 0dB on each page load
n@1154 2794 this.valueLin = 1.0;
n@1154 2795 this.valueDB = 0.0;
n@1154 2796 this.object = document.createElement('div');
n@1154 2797 this.object.id = 'master-volume-holder';
n@1154 2798 this.slider = document.createElement('input');
n@1154 2799 this.slider.id = 'master-volume-control';
n@1154 2800 this.slider.type = 'range';
n@1154 2801 this.valueText = document.createElement('span');
n@1154 2802 this.valueText.id = 'master-volume-feedback';
n@1154 2803 this.valueText.textContent = '0dB';
n@1154 2804
n@1154 2805 this.slider.min = -60;
n@1154 2806 this.slider.max = 12;
n@1154 2807 this.slider.value = 0;
n@1154 2808 this.slider.step = 1;
n@1154 2809 this.slider.onmousemove = function(event)
n@1154 2810 {
n@1154 2811 interfaceContext.volume.valueDB = event.currentTarget.value;
n@1154 2812 interfaceContext.volume.valueLin = decibelToLinear(interfaceContext.volume.valueDB);
n@1154 2813 interfaceContext.volume.valueText.textContent = interfaceContext.volume.valueDB+'dB';
n@1154 2814 audioEngineContext.outputGain.gain.value = interfaceContext.volume.valueLin;
n@1154 2815 }
n@1154 2816 this.slider.onmouseup = function(event)
n@1154 2817 {
n@1154 2818 var storePoint = testState.currentStore.XMLDOM.children[0].getAllElementsByName('volumeTracker');
n@1154 2819 if (storePoint.length == 0)
n@1154 2820 {
n@1154 2821 storePoint = storage.document.createElement('metricresult');
n@1154 2822 storePoint.setAttribute('name','volumeTracker');
n@1154 2823 testState.currentStore.XMLDOM.children[0].appendChild(storePoint);
n@1154 2824 }
n@1154 2825 else {
n@1154 2826 storePoint = storePoint[0];
n@1154 2827 }
n@1154 2828 var node = storage.document.createElement('movement');
n@1154 2829 node.setAttribute('test-time',audioEngineContext.timer.getTestTime());
n@1154 2830 node.setAttribute('volume',interfaceContext.volume.valueDB);
n@1154 2831 node.setAttribute('format','dBFS');
n@1154 2832 storePoint.appendChild(node);
n@1154 2833 }
n@1154 2834
n@1155 2835 var title = document.createElement('div');
n@1155 2836 title.innerHTML = '<span>Master Volume Control</span>';
n@1155 2837 title.style.fontSize = '0.75em';
n@1155 2838 title.style.width = "100%";
n@1155 2839 title.align = 'center';
n@1155 2840 this.object.appendChild(title);
n@1155 2841
n@1154 2842 this.object.appendChild(this.slider);
n@1154 2843 this.object.appendChild(this.valueText);
n@1154 2844 }
n@1118 2845 // Global Checkers
n@1118 2846 // These functions will help enforce the checkers
n@1118 2847 this.checkHiddenAnchor = function()
n@1118 2848 {
n@1124 2849 for (var ao of audioEngineContext.audioObjects)
n@1118 2850 {
n@1124 2851 if (ao.specification.type == "anchor")
n@1118 2852 {
n@1125 2853 if (ao.interfaceDOM.getValue() > (ao.specification.marker/100) && ao.specification.marker > 0) {
n@1124 2854 // Anchor is not set below
n@1124 2855 console.log('Anchor node not below marker value');
n@1124 2856 alert('Please keep listening');
n@1167 2857 this.storeErrorNode('Anchor node not below marker value');
n@1124 2858 return false;
n@1124 2859 }
n@1118 2860 }
n@1118 2861 }
n@1118 2862 return true;
n@1118 2863 };
n@1118 2864
n@1118 2865 this.checkHiddenReference = function()
n@1118 2866 {
n@1124 2867 for (var ao of audioEngineContext.audioObjects)
n@1118 2868 {
n@1124 2869 if (ao.specification.type == "reference")
n@1118 2870 {
n@1125 2871 if (ao.interfaceDOM.getValue() < (ao.specification.marker/100) && ao.specification.marker > 0) {
n@1124 2872 // Anchor is not set below
n@1167 2873 console.log('Reference node not above marker value');
n@1167 2874 this.storeErrorNode('Reference node not above marker value');
n@1124 2875 alert('Please keep listening');
n@1124 2876 return false;
n@1124 2877 }
n@1118 2878 }
n@1118 2879 }
n@1118 2880 return true;
n@1118 2881 };
n@1118 2882
n@1118 2883 this.checkFragmentsFullyPlayed = function ()
n@1118 2884 {
n@1118 2885 // Checks the entire file has been played back
n@1118 2886 // NOTE ! This will return true IF playback is Looped!!!
n@1118 2887 if (audioEngineContext.loopPlayback)
n@1118 2888 {
n@1118 2889 console.log("WARNING - Looped source: Cannot check fragments are fully played");
n@1118 2890 return true;
n@1118 2891 }
n@1118 2892 var check_pass = true;
n@1118 2893 var error_obj = [];
n@1118 2894 for (var i = 0; i<audioEngineContext.audioObjects.length; i++)
n@1118 2895 {
n@1118 2896 var object = audioEngineContext.audioObjects[i];
n@1118 2897 var time = object.buffer.buffer.duration;
n@1118 2898 var metric = object.metric;
n@1118 2899 var passed = false;
n@1118 2900 for (var j=0; j<metric.listenTracker.length; j++)
n@1118 2901 {
n@1118 2902 var bt = metric.listenTracker[j].getElementsByTagName('buffertime');
n@1118 2903 var start_time = Number(bt[0].getAttribute('start'));
n@1118 2904 var stop_time = Number(bt[0].getAttribute('stop'));
n@1118 2905 var delta = stop_time - start_time;
n@1118 2906 if (delta >= time)
n@1118 2907 {
n@1118 2908 passed = true;
n@1118 2909 break;
n@1118 2910 }
n@1118 2911 }
n@1118 2912 if (passed == false)
n@1118 2913 {
n@1118 2914 check_pass = false;
n@1140 2915 console.log("Continue listening to track-"+audioEngineContext.audioObjects.interfaceDOM.getPresentedId());
n@1140 2916 error_obj.push(audioEngineContext.audioObjects.interfaceDOM.getPresentedId());
n@1118 2917 }
n@1118 2918 }
n@1118 2919 if (check_pass == false)
n@1118 2920 {
n@1118 2921 var str_start = "You have not completely listened to fragments ";
n@1118 2922 for (var i=0; i<error_obj.length; i++)
n@1118 2923 {
n@1118 2924 str_start += error_obj[i];
n@1118 2925 if (i != error_obj.length-1)
n@1118 2926 {
n@1118 2927 str_start += ', ';
n@1118 2928 }
n@1118 2929 }
n@1118 2930 str_start += ". Please keep listening";
n@1118 2931 console.log("[ALERT]: "+str_start);
n@1167 2932 this.storeErrorNode("[ALERT]: "+str_start);
n@1118 2933 alert(str_start);
n@1118 2934 }
n@1118 2935 };
n@1118 2936 this.checkAllMoved = function()
n@1118 2937 {
n@1118 2938 var str = "You have not moved ";
n@1118 2939 var failed = [];
n@1140 2940 for (var ao of audioEngineContext.audioObjects)
n@1118 2941 {
n@1140 2942 if(ao.metric.wasMoved == false && ao.interfaceDOM.canMove() == true)
n@1118 2943 {
n@1140 2944 failed.push(ao.interfaceDOM.getPresentedId());
n@1118 2945 }
n@1118 2946 }
n@1118 2947 if (failed.length == 0)
n@1118 2948 {
n@1118 2949 return true;
n@1118 2950 } else if (failed.length == 1)
n@1118 2951 {
n@1118 2952 str += 'track '+failed[0];
n@1118 2953 } else {
n@1118 2954 str += 'tracks ';
n@1118 2955 for (var i=0; i<failed.length-1; i++)
n@1118 2956 {
n@1118 2957 str += failed[i]+', ';
n@1118 2958 }
n@1118 2959 str += 'and '+failed[i];
n@1118 2960 }
n@1118 2961 str +='.';
n@1118 2962 alert(str);
n@1118 2963 console.log(str);
n@1167 2964 this.storeErrorNode(str);
n@1118 2965 return false;
n@1118 2966 };
n@1118 2967 this.checkAllPlayed = function()
n@1118 2968 {
n@1118 2969 var str = "You have not played ";
n@1118 2970 var failed = [];
n@1140 2971 for (var ao of audioEngineContext.audioObjects)
n@1118 2972 {
n@1140 2973 if(ao.metric.wasListenedTo == false)
n@1118 2974 {
n@1140 2975 failed.push(ao.interfaceDOM.getPresentedId());
n@1118 2976 }
n@1118 2977 }
n@1118 2978 if (failed.length == 0)
n@1118 2979 {
n@1118 2980 return true;
n@1118 2981 } else if (failed.length == 1)
n@1118 2982 {
n@1118 2983 str += 'track '+failed[0];
n@1118 2984 } else {
n@1118 2985 str += 'tracks ';
n@1118 2986 for (var i=0; i<failed.length-1; i++)
n@1118 2987 {
n@1118 2988 str += failed[i]+', ';
n@1118 2989 }
n@1118 2990 str += 'and '+failed[i];
n@1118 2991 }
n@1118 2992 str +='.';
n@1118 2993 alert(str);
n@1118 2994 console.log(str);
n@1167 2995 this.storeErrorNode(str);
n@1118 2996 return false;
n@1118 2997 };
n@1167 2998
n@1167 2999 this.storeErrorNode = function(errorMessage)
n@1167 3000 {
n@1167 3001 var time = audioEngineContext.timer.getTestTime();
n@1167 3002 var node = storage.document.createElement('error');
n@1167 3003 node.setAttribute('time',time);
n@1167 3004 node.textContent = errorMessage;
n@1167 3005 testState.currentStore.XMLDOM.appendChild(node);
n@1167 3006 };
n@1124 3007 }
n@1124 3008
n@1124 3009 function Storage()
n@1124 3010 {
n@1124 3011 // Holds results in XML format until ready for collection
n@1124 3012 this.globalPreTest = null;
n@1124 3013 this.globalPostTest = null;
n@1124 3014 this.testPages = [];
n@1124 3015 this.document = document.implementation.createDocument(null,"waetresult");
n@1124 3016 this.root = this.document.children[0];
n@1124 3017 this.state = 0;
n@1124 3018
n@1124 3019 this.initialise = function()
n@1124 3020 {
n@1142 3021 if (specification.preTest != undefined){this.globalPreTest = new this.surveyNode(this,this.root,specification.preTest);}
n@1142 3022 if (specification.postTest != undefined){this.globalPostTest = new this.surveyNode(this,this.root,specification.postTest);}
n@1124 3023 };
n@1124 3024
n@1124 3025 this.createTestPageStore = function(specification)
n@1124 3026 {
n@1124 3027 var store = new this.pageNode(this,specification);
n@1124 3028 this.testPages.push(store);
n@1124 3029 return this.testPages[this.testPages.length-1];
n@1124 3030 };
n@1124 3031
n@1124 3032 this.surveyNode = function(parent,root,specification)
n@1124 3033 {
n@1124 3034 this.specification = specification;
n@1124 3035 this.parent = parent;
n@1124 3036 this.XMLDOM = this.parent.document.createElement('survey');
n@1124 3037 this.XMLDOM.setAttribute('location',this.specification.location);
n@1124 3038 for (var optNode of this.specification.options)
n@1124 3039 {
n@1124 3040 if (optNode.type != 'statement')
n@1124 3041 {
n@1124 3042 var node = this.parent.document.createElement('surveyresult');
n@1124 3043 node.id = optNode.id;
n@1124 3044 node.setAttribute('type',optNode.type);
n@1124 3045 this.XMLDOM.appendChild(node);
n@1124 3046 }
n@1124 3047 }
n@1124 3048 root.appendChild(this.XMLDOM);
n@1124 3049
n@1124 3050 this.postResult = function(node)
n@1124 3051 {
n@1124 3052 // From popup: node is the popupOption node containing both spec. and results
n@1124 3053 // ID is the position
n@1124 3054 if (node.specification.type == 'statement'){return;}
n@1124 3055 var surveyresult = this.parent.document.getElementById(node.specification.id);
n@1124 3056 switch(node.specification.type)
n@1124 3057 {
n@1124 3058 case "number":
n@1124 3059 case "question":
n@1124 3060 var child = this.parent.document.createElement('response');
n@1124 3061 child.textContent = node.response;
n@1124 3062 surveyresult.appendChild(child);
n@1124 3063 break;
n@1124 3064 case "radio":
n@1124 3065 var child = this.parent.document.createElement('response');
n@1124 3066 child.setAttribute('name',node.response.name);
n@1124 3067 child.textContent = node.response.text;
n@1124 3068 surveyresult.appendChild(child);
n@1124 3069 break;
n@1124 3070 case "checkbox":
n@1124 3071 for (var i=0; i<node.response.length; i++)
n@1124 3072 {
n@1124 3073 var checkNode = this.parent.document.createElement('response');
n@1147 3074 checkNode.setAttribute('name',node.response[i].name);
n@1147 3075 checkNode.setAttribute('checked',node.response[i].checked);
n@1126 3076 surveyresult.appendChild(checkNode);
n@1124 3077 }
n@1124 3078 break;
n@1124 3079 }
n@1124 3080 };
n@1124 3081 };
n@1124 3082
n@1124 3083 this.pageNode = function(parent,specification)
n@1124 3084 {
n@1124 3085 // Create one store per test page
n@1124 3086 this.specification = specification;
n@1124 3087 this.parent = parent;
n@1124 3088 this.XMLDOM = this.parent.document.createElement('page');
n@1124 3089 this.XMLDOM.setAttribute('id',specification.id);
n@1124 3090 this.XMLDOM.setAttribute('presentedId',specification.presentedId);
n@1145 3091 if (specification.preTest != undefined){this.preTest = new this.parent.surveyNode(this.parent,this.XMLDOM,this.specification.preTest);}
n@1145 3092 if (specification.postTest != undefined){this.postTest = new this.parent.surveyNode(this.parent,this.XMLDOM,this.specification.postTest);}
n@1124 3093
n@1124 3094 // Add any page metrics
n@1124 3095 var page_metric = this.parent.document.createElement('metric');
n@1124 3096 this.XMLDOM.appendChild(page_metric);
n@1124 3097
n@1124 3098 // Add the audioelement
n@1124 3099 for (var element of this.specification.audioElements)
n@1124 3100 {
n@1124 3101 var aeNode = this.parent.document.createElement('audioelement');
n@1124 3102 aeNode.id = element.id;
n@1124 3103 aeNode.setAttribute('type',element.type);
n@1124 3104 aeNode.setAttribute('url', element.url);
n@1124 3105 aeNode.setAttribute('gain', element.gain);
n@1124 3106 if (element.type == 'anchor' || element.type == 'reference')
n@1124 3107 {
n@1124 3108 if (element.marker > 0)
n@1124 3109 {
n@1124 3110 aeNode.setAttribute('marker',element.marker);
n@1124 3111 }
n@1124 3112 }
n@1124 3113 var ae_metric = this.parent.document.createElement('metric');
n@1124 3114 aeNode.appendChild(ae_metric);
n@1124 3115 this.XMLDOM.appendChild(aeNode);
n@1124 3116 }
n@1124 3117
n@1124 3118 // Add any commentQuestions
n@1124 3119 for (var element of this.specification.commentQuestions)
n@1124 3120 {
n@1124 3121 var cqNode = this.parent.document.createElement('commentquestion');
n@1124 3122 cqNode.id = element.id;
n@1124 3123 cqNode.setAttribute('type',element.type);
n@1124 3124 var statement = this.parent.document.createElement('statement');
n@1124 3125 statement.textContent = cqNode.statement;
n@1124 3126 cqNode.appendChild(statement);
n@1124 3127 var response = this.parent.document.createElement('response');
n@1124 3128 cqNode.appendChild(response);
n@1124 3129 this.XMLDOM.appendChild(cqNode);
n@1124 3130 }
n@1124 3131
n@1124 3132 this.parent.root.appendChild(this.XMLDOM);
n@1124 3133 };
n@1124 3134 this.finish = function()
n@1124 3135 {
n@1124 3136 if (this.state == 0)
n@1124 3137 {
n@1124 3138 var projectDocument = specification.projectXML;
n@1124 3139 projectDocument.setAttribute('file-name',url);
n@1124 3140 this.root.appendChild(projectDocument);
n@1124 3141 this.root.appendChild(returnDateNode());
n@1124 3142 this.root.appendChild(interfaceContext.returnNavigator());
n@1124 3143 }
n@1124 3144 this.state = 1;
n@1124 3145 return this.root;
n@1124 3146 };
n@1124 3147 }