annotate core.js @ 1588:a64c6a1d869c

Added check box mode to pre/post test survey
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Fri, 05 Jun 2015 12:10:49 +0100
parents c6ef0ce95e4a
children dd36d98cd4f9
rev   line source
nickjillings@1575 1 /**
nickjillings@1575 2 * core.js
nickjillings@1575 3 *
nickjillings@1575 4 * Main script to run, calls all other core functions and manages loading/store to backend.
nickjillings@1575 5 * Also contains all global variables.
nickjillings@1575 6 */
nickjillings@1575 7
nickjillings@1575 8 /* create the web audio API context and store in audioContext*/
nickjillings@1575 9 var audioContext; // Hold the browser web audio API
nickjillings@1575 10 var projectXML; // Hold the parsed setup XML
nickjillings@1581 11 var specification;
nickjillings@1582 12 var interfaceContext;
nickjillings@1575 13 var popup; // Hold the interfacePopup object
nickjillings@1575 14 var testState;
nickjillings@1575 15 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
nickjillings@1575 16 var audioEngineContext; // The custome AudioEngine object
nickjillings@1575 17 var projectReturn; // Hold the URL for the return
nickjillings@1575 18
nickjillings@1575 19
nickjillings@1575 20 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
nickjillings@1575 21 AudioBufferSourceNode.prototype.owner = undefined;
nickjillings@1575 22
nickjillings@1575 23 window.onload = function() {
nickjillings@1575 24 // Function called once the browser has loaded all files.
nickjillings@1575 25 // This should perform any initial commands such as structure / loading documents
nickjillings@1575 26
nickjillings@1575 27 // Create a web audio API context
nickjillings@1575 28 // Fixed for cross-browser support
nickjillings@1575 29 var AudioContext = window.AudioContext || window.webkitAudioContext;
nickjillings@1575 30 audioContext = new AudioContext;
nickjillings@1575 31
nickjillings@1575 32 // Create test state
nickjillings@1575 33 testState = new stateMachine();
nickjillings@1575 34
nickjillings@1575 35 // Create the audio engine object
nickjillings@1575 36 audioEngineContext = new AudioEngine();
nickjillings@1575 37
nickjillings@1575 38 // Create the popup interface object
nickjillings@1575 39 popup = new interfacePopup();
nickjillings@1581 40
nickjillings@1581 41 // Create the specification object
nickjillings@1581 42 specification = new Specification();
nickjillings@1582 43
nickjillings@1582 44 // Create the interface object
nickjillings@1582 45 interfaceContext = new Interface(specification);
nickjillings@1575 46 };
nickjillings@1575 47
nickjillings@1575 48 function interfacePopup() {
nickjillings@1575 49 // Creates an object to manage the popup
nickjillings@1575 50 this.popup = null;
nickjillings@1575 51 this.popupContent = null;
nickjillings@1575 52 this.popupButton = null;
nickjillings@1575 53 this.popupOptions = null;
nickjillings@1575 54 this.currentIndex = null;
nickjillings@1575 55 this.responses = null;
nickjillings@1581 56
nickjillings@1575 57 this.createPopup = function(){
nickjillings@1575 58 // Create popup window interface
nickjillings@1575 59 var insertPoint = document.getElementById("topLevelBody");
nickjillings@1575 60 var blank = document.createElement('div');
nickjillings@1575 61 blank.className = 'testHalt';
nickjillings@1575 62
nickjillings@1575 63 this.popup = document.createElement('div');
nickjillings@1575 64 this.popup.id = 'popupHolder';
nickjillings@1575 65 this.popup.className = 'popupHolder';
nickjillings@1575 66 this.popup.style.position = 'absolute';
nickjillings@1575 67 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
nickjillings@1575 68 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
nickjillings@1575 69
nickjillings@1575 70 this.popupContent = document.createElement('div');
nickjillings@1575 71 this.popupContent.id = 'popupContent';
nickjillings@1575 72 this.popupContent.style.marginTop = '25px';
nickjillings@1575 73 this.popupContent.align = 'center';
nickjillings@1575 74 this.popup.appendChild(this.popupContent);
nickjillings@1575 75
nickjillings@1575 76 this.popupButton = document.createElement('button');
nickjillings@1575 77 this.popupButton.className = 'popupButton';
nickjillings@1575 78 this.popupButton.innerHTML = 'Next';
nickjillings@1575 79 this.popupButton.onclick = function(){popup.buttonClicked();};
nickjillings@1581 80 this.popup.style.zIndex = -1;
nickjillings@1581 81 this.popup.style.visibility = 'hidden';
nickjillings@1581 82 blank.style.zIndex = -2;
nickjillings@1581 83 blank.style.visibility = 'hidden';
nickjillings@1575 84 insertPoint.appendChild(this.popup);
nickjillings@1575 85 insertPoint.appendChild(blank);
nickjillings@1575 86 };
nickjillings@1575 87
nickjillings@1575 88 this.showPopup = function(){
nickjillings@1581 89 if (this.popup == null) {
nickjillings@1575 90 this.createPopup();
nickjillings@1575 91 }
nickjillings@1575 92 this.popup.style.zIndex = 3;
nickjillings@1575 93 this.popup.style.visibility = 'visible';
nickjillings@1575 94 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1575 95 blank.style.zIndex = 2;
nickjillings@1575 96 blank.style.visibility = 'visible';
nickjillings@1575 97 };
nickjillings@1575 98
nickjillings@1575 99 this.hidePopup = function(){
nickjillings@1575 100 this.popup.style.zIndex = -1;
nickjillings@1575 101 this.popup.style.visibility = 'hidden';
nickjillings@1575 102 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1575 103 blank.style.zIndex = -2;
nickjillings@1575 104 blank.style.visibility = 'hidden';
nickjillings@1575 105 };
nickjillings@1575 106
nickjillings@1575 107 this.postNode = function() {
nickjillings@1575 108 // This will take the node from the popupOptions and display it
nickjillings@1575 109 var node = this.popupOptions[this.currentIndex];
nickjillings@1575 110 this.popupContent.innerHTML = null;
nickjillings@1581 111 if (node.type == 'statement') {
nickjillings@1575 112 var span = document.createElement('span');
nickjillings@1581 113 span.textContent = node.statement;
nickjillings@1575 114 this.popupContent.appendChild(span);
nickjillings@1581 115 } else if (node.type == 'question') {
nickjillings@1575 116 var span = document.createElement('span');
nickjillings@1581 117 span.textContent = node.question;
nickjillings@1575 118 var textArea = document.createElement('textarea');
nickjillings@1575 119 var br = document.createElement('br');
nickjillings@1575 120 this.popupContent.appendChild(span);
nickjillings@1575 121 this.popupContent.appendChild(br);
nickjillings@1575 122 this.popupContent.appendChild(textArea);
nickjillings@1575 123 this.popupContent.childNodes[2].focus();
nickjillings@1588 124 } else if (node.type == 'checkbox') {
nickjillings@1588 125 var span = document.createElement('span');
nickjillings@1588 126 span.textContent = node.statement;
nickjillings@1588 127 this.popupContent.appendChild(span);
nickjillings@1588 128 var optHold = document.createElement('div');
nickjillings@1588 129 optHold.id = 'option-holder';
nickjillings@1588 130 optHold.align = 'left';
nickjillings@1588 131 for (var i=0; i<node.options.length; i++) {
nickjillings@1588 132 var option = node.options[i];
nickjillings@1588 133 var input = document.createElement('input');
nickjillings@1588 134 input.id = option.id;
nickjillings@1588 135 input.type = 'checkbox';
nickjillings@1588 136 var span = document.createElement('span');
nickjillings@1588 137 span.textContent = option.text;
nickjillings@1588 138 var hold = document.createElement('div');
nickjillings@1588 139 hold.setAttribute('name','option');
nickjillings@1588 140 hold.style.float = 'left';
nickjillings@1588 141 hold.style.padding = '4px';
nickjillings@1588 142 hold.appendChild(input);
nickjillings@1588 143 hold.appendChild(span);
nickjillings@1588 144 optHold.appendChild(hold);
nickjillings@1588 145 }
nickjillings@1588 146 this.popupContent.appendChild(optHold);
nickjillings@1575 147 }
nickjillings@1575 148 this.popupContent.appendChild(this.popupButton);
nickjillings@1575 149 };
nickjillings@1575 150
nickjillings@1575 151 this.initState = function(node) {
nickjillings@1575 152 //Call this with your preTest and postTest nodes when needed to
nickjillings@1575 153 // initialise the popup procedure.
nickjillings@1581 154 this.popupOptions = node.options;
nickjillings@1575 155 if (this.popupOptions.length > 0) {
nickjillings@1581 156 if (node.type == 'pretest') {
nickjillings@1575 157 this.responses = document.createElement('PreTest');
nickjillings@1581 158 } else if (node.type == 'posttest') {
nickjillings@1575 159 this.responses = document.createElement('PostTest');
nickjillings@1575 160 } else {
nickjillings@1575 161 console.log ('WARNING - popup node neither pre or post!');
nickjillings@1575 162 this.responses = document.createElement('responses');
nickjillings@1575 163 }
nickjillings@1575 164 this.currentIndex = 0;
nickjillings@1575 165 this.showPopup();
nickjillings@1575 166 this.postNode();
nickjillings@1581 167 } else {
nickjillings@1581 168 advanceState();
nickjillings@1575 169 }
nickjillings@1575 170 };
nickjillings@1575 171
nickjillings@1575 172 this.buttonClicked = function() {
nickjillings@1575 173 // Each time the popup button is clicked!
nickjillings@1575 174 var node = this.popupOptions[this.currentIndex];
nickjillings@1581 175 if (node.type == 'question') {
nickjillings@1575 176 // Must extract the question data
nickjillings@1575 177 var textArea = $(popup.popupContent).find('textarea')[0];
nickjillings@1581 178 if (node.mandatory == true && textArea.value.length == 0) {
nickjillings@1575 179 alert('This question is mandatory');
nickjillings@1575 180 return;
nickjillings@1575 181 } else {
nickjillings@1575 182 // Save the text content
nickjillings@1575 183 var hold = document.createElement('comment');
nickjillings@1581 184 hold.id = node.id;
nickjillings@1575 185 hold.innerHTML = textArea.value;
nickjillings@1575 186 console.log("Question: "+ node.textContent);
nickjillings@1575 187 console.log("Question Response: "+ textArea.value);
nickjillings@1575 188 this.responses.appendChild(hold);
nickjillings@1575 189 }
nickjillings@1588 190 } else if (node.type == 'checkbox') {
nickjillings@1588 191 // Must extract checkbox data
nickjillings@1588 192 var optHold = document.getElementById('option-holder');
nickjillings@1588 193 var hold = document.createElement('checkbox');
nickjillings@1588 194 console.log("Checkbox: "+ node.statement);
nickjillings@1588 195 for (var i=0; i<optHold.childElementCount; i++) {
nickjillings@1588 196 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
nickjillings@1588 197 var statement = optHold.childNodes[i].getElementsByTagName('span')[0];
nickjillings@1588 198 var response = document.createElement('option');
nickjillings@1588 199 response.setAttribute('id',input.id);
nickjillings@1588 200 response.setAttribute('checked',input.checked);
nickjillings@1588 201 hold.appendChild(response);
nickjillings@1588 202 console.log(input.id +': '+ input.checked);
nickjillings@1588 203 }
nickjillings@1588 204 this.responses.appendChild(hold);
nickjillings@1575 205 }
nickjillings@1575 206 this.currentIndex++;
nickjillings@1575 207 if (this.currentIndex < this.popupOptions.length) {
nickjillings@1575 208 this.postNode();
nickjillings@1575 209 } else {
nickjillings@1575 210 // Reached the end of the popupOptions
nickjillings@1575 211 this.hidePopup();
nickjillings@1575 212 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) {
nickjillings@1575 213 testState.stateResults[testState.stateIndex] = this.responses;
nickjillings@1575 214 } else {
nickjillings@1575 215 testState.stateResults[testState.stateIndex].appendChild(this.responses);
nickjillings@1575 216 }
nickjillings@1575 217 advanceState();
nickjillings@1575 218 }
nickjillings@1575 219 };
nickjillings@1575 220 }
nickjillings@1575 221
nickjillings@1575 222 function advanceState()
nickjillings@1575 223 {
nickjillings@1575 224 // Just for complete clarity
nickjillings@1575 225 testState.advanceState();
nickjillings@1575 226 }
nickjillings@1575 227
nickjillings@1575 228 function stateMachine()
nickjillings@1575 229 {
nickjillings@1575 230 // Object prototype for tracking and managing the test state
nickjillings@1575 231 this.stateMap = [];
nickjillings@1575 232 this.stateIndex = null;
nickjillings@1575 233 this.currentStateMap = [];
nickjillings@1575 234 this.currentIndex = null;
nickjillings@1575 235 this.currentTestId = 0;
nickjillings@1575 236 this.stateResults = [];
nickjillings@1575 237 this.timerCallBackHolders = null;
nickjillings@1575 238 this.initialise = function(){
nickjillings@1575 239 if (this.stateMap.length > 0) {
nickjillings@1575 240 if(this.stateIndex != null) {
nickjillings@1575 241 console.log('NOTE - State already initialise');
nickjillings@1575 242 }
nickjillings@1575 243 this.stateIndex = -1;
nickjillings@1575 244 var that = this;
nickjillings@1575 245 for (var id=0; id<this.stateMap.length; id++){
nickjillings@1581 246 var name = this.stateMap[id].type;
nickjillings@1575 247 var obj = document.createElement(name);
nickjillings@1587 248 if (name == 'audioHolder') {
nickjillings@1587 249 obj.id = this.stateMap[id].id;
nickjillings@1587 250 }
nickjillings@1575 251 this.stateResults.push(obj);
nickjillings@1575 252 }
nickjillings@1575 253 } else {
nickjillings@1575 254 conolse.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
nickjillings@1575 255 }
nickjillings@1575 256 };
nickjillings@1575 257 this.advanceState = function(){
nickjillings@1575 258 if (this.stateIndex == null) {
nickjillings@1575 259 this.initialise();
nickjillings@1575 260 }
nickjillings@1575 261 if (this.stateIndex == -1) {
nickjillings@1575 262 console.log('Starting test...');
nickjillings@1575 263 }
nickjillings@1575 264 if (this.currentIndex == null){
nickjillings@1581 265 if (this.currentStateMap.type == "audioHolder") {
nickjillings@1575 266 // Save current page
nickjillings@1575 267 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId);
nickjillings@1575 268 this.currentTestId++;
nickjillings@1575 269 }
nickjillings@1575 270 this.stateIndex++;
nickjillings@1575 271 if (this.stateIndex >= this.stateMap.length) {
nickjillings@1575 272 console.log('Test Completed');
nickjillings@1582 273 createProjectSave(specification.projectReturn);
nickjillings@1575 274 } else {
nickjillings@1575 275 this.currentStateMap = this.stateMap[this.stateIndex];
nickjillings@1581 276 if (this.currentStateMap.type == "audioHolder") {
nickjillings@1575 277 console.log('Loading test page');
nickjillings@1575 278 loadTest(this.currentStateMap);
nickjillings@1575 279 this.initialiseInnerState(this.currentStateMap);
nickjillings@1581 280 } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") {
nickjillings@1581 281 if (this.currentStateMap.options.length >= 1) {
nickjillings@1575 282 popup.initState(this.currentStateMap);
nickjillings@1575 283 } else {
nickjillings@1575 284 this.advanceState();
nickjillings@1575 285 }
nickjillings@1575 286 } else {
nickjillings@1575 287 this.advanceState();
nickjillings@1575 288 }
nickjillings@1575 289 }
nickjillings@1575 290 } else {
nickjillings@1575 291 this.advanceInnerState();
nickjillings@1575 292 }
nickjillings@1575 293 };
nickjillings@1575 294
nickjillings@1575 295 this.testPageCompleted = function(store, testXML, testId) {
nickjillings@1575 296 // Function called each time a test page has been completed
nickjillings@1575 297 // Can be used to over-rule default behaviour
nickjillings@1575 298
nickjillings@1581 299 pageXMLSave(store, testXML);
nickjillings@1576 300 };
nickjillings@1575 301
nickjillings@1581 302 this.initialiseInnerState = function(node) {
nickjillings@1575 303 // Parses the received testXML for pre and post test options
nickjillings@1575 304 this.currentStateMap = [];
nickjillings@1581 305 var preTest = node.preTest;
nickjillings@1581 306 var postTest = node.postTest;
nickjillings@1575 307 if (preTest == undefined) {preTest = document.createElement("preTest");}
nickjillings@1575 308 if (postTest == undefined){postTest= document.createElement("postTest");}
nickjillings@1575 309 this.currentStateMap.push(preTest);
nickjillings@1581 310 this.currentStateMap.push(node);
nickjillings@1575 311 this.currentStateMap.push(postTest);
nickjillings@1575 312 this.currentIndex = -1;
nickjillings@1575 313 this.advanceInnerState();
nickjillings@1576 314 };
nickjillings@1575 315
nickjillings@1575 316 this.advanceInnerState = function() {
nickjillings@1575 317 this.currentIndex++;
nickjillings@1575 318 if (this.currentIndex >= this.currentStateMap.length) {
nickjillings@1575 319 this.currentIndex = null;
nickjillings@1575 320 this.currentStateMap = this.stateMap[this.stateIndex];
nickjillings@1575 321 this.advanceState();
nickjillings@1575 322 } else {
nickjillings@1581 323 if (this.currentStateMap[this.currentIndex].type == "audioHolder") {
nickjillings@1575 324 console.log("Loading test page"+this.currentTestId);
nickjillings@1581 325 } else if (this.currentStateMap[this.currentIndex].type == "pretest") {
nickjillings@1575 326 popup.initState(this.currentStateMap[this.currentIndex]);
nickjillings@1581 327 } else if (this.currentStateMap[this.currentIndex].type == "posttest") {
nickjillings@1575 328 popup.initState(this.currentStateMap[this.currentIndex]);
nickjillings@1575 329 } else {
nickjillings@1575 330 this.advanceInnerState();
nickjillings@1575 331 }
nickjillings@1575 332 }
nickjillings@1576 333 };
nickjillings@1575 334
nickjillings@1575 335 this.previousState = function(){};
nickjillings@1575 336 }
nickjillings@1575 337
nickjillings@1575 338 function testEnded(testId)
nickjillings@1575 339 {
nickjillings@1575 340 pageXMLSave(testId);
nickjillings@1575 341 if (testXMLSetups.length-1 > testId)
nickjillings@1575 342 {
nickjillings@1575 343 // Yes we have another test to perform
nickjillings@1575 344 testId = (Number(testId)+1);
nickjillings@1575 345 currentState = 'testRun-'+testId;
nickjillings@1575 346 loadTest(testId);
nickjillings@1575 347 } else {
nickjillings@1575 348 console.log('Testing Completed!');
nickjillings@1575 349 currentState = 'postTest';
nickjillings@1575 350 // Check for any post tests
nickjillings@1575 351 var xmlSetup = projectXML.find('setup');
nickjillings@1575 352 var postTest = xmlSetup.find('PostTest')[0];
nickjillings@1575 353 popup.initState(postTest);
nickjillings@1575 354 }
nickjillings@1575 355 }
nickjillings@1575 356
nickjillings@1575 357 function loadProjectSpec(url) {
nickjillings@1575 358 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
nickjillings@1575 359 // If url is null, request client to upload project XML document
nickjillings@1575 360 var r = new XMLHttpRequest();
nickjillings@1575 361 r.open('GET',url,true);
nickjillings@1575 362 r.onload = function() {
nickjillings@1575 363 loadProjectSpecCallback(r.response);
nickjillings@1575 364 };
nickjillings@1575 365 r.send();
nickjillings@1575 366 };
nickjillings@1575 367
nickjillings@1575 368 function loadProjectSpecCallback(response) {
nickjillings@1575 369 // Function called after asynchronous download of XML project specification
nickjillings@1580 370 //var decode = $.parseXML(response);
nickjillings@1580 371 //projectXML = $(decode);
nickjillings@1580 372
nickjillings@1580 373 var parse = new DOMParser();
nickjillings@1580 374 projectXML = parse.parseFromString(response,'text/xml');
nickjillings@1575 375
nickjillings@1581 376 // Build the specification
nickjillings@1581 377 specification.decode();
nickjillings@1576 378
nickjillings@1581 379 testState.stateMap.push(specification.preTest);
nickjillings@1576 380
nickjillings@1576 381 // New check if we need to randomise the test order
nickjillings@1581 382 if (specification.randomiseOrder)
nickjillings@1581 383 {
nickjillings@1581 384 specification.audioHolders = randomiseOrder(specification.audioHolders);
nickjillings@1576 385 }
nickjillings@1576 386
nickjillings@1581 387 $(specification.audioHolders).each(function(index,elem){
nickjillings@1576 388 testState.stateMap.push(elem);
nickjillings@1576 389 });
nickjillings@1576 390
nickjillings@1581 391 testState.stateMap.push(specification.postTest);
nickjillings@1576 392
nickjillings@1576 393 // Obtain the metrics enabled
nickjillings@1581 394 $(specification.metrics).each(function(index,node){
nickjillings@1576 395 var enabled = node.textContent;
nickjillings@1581 396 switch(node.enabled)
nickjillings@1576 397 {
nickjillings@1576 398 case 'testTimer':
nickjillings@1576 399 sessionMetrics.prototype.enableTestTimer = true;
nickjillings@1576 400 break;
nickjillings@1576 401 case 'elementTimer':
nickjillings@1576 402 sessionMetrics.prototype.enableElementTimer = true;
nickjillings@1576 403 break;
nickjillings@1576 404 case 'elementTracker':
nickjillings@1576 405 sessionMetrics.prototype.enableElementTracker = true;
nickjillings@1576 406 break;
nickjillings@1576 407 case 'elementListenTracker':
nickjillings@1576 408 sessionMetrics.prototype.enableElementListenTracker = true;
nickjillings@1576 409 break;
nickjillings@1576 410 case 'elementInitialPosition':
nickjillings@1576 411 sessionMetrics.prototype.enableElementInitialPosition = true;
nickjillings@1576 412 break;
nickjillings@1576 413 case 'elementFlagListenedTo':
nickjillings@1576 414 sessionMetrics.prototype.enableFlagListenedTo = true;
nickjillings@1576 415 break;
nickjillings@1576 416 case 'elementFlagMoved':
nickjillings@1576 417 sessionMetrics.prototype.enableFlagMoved = true;
nickjillings@1576 418 break;
nickjillings@1576 419 case 'elementFlagComments':
nickjillings@1576 420 sessionMetrics.prototype.enableFlagComments = true;
nickjillings@1576 421 break;
nickjillings@1576 422 }
nickjillings@1576 423 });
nickjillings@1576 424
nickjillings@1576 425
nickjillings@1576 426
nickjillings@1575 427 // Detect the interface to use and load the relevant javascripts.
nickjillings@1575 428 var interfaceJS = document.createElement('script');
nickjillings@1575 429 interfaceJS.setAttribute("type","text/javascript");
nickjillings@1581 430 if (specification.interfaceType == 'APE') {
nickjillings@1575 431 interfaceJS.setAttribute("src","ape.js");
nickjillings@1575 432
nickjillings@1575 433 // APE comes with a css file
nickjillings@1575 434 var css = document.createElement('link');
nickjillings@1575 435 css.rel = 'stylesheet';
nickjillings@1575 436 css.type = 'text/css';
nickjillings@1575 437 css.href = 'ape.css';
nickjillings@1575 438
nickjillings@1575 439 document.getElementsByTagName("head")[0].appendChild(css);
nickjillings@1575 440 }
nickjillings@1575 441 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
nickjillings@1575 442
nickjillings@1575 443 // Define window callbacks for interface
nickjillings@1575 444 window.onresize = function(event){resizeWindow(event);};
nickjillings@1575 445 }
nickjillings@1575 446
nickjillings@1575 447 function createProjectSave(destURL) {
nickjillings@1575 448 // Save the data from interface into XML and send to destURL
nickjillings@1575 449 // If destURL is null then download XML in client
nickjillings@1575 450 // Now time to render file locally
nickjillings@1575 451 var xmlDoc = interfaceXMLSave();
nickjillings@1575 452 var parent = document.createElement("div");
nickjillings@1575 453 parent.appendChild(xmlDoc);
nickjillings@1575 454 var file = [parent.innerHTML];
nickjillings@1575 455 if (destURL == "null" || destURL == undefined) {
nickjillings@1575 456 var bb = new Blob(file,{type : 'application/xml'});
nickjillings@1575 457 var dnlk = window.URL.createObjectURL(bb);
nickjillings@1575 458 var a = document.createElement("a");
nickjillings@1575 459 a.hidden = '';
nickjillings@1575 460 a.href = dnlk;
nickjillings@1575 461 a.download = "save.xml";
nickjillings@1575 462 a.textContent = "Save File";
nickjillings@1575 463
nickjillings@1575 464 popup.showPopup();
nickjillings@1575 465 popup.popupContent.innerHTML = null;
nickjillings@1582 466 popup.popupContent.appendChild(a);
nickjillings@1575 467 } else {
nickjillings@1575 468 var xmlhttp = new XMLHttpRequest;
nickjillings@1575 469 xmlhttp.open("POST",destURL,true);
nickjillings@1575 470 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
nickjillings@1575 471 xmlhttp.onerror = function(){
nickjillings@1575 472 console.log('Error saving file to server! Presenting download locally');
nickjillings@1575 473 createProjectSave(null);
nickjillings@1575 474 };
nickjillings@1575 475 xmlhttp.onreadystatechange = function() {
nickjillings@1575 476 console.log(xmlhttp.status);
nickjillings@1575 477 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) {
nickjillings@1575 478 createProjectSave(null);
nickjillings@1575 479 }
nickjillings@1575 480 };
nickjillings@1575 481 xmlhttp.send(file);
nickjillings@1575 482 }
nickjillings@1575 483 }
nickjillings@1575 484
nickjillings@1575 485 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nickjillings@1575 486 function interfaceXMLSave(){
nickjillings@1575 487 // Create the XML string to be exported with results
nickjillings@1575 488 var xmlDoc = document.createElement("BrowserEvaluationResult");
nickjillings@1575 489 xmlDoc.appendChild(returnDateNode());
nickjillings@1575 490 for (var i=0; i<testState.stateResults.length; i++)
nickjillings@1575 491 {
nickjillings@1575 492 xmlDoc.appendChild(testState.stateResults[i]);
nickjillings@1575 493 }
nickjillings@1575 494
nickjillings@1575 495 return xmlDoc;
nickjillings@1575 496 }
nickjillings@1575 497
nickjillings@1575 498 function AudioEngine() {
nickjillings@1575 499
nickjillings@1575 500 // Create two output paths, the main outputGain and fooGain.
nickjillings@1575 501 // Output gain is default to 1 and any items for playback route here
nickjillings@1575 502 // Foo gain is used for analysis to ensure paths get processed, but are not heard
nickjillings@1575 503 // because web audio will optimise and any route which does not go to the destination gets ignored.
nickjillings@1575 504 this.outputGain = audioContext.createGain();
nickjillings@1575 505 this.fooGain = audioContext.createGain();
nickjillings@1575 506 this.fooGain.gain = 0;
nickjillings@1575 507
nickjillings@1575 508 // Use this to detect playback state: 0 - stopped, 1 - playing
nickjillings@1575 509 this.status = 0;
nickjillings@1575 510 this.audioObjectsReady = false;
nickjillings@1575 511
nickjillings@1575 512 // Connect both gains to output
nickjillings@1575 513 this.outputGain.connect(audioContext.destination);
nickjillings@1575 514 this.fooGain.connect(audioContext.destination);
nickjillings@1575 515
nickjillings@1575 516 // Create the timer Object
nickjillings@1575 517 this.timer = new timer();
nickjillings@1575 518 // Create session metrics
nickjillings@1575 519 this.metric = new sessionMetrics(this);
nickjillings@1575 520
nickjillings@1575 521 this.loopPlayback = false;
nickjillings@1575 522
nickjillings@1575 523 // Create store for new audioObjects
nickjillings@1575 524 this.audioObjects = [];
nickjillings@1575 525
nickjillings@1575 526 this.play = function() {
nickjillings@1575 527 // Start the timer and set the audioEngine state to playing (1)
nickjillings@1575 528 if (this.status == 0) {
nickjillings@1575 529 // Check if all audioObjects are ready
nickjillings@1575 530 if (this.audioObjectsReady == false) {
nickjillings@1575 531 this.audioObjectsReady = this.checkAllReady();
nickjillings@1575 532 }
nickjillings@1575 533 if (this.audioObjectsReady == true) {
nickjillings@1575 534 this.timer.startTest();
nickjillings@1575 535 if (this.loopPlayback) {
nickjillings@1575 536 for(var i=0; i<this.audioObjects.length; i++) {
nickjillings@1575 537 this.audioObjects[i].play(this.timer.getTestTime()+1);
nickjillings@1575 538 }
nickjillings@1575 539 }
nickjillings@1575 540 this.status = 1;
nickjillings@1575 541 }
nickjillings@1575 542 }
nickjillings@1575 543 };
nickjillings@1575 544
nickjillings@1575 545 this.stop = function() {
nickjillings@1575 546 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
nickjillings@1575 547 if (this.status == 1) {
nickjillings@1575 548 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1575 549 {
nickjillings@1575 550 this.audioObjects[i].stop();
nickjillings@1575 551 }
nickjillings@1575 552 this.status = 0;
nickjillings@1575 553 }
nickjillings@1575 554 };
nickjillings@1575 555
nickjillings@1575 556
nickjillings@1582 557 this.newTrack = function(element) {
nickjillings@1575 558 // Pull data from given URL into new audio buffer
nickjillings@1575 559 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
nickjillings@1575 560
nickjillings@1575 561 // Create the audioObject with ID of the new track length;
nickjillings@1575 562 audioObjectId = this.audioObjects.length;
nickjillings@1575 563 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
nickjillings@1575 564
nickjillings@1575 565 // AudioObject will get track itself.
nickjillings@1582 566 this.audioObjects[audioObjectId].specification = element;
nickjillings@1582 567 this.audioObjects[audioObjectId].constructTrack(element.parent.hostURL + element.url);
nickjillings@1579 568 return this.audioObjects[audioObjectId];
nickjillings@1575 569 };
nickjillings@1575 570
nickjillings@1575 571 this.newTestPage = function() {
nickjillings@1575 572 this.state = 0;
nickjillings@1575 573 this.audioObjectsReady = false;
nickjillings@1575 574 this.metric.reset();
nickjillings@1575 575 this.audioObjects = [];
nickjillings@1575 576 };
nickjillings@1575 577
nickjillings@1575 578 this.checkAllPlayed = function() {
nickjillings@1575 579 arr = [];
nickjillings@1575 580 for (var id=0; id<this.audioObjects.length; id++) {
nickjillings@1575 581 if (this.audioObjects[id].metric.wasListenedTo == false) {
nickjillings@1575 582 arr.push(this.audioObjects[id].id);
nickjillings@1575 583 }
nickjillings@1575 584 }
nickjillings@1575 585 return arr;
nickjillings@1575 586 };
nickjillings@1575 587
nickjillings@1575 588 this.checkAllReady = function() {
nickjillings@1575 589 var ready = true;
nickjillings@1575 590 for (var i=0; i<this.audioObjects.length; i++) {
nickjillings@1575 591 if (this.audioObjects[i].state == 0) {
nickjillings@1575 592 // Track not ready
nickjillings@1575 593 console.log('WAIT -- audioObject '+i+' not ready yet!');
nickjillings@1575 594 ready = false;
nickjillings@1575 595 };
nickjillings@1575 596 }
nickjillings@1575 597 return ready;
nickjillings@1575 598 };
nickjillings@1575 599
nickjillings@1575 600 }
nickjillings@1575 601
nickjillings@1575 602 function audioObject(id) {
nickjillings@1575 603 // The main buffer object with common control nodes to the AudioEngine
nickjillings@1575 604
nickjillings@1582 605 this.specification;
nickjillings@1575 606 this.id = id;
nickjillings@1575 607 this.state = 0; // 0 - no data, 1 - ready
nickjillings@1575 608 this.url = null; // Hold the URL given for the output back to the results.
nickjillings@1575 609 this.metric = new metricTracker(this);
nickjillings@1575 610
nickjillings@1577 611 // Bindings for GUI
nickjillings@1583 612 this.interfaceDOM = null;
nickjillings@1577 613 this.commentDOM = null;
nickjillings@1577 614
nickjillings@1575 615 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
nickjillings@1575 616 this.bufferNode = undefined;
nickjillings@1575 617 this.outputGain = audioContext.createGain();
nickjillings@1575 618
nickjillings@1575 619 // Default output gain to be zero
nickjillings@1575 620 this.outputGain.gain.value = 0.0;
nickjillings@1575 621
nickjillings@1575 622 // Connect buffer to the audio graph
nickjillings@1575 623 this.outputGain.connect(audioEngineContext.outputGain);
nickjillings@1575 624
nickjillings@1575 625 // the audiobuffer is not designed for multi-start playback
nickjillings@1575 626 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
nickjillings@1575 627 this.buffer;
nickjillings@1575 628
nickjillings@1575 629 this.loopStart = function() {
nickjillings@1575 630 this.outputGain.gain.value = 1.0;
nickjillings@1575 631 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1577 632 };
nickjillings@1575 633
nickjillings@1575 634 this.loopStop = function() {
nickjillings@1575 635 if (this.outputGain.gain.value != 0.0) {
nickjillings@1575 636 this.outputGain.gain.value = 0.0;
nickjillings@1575 637 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1575 638 }
nickjillings@1577 639 };
nickjillings@1575 640
nickjillings@1575 641 this.play = function(startTime) {
nickjillings@1575 642 this.bufferNode = audioContext.createBufferSource();
nickjillings@1575 643 this.bufferNode.owner = this;
nickjillings@1575 644 this.bufferNode.connect(this.outputGain);
nickjillings@1575 645 this.bufferNode.buffer = this.buffer;
nickjillings@1575 646 this.bufferNode.loop = audioEngineContext.loopPlayback;
nickjillings@1575 647 this.bufferNode.onended = function() {
nickjillings@1575 648 // Safari does not like using 'this' to reference the calling object!
nickjillings@1575 649 event.srcElement.owner.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1575 650 };
nickjillings@1575 651 if (this.bufferNode.loop == false) {
nickjillings@1575 652 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1575 653 }
nickjillings@1575 654 this.bufferNode.start(startTime);
nickjillings@1575 655 };
nickjillings@1575 656
nickjillings@1575 657 this.stop = function() {
nickjillings@1575 658 if (this.bufferNode != undefined)
nickjillings@1575 659 {
nickjillings@1575 660 this.bufferNode.stop(0);
nickjillings@1575 661 this.bufferNode = undefined;
nickjillings@1575 662 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1575 663 }
nickjillings@1575 664 };
nickjillings@1575 665
nickjillings@1575 666 this.getCurrentPosition = function() {
nickjillings@1575 667 var time = audioEngineContext.timer.getTestTime();
nickjillings@1575 668 if (this.bufferNode != undefined) {
nickjillings@1575 669 if (this.bufferNode.loop == true) {
nickjillings@1575 670 if (audioEngineContext.status == 1) {
nickjillings@1575 671 return time%this.buffer.duration;
nickjillings@1575 672 } else {
nickjillings@1575 673 return 0;
nickjillings@1575 674 }
nickjillings@1575 675 } else {
nickjillings@1575 676 if (this.metric.listenHold) {
nickjillings@1575 677 return time - this.metric.listenStart;
nickjillings@1575 678 } else {
nickjillings@1575 679 return 0;
nickjillings@1575 680 }
nickjillings@1575 681 }
nickjillings@1575 682 } else {
nickjillings@1575 683 return 0;
nickjillings@1575 684 }
nickjillings@1575 685 };
nickjillings@1575 686
nickjillings@1575 687 this.constructTrack = function(url) {
nickjillings@1575 688 var request = new XMLHttpRequest();
nickjillings@1575 689 this.url = url;
nickjillings@1575 690 request.open('GET',url,true);
nickjillings@1575 691 request.responseType = 'arraybuffer';
nickjillings@1575 692
nickjillings@1575 693 var audioObj = this;
nickjillings@1575 694
nickjillings@1575 695 // Create callback to decode the data asynchronously
nickjillings@1575 696 request.onloadend = function() {
nickjillings@1575 697 audioContext.decodeAudioData(request.response, function(decodedData) {
nickjillings@1575 698 audioObj.buffer = decodedData;
nickjillings@1575 699 audioObj.state = 1;
nickjillings@1575 700 }, function(){
nickjillings@1575 701 // Should only be called if there was an error, but sometimes gets called continuously
nickjillings@1575 702 // Check here if the error is genuine
nickjillings@1575 703 if (audioObj.state == 0 || audioObj.buffer == undefined) {
nickjillings@1575 704 // Genuine error
nickjillings@1575 705 console.log('FATAL - Error loading buffer on '+audioObj.id);
nickjillings@1575 706 }
nickjillings@1575 707 });
nickjillings@1575 708 };
nickjillings@1575 709 request.send();
nickjillings@1575 710 };
nickjillings@1583 711
nickjillings@1583 712 this.exportXMLDOM = function() {
nickjillings@1583 713 var root = document.createElement('audioElement');
nickjillings@1583 714 root.id = this.specification.id;
nickjillings@1583 715 root.setAttribute('url',this.url);
nickjillings@1583 716 root.appendChild(this.interfaceDOM.exportXMLDOM());
nickjillings@1583 717 root.appendChild(this.commentDOM.exportXMLDOM());
nickjillings@1583 718 root.appendChild(this.metric.exportXMLDOM());
nickjillings@1583 719 return root;
nickjillings@1583 720 };
nickjillings@1575 721 }
nickjillings@1575 722
nickjillings@1575 723 function timer()
nickjillings@1575 724 {
nickjillings@1575 725 /* Timer object used in audioEngine to keep track of session timings
nickjillings@1575 726 * Uses the timer of the web audio API, so sample resolution
nickjillings@1575 727 */
nickjillings@1575 728 this.testStarted = false;
nickjillings@1575 729 this.testStartTime = 0;
nickjillings@1575 730 this.testDuration = 0;
nickjillings@1575 731 this.minimumTestTime = 0; // No minimum test time
nickjillings@1575 732 this.startTest = function()
nickjillings@1575 733 {
nickjillings@1575 734 if (this.testStarted == false)
nickjillings@1575 735 {
nickjillings@1575 736 this.testStartTime = audioContext.currentTime;
nickjillings@1575 737 this.testStarted = true;
nickjillings@1575 738 this.updateTestTime();
nickjillings@1575 739 audioEngineContext.metric.initialiseTest();
nickjillings@1575 740 }
nickjillings@1575 741 };
nickjillings@1575 742 this.stopTest = function()
nickjillings@1575 743 {
nickjillings@1575 744 if (this.testStarted)
nickjillings@1575 745 {
nickjillings@1575 746 this.testDuration = this.getTestTime();
nickjillings@1575 747 this.testStarted = false;
nickjillings@1575 748 } else {
nickjillings@1575 749 console.log('ERR: Test tried to end before beginning');
nickjillings@1575 750 }
nickjillings@1575 751 };
nickjillings@1575 752 this.updateTestTime = function()
nickjillings@1575 753 {
nickjillings@1575 754 if (this.testStarted)
nickjillings@1575 755 {
nickjillings@1575 756 this.testDuration = audioContext.currentTime - this.testStartTime;
nickjillings@1575 757 }
nickjillings@1575 758 };
nickjillings@1575 759 this.getTestTime = function()
nickjillings@1575 760 {
nickjillings@1575 761 this.updateTestTime();
nickjillings@1575 762 return this.testDuration;
nickjillings@1575 763 };
nickjillings@1575 764 }
nickjillings@1575 765
nickjillings@1575 766 function sessionMetrics(engine)
nickjillings@1575 767 {
nickjillings@1575 768 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
nickjillings@1575 769 */
nickjillings@1575 770 this.engine = engine;
nickjillings@1575 771 this.lastClicked = -1;
nickjillings@1575 772 this.data = -1;
nickjillings@1575 773 this.reset = function() {
nickjillings@1575 774 this.lastClicked = -1;
nickjillings@1575 775 this.data = -1;
nickjillings@1575 776 };
nickjillings@1575 777 this.initialiseTest = function(){};
nickjillings@1575 778 }
nickjillings@1575 779
nickjillings@1575 780 function metricTracker(caller)
nickjillings@1575 781 {
nickjillings@1575 782 /* Custom object to track and collect metric data
nickjillings@1575 783 * Used only inside the audioObjects object.
nickjillings@1575 784 */
nickjillings@1575 785
nickjillings@1575 786 this.listenedTimer = 0;
nickjillings@1575 787 this.listenStart = 0;
nickjillings@1575 788 this.listenHold = false;
nickjillings@1575 789 this.initialPosition = -1;
nickjillings@1575 790 this.movementTracker = [];
nickjillings@1575 791 this.listenTracker =[];
nickjillings@1575 792 this.wasListenedTo = false;
nickjillings@1575 793 this.wasMoved = false;
nickjillings@1575 794 this.hasComments = false;
nickjillings@1575 795 this.parent = caller;
nickjillings@1575 796
nickjillings@1575 797 this.initialised = function(position)
nickjillings@1575 798 {
nickjillings@1575 799 if (this.initialPosition == -1) {
nickjillings@1575 800 this.initialPosition = position;
nickjillings@1575 801 }
nickjillings@1575 802 };
nickjillings@1575 803
nickjillings@1575 804 this.moved = function(time,position)
nickjillings@1575 805 {
nickjillings@1575 806 this.wasMoved = true;
nickjillings@1575 807 this.movementTracker[this.movementTracker.length] = [time, position];
nickjillings@1575 808 };
nickjillings@1575 809
nickjillings@1575 810 this.startListening = function(time)
nickjillings@1575 811 {
nickjillings@1575 812 if (this.listenHold == false)
nickjillings@1575 813 {
nickjillings@1575 814 this.wasListenedTo = true;
nickjillings@1575 815 this.listenStart = time;
nickjillings@1575 816 this.listenHold = true;
nickjillings@1575 817
nickjillings@1575 818 var evnt = document.createElement('event');
nickjillings@1575 819 var testTime = document.createElement('testTime');
nickjillings@1575 820 testTime.setAttribute('start',time);
nickjillings@1575 821 var bufferTime = document.createElement('bufferTime');
nickjillings@1575 822 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
nickjillings@1575 823 evnt.appendChild(testTime);
nickjillings@1575 824 evnt.appendChild(bufferTime);
nickjillings@1575 825 this.listenTracker.push(evnt);
nickjillings@1575 826
nickjillings@1575 827 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1575 828 }
nickjillings@1575 829 };
nickjillings@1575 830
nickjillings@1575 831 this.stopListening = function(time)
nickjillings@1575 832 {
nickjillings@1575 833 if (this.listenHold == true)
nickjillings@1575 834 {
nickjillings@1575 835 var diff = time - this.listenStart;
nickjillings@1575 836 this.listenedTimer += (diff);
nickjillings@1575 837 this.listenStart = 0;
nickjillings@1575 838 this.listenHold = false;
nickjillings@1575 839
nickjillings@1575 840 var evnt = this.listenTracker[this.listenTracker.length-1];
nickjillings@1575 841 var testTime = evnt.getElementsByTagName('testTime')[0];
nickjillings@1575 842 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
nickjillings@1575 843 testTime.setAttribute('stop',time);
nickjillings@1575 844 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
nickjillings@1575 845 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1575 846 }
nickjillings@1575 847 };
nickjillings@1577 848
nickjillings@1577 849 this.exportXMLDOM = function() {
nickjillings@1577 850 var root = document.createElement('metric');
nickjillings@1577 851 if (audioEngineContext.metric.enableElementTimer) {
nickjillings@1577 852 var mElementTimer = document.createElement('metricresult');
nickjillings@1577 853 mElementTimer.setAttribute('name','enableElementTimer');
nickjillings@1577 854 mElementTimer.textContent = this.listenedTimer;
nickjillings@1577 855 root.appendChild(mElementTimer);
nickjillings@1577 856 }
nickjillings@1577 857 if (audioEngineContext.metric.enableElementTracker) {
nickjillings@1577 858 var elementTrackerFull = document.createElement('metricResult');
nickjillings@1577 859 elementTrackerFull.setAttribute('name','elementTrackerFull');
nickjillings@1577 860 for (var k=0; k<this.movementTracker.length; k++)
nickjillings@1577 861 {
nickjillings@1577 862 var timePos = document.createElement('timePos');
nickjillings@1577 863 timePos.id = k;
nickjillings@1577 864 var time = document.createElement('time');
nickjillings@1577 865 time.textContent = this.movementTracker[k][0];
nickjillings@1577 866 var position = document.createElement('position');
nickjillings@1577 867 position.textContent = this.movementTracker[k][1];
nickjillings@1577 868 timePos.appendChild(time);
nickjillings@1577 869 timePos.appendChild(position);
nickjillings@1577 870 elementTrackerFull.appendChild(timePos);
nickjillings@1577 871 }
nickjillings@1577 872 root.appendChild(elementTrackerFull);
nickjillings@1577 873 }
nickjillings@1577 874 if (audioEngineContext.metric.enableElementListenTracker) {
nickjillings@1577 875 var elementListenTracker = document.createElement('metricResult');
nickjillings@1577 876 elementListenTracker.setAttribute('name','elementListenTracker');
nickjillings@1577 877 for (var k=0; k<this.listenTracker.length; k++) {
nickjillings@1577 878 elementListenTracker.appendChild(this.listenTracker[k]);
nickjillings@1577 879 }
nickjillings@1577 880 root.appendChild(elementListenTracker);
nickjillings@1577 881 }
nickjillings@1577 882 if (audioEngineContext.metric.enableElementInitialPosition) {
nickjillings@1577 883 var elementInitial = document.createElement('metricResult');
nickjillings@1577 884 elementInitial.setAttribute('name','elementInitialPosition');
nickjillings@1577 885 elementInitial.textContent = this.initialPosition;
nickjillings@1577 886 root.appendChild(elementInitial);
nickjillings@1577 887 }
nickjillings@1577 888 if (audioEngineContext.metric.enableFlagListenedTo) {
nickjillings@1577 889 var flagListenedTo = document.createElement('metricResult');
nickjillings@1577 890 flagListenedTo.setAttribute('name','elementFlagListenedTo');
nickjillings@1577 891 flagListenedTo.textContent = this.wasListenedTo;
nickjillings@1577 892 root.appendChild(flagListenedTo);
nickjillings@1577 893 }
nickjillings@1577 894 if (audioEngineContext.metric.enableFlagMoved) {
nickjillings@1577 895 var flagMoved = document.createElement('metricResult');
nickjillings@1577 896 flagMoved.setAttribute('name','elementFlagMoved');
nickjillings@1577 897 flagMoved.textContent = this.wasMoved;
nickjillings@1577 898 root.appendChild(flagMoved);
nickjillings@1577 899 }
nickjillings@1577 900 if (audioEngineContext.metric.enableFlagComments) {
nickjillings@1577 901 var flagComments = document.createElement('metricResult');
nickjillings@1577 902 flagComments.setAttribute('name','elementFlagComments');
nickjillings@1577 903 if (this.parent.commentDOM == null)
nickjillings@1577 904 {flag.textContent = 'false';}
nickjillings@1577 905 else if (this.parent.commentDOM.textContent.length == 0)
nickjillings@1577 906 {flag.textContent = 'false';}
nickjillings@1577 907 else
nickjillings@1577 908 {flag.textContet = 'true';}
nickjillings@1577 909 root.appendChild(flagComments);
nickjillings@1577 910 }
nickjillings@1577 911
nickjillings@1577 912 return root;
nickjillings@1577 913 };
nickjillings@1575 914 }
nickjillings@1575 915
nickjillings@1575 916 function randomiseOrder(input)
nickjillings@1575 917 {
nickjillings@1575 918 // This takes an array of information and randomises the order
nickjillings@1575 919 var N = input.length;
nickjillings@1575 920 var K = N;
nickjillings@1575 921 var holdArr = [];
nickjillings@1575 922 for (var n=0; n<N; n++)
nickjillings@1575 923 {
nickjillings@1575 924 // First pick a random number
nickjillings@1575 925 var r = Math.random();
nickjillings@1575 926 // Multiply and floor by the number of elements left
nickjillings@1575 927 r = Math.floor(r*input.length);
nickjillings@1575 928 // Pick out that element and delete from the array
nickjillings@1575 929 holdArr.push(input.splice(r,1)[0]);
nickjillings@1575 930 }
nickjillings@1575 931 return holdArr;
nickjillings@1575 932 }
nickjillings@1575 933
nickjillings@1575 934 function returnDateNode()
nickjillings@1575 935 {
nickjillings@1575 936 // Create an XML Node for the Date and Time a test was conducted
nickjillings@1575 937 // Structure is
nickjillings@1575 938 // <datetime>
nickjillings@1575 939 // <date year="##" month="##" day="##">DD/MM/YY</date>
nickjillings@1575 940 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
nickjillings@1575 941 // </datetime>
nickjillings@1575 942 var dateTime = new Date();
nickjillings@1575 943 var year = document.createAttribute('year');
nickjillings@1575 944 var month = document.createAttribute('month');
nickjillings@1575 945 var day = document.createAttribute('day');
nickjillings@1575 946 var hour = document.createAttribute('hour');
nickjillings@1575 947 var minute = document.createAttribute('minute');
nickjillings@1575 948 var secs = document.createAttribute('secs');
nickjillings@1575 949
nickjillings@1575 950 year.nodeValue = dateTime.getFullYear();
nickjillings@1575 951 month.nodeValue = dateTime.getMonth()+1;
nickjillings@1575 952 day.nodeValue = dateTime.getDate();
nickjillings@1575 953 hour.nodeValue = dateTime.getHours();
nickjillings@1575 954 minute.nodeValue = dateTime.getMinutes();
nickjillings@1575 955 secs.nodeValue = dateTime.getSeconds();
nickjillings@1575 956
nickjillings@1575 957 var hold = document.createElement("datetime");
nickjillings@1575 958 var date = document.createElement("date");
nickjillings@1575 959 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
nickjillings@1575 960 var time = document.createElement("time");
nickjillings@1575 961 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
nickjillings@1575 962
nickjillings@1575 963 date.setAttributeNode(year);
nickjillings@1575 964 date.setAttributeNode(month);
nickjillings@1575 965 date.setAttributeNode(day);
nickjillings@1575 966 time.setAttributeNode(hour);
nickjillings@1575 967 time.setAttributeNode(minute);
nickjillings@1575 968 time.setAttributeNode(secs);
nickjillings@1575 969
nickjillings@1575 970 hold.appendChild(date);
nickjillings@1575 971 hold.appendChild(time);
nickjillings@1575 972 return hold
nickjillings@1575 973
nickjillings@1575 974 }
nickjillings@1575 975
nickjillings@1575 976 function testWaitIndicator() {
nickjillings@1575 977 if (audioEngineContext.checkAllReady() == false) {
nickjillings@1575 978 var hold = document.createElement("div");
nickjillings@1575 979 hold.id = "testWaitIndicator";
nickjillings@1575 980 hold.className = "indicator-box";
nickjillings@1585 981 hold.style.zIndex = 3;
nickjillings@1575 982 var span = document.createElement("span");
nickjillings@1575 983 span.textContent = "Please wait! Elements still loading";
nickjillings@1575 984 hold.appendChild(span);
nickjillings@1585 985 var blank = document.createElement('div');
nickjillings@1585 986 blank.className = 'testHalt';
nickjillings@1585 987 blank.id = "testHaltBlank";
nickjillings@1575 988 var body = document.getElementsByTagName('body')[0];
nickjillings@1575 989 body.appendChild(hold);
nickjillings@1585 990 body.appendChild(blank);
nickjillings@1575 991 testWaitTimerIntervalHolder = setInterval(function(){
nickjillings@1575 992 var ready = audioEngineContext.checkAllReady();
nickjillings@1575 993 if (ready) {
nickjillings@1575 994 var elem = document.getElementById('testWaitIndicator');
nickjillings@1585 995 var blank = document.getElementById('testHaltBlank');
nickjillings@1575 996 var body = document.getElementsByTagName('body')[0];
nickjillings@1575 997 body.removeChild(elem);
nickjillings@1585 998 body.removeChild(blank);
nickjillings@1575 999 clearInterval(testWaitTimerIntervalHolder);
nickjillings@1575 1000 }
nickjillings@1575 1001 },500,false);
nickjillings@1575 1002 }
nickjillings@1575 1003 }
nickjillings@1575 1004
nickjillings@1575 1005 var testWaitTimerIntervalHolder = null;
nickjillings@1580 1006
nickjillings@1580 1007 function Specification() {
nickjillings@1580 1008 // Handles the decoding of the project specification XML into a simple JavaScript Object.
nickjillings@1580 1009
nickjillings@1580 1010 this.interfaceType;
nickjillings@1580 1011 this.projectReturn;
nickjillings@1580 1012 this.randomiseOrder;
nickjillings@1580 1013 this.collectMetrics;
nickjillings@1580 1014 this.preTest;
nickjillings@1580 1015 this.postTest;
nickjillings@1580 1016 this.metrics =[];
nickjillings@1580 1017
nickjillings@1580 1018 this.audioHolders = [];
nickjillings@1580 1019
nickjillings@1580 1020 this.decode = function() {
nickjillings@1580 1021 // projectXML - DOM Parsed document
nickjillings@1580 1022 var setupNode = projectXML.getElementsByTagName('setup')[0];
nickjillings@1580 1023 this.interfaceType = setupNode.getAttribute('interface');
nickjillings@1580 1024 this.projectReturn = setupNode.getAttribute('projectReturn');
nickjillings@1580 1025 if (setupNode.getAttribute('randomiseOrder') == "true") {
nickjillings@1580 1026 this.randomiseOrder = true;
nickjillings@1584 1027 } else {this.randomiseOrder = false;}
nickjillings@1580 1028 if (setupNode.getAttribute('collectMetrics') == "true") {
nickjillings@1580 1029 this.collectMetrics = true;
nickjillings@1584 1030 } else {this.collectMetrics = false;}
nickjillings@1580 1031 var metricCollection = setupNode.getElementsByTagName('Metric');
nickjillings@1580 1032
nickjillings@1581 1033 this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest'));
nickjillings@1581 1034 this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest'));
nickjillings@1580 1035
nickjillings@1580 1036 if (metricCollection.length > 0) {
nickjillings@1580 1037 metricCollection = metricCollection[0].getElementsByTagName('metricEnable');
nickjillings@1580 1038 for (var i=0; i<metricCollection.length; i++) {
nickjillings@1581 1039 this.metrics.push(new this.metricNode(metricCollection[i].textContent));
nickjillings@1580 1040 }
nickjillings@1580 1041 }
nickjillings@1580 1042
nickjillings@1580 1043 var audioHolders = projectXML.getElementsByTagName('audioHolder');
nickjillings@1580 1044 for (var i=0; i<audioHolders.length; i++) {
nickjillings@1580 1045 this.audioHolders.push(new this.audioHolderNode(this,audioHolders[i]));
nickjillings@1580 1046 }
nickjillings@1580 1047
nickjillings@1580 1048 };
nickjillings@1580 1049
nickjillings@1580 1050 this.prepostNode = function(type,Collection) {
nickjillings@1580 1051 this.type = type;
nickjillings@1580 1052 this.options = [];
nickjillings@1580 1053
nickjillings@1580 1054 this.OptionNode = function(child) {
nickjillings@1588 1055
nickjillings@1588 1056 this.childOption = function(element) {
nickjillings@1588 1057 this.type = 'option';
nickjillings@1588 1058 this.id = element.id;
nickjillings@1588 1059 this.text = element.textContent;
nickjillings@1588 1060 }
nickjillings@1588 1061
nickjillings@1580 1062 this.type = child.nodeName;
nickjillings@1580 1063 if (child.nodeName == "question") {
nickjillings@1580 1064 this.id = child.id;
nickjillings@1580 1065 this.mandatory;
nickjillings@1580 1066 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;}
nickjillings@1580 1067 else {this.mandatory = false;}
nickjillings@1580 1068 this.question = child.textContent;
nickjillings@1580 1069 } else if (child.nodeName == "statement") {
nickjillings@1581 1070 this.statement = child.textContent;
nickjillings@1588 1071 } else if (child.nodeName == "checkbox") {
nickjillings@1588 1072 var element = child.firstElementChild;
nickjillings@1588 1073 if (element == null) {
nickjillings@1588 1074 console.log('Malformed checkbox entry');
nickjillings@1588 1075 this.statement = 'Malformed checkbox entry';
nickjillings@1588 1076 this.type = 'statement';
nickjillings@1588 1077 } else {
nickjillings@1588 1078 this.options = [];
nickjillings@1588 1079 while (element != null) {
nickjillings@1588 1080 if (element.nodeName == 'statement' && this.statement == undefined){
nickjillings@1588 1081 this.statement = element.textContent;
nickjillings@1588 1082 } else if (element.nodeName == 'option') {
nickjillings@1588 1083 this.options.push(new this.childOption(element));
nickjillings@1588 1084 }
nickjillings@1588 1085 element = element.nextElementSibling;
nickjillings@1588 1086 }
nickjillings@1588 1087 }
nickjillings@1580 1088 }
nickjillings@1580 1089 };
nickjillings@1580 1090
nickjillings@1580 1091 // On construction:
nickjillings@1580 1092 if (Collection.length != 0) {
nickjillings@1580 1093 Collection = Collection[0];
nickjillings@1586 1094 if (Collection.childElementCount != 0) {
nickjillings@1586 1095 var child = Collection.firstElementChild;
nickjillings@1580 1096 this.options.push(new this.OptionNode(child));
nickjillings@1586 1097 while (child.nextElementSibling != null) {
nickjillings@1586 1098 child = child.nextElementSibling;
nickjillings@1586 1099 this.options.push(new this.OptionNode(child));
nickjillings@1586 1100 }
nickjillings@1580 1101 }
nickjillings@1580 1102 }
nickjillings@1580 1103 };
nickjillings@1580 1104
nickjillings@1580 1105 this.metricNode = function(name) {
nickjillings@1580 1106 this.enabled = name;
nickjillings@1580 1107 };
nickjillings@1580 1108
nickjillings@1580 1109 this.audioHolderNode = function(parent,xml) {
nickjillings@1581 1110 this.type = 'audioHolder';
nickjillings@1580 1111 this.interfaceNode = function(DOM) {
nickjillings@1580 1112 var title = DOM.getElementsByTagName('title');
nickjillings@1580 1113 if (title.length == 0) {this.title = null;}
nickjillings@1580 1114 else {this.title = title[0].textContent;}
nickjillings@1580 1115
nickjillings@1580 1116 var scale = DOM.getElementsByTagName('scale');
nickjillings@1580 1117 this.scale = [];
nickjillings@1580 1118 for (var i=0; i<scale.length; i++) {
nickjillings@1580 1119 var arr = [null, null];
nickjillings@1580 1120 arr[0] = scale[i].getAttribute('position');
nickjillings@1580 1121 arr[1] = scale[i].textContent;
nickjillings@1580 1122 this.scale.push(arr);
nickjillings@1580 1123 }
nickjillings@1580 1124 };
nickjillings@1580 1125
nickjillings@1582 1126 this.audioElementNode = function(parent,xml) {
nickjillings@1580 1127 this.url = xml.getAttribute('url');
nickjillings@1580 1128 this.id = xml.id;
nickjillings@1582 1129 this.parent = parent;
nickjillings@1580 1130 };
nickjillings@1580 1131
nickjillings@1580 1132 this.commentQuestionNode = function(xml) {
nickjillings@1580 1133 this.id = xml.id;
nickjillings@1580 1134 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;}
nickjillings@1580 1135 else {this.mandatory = false;}
nickjillings@1580 1136 this.question = xml.textContent;
nickjillings@1580 1137 };
nickjillings@1580 1138
nickjillings@1580 1139 this.id = xml.id;
nickjillings@1580 1140 this.hostURL = xml.getAttribute('hostURL');
nickjillings@1580 1141 this.sampleRate = xml.getAttribute('sampleRate');
nickjillings@1580 1142 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;}
nickjillings@1580 1143 else {this.randomiseOrder = false;}
nickjillings@1580 1144 this.repeatCount = xml.getAttribute('repeatCount');
nickjillings@1580 1145 if (xml.getAttribute('loop') == 'true') {this.loop = true;}
nickjillings@1580 1146 else {this.loop == false;}
nickjillings@1580 1147 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;}
nickjillings@1580 1148 else {this.elementComments = false;}
nickjillings@1580 1149
nickjillings@1581 1150 this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest'));
nickjillings@1581 1151 this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest'));
nickjillings@1580 1152
nickjillings@1580 1153 this.interfaces = [];
nickjillings@1580 1154 var interfaceDOM = xml.getElementsByTagName('interface');
nickjillings@1580 1155 for (var i=0; i<interfaceDOM.length; i++) {
nickjillings@1580 1156 this.interfaces.push(new this.interfaceNode(interfaceDOM[i]));
nickjillings@1580 1157 }
nickjillings@1580 1158
nickjillings@1580 1159 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix');
nickjillings@1580 1160 if (this.commentBoxPrefix.length != 0) {
nickjillings@1580 1161 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent;
nickjillings@1580 1162 } else {
nickjillings@1580 1163 this.commentBoxPrefix = "Comment on track";
nickjillings@1580 1164 }
nickjillings@1580 1165
nickjillings@1580 1166 this.audioElements =[];
nickjillings@1580 1167 var audioElementsDOM = xml.getElementsByTagName('audioElements');
nickjillings@1580 1168 for (var i=0; i<audioElementsDOM.length; i++) {
nickjillings@1582 1169 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
nickjillings@1580 1170 }
nickjillings@1580 1171
nickjillings@1580 1172 this.commentQuestions = [];
nickjillings@1580 1173 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion');
nickjillings@1580 1174 for (var i=0; i<commentQuestionsDOM.length; i++) {
nickjillings@1580 1175 this.commentQuestions.push(new this.commentQuestionNode(commentQuestionsDOM[i]));
nickjillings@1580 1176 }
nickjillings@1580 1177 };
nickjillings@1580 1178 }
nickjillings@1580 1179
nickjillings@1582 1180 function Interface(specificationObject) {
nickjillings@1580 1181 // This handles the bindings between the interface and the audioEngineContext;
nickjillings@1582 1182 this.specification = specificationObject;
nickjillings@1582 1183 this.insertPoint = document.getElementById("topLevelBody");
nickjillings@1580 1184
nickjillings@1582 1185 // Bounded by interface!!
nickjillings@1582 1186 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
nickjillings@1582 1187 // For example, APE returns the slider position normalised in a <value> tag.
nickjillings@1582 1188 this.interfaceObjects = [];
nickjillings@1582 1189 this.interfaceObject = function(){};
nickjillings@1582 1190
nickjillings@1582 1191 this.commentBoxes = [];
nickjillings@1582 1192 this.commentBox = function(audioObject) {
nickjillings@1582 1193 var element = audioObject.specification;
nickjillings@1583 1194 this.audioObject = audioObject;
nickjillings@1582 1195 this.id = audioObject.id;
nickjillings@1582 1196 var audioHolderObject = audioObject.specification.parent;
nickjillings@1582 1197 // Create document objects to hold the comment boxes
nickjillings@1582 1198 this.trackComment = document.createElement('div');
nickjillings@1582 1199 this.trackComment.className = 'comment-div';
nickjillings@1582 1200 this.trackComment.id = 'comment-div-'+audioObject.id;
nickjillings@1582 1201 // Create a string next to each comment asking for a comment
nickjillings@1583 1202 this.trackString = document.createElement('span');
nickjillings@1583 1203 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id;
nickjillings@1582 1204 // Create the HTML5 comment box 'textarea'
nickjillings@1583 1205 this.trackCommentBox = document.createElement('textarea');
nickjillings@1583 1206 this.trackCommentBox.rows = '4';
nickjillings@1583 1207 this.trackCommentBox.cols = '100';
nickjillings@1583 1208 this.trackCommentBox.name = 'trackComment'+audioObject.id;
nickjillings@1583 1209 this.trackCommentBox.className = 'trackComment';
nickjillings@1582 1210 var br = document.createElement('br');
nickjillings@1582 1211 // Add to the holder.
nickjillings@1583 1212 this.trackComment.appendChild(this.trackString);
nickjillings@1582 1213 this.trackComment.appendChild(br);
nickjillings@1583 1214 this.trackComment.appendChild(this.trackCommentBox);
nickjillings@1583 1215
nickjillings@1583 1216 this.exportXMLDOM = function() {
nickjillings@1583 1217 var root = document.createElement('comment');
nickjillings@1583 1218 if (this.audioObject.specification.parent.elementComments) {
nickjillings@1583 1219 var question = document.createElement('question');
nickjillings@1583 1220 question.textContent = this.trackString.textContent;
nickjillings@1583 1221 var response = document.createElement('response');
nickjillings@1583 1222 response.textContent = this.trackCommentBox.value;
nickjillings@1583 1223 root.appendChild(question);
nickjillings@1583 1224 root.appendChild(response);
nickjillings@1583 1225 }
nickjillings@1583 1226 return root;
nickjillings@1583 1227 };
nickjillings@1582 1228 };
nickjillings@1582 1229
nickjillings@1582 1230 this.createCommentBox = function(audioObject) {
nickjillings@1582 1231 var node = new this.commentBox(audioObject);
nickjillings@1582 1232 this.commentBoxes.push(node);
nickjillings@1582 1233 audioObject.commentDOM = node;
nickjillings@1582 1234 return node;
nickjillings@1582 1235 };
nickjillings@1582 1236
nickjillings@1582 1237 this.sortCommentBoxes = function() {
nickjillings@1582 1238 var holder = [];
nickjillings@1582 1239 while (this.commentBoxes.length > 0) {
nickjillings@1582 1240 var node = this.commentBoxes.pop(0);
nickjillings@1582 1241 holder[node.id] = node;
nickjillings@1582 1242 }
nickjillings@1582 1243 this.commentBoxes = holder;
nickjillings@1582 1244 };
nickjillings@1582 1245
nickjillings@1582 1246 this.showCommentBoxes = function(inject, sort) {
nickjillings@1582 1247 if (sort) {interfaceContext.sortCommentBoxes();}
nickjillings@1582 1248 for (var i=0; i<interfaceContext.commentBoxes.length; i++) {
nickjillings@1582 1249 inject.appendChild(this.commentBoxes[i].trackComment);
nickjillings@1582 1250 }
nickjillings@1582 1251 };
nickjillings@1580 1252 }
nickjillings@1580 1253