annotate core.js @ 869:02ca9ef612c3

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