annotate core.js @ 806:e7ea0686b094

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