annotate core.js @ 858:30d5aa52b034

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