annotate core.js @ 1579:4ffbccf448c2

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