annotate core.js @ 265:ec693ceb2444 Dev_main

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