annotate core.js @ 833:39f982f9f9f1

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