annotate Perceptual Evaluation/webaudioevaluationtool/core.js @ 0:55c282f01a30 tip

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