annotate core.js @ 859:2d99d8888b0a

Anchor and Reference audioElements now tagged on output
author Nicholas Jillings <nicholas.jillings@eecs.qmul.ac.uk>
date Wed, 22 Jul 2015 13:22:10 +0100
parents 30d5aa52b034
children 3ce1cd41644d
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@859 939 if(this.specification.type == 'anchor') {
nicholas@859 940 root.setAttribute('anchor',true);
nicholas@859 941 } else if(this.specification.type == 'reference') {
nicholas@859 942 root.setAttribute('reference',true);
nicholas@859 943 }
nicholas@858 944 }
nicholas@858 945 root.appendChild(this.metric.exportXMLDOM());
nicholas@858 946 return root;
nicholas@858 947 };
nicholas@858 948 }
nicholas@858 949
nicholas@858 950 function timer()
nicholas@858 951 {
nicholas@858 952 /* Timer object used in audioEngine to keep track of session timings
nicholas@858 953 * Uses the timer of the web audio API, so sample resolution
nicholas@858 954 */
nicholas@858 955 this.testStarted = false;
nicholas@858 956 this.testStartTime = 0;
nicholas@858 957 this.testDuration = 0;
nicholas@858 958 this.minimumTestTime = 0; // No minimum test time
nicholas@858 959 this.startTest = function()
nicholas@858 960 {
nicholas@858 961 if (this.testStarted == false)
nicholas@858 962 {
nicholas@858 963 this.testStartTime = audioContext.currentTime;
nicholas@858 964 this.testStarted = true;
nicholas@858 965 this.updateTestTime();
nicholas@858 966 audioEngineContext.metric.initialiseTest();
nicholas@858 967 }
nicholas@858 968 };
nicholas@858 969 this.stopTest = function()
nicholas@858 970 {
nicholas@858 971 if (this.testStarted)
nicholas@858 972 {
nicholas@858 973 this.testDuration = this.getTestTime();
nicholas@858 974 this.testStarted = false;
nicholas@858 975 } else {
nicholas@858 976 console.log('ERR: Test tried to end before beginning');
nicholas@858 977 }
nicholas@858 978 };
nicholas@858 979 this.updateTestTime = function()
nicholas@858 980 {
nicholas@858 981 if (this.testStarted)
nicholas@858 982 {
nicholas@858 983 this.testDuration = audioContext.currentTime - this.testStartTime;
nicholas@858 984 }
nicholas@858 985 };
nicholas@858 986 this.getTestTime = function()
nicholas@858 987 {
nicholas@858 988 this.updateTestTime();
nicholas@858 989 return this.testDuration;
nicholas@858 990 };
nicholas@858 991 }
nicholas@858 992
nicholas@858 993 function sessionMetrics(engine)
nicholas@858 994 {
nicholas@858 995 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
nicholas@858 996 */
nicholas@858 997 this.engine = engine;
nicholas@858 998 this.lastClicked = -1;
nicholas@858 999 this.data = -1;
nicholas@858 1000 this.reset = function() {
nicholas@858 1001 this.lastClicked = -1;
nicholas@858 1002 this.data = -1;
nicholas@858 1003 };
nicholas@858 1004 this.initialiseTest = function(){};
nicholas@858 1005 }
nicholas@858 1006
nicholas@858 1007 function metricTracker(caller)
nicholas@858 1008 {
nicholas@858 1009 /* Custom object to track and collect metric data
nicholas@858 1010 * Used only inside the audioObjects object.
nicholas@858 1011 */
nicholas@858 1012
nicholas@858 1013 this.listenedTimer = 0;
nicholas@858 1014 this.listenStart = 0;
nicholas@858 1015 this.listenHold = false;
nicholas@858 1016 this.initialPosition = -1;
nicholas@858 1017 this.movementTracker = [];
nicholas@858 1018 this.listenTracker =[];
nicholas@858 1019 this.wasListenedTo = false;
nicholas@858 1020 this.wasMoved = false;
nicholas@858 1021 this.hasComments = false;
nicholas@858 1022 this.parent = caller;
nicholas@858 1023
nicholas@858 1024 this.initialised = function(position)
nicholas@858 1025 {
nicholas@858 1026 if (this.initialPosition == -1) {
nicholas@858 1027 this.initialPosition = position;
nicholas@858 1028 }
nicholas@858 1029 };
nicholas@858 1030
nicholas@858 1031 this.moved = function(time,position)
nicholas@858 1032 {
nicholas@858 1033 this.wasMoved = true;
nicholas@858 1034 this.movementTracker[this.movementTracker.length] = [time, position];
nicholas@858 1035 };
nicholas@858 1036
nicholas@858 1037 this.startListening = function(time)
nicholas@858 1038 {
nicholas@858 1039 if (this.listenHold == false)
nicholas@858 1040 {
nicholas@858 1041 this.wasListenedTo = true;
nicholas@858 1042 this.listenStart = time;
nicholas@858 1043 this.listenHold = true;
nicholas@858 1044
nicholas@858 1045 var evnt = document.createElement('event');
nicholas@858 1046 var testTime = document.createElement('testTime');
nicholas@858 1047 testTime.setAttribute('start',time);
nicholas@858 1048 var bufferTime = document.createElement('bufferTime');
nicholas@858 1049 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
nicholas@858 1050 evnt.appendChild(testTime);
nicholas@858 1051 evnt.appendChild(bufferTime);
nicholas@858 1052 this.listenTracker.push(evnt);
nicholas@858 1053
nicholas@858 1054 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
nicholas@858 1055 }
nicholas@858 1056 };
nicholas@858 1057
nicholas@858 1058 this.stopListening = function(time,bufferStopTime)
nicholas@858 1059 {
nicholas@858 1060 if (this.listenHold == true)
nicholas@858 1061 {
nicholas@858 1062 var diff = time - this.listenStart;
nicholas@858 1063 this.listenedTimer += (diff);
nicholas@858 1064 this.listenStart = 0;
nicholas@858 1065 this.listenHold = false;
nicholas@858 1066
nicholas@858 1067 var evnt = this.listenTracker[this.listenTracker.length-1];
nicholas@858 1068 var testTime = evnt.getElementsByTagName('testTime')[0];
nicholas@858 1069 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
nicholas@858 1070 testTime.setAttribute('stop',time);
nicholas@858 1071 if (bufferStopTime == undefined) {
nicholas@858 1072 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
nicholas@858 1073 } else {
nicholas@858 1074 bufferTime.setAttribute('stop',bufferStopTime);
nicholas@858 1075 }
nicholas@858 1076 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
nicholas@858 1077 }
nicholas@858 1078 };
nicholas@858 1079
nicholas@858 1080 this.exportXMLDOM = function() {
nicholas@858 1081 var root = document.createElement('metric');
nicholas@858 1082 if (audioEngineContext.metric.enableElementTimer) {
nicholas@858 1083 var mElementTimer = document.createElement('metricresult');
nicholas@858 1084 mElementTimer.setAttribute('name','enableElementTimer');
nicholas@858 1085 mElementTimer.textContent = this.listenedTimer;
nicholas@858 1086 root.appendChild(mElementTimer);
nicholas@858 1087 }
nicholas@858 1088 if (audioEngineContext.metric.enableElementTracker) {
nicholas@858 1089 var elementTrackerFull = document.createElement('metricResult');
nicholas@858 1090 elementTrackerFull.setAttribute('name','elementTrackerFull');
nicholas@858 1091 for (var k=0; k<this.movementTracker.length; k++)
nicholas@858 1092 {
nicholas@858 1093 var timePos = document.createElement('timePos');
nicholas@858 1094 timePos.id = k;
nicholas@858 1095 var time = document.createElement('time');
nicholas@858 1096 time.textContent = this.movementTracker[k][0];
nicholas@858 1097 var position = document.createElement('position');
nicholas@858 1098 position.textContent = this.movementTracker[k][1];
nicholas@858 1099 timePos.appendChild(time);
nicholas@858 1100 timePos.appendChild(position);
nicholas@858 1101 elementTrackerFull.appendChild(timePos);
nicholas@858 1102 }
nicholas@858 1103 root.appendChild(elementTrackerFull);
nicholas@858 1104 }
nicholas@858 1105 if (audioEngineContext.metric.enableElementListenTracker) {
nicholas@858 1106 var elementListenTracker = document.createElement('metricResult');
nicholas@858 1107 elementListenTracker.setAttribute('name','elementListenTracker');
nicholas@858 1108 for (var k=0; k<this.listenTracker.length; k++) {
nicholas@858 1109 elementListenTracker.appendChild(this.listenTracker[k]);
nicholas@858 1110 }
nicholas@858 1111 root.appendChild(elementListenTracker);
nicholas@858 1112 }
nicholas@858 1113 if (audioEngineContext.metric.enableElementInitialPosition) {
nicholas@858 1114 var elementInitial = document.createElement('metricResult');
nicholas@858 1115 elementInitial.setAttribute('name','elementInitialPosition');
nicholas@858 1116 elementInitial.textContent = this.initialPosition;
nicholas@858 1117 root.appendChild(elementInitial);
nicholas@858 1118 }
nicholas@858 1119 if (audioEngineContext.metric.enableFlagListenedTo) {
nicholas@858 1120 var flagListenedTo = document.createElement('metricResult');
nicholas@858 1121 flagListenedTo.setAttribute('name','elementFlagListenedTo');
nicholas@858 1122 flagListenedTo.textContent = this.wasListenedTo;
nicholas@858 1123 root.appendChild(flagListenedTo);
nicholas@858 1124 }
nicholas@858 1125 if (audioEngineContext.metric.enableFlagMoved) {
nicholas@858 1126 var flagMoved = document.createElement('metricResult');
nicholas@858 1127 flagMoved.setAttribute('name','elementFlagMoved');
nicholas@858 1128 flagMoved.textContent = this.wasMoved;
nicholas@858 1129 root.appendChild(flagMoved);
nicholas@858 1130 }
nicholas@858 1131 if (audioEngineContext.metric.enableFlagComments) {
nicholas@858 1132 var flagComments = document.createElement('metricResult');
nicholas@858 1133 flagComments.setAttribute('name','elementFlagComments');
nicholas@858 1134 if (this.parent.commentDOM == null)
nicholas@858 1135 {flag.textContent = 'false';}
nicholas@858 1136 else if (this.parent.commentDOM.textContent.length == 0)
nicholas@858 1137 {flag.textContent = 'false';}
nicholas@858 1138 else
nicholas@858 1139 {flag.textContet = 'true';}
nicholas@858 1140 root.appendChild(flagComments);
nicholas@858 1141 }
nicholas@858 1142
nicholas@858 1143 return root;
nicholas@858 1144 };
nicholas@858 1145 }
nicholas@858 1146
nicholas@858 1147 function randomiseOrder(input)
nicholas@858 1148 {
nicholas@858 1149 // This takes an array of information and randomises the order
nicholas@858 1150 var N = input.length;
nicholas@858 1151
nicholas@858 1152 var inputSequence = []; // For safety purposes: keep track of randomisation
nicholas@858 1153 for (var counter = 0; counter < N; ++counter)
nicholas@858 1154 inputSequence.push(counter) // Fill array
nicholas@858 1155 var inputSequenceClone = inputSequence.slice(0);
nicholas@858 1156
nicholas@858 1157 var holdArr = [];
nicholas@858 1158 var outputSequence = [];
nicholas@858 1159 for (var n=0; n<N; n++)
nicholas@858 1160 {
nicholas@858 1161 // First pick a random number
nicholas@858 1162 var r = Math.random();
nicholas@858 1163 // Multiply and floor by the number of elements left
nicholas@858 1164 r = Math.floor(r*input.length);
nicholas@858 1165 // Pick out that element and delete from the array
nicholas@858 1166 holdArr.push(input.splice(r,1)[0]);
nicholas@858 1167 // Do the same with sequence
nicholas@858 1168 outputSequence.push(inputSequence.splice(r,1)[0]);
nicholas@858 1169 }
nicholas@858 1170 console.log(inputSequenceClone.toString()); // print original array to console
nicholas@858 1171 console.log(outputSequence.toString()); // print randomised array to console
nicholas@858 1172 return holdArr;
nicholas@858 1173 }
nicholas@858 1174
nicholas@858 1175 function returnDateNode()
nicholas@858 1176 {
nicholas@858 1177 // Create an XML Node for the Date and Time a test was conducted
nicholas@858 1178 // Structure is
nicholas@858 1179 // <datetime>
nicholas@858 1180 // <date year="##" month="##" day="##">DD/MM/YY</date>
nicholas@858 1181 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
nicholas@858 1182 // </datetime>
nicholas@858 1183 var dateTime = new Date();
nicholas@858 1184 var year = document.createAttribute('year');
nicholas@858 1185 var month = document.createAttribute('month');
nicholas@858 1186 var day = document.createAttribute('day');
nicholas@858 1187 var hour = document.createAttribute('hour');
nicholas@858 1188 var minute = document.createAttribute('minute');
nicholas@858 1189 var secs = document.createAttribute('secs');
nicholas@858 1190
nicholas@858 1191 year.nodeValue = dateTime.getFullYear();
nicholas@858 1192 month.nodeValue = dateTime.getMonth()+1;
nicholas@858 1193 day.nodeValue = dateTime.getDate();
nicholas@858 1194 hour.nodeValue = dateTime.getHours();
nicholas@858 1195 minute.nodeValue = dateTime.getMinutes();
nicholas@858 1196 secs.nodeValue = dateTime.getSeconds();
nicholas@858 1197
nicholas@858 1198 var hold = document.createElement("datetime");
nicholas@858 1199 var date = document.createElement("date");
nicholas@858 1200 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
nicholas@858 1201 var time = document.createElement("time");
nicholas@858 1202 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
nicholas@858 1203
nicholas@858 1204 date.setAttributeNode(year);
nicholas@858 1205 date.setAttributeNode(month);
nicholas@858 1206 date.setAttributeNode(day);
nicholas@858 1207 time.setAttributeNode(hour);
nicholas@858 1208 time.setAttributeNode(minute);
nicholas@858 1209 time.setAttributeNode(secs);
nicholas@858 1210
nicholas@858 1211 hold.appendChild(date);
nicholas@858 1212 hold.appendChild(time);
nicholas@858 1213 return hold
nicholas@858 1214
nicholas@858 1215 }
nicholas@858 1216
nicholas@858 1217 function testWaitIndicator() {
nicholas@858 1218 if (audioEngineContext.checkAllReady() == false) {
nicholas@858 1219 var hold = document.createElement("div");
nicholas@858 1220 hold.id = "testWaitIndicator";
nicholas@858 1221 hold.className = "indicator-box";
nicholas@858 1222 hold.style.zIndex = 3;
nicholas@858 1223 var span = document.createElement("span");
nicholas@858 1224 span.textContent = "Please wait! Elements still loading";
nicholas@858 1225 hold.appendChild(span);
nicholas@858 1226 var blank = document.createElement('div');
nicholas@858 1227 blank.className = 'testHalt';
nicholas@858 1228 blank.id = "testHaltBlank";
nicholas@858 1229 var body = document.getElementsByTagName('body')[0];
nicholas@858 1230 body.appendChild(hold);
nicholas@858 1231 body.appendChild(blank);
nicholas@858 1232 testWaitTimerIntervalHolder = setInterval(function(){
nicholas@858 1233 var ready = audioEngineContext.checkAllReady();
nicholas@858 1234 if (ready) {
nicholas@858 1235 var elem = document.getElementById('testWaitIndicator');
nicholas@858 1236 var blank = document.getElementById('testHaltBlank');
nicholas@858 1237 var body = document.getElementsByTagName('body')[0];
nicholas@858 1238 body.removeChild(elem);
nicholas@858 1239 body.removeChild(blank);
nicholas@858 1240 clearInterval(testWaitTimerIntervalHolder);
nicholas@858 1241 }
nicholas@858 1242 },500,false);
nicholas@858 1243 }
nicholas@858 1244 }
nicholas@858 1245
nicholas@858 1246 var testWaitTimerIntervalHolder = null;
nicholas@858 1247
nicholas@858 1248 function Specification() {
nicholas@858 1249 // Handles the decoding of the project specification XML into a simple JavaScript Object.
nicholas@858 1250
nicholas@858 1251 this.interfaceType;
nicholas@858 1252 this.commonInterface;
nicholas@858 1253 this.projectReturn;
nicholas@858 1254 this.randomiseOrder;
nicholas@858 1255 this.collectMetrics;
nicholas@858 1256 this.preTest;
nicholas@858 1257 this.postTest;
nicholas@858 1258 this.metrics =[];
nicholas@858 1259
nicholas@858 1260 this.audioHolders = [];
nicholas@858 1261
nicholas@858 1262 this.decode = function() {
nicholas@858 1263 // projectXML - DOM Parsed document
nicholas@858 1264 this.projectXML = projectXML.childNodes[0];
nicholas@858 1265 var setupNode = projectXML.getElementsByTagName('setup')[0];
nicholas@858 1266 this.interfaceType = setupNode.getAttribute('interface');
nicholas@858 1267 this.projectReturn = setupNode.getAttribute('projectReturn');
nicholas@858 1268 if (setupNode.getAttribute('randomiseOrder') == "true") {
nicholas@858 1269 this.randomiseOrder = true;
nicholas@858 1270 } else {this.randomiseOrder = false;}
nicholas@858 1271 if (setupNode.getAttribute('collectMetrics') == "true") {
nicholas@858 1272 this.collectMetrics = true;
nicholas@858 1273 } else {this.collectMetrics = false;}
nicholas@858 1274 var metricCollection = setupNode.getElementsByTagName('Metric');
nicholas@858 1275
nicholas@858 1276 this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest'));
nicholas@858 1277 this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest'));
nicholas@858 1278
nicholas@858 1279 if (metricCollection.length > 0) {
nicholas@858 1280 metricCollection = metricCollection[0].getElementsByTagName('metricEnable');
nicholas@858 1281 for (var i=0; i<metricCollection.length; i++) {
nicholas@858 1282 this.metrics.push(new this.metricNode(metricCollection[i].textContent));
nicholas@858 1283 }
nicholas@858 1284 }
nicholas@858 1285
nicholas@858 1286 var commonInterfaceNode = setupNode.getElementsByTagName('interface');
nicholas@858 1287 if (commonInterfaceNode.length > 0) {
nicholas@858 1288 commonInterfaceNode = commonInterfaceNode[0];
nicholas@858 1289 } else {
nicholas@858 1290 commonInterfaceNode = undefined;
nicholas@858 1291 }
nicholas@858 1292
nicholas@858 1293 this.commonInterface = new function() {
nicholas@858 1294 this.OptionNode = function(child) {
nicholas@858 1295 this.type = child.nodeName;
nicholas@858 1296 if (this.type == 'option')
nicholas@858 1297 {
nicholas@858 1298 this.name = child.getAttribute('name');
nicholas@858 1299 }
nicholas@858 1300 else if (this.type == 'check') {
nicholas@858 1301 this.check = child.getAttribute('name');
nicholas@858 1302 if (this.check == 'scalerange') {
nicholas@858 1303 this.min = child.getAttribute('min');
nicholas@858 1304 this.max = child.getAttribute('max');
nicholas@858 1305 if (this.min == null) {this.min = 1;}
nicholas@858 1306 else if (Number(this.min) > 1 && this.min != null) {
nicholas@858 1307 this.min = Number(this.min)/100;
nicholas@858 1308 } else {
nicholas@858 1309 this.min = Number(this.min);
nicholas@858 1310 }
nicholas@858 1311 if (this.max == null) {this.max = 0;}
nicholas@858 1312 else if (Number(this.max) > 1 && this.max != null) {
nicholas@858 1313 this.max = Number(this.max)/100;
nicholas@858 1314 } else {
nicholas@858 1315 this.max = Number(this.max);
nicholas@858 1316 }
nicholas@858 1317 }
nicholas@858 1318 } else if (this.type == 'anchor' || this.type == 'reference') {
nicholas@858 1319 this.value = Number(child.textContent);
nicholas@858 1320 }
nicholas@858 1321 };
nicholas@858 1322 this.options = [];
nicholas@858 1323 if (commonInterfaceNode != undefined) {
nicholas@858 1324 var child = commonInterfaceNode.firstElementChild;
nicholas@858 1325 while (child != undefined) {
nicholas@858 1326 this.options.push(new this.OptionNode(child));
nicholas@858 1327 child = child.nextElementSibling;
nicholas@858 1328 }
nicholas@858 1329 }
nicholas@858 1330 };
nicholas@858 1331
nicholas@858 1332 var audioHolders = projectXML.getElementsByTagName('audioHolder');
nicholas@858 1333 for (var i=0; i<audioHolders.length; i++) {
nicholas@858 1334 this.audioHolders.push(new this.audioHolderNode(this,audioHolders[i]));
nicholas@858 1335 }
nicholas@858 1336
nicholas@858 1337 };
nicholas@858 1338
nicholas@858 1339 this.prepostNode = function(type,Collection) {
nicholas@858 1340 this.type = type;
nicholas@858 1341 this.options = [];
nicholas@858 1342
nicholas@858 1343 this.OptionNode = function(child) {
nicholas@858 1344
nicholas@858 1345 this.childOption = function(element) {
nicholas@858 1346 this.type = 'option';
nicholas@858 1347 this.id = element.id;
nicholas@858 1348 this.name = element.getAttribute('name');
nicholas@858 1349 this.text = element.textContent;
nicholas@858 1350 };
nicholas@858 1351
nicholas@858 1352 this.type = child.nodeName;
nicholas@858 1353 if (child.nodeName == "question") {
nicholas@858 1354 this.id = child.id;
nicholas@858 1355 this.mandatory;
nicholas@858 1356 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;}
nicholas@858 1357 else {this.mandatory = false;}
nicholas@858 1358 this.question = child.textContent;
nicholas@858 1359 if (child.getAttribute('boxsize') == null) {
nicholas@858 1360 this.boxsize = 'normal';
nicholas@858 1361 } else {
nicholas@858 1362 this.boxsize = child.getAttribute('boxsize');
nicholas@858 1363 }
nicholas@858 1364 } else if (child.nodeName == "statement") {
nicholas@858 1365 this.statement = child.textContent;
nicholas@858 1366 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") {
nicholas@858 1367 var element = child.firstElementChild;
nicholas@858 1368 this.id = child.id;
nicholas@858 1369 if (element == null) {
nicholas@858 1370 console.log('Malformed' +child.nodeName+ 'entry');
nicholas@858 1371 this.statement = 'Malformed' +child.nodeName+ 'entry';
nicholas@858 1372 this.type = 'statement';
nicholas@858 1373 } else {
nicholas@858 1374 this.options = [];
nicholas@858 1375 while (element != null) {
nicholas@858 1376 if (element.nodeName == 'statement' && this.statement == undefined){
nicholas@858 1377 this.statement = element.textContent;
nicholas@858 1378 } else if (element.nodeName == 'option') {
nicholas@858 1379 this.options.push(new this.childOption(element));
nicholas@858 1380 }
nicholas@858 1381 element = element.nextElementSibling;
nicholas@858 1382 }
nicholas@858 1383 }
nicholas@858 1384 } else if (child.nodeName == "number") {
nicholas@858 1385 this.statement = child.textContent;
nicholas@858 1386 this.id = child.id;
nicholas@858 1387 this.min = child.getAttribute('min');
nicholas@858 1388 this.max = child.getAttribute('max');
nicholas@858 1389 this.step = child.getAttribute('step');
nicholas@858 1390 }
nicholas@858 1391 };
nicholas@858 1392
nicholas@858 1393 // On construction:
nicholas@858 1394 if (Collection.length != 0) {
nicholas@858 1395 Collection = Collection[0];
nicholas@858 1396 if (Collection.childElementCount != 0) {
nicholas@858 1397 var child = Collection.firstElementChild;
nicholas@858 1398 this.options.push(new this.OptionNode(child));
nicholas@858 1399 while (child.nextElementSibling != null) {
nicholas@858 1400 child = child.nextElementSibling;
nicholas@858 1401 this.options.push(new this.OptionNode(child));
nicholas@858 1402 }
nicholas@858 1403 }
nicholas@858 1404 }
nicholas@858 1405 };
nicholas@858 1406
nicholas@858 1407 this.metricNode = function(name) {
nicholas@858 1408 this.enabled = name;
nicholas@858 1409 };
nicholas@858 1410
nicholas@858 1411 this.audioHolderNode = function(parent,xml) {
nicholas@858 1412 this.type = 'audioHolder';
nicholas@858 1413 this.interfaceNode = function(DOM) {
nicholas@858 1414 var title = DOM.getElementsByTagName('title');
nicholas@858 1415 if (title.length == 0) {this.title = null;}
nicholas@858 1416 else {this.title = title[0].textContent;}
nicholas@858 1417 this.options = parent.commonInterface.options;
nicholas@858 1418 var scale = DOM.getElementsByTagName('scale');
nicholas@858 1419 this.scale = [];
nicholas@858 1420 for (var i=0; i<scale.length; i++) {
nicholas@858 1421 var arr = [null, null];
nicholas@858 1422 arr[0] = scale[i].getAttribute('position');
nicholas@858 1423 arr[1] = scale[i].textContent;
nicholas@858 1424 this.scale.push(arr);
nicholas@858 1425 }
nicholas@858 1426 };
nicholas@858 1427
nicholas@858 1428 this.audioElementNode = function(parent,xml) {
nicholas@858 1429 this.url = xml.getAttribute('url');
nicholas@858 1430 this.id = xml.id;
nicholas@858 1431 this.parent = parent;
nicholas@858 1432 this.type = xml.getAttribute('type');
nicholas@858 1433 if (this.type == null) {this.type = "normal";}
nicholas@858 1434 if (this.type == 'anchor') {this.anchor = true;}
nicholas@858 1435 else {this.anchor = false;}
nicholas@858 1436 if (this.type == 'reference') {this.reference = true;}
nicholas@858 1437 else {this.reference = false;}
nicholas@858 1438
nicholas@858 1439 this.marker = xml.getAttribute('marker');
nicholas@858 1440 if (this.marker == null) {this.marker = undefined;}
nicholas@858 1441
nicholas@858 1442 if (this.anchor == true && this.marker == undefined) {
nicholas@858 1443 this.marker = anchor;
nicholas@858 1444 }
nicholas@858 1445 else if (this.reference == true && this.marker == undefined) {
nicholas@858 1446 this.marker = reference;
nicholas@858 1447 }
nicholas@858 1448
nicholas@858 1449 if (this.marker != undefined) {
nicholas@858 1450 this.marker = Number(this.marker);
nicholas@858 1451 if (this.marker > 1) {this.marker /= 100;}
nicholas@858 1452 }
nicholas@858 1453 };
nicholas@858 1454
nicholas@858 1455 this.commentQuestionNode = function(xml) {
nicholas@858 1456 this.childOption = function(element) {
nicholas@858 1457 this.type = 'option';
nicholas@858 1458 this.name = element.getAttribute('name');
nicholas@858 1459 this.text = element.textContent;
nicholas@858 1460 };
nicholas@858 1461 this.id = xml.id;
nicholas@858 1462 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;}
nicholas@858 1463 else {this.mandatory = false;}
nicholas@858 1464 this.type = xml.getAttribute('type');
nicholas@858 1465 if (this.type == undefined) {this.type = 'text';}
nicholas@858 1466 switch (this.type) {
nicholas@858 1467 case 'text':
nicholas@858 1468 this.question = xml.textContent;
nicholas@858 1469 break;
nicholas@858 1470 case 'radio':
nicholas@858 1471 var child = xml.firstElementChild;
nicholas@858 1472 this.options = [];
nicholas@858 1473 while (child != undefined) {
nicholas@858 1474 if (child.nodeName == 'statement' && this.statement == undefined) {
nicholas@858 1475 this.statement = child.textContent;
nicholas@858 1476 } else if (child.nodeName == 'option') {
nicholas@858 1477 this.options.push(new this.childOption(child));
nicholas@858 1478 }
nicholas@858 1479 child = child.nextElementSibling;
nicholas@858 1480 }
nicholas@858 1481 break;
nicholas@858 1482 case 'checkbox':
nicholas@858 1483 var child = xml.firstElementChild;
nicholas@858 1484 this.options = [];
nicholas@858 1485 while (child != undefined) {
nicholas@858 1486 if (child.nodeName == 'statement' && this.statement == undefined) {
nicholas@858 1487 this.statement = child.textContent;
nicholas@858 1488 } else if (child.nodeName == 'option') {
nicholas@858 1489 this.options.push(new this.childOption(child));
nicholas@858 1490 }
nicholas@858 1491 child = child.nextElementSibling;
nicholas@858 1492 }
nicholas@858 1493 break;
nicholas@858 1494 }
nicholas@858 1495 };
nicholas@858 1496
nicholas@858 1497 this.id = xml.id;
nicholas@858 1498 this.hostURL = xml.getAttribute('hostURL');
nicholas@858 1499 this.sampleRate = xml.getAttribute('sampleRate');
nicholas@858 1500 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;}
nicholas@858 1501 else {this.randomiseOrder = false;}
nicholas@858 1502 this.repeatCount = xml.getAttribute('repeatCount');
nicholas@858 1503 if (xml.getAttribute('loop') == 'true') {this.loop = true;}
nicholas@858 1504 else {this.loop == false;}
nicholas@858 1505 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;}
nicholas@858 1506 else {this.elementComments = false;}
nicholas@858 1507
nicholas@858 1508 var anchor = xml.getElementsByTagName('anchor');
nicholas@858 1509 if (anchor.length == 0) {
nicholas@858 1510 // Find anchor in commonInterface;
nicholas@858 1511 for (var i=0; i<parent.commonInterface.options.length; i++) {
nicholas@858 1512 if(parent.commonInterface.options[i].type == 'anchor') {
nicholas@858 1513 anchor = parent.commonInterface.options[i].value;
nicholas@858 1514 break;
nicholas@858 1515 }
nicholas@858 1516 }
nicholas@858 1517 if (typeof(anchor) == "object") {
nicholas@858 1518 anchor = null;
nicholas@858 1519 }
nicholas@858 1520 } else {
nicholas@858 1521 anchor = anchor[0].textContent;
nicholas@858 1522 }
nicholas@858 1523
nicholas@858 1524 var reference = xml.getElementsByTagName('anchor');
nicholas@858 1525 if (reference.length == 0) {
nicholas@858 1526 // Find anchor in commonInterface;
nicholas@858 1527 for (var i=0; i<parent.commonInterface.options.length; i++) {
nicholas@858 1528 if(parent.commonInterface.options[i].type == 'reference') {
nicholas@858 1529 reference = parent.commonInterface.options[i].value;
nicholas@858 1530 break;
nicholas@858 1531 }
nicholas@858 1532 }
nicholas@858 1533 if (typeof(reference) == "object") {
nicholas@858 1534 reference = null;
nicholas@858 1535 }
nicholas@858 1536 } else {
nicholas@858 1537 reference = reference[0].textContent;
nicholas@858 1538 }
nicholas@858 1539
nicholas@858 1540 if (typeof(anchor) == 'number') {
nicholas@858 1541 if (anchor > 1 && anchor < 100) {anchor /= 100.0;}
nicholas@858 1542 }
nicholas@858 1543
nicholas@858 1544 if (typeof(reference) == 'number') {
nicholas@858 1545 if (reference > 1 && reference < 100) {reference /= 100.0;}
nicholas@858 1546 }
nicholas@858 1547
nicholas@858 1548 this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest'));
nicholas@858 1549 this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest'));
nicholas@858 1550
nicholas@858 1551 this.interfaces = [];
nicholas@858 1552 var interfaceDOM = xml.getElementsByTagName('interface');
nicholas@858 1553 for (var i=0; i<interfaceDOM.length; i++) {
nicholas@858 1554 this.interfaces.push(new this.interfaceNode(interfaceDOM[i]));
nicholas@858 1555 }
nicholas@858 1556
nicholas@858 1557 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix');
nicholas@858 1558 if (this.commentBoxPrefix.length != 0) {
nicholas@858 1559 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent;
nicholas@858 1560 } else {
nicholas@858 1561 this.commentBoxPrefix = "Comment on track";
nicholas@858 1562 }
nicholas@858 1563
nicholas@858 1564 this.audioElements =[];
nicholas@858 1565 var audioElementsDOM = xml.getElementsByTagName('audioElements');
nicholas@858 1566 this.outsideReference = null;
nicholas@858 1567 for (var i=0; i<audioElementsDOM.length; i++) {
nicholas@858 1568 if (audioElementsDOM[i].getAttribute('type') == 'outsidereference') {
nicholas@858 1569 if (this.outsideReference == null) {
nicholas@858 1570 this.outsideReference = new this.audioElementNode(this,audioElementsDOM[i]);
nicholas@858 1571 } else {
nicholas@858 1572 console.log('Error only one audioelement can be of type outsidereference per audioholder');
nicholas@858 1573 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
nicholas@858 1574 console.log('Element id '+audioElementsDOM[i].id+' made into normal node');
nicholas@858 1575 }
nicholas@858 1576 } else {
nicholas@858 1577 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
nicholas@858 1578 }
nicholas@858 1579 }
nicholas@858 1580
nicholas@858 1581 if (this.randomiseOrder) {
nicholas@858 1582 this.audioElements = randomiseOrder(this.audioElements);
nicholas@858 1583 }
nicholas@858 1584
nicholas@858 1585 // Check only one anchor and one reference per audioNode
nicholas@858 1586 var anchor = [];
nicholas@858 1587 var reference = [];
nicholas@858 1588 this.anchorId = null;
nicholas@858 1589 this.referenceId = null;
nicholas@858 1590 for (var i=0; i<this.audioElements.length; i++)
nicholas@858 1591 {
nicholas@858 1592 if (this.audioElements[i].anchor == true) {anchor.push(i);}
nicholas@858 1593 if (this.audioElements[i].reference == true) {reference.push(i);}
nicholas@858 1594 }
nicholas@858 1595
nicholas@858 1596 if (anchor.length > 1) {
nicholas@858 1597 console.log('Error - cannot have more than one anchor!');
nicholas@858 1598 console.log('Each anchor node will be a normal mode to continue the test');
nicholas@858 1599 for (var i=0; i<anchor.length; i++)
nicholas@858 1600 {
nicholas@858 1601 this.audioElements[anchor[i]].anchor = false;
nicholas@858 1602 this.audioElements[anchor[i]].value = undefined;
nicholas@858 1603 }
nicholas@858 1604 } else {this.anchorId = anchor[0];}
nicholas@858 1605 if (reference.length > 1) {
nicholas@858 1606 console.log('Error - cannot have more than one anchor!');
nicholas@858 1607 console.log('Each anchor node will be a normal mode to continue the test');
nicholas@858 1608 for (var i=0; i<reference.length; i++)
nicholas@858 1609 {
nicholas@858 1610 this.audioElements[reference[i]].reference = false;
nicholas@858 1611 this.audioElements[reference[i]].value = undefined;
nicholas@858 1612 }
nicholas@858 1613 } else {this.referenceId = reference[0];}
nicholas@858 1614
nicholas@858 1615 this.commentQuestions = [];
nicholas@858 1616 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion');
nicholas@858 1617 for (var i=0; i<commentQuestionsDOM.length; i++) {
nicholas@858 1618 this.commentQuestions.push(new this.commentQuestionNode(commentQuestionsDOM[i]));
nicholas@858 1619 }
nicholas@858 1620 };
nicholas@858 1621 }
nicholas@858 1622
nicholas@858 1623 function Interface(specificationObject) {
nicholas@858 1624 // This handles the bindings between the interface and the audioEngineContext;
nicholas@858 1625 this.specification = specificationObject;
nicholas@858 1626 this.insertPoint = document.getElementById("topLevelBody");
nicholas@858 1627
nicholas@858 1628 // Bounded by interface!!
nicholas@858 1629 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
nicholas@858 1630 // For example, APE returns the slider position normalised in a <value> tag.
nicholas@858 1631 this.interfaceObjects = [];
nicholas@858 1632 this.interfaceObject = function(){};
nicholas@858 1633
nicholas@858 1634 this.commentBoxes = [];
nicholas@858 1635 this.elementCommentBox = function(audioObject) {
nicholas@858 1636 var element = audioObject.specification;
nicholas@858 1637 this.audioObject = audioObject;
nicholas@858 1638 this.id = audioObject.id;
nicholas@858 1639 var audioHolderObject = audioObject.specification.parent;
nicholas@858 1640 // Create document objects to hold the comment boxes
nicholas@858 1641 this.trackComment = document.createElement('div');
nicholas@858 1642 this.trackComment.className = 'comment-div';
nicholas@858 1643 this.trackComment.id = 'comment-div-'+audioObject.id;
nicholas@858 1644 // Create a string next to each comment asking for a comment
nicholas@858 1645 this.trackString = document.createElement('span');
nicholas@858 1646 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id;
nicholas@858 1647 // Create the HTML5 comment box 'textarea'
nicholas@858 1648 this.trackCommentBox = document.createElement('textarea');
nicholas@858 1649 this.trackCommentBox.rows = '4';
nicholas@858 1650 this.trackCommentBox.cols = '100';
nicholas@858 1651 this.trackCommentBox.name = 'trackComment'+audioObject.id;
nicholas@858 1652 this.trackCommentBox.className = 'trackComment';
nicholas@858 1653 var br = document.createElement('br');
nicholas@858 1654 // Add to the holder.
nicholas@858 1655 this.trackComment.appendChild(this.trackString);
nicholas@858 1656 this.trackComment.appendChild(br);
nicholas@858 1657 this.trackComment.appendChild(this.trackCommentBox);
nicholas@858 1658
nicholas@858 1659 this.exportXMLDOM = function() {
nicholas@858 1660 var root = document.createElement('comment');
nicholas@858 1661 if (this.audioObject.specification.parent.elementComments) {
nicholas@858 1662 var question = document.createElement('question');
nicholas@858 1663 question.textContent = this.trackString.textContent;
nicholas@858 1664 var response = document.createElement('response');
nicholas@858 1665 response.textContent = this.trackCommentBox.value;
nicholas@858 1666 console.log("Comment frag-"+this.id+": "+response.textContent);
nicholas@858 1667 root.appendChild(question);
nicholas@858 1668 root.appendChild(response);
nicholas@858 1669 }
nicholas@858 1670 return root;
nicholas@858 1671 };
nicholas@858 1672 };
nicholas@858 1673
nicholas@858 1674 this.commentQuestions = [];
nicholas@858 1675
nicholas@858 1676 this.commentBox = function(commentQuestion) {
nicholas@858 1677 this.specification = commentQuestion;
nicholas@858 1678 // Create document objects to hold the comment boxes
nicholas@858 1679 this.holder = document.createElement('div');
nicholas@858 1680 this.holder.className = 'comment-div';
nicholas@858 1681 // Create a string next to each comment asking for a comment
nicholas@858 1682 this.string = document.createElement('span');
nicholas@858 1683 this.string.innerHTML = commentQuestion.question;
nicholas@858 1684 // Create the HTML5 comment box 'textarea'
nicholas@858 1685 this.textArea = document.createElement('textarea');
nicholas@858 1686 this.textArea.rows = '4';
nicholas@858 1687 this.textArea.cols = '100';
nicholas@858 1688 this.textArea.className = 'trackComment';
nicholas@858 1689 var br = document.createElement('br');
nicholas@858 1690 // Add to the holder.
nicholas@858 1691 this.holder.appendChild(this.string);
nicholas@858 1692 this.holder.appendChild(br);
nicholas@858 1693 this.holder.appendChild(this.textArea);
nicholas@858 1694
nicholas@858 1695 this.exportXMLDOM = function() {
nicholas@858 1696 var root = document.createElement('comment');
nicholas@858 1697 root.id = this.specification.id;
nicholas@858 1698 root.setAttribute('type',this.specification.type);
nicholas@858 1699 root.textContent = this.textArea.value;
nicholas@858 1700 console.log("Question: "+this.string.textContent);
nicholas@858 1701 console.log("Response: "+root.textContent);
nicholas@858 1702 return root;
nicholas@858 1703 };
nicholas@858 1704 };
nicholas@858 1705
nicholas@858 1706 this.radioBox = function(commentQuestion) {
nicholas@858 1707 this.specification = commentQuestion;
nicholas@858 1708 // Create document objects to hold the comment boxes
nicholas@858 1709 this.holder = document.createElement('div');
nicholas@858 1710 this.holder.className = 'comment-div';
nicholas@858 1711 // Create a string next to each comment asking for a comment
nicholas@858 1712 this.string = document.createElement('span');
nicholas@858 1713 this.string.innerHTML = commentQuestion.statement;
nicholas@858 1714 var br = document.createElement('br');
nicholas@858 1715 // Add to the holder.
nicholas@858 1716 this.holder.appendChild(this.string);
nicholas@858 1717 this.holder.appendChild(br);
nicholas@858 1718 this.options = [];
nicholas@858 1719 this.inputs = document.createElement('div');
nicholas@858 1720 this.span = document.createElement('div');
nicholas@858 1721 this.inputs.align = 'center';
nicholas@858 1722 this.inputs.style.marginLeft = '12px';
nicholas@858 1723 this.span.style.marginLeft = '12px';
nicholas@858 1724 this.span.align = 'center';
nicholas@858 1725 this.span.style.marginTop = '15px';
nicholas@858 1726
nicholas@858 1727 var optCount = commentQuestion.options.length;
nicholas@858 1728 var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px';
nicholas@858 1729 console.log(spanMargin);
nicholas@858 1730 for (var i=0; i<optCount; i++)
nicholas@858 1731 {
nicholas@858 1732 var div = document.createElement('div');
nicholas@858 1733 div.style.width = '100px';
nicholas@858 1734 div.style.float = 'left';
nicholas@858 1735 div.style.marginRight = spanMargin;
nicholas@858 1736 div.style.marginLeft = spanMargin;
nicholas@858 1737 var input = document.createElement('input');
nicholas@858 1738 input.type = 'radio';
nicholas@858 1739 input.name = commentQuestion.id;
nicholas@858 1740 input.setAttribute('setvalue',commentQuestion.options[i].name);
nicholas@858 1741 input.className = 'comment-radio';
nicholas@858 1742 div.appendChild(input);
nicholas@858 1743 this.inputs.appendChild(div);
nicholas@858 1744
nicholas@858 1745
nicholas@858 1746 div = document.createElement('div');
nicholas@858 1747 div.style.width = '100px';
nicholas@858 1748 div.style.float = 'left';
nicholas@858 1749 div.style.marginRight = spanMargin;
nicholas@858 1750 div.style.marginLeft = spanMargin;
nicholas@858 1751 div.align = 'center';
nicholas@858 1752 var span = document.createElement('span');
nicholas@858 1753 span.textContent = commentQuestion.options[i].text;
nicholas@858 1754 span.className = 'comment-radio-span';
nicholas@858 1755 div.appendChild(span);
nicholas@858 1756 this.span.appendChild(div);
nicholas@858 1757 this.options.push(input);
nicholas@858 1758 }
nicholas@858 1759 this.holder.appendChild(this.span);
nicholas@858 1760 this.holder.appendChild(this.inputs);
nicholas@858 1761
nicholas@858 1762 this.exportXMLDOM = function() {
nicholas@858 1763 var root = document.createElement('comment');
nicholas@858 1764 root.id = this.specification.id;
nicholas@858 1765 root.setAttribute('type',this.specification.type);
nicholas@858 1766 var question = document.createElement('question');
nicholas@858 1767 question.textContent = this.string.textContent;
nicholas@858 1768 var response = document.createElement('response');
nicholas@858 1769 var i=0;
nicholas@858 1770 while(this.options[i].checked == false) {
nicholas@858 1771 i++;
nicholas@858 1772 if (i >= this.options.length) {
nicholas@858 1773 break;
nicholas@858 1774 }
nicholas@858 1775 }
nicholas@858 1776 if (i >= this.options.length) {
nicholas@858 1777 response.textContent = 'null';
nicholas@858 1778 } else {
nicholas@858 1779 response.textContent = this.options[i].getAttribute('setvalue');
nicholas@858 1780 response.setAttribute('number',i);
nicholas@858 1781 }
nicholas@858 1782 console.log('Comment: '+question.textContent);
nicholas@858 1783 console.log('Response: '+response.textContent);
nicholas@858 1784 root.appendChild(question);
nicholas@858 1785 root.appendChild(response);
nicholas@858 1786 return root;
nicholas@858 1787 };
nicholas@858 1788 };
nicholas@858 1789
nicholas@858 1790 this.checkboxBox = function(commentQuestion) {
nicholas@858 1791 this.specification = commentQuestion;
nicholas@858 1792 // Create document objects to hold the comment boxes
nicholas@858 1793 this.holder = document.createElement('div');
nicholas@858 1794 this.holder.className = 'comment-div';
nicholas@858 1795 // Create a string next to each comment asking for a comment
nicholas@858 1796 this.string = document.createElement('span');
nicholas@858 1797 this.string.innerHTML = commentQuestion.statement;
nicholas@858 1798 var br = document.createElement('br');
nicholas@858 1799 // Add to the holder.
nicholas@858 1800 this.holder.appendChild(this.string);
nicholas@858 1801 this.holder.appendChild(br);
nicholas@858 1802 this.options = [];
nicholas@858 1803 this.inputs = document.createElement('div');
nicholas@858 1804 this.span = document.createElement('div');
nicholas@858 1805 this.inputs.align = 'center';
nicholas@858 1806 this.inputs.style.marginLeft = '12px';
nicholas@858 1807 this.span.style.marginLeft = '12px';
nicholas@858 1808 this.span.align = 'center';
nicholas@858 1809 this.span.style.marginTop = '15px';
nicholas@858 1810
nicholas@858 1811 var optCount = commentQuestion.options.length;
nicholas@858 1812 var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px';
nicholas@858 1813 console.log(spanMargin);
nicholas@858 1814 for (var i=0; i<optCount; i++)
nicholas@858 1815 {
nicholas@858 1816 var div = document.createElement('div');
nicholas@858 1817 div.style.width = '100px';
nicholas@858 1818 div.style.float = 'left';
nicholas@858 1819 div.style.marginRight = spanMargin;
nicholas@858 1820 div.style.marginLeft = spanMargin;
nicholas@858 1821 var input = document.createElement('input');
nicholas@858 1822 input.type = 'checkbox';
nicholas@858 1823 input.name = commentQuestion.id;
nicholas@858 1824 input.setAttribute('setvalue',commentQuestion.options[i].name);
nicholas@858 1825 input.className = 'comment-radio';
nicholas@858 1826 div.appendChild(input);
nicholas@858 1827 this.inputs.appendChild(div);
nicholas@858 1828
nicholas@858 1829
nicholas@858 1830 div = document.createElement('div');
nicholas@858 1831 div.style.width = '100px';
nicholas@858 1832 div.style.float = 'left';
nicholas@858 1833 div.style.marginRight = spanMargin;
nicholas@858 1834 div.style.marginLeft = spanMargin;
nicholas@858 1835 div.align = 'center';
nicholas@858 1836 var span = document.createElement('span');
nicholas@858 1837 span.textContent = commentQuestion.options[i].text;
nicholas@858 1838 span.className = 'comment-radio-span';
nicholas@858 1839 div.appendChild(span);
nicholas@858 1840 this.span.appendChild(div);
nicholas@858 1841 this.options.push(input);
nicholas@858 1842 }
nicholas@858 1843 this.holder.appendChild(this.span);
nicholas@858 1844 this.holder.appendChild(this.inputs);
nicholas@858 1845
nicholas@858 1846 this.exportXMLDOM = function() {
nicholas@858 1847 var root = document.createElement('comment');
nicholas@858 1848 root.id = this.specification.id;
nicholas@858 1849 root.setAttribute('type',this.specification.type);
nicholas@858 1850 var question = document.createElement('question');
nicholas@858 1851 question.textContent = this.string.textContent;
nicholas@858 1852 root.appendChild(question);
nicholas@858 1853 console.log('Comment: '+question.textContent);
nicholas@858 1854 for (var i=0; i<this.options.length; i++) {
nicholas@858 1855 var response = document.createElement('response');
nicholas@858 1856 response.textContent = this.options[i].checked;
nicholas@858 1857 response.setAttribute('name',this.options[i].getAttribute('setvalue'));
nicholas@858 1858 root.appendChild(response);
nicholas@858 1859 console.log('Response '+response.getAttribute('name') +': '+response.textContent);
nicholas@858 1860 }
nicholas@858 1861 return root;
nicholas@858 1862 };
nicholas@858 1863 };
nicholas@858 1864
nicholas@858 1865 this.createCommentBox = function(audioObject) {
nicholas@858 1866 var node = new this.elementCommentBox(audioObject);
nicholas@858 1867 this.commentBoxes.push(node);
nicholas@858 1868 audioObject.commentDOM = node;
nicholas@858 1869 return node;
nicholas@858 1870 };
nicholas@858 1871
nicholas@858 1872 this.sortCommentBoxes = function() {
nicholas@858 1873 var holder = [];
nicholas@858 1874 while (this.commentBoxes.length > 0) {
nicholas@858 1875 var node = this.commentBoxes.pop(0);
nicholas@858 1876 holder[node.id] = node;
nicholas@858 1877 }
nicholas@858 1878 this.commentBoxes = holder;
nicholas@858 1879 };
nicholas@858 1880
nicholas@858 1881 this.showCommentBoxes = function(inject, sort) {
nicholas@858 1882 if (sort) {interfaceContext.sortCommentBoxes();}
nicholas@858 1883 for (var i=0; i<interfaceContext.commentBoxes.length; i++) {
nicholas@858 1884 inject.appendChild(this.commentBoxes[i].trackComment);
nicholas@858 1885 }
nicholas@858 1886 };
nicholas@858 1887
nicholas@858 1888 this.deleteCommentBoxes = function() {
nicholas@858 1889 this.commentBoxes = [];
nicholas@858 1890 };
nicholas@858 1891
nicholas@858 1892 this.createCommentQuestion = function(element) {
nicholas@858 1893 var node;
nicholas@858 1894 if (element.type == 'text') {
nicholas@858 1895 node = new this.commentBox(element);
nicholas@858 1896 } else if (element.type == 'radio') {
nicholas@858 1897 node = new this.radioBox(element);
nicholas@858 1898 } else if (element.type == 'checkbox') {
nicholas@858 1899 node = new this.checkboxBox(element);
nicholas@858 1900 }
nicholas@858 1901 this.commentQuestions.push(node);
nicholas@858 1902 return node;
nicholas@858 1903 };
nicholas@858 1904
nicholas@858 1905 this.deleteCommentQuestions = function()
nicholas@858 1906 {
nicholas@858 1907 this.commentQuestions = [];
nicholas@858 1908 };
nicholas@858 1909
nicholas@858 1910 this.playhead = new function()
nicholas@858 1911 {
nicholas@858 1912 this.object = document.createElement('div');
nicholas@858 1913 this.object.className = 'playhead';
nicholas@858 1914 this.object.align = 'left';
nicholas@858 1915 var curTime = document.createElement('div');
nicholas@858 1916 curTime.style.width = '50px';
nicholas@858 1917 this.curTimeSpan = document.createElement('span');
nicholas@858 1918 this.curTimeSpan.textContent = '00:00';
nicholas@858 1919 curTime.appendChild(this.curTimeSpan);
nicholas@858 1920 this.object.appendChild(curTime);
nicholas@858 1921 this.scrubberTrack = document.createElement('div');
nicholas@858 1922 this.scrubberTrack.className = 'playhead-scrub-track';
nicholas@858 1923
nicholas@858 1924 this.scrubberHead = document.createElement('div');
nicholas@858 1925 this.scrubberHead.id = 'playhead-scrubber';
nicholas@858 1926 this.scrubberTrack.appendChild(this.scrubberHead);
nicholas@858 1927 this.object.appendChild(this.scrubberTrack);
nicholas@858 1928
nicholas@858 1929 this.timePerPixel = 0;
nicholas@858 1930 this.maxTime = 0;
nicholas@858 1931
nicholas@858 1932 this.playbackObject;
nicholas@858 1933
nicholas@858 1934 this.setTimePerPixel = function(audioObject) {
nicholas@858 1935 //maxTime must be in seconds
nicholas@858 1936 this.playbackObject = audioObject;
nicholas@858 1937 this.maxTime = audioObject.buffer.duration;
nicholas@858 1938 var width = 490; //500 - 10, 5 each side of the tracker head
nicholas@858 1939 this.timePerPixel = this.maxTime/490;
nicholas@858 1940 if (this.maxTime < 60) {
nicholas@858 1941 this.curTimeSpan.textContent = '0.00';
nicholas@858 1942 } else {
nicholas@858 1943 this.curTimeSpan.textContent = '00:00';
nicholas@858 1944 }
nicholas@858 1945 };
nicholas@858 1946
nicholas@858 1947 this.update = function() {
nicholas@858 1948 // Update the playhead position, startPlay must be called
nicholas@858 1949 if (this.timePerPixel > 0) {
nicholas@858 1950 var time = this.playbackObject.getCurrentPosition();
nicholas@858 1951 var width = 490;
nicholas@858 1952 var pix = Math.floor(time/this.timePerPixel);
nicholas@858 1953 this.scrubberHead.style.left = pix+'px';
nicholas@858 1954 if (this.maxTime > 60.0) {
nicholas@858 1955 var secs = time%60;
nicholas@858 1956 var mins = Math.floor((time-secs)/60);
nicholas@858 1957 secs = secs.toString();
nicholas@858 1958 secs = secs.substr(0,2);
nicholas@858 1959 mins = mins.toString();
nicholas@858 1960 this.curTimeSpan.textContent = mins+':'+secs;
nicholas@858 1961 } else {
nicholas@858 1962 time = time.toString();
nicholas@858 1963 this.curTimeSpan.textContent = time.substr(0,4);
nicholas@858 1964 }
nicholas@858 1965 }
nicholas@858 1966 };
nicholas@858 1967
nicholas@858 1968 this.interval = undefined;
nicholas@858 1969
nicholas@858 1970 this.start = function() {
nicholas@858 1971 if (this.playbackObject != undefined && this.interval == undefined) {
nicholas@858 1972 this.interval = setInterval(function(){interfaceContext.playhead.update();},100);
nicholas@858 1973 }
nicholas@858 1974 };
nicholas@858 1975 this.stop = function() {
nicholas@858 1976 clearInterval(this.interval);
nicholas@858 1977 this.interval = undefined;
nicholas@858 1978 };
nicholas@858 1979 };
nicholas@858 1980
nicholas@858 1981 // Global Checkers
nicholas@858 1982 // These functions will help enforce the checkers
nicholas@858 1983 this.checkHiddenAnchor = function()
nicholas@858 1984 {
nicholas@858 1985 var audioHolder = testState.currentStateMap[testState.currentIndex];
nicholas@858 1986 if (audioHolder.anchorId != null)
nicholas@858 1987 {
nicholas@858 1988 var audioObject = audioEngineContext.audioObjects[audioHolder.anchorId];
nicholas@858 1989 if (audioObject.interfaceDOM.getValue() > audioObject.specification.marker)
nicholas@858 1990 {
nicholas@858 1991 // Anchor is not set below
nicholas@858 1992 console.log('Anchor node not below marker value');
nicholas@858 1993 alert('Please keep listening');
nicholas@858 1994 return false;
nicholas@858 1995 }
nicholas@858 1996 }
nicholas@858 1997 return true;
nicholas@858 1998 };
nicholas@858 1999
nicholas@858 2000 this.checkHiddenReference = function()
nicholas@858 2001 {
nicholas@858 2002 var audioHolder = testState.currentStateMap[testState.currentIndex];
nicholas@858 2003 if (audioHolder.referenceId != null)
nicholas@858 2004 {
nicholas@858 2005 var audioObject = audioEngineContext.audioObjects[audioHolder.referenceId];
nicholas@858 2006 if (audioObject.interfaceDOM.getValue() < audioObject.specification.marker)
nicholas@858 2007 {
nicholas@858 2008 // Anchor is not set below
nicholas@858 2009 console.log('Reference node not above marker value');
nicholas@858 2010 alert('Please keep listening');
nicholas@858 2011 return false;
nicholas@858 2012 }
nicholas@858 2013 }
nicholas@858 2014 return true;
nicholas@858 2015 };
nicholas@858 2016 }