annotate core.js @ 837:31d02b334ba8

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