annotate core.js @ 1581:284251e3a6a3

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