annotate core.js @ 871:7aa8862850ef

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