annotate core.js @ 865:d0870902d0a6

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