annotate core.js @ 767:d7f2912bf487

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