annotate core.js @ 813:f452455b5977

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