annotate core.js @ 856:fb41d65cc89f

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