annotate core.js @ 1198:4560587673b4

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