annotate core.js @ 538:6d652a6c80ed Dev_main

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