annotate core.js @ 821:e266584705fc

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