annotate core.js @ 1079:24eeac8994bf

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