annotate core.js @ 841:71fd099e9fd2

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