annotate core.js @ 814:22ad83e232f7

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