annotate core.js @ 855:1dc56cb86152

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