annotate core.js @ 1192:d5fd92bcaa6f

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