annotate core.js @ 888:a17a380e2469

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