annotate core.js @ 751:3b8069ea47d2

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