annotate core.js @ 1622:ee9184f7f8d5

Popup now in core.js. Needs stress testing!!
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Tue, 26 May 2015 13:01:39 +0100
parents f8220eeeeddc
children 17353d015d33
rev   line source
b@1608 1 /**
b@1608 2 * core.js
b@1608 3 *
b@1608 4 * Main script to run, calls all other core functions and manages loading/store to backend.
b@1608 5 * Also contains all global variables.
b@1608 6 */
b@1608 7
b@1608 8 /* create the web audio API context and store in audioContext*/
b@1608 9 var audioContext; // Hold the browser web audio API
b@1608 10 var projectXML; // Hold the parsed setup XML
nickjillings@1622 11 var popup; // Hold the interfacePopup object
nickjillings@1622 12 var currentState; // Keep track of the current state (pre/post test, which test, final test? first test?)
b@1608 13 var testXMLSetups = []; // Hold the parsed test instances
b@1608 14 var testResultsHolders =[]; // Hold the results from each test for publishing to XML
b@1608 15 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
b@1608 16 var currentTestHolder; // Hold any intermediate results during test - metrics
b@1608 17 var audioEngineContext; // The custome AudioEngine object
b@1608 18 var projectReturn; // Hold the URL for the return
b@1608 19 var preTestQuestions = document.createElement('PreTest'); // Store any pre-test question response
b@1608 20 var postTestQuestions = document.createElement('PostTest'); // Store any post-test question response
b@1608 21
b@1608 22 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
b@1608 23 AudioBufferSourceNode.prototype.owner = undefined;
b@1608 24
b@1608 25 window.onload = function() {
b@1608 26 // Function called once the browser has loaded all files.
b@1608 27 // This should perform any initial commands such as structure / loading documents
b@1608 28
b@1608 29 // Create a web audio API context
b@1608 30 // Fixed for cross-browser support
b@1608 31 var AudioContext = window.AudioContext || window.webkitAudioContext;
b@1608 32 audioContext = new AudioContext;
b@1608 33
b@1608 34 // Create the audio engine object
b@1608 35 audioEngineContext = new AudioEngine();
nickjillings@1622 36
nickjillings@1622 37 // Create the popup interface object
nickjillings@1622 38 popup = new interfacePopup();
b@1608 39 };
b@1608 40
nickjillings@1622 41 function interfacePopup() {
nickjillings@1622 42 // Creates an object to manage the popup
nickjillings@1622 43 this.popup = null;
nickjillings@1622 44 this.popupContent = null;
nickjillings@1622 45 this.popupButton = null;
nickjillings@1622 46 this.popupOptions = null;
nickjillings@1622 47 this.currentIndex = null;
nickjillings@1622 48 this.responses = null;
nickjillings@1622 49 this.createPopup = function(){
nickjillings@1622 50 // Create popup window interface
nickjillings@1622 51 var insertPoint = document.getElementById("topLevelBody");
nickjillings@1622 52 var blank = document.createElement('div');
nickjillings@1622 53 blank.className = 'testHalt';
nickjillings@1622 54
nickjillings@1622 55 this.popup = document.createElement('div');
nickjillings@1622 56 this.popup.id = 'popupHolder';
nickjillings@1622 57 this.popup.className = 'popupHolder';
nickjillings@1622 58 this.popup.style.position = 'absolute';
nickjillings@1622 59 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
nickjillings@1622 60 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
nickjillings@1622 61
nickjillings@1622 62 this.popupContent = document.createElement('div');
nickjillings@1622 63 this.popupContent.id = 'popupContent';
nickjillings@1622 64 this.popupContent.style.marginTop = '25px';
nickjillings@1622 65 this.popupContent.align = 'center';
nickjillings@1622 66 this.popup.appendChild(this.popupContent);
nickjillings@1622 67
nickjillings@1622 68 this.popupButton = document.createElement('button');
nickjillings@1622 69 this.popupButton.className = 'popupButton';
nickjillings@1622 70 this.popupButton.innerHTML = 'Next';
nickjillings@1622 71 this.popupButton.onclick = function(){popup.buttonClicked();};
nickjillings@1622 72 insertPoint.appendChild(this.popup);
nickjillings@1622 73 insertPoint.appendChild(blank);
nickjillings@1622 74 };
nickjillings@1621 75
nickjillings@1622 76 this.showPopup = function(){
nickjillings@1622 77 if (this.popup == null || this.popup == undefined) {
nickjillings@1622 78 this.createPopup();
nickjillings@1622 79 }
nickjillings@1622 80 this.popup.style.zIndex = 3;
nickjillings@1622 81 this.popup.style.visibility = 'visible';
nickjillings@1622 82 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1622 83 blank.style.zIndex = 2;
nickjillings@1622 84 blank.style.visibility = 'visible';
nickjillings@1622 85 };
nickjillings@1622 86
nickjillings@1622 87 this.hidePopup = function(){
nickjillings@1622 88 this.popup.style.zIndex = -1;
nickjillings@1622 89 this.popup.style.visibility = 'hidden';
nickjillings@1622 90 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1622 91 blank.style.zIndex = -2;
nickjillings@1622 92 blank.style.visibility = 'hidden';
nickjillings@1622 93 };
nickjillings@1622 94
nickjillings@1622 95 this.postNode = function() {
nickjillings@1622 96 // This will take the node from the popupOptions and display it
nickjillings@1622 97 var node = this.popupOptions[this.currentIndex];
nickjillings@1622 98 this.popupContent.innerHTML = null;
nickjillings@1622 99 if (node.nodeName == 'statement') {
nickjillings@1622 100 var span = document.createElement('span');
nickjillings@1622 101 span.textContent = node.textContent;
nickjillings@1622 102 this.popupContent.appendChild(span);
nickjillings@1622 103 } else if (node.nodeName == 'question') {
nickjillings@1622 104 var span = document.createElement('span');
nickjillings@1622 105 span.textContent = node.textContent;
nickjillings@1622 106 var textArea = document.createElement('textarea');
nickjillings@1622 107 var br = document.createElement('br');
nickjillings@1622 108 this.popupContent.appendChild(span);
nickjillings@1622 109 this.popupContent.appendChild(br);
nickjillings@1622 110 this.popupContent.appendChild(textArea);
nickjillings@1622 111 }
nickjillings@1622 112 this.popupContent.appendChild(this.popupButton);
nickjillings@1622 113 }
nickjillings@1622 114
nickjillings@1622 115 this.initState = function(node) {
nickjillings@1622 116 //Call this with your preTest and postTest nodes when needed to
nickjillings@1622 117 // initialise the popup procedure.
nickjillings@1622 118 this.popupOptions = $(node).children();
nickjillings@1622 119 if (this.popupOptions.length > 0) {
nickjillings@1622 120 if (node.nodeName == 'preTest' || node.nodeName == 'PreTest') {
nickjillings@1622 121 this.responses = document.createElement('PreTest');
nickjillings@1622 122 } else if (node.nodeName == 'postTest' || node.nodeName == 'PostTest') {
nickjillings@1622 123 this.responses = document.createElement('PostTest');
nickjillings@1622 124 } else {
nickjillings@1622 125 console.log ('WARNING - popup node neither pre or post!');
nickjillings@1622 126 this.responses = document.createElement('responses');
nickjillings@1622 127 }
nickjillings@1622 128 this.currentIndex = 0;
nickjillings@1622 129 this.showPopup();
nickjillings@1622 130 this.postNode();
nickjillings@1622 131 }
nickjillings@1622 132 }
nickjillings@1622 133
nickjillings@1622 134 this.buttonClicked = function() {
nickjillings@1622 135 // Each time the popup button is clicked!
nickjillings@1622 136 var node = this.popupOptions[this.currentIndex];
nickjillings@1622 137 if (node.nodeName == 'question') {
nickjillings@1622 138 // Must extract the question data
nickjillings@1622 139 var mandatory = node.attributes['mandatory'];
nickjillings@1622 140 if (mandatory == undefined) {
nickjillings@1622 141 mandatory = false;
nickjillings@1622 142 } else {
nickjillings@1622 143 if (mandatory.value == 'true'){mandatory = true;}
nickjillings@1622 144 else {mandatory = false;}
nickjillings@1622 145 }
nickjillings@1622 146 var textArea = $(popup.popupContent).find('textarea')[0];
nickjillings@1622 147 if (mandatory == true && textArea.value.length == 0) {
nickjillings@1622 148 alert('This question is mandatory');
nickjillings@1622 149 return;
nickjillings@1622 150 } else {
nickjillings@1622 151 // Save the text content
nickjillings@1622 152 var hold = document.createElement('comment');
nickjillings@1622 153 hold.id = node.attributes['id'].value;
nickjillings@1622 154 hold.innerHTML = textArea.value;
nickjillings@1622 155 this.responses.appendChild(hold);
nickjillings@1622 156 }
nickjillings@1622 157 }
nickjillings@1622 158 this.currentIndex++;
nickjillings@1622 159 if (this.currentIndex < this.popupOptions.length) {
nickjillings@1622 160 this.postNode();
nickjillings@1622 161 } else {
nickjillings@1622 162 // Reached the end of the popupOptions
nickjillings@1622 163 this.hidePopup();
nickjillings@1622 164 advanceState();
nickjillings@1622 165 }
nickjillings@1622 166 }
nickjillings@1621 167 }
nickjillings@1621 168
nickjillings@1622 169 function advanceState()
nickjillings@1621 170 {
nickjillings@1622 171 console.log(currentState);
nickjillings@1622 172 if (currentState == 'preTest')
nickjillings@1622 173 {
nickjillings@1622 174 // End of pre-test, begin the test
nickjillings@1622 175 preTestQuestions = popup.responses;
nickjillings@1622 176 loadTest(0);
nickjillings@1622 177 } else if (currentState == 'postTest') {
nickjillings@1622 178 postTestQuestions = popup.responses;
nickjillings@1622 179 console.log('ALL COLLECTED!');
nickjillings@1622 180 }else if (currentState.substr(0,10) == 'testRunPre')
nickjillings@1622 181 {
nickjillings@1622 182 // Start the test
nickjillings@1622 183 var testId = currentState.substr(11,currentState.length-10);
nickjillings@1622 184 currentState = 'testRun-'+testId;
nickjillings@1622 185 currentTestHolder.appendChild(popup.responses);
nickjillings@1622 186 //audioEngineContext.timer.startTest();
nickjillings@1622 187 //audioEngineContext.play();
nickjillings@1622 188 } else if (currentState.substr(0,11) == 'testRunPost')
nickjillings@1622 189 {
nickjillings@1622 190 var testId = currentState.substr(12,currentState.length-11);
nickjillings@1622 191 currentTestHolder.appendChild(popup.responses);
nickjillings@1622 192 testEnded(testId);
nickjillings@1622 193 } else if (currentState.substr(0,7) == 'testRun')
nickjillings@1622 194 {
nickjillings@1622 195 var testId = currentState.substr(8,currentState.length-7);
nickjillings@1622 196 // Check if we have any post tests to perform
nickjillings@1622 197 var postXML = $(testXMLSetups[testId]).find('PostTest')[0];
nickjillings@1622 198 if (postXML == undefined || postXML.childElementCount == 0) {
nickjillings@1622 199 testEnded(testId);
nickjillings@1622 200 }
nickjillings@1622 201 else if (postXML.childElementCount > 0)
nickjillings@1622 202 {
nickjillings@1622 203 currentState = 'testRunPost-'+testId;
nickjillings@1622 204 popup.initState(postXML);
nickjillings@1622 205 }
nickjillings@1622 206 else {
nickjillings@1622 207
nickjillings@1622 208
nickjillings@1622 209 // No post tests, check if we have another test to perform instead
nickjillings@1622 210
nickjillings@1622 211 }
nickjillings@1621 212 }
nickjillings@1622 213 console.log(currentState);
nickjillings@1621 214 }
nickjillings@1621 215
nickjillings@1622 216 function testEnded(testId)
nickjillings@1621 217 {
nickjillings@1622 218 pageXMLSave(testId);
nickjillings@1622 219 if (testXMLSetups.length-1 > testId)
nickjillings@1622 220 {
nickjillings@1622 221 // Yes we have another test to perform
nickjillings@1622 222 testId = (Number(testId)+1);
nickjillings@1622 223 currentState = 'testRun-'+testId;
nickjillings@1622 224 loadTest(testId);
nickjillings@1622 225 } else {
nickjillings@1622 226 console.log('Testing Completed!');
nickjillings@1622 227 currentState = 'postTest';
nickjillings@1622 228 // Check for any post tests
nickjillings@1622 229 var xmlSetup = projectXML.find('setup');
nickjillings@1622 230 var postTest = xmlSetup.find('PostTest')[0];
nickjillings@1622 231 popup.initState(postTest);
nickjillings@1622 232 }
nickjillings@1621 233 }
nickjillings@1621 234
b@1608 235 function loadProjectSpec(url) {
b@1608 236 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
b@1608 237 // If url is null, request client to upload project XML document
b@1608 238 var r = new XMLHttpRequest();
b@1608 239 r.open('GET',url,true);
b@1608 240 r.onload = function() {
b@1608 241 loadProjectSpecCallback(r.response);
b@1608 242 };
b@1608 243 r.send();
b@1608 244 };
b@1608 245
b@1608 246 function loadProjectSpecCallback(response) {
b@1608 247 // Function called after asynchronous download of XML project specification
b@1608 248 var decode = $.parseXML(response);
b@1608 249 projectXML = $(decode);
b@1608 250
b@1608 251 // Now extract the setup tag
b@1608 252 var xmlSetup = projectXML.find('setup');
b@1608 253 // Detect the interface to use and load the relevant javascripts.
b@1608 254 var interfaceType = xmlSetup[0].attributes['interface'];
b@1608 255 var interfaceJS = document.createElement('script');
b@1608 256 interfaceJS.setAttribute("type","text/javascript");
b@1608 257 if (interfaceType.value == 'APE') {
b@1608 258 interfaceJS.setAttribute("src","ape.js");
b@1608 259
b@1608 260 // APE comes with a css file
b@1608 261 var css = document.createElement('link');
b@1608 262 css.rel = 'stylesheet';
b@1608 263 css.type = 'text/css';
b@1608 264 css.href = 'ape.css';
b@1608 265
b@1608 266 document.getElementsByTagName("head")[0].appendChild(css);
b@1608 267 }
b@1608 268 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
b@1608 269 }
b@1608 270
b@1608 271 function createProjectSave(destURL) {
b@1608 272 // Save the data from interface into XML and send to destURL
b@1608 273 // If destURL is null then download XML in client
b@1608 274 // Now time to render file locally
b@1608 275 var xmlDoc = interfaceXMLSave();
b@1608 276 if (destURL == "null" || destURL == undefined) {
b@1608 277 var parent = document.createElement("div");
b@1608 278 parent.appendChild(xmlDoc);
b@1608 279 var file = [parent.innerHTML];
b@1608 280 var bb = new Blob(file,{type : 'application/xml'});
b@1608 281 var dnlk = window.URL.createObjectURL(bb);
b@1608 282 var a = document.createElement("a");
b@1608 283 a.hidden = '';
b@1608 284 a.href = dnlk;
b@1608 285 a.download = "save.xml";
b@1608 286 a.textContent = "Save File";
b@1608 287
b@1608 288 var submitDiv = document.getElementById('download-point');
b@1608 289 submitDiv.appendChild(a);
b@1608 290 }
b@1608 291 return submitDiv;
b@1608 292 }
b@1608 293
b@1608 294 function AudioEngine() {
b@1608 295
b@1608 296 // Create two output paths, the main outputGain and fooGain.
b@1608 297 // Output gain is default to 1 and any items for playback route here
b@1608 298 // Foo gain is used for analysis to ensure paths get processed, but are not heard
b@1608 299 // because web audio will optimise and any route which does not go to the destination gets ignored.
b@1608 300 this.outputGain = audioContext.createGain();
b@1608 301 this.fooGain = audioContext.createGain();
b@1608 302 this.fooGain.gain = 0;
b@1608 303
b@1608 304 // Use this to detect playback state: 0 - stopped, 1 - playing
b@1608 305 this.status = 0;
nickjillings@1620 306 this.audioObjectsReady = false;
b@1608 307
b@1608 308 // Connect both gains to output
b@1608 309 this.outputGain.connect(audioContext.destination);
b@1608 310 this.fooGain.connect(audioContext.destination);
b@1608 311
b@1608 312 // Create the timer Object
b@1608 313 this.timer = new timer();
b@1608 314 // Create session metrics
b@1608 315 this.metric = new sessionMetrics(this);
b@1608 316
b@1608 317 this.loopPlayback = false;
b@1608 318
b@1608 319 // Create store for new audioObjects
b@1608 320 this.audioObjects = [];
b@1608 321
nickjillings@1620 322 this.play = function() {
nickjillings@1620 323 // Start the timer and set the audioEngine state to playing (1)
nickjillings@1620 324 if (this.status == 0) {
nickjillings@1620 325 // Check if all audioObjects are ready
nickjillings@1620 326 if (this.audioObjectsReady == false) {
nickjillings@1620 327 this.audioObjectsReady = this.checkAllReady();
nickjillings@1620 328 }
nickjillings@1620 329 if (this.audioObjectsReady == true) {
nickjillings@1620 330 this.timer.startTest();
nickjillings@1620 331 this.status = 1;
nickjillings@1620 332 }
nickjillings@1620 333 }
nickjillings@1620 334 };
b@1608 335
nickjillings@1620 336 this.stop = function() {
nickjillings@1620 337 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
nickjillings@1620 338 if (this.status == 1) {
nickjillings@1620 339 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1620 340 {
nickjillings@1620 341 this.audioObjects[i].stop();
nickjillings@1620 342 }
nickjillings@1620 343 this.status = 0;
nickjillings@1620 344 }
nickjillings@1620 345 };
b@1608 346
b@1608 347
b@1608 348 this.newTrack = function(url) {
b@1608 349 // Pull data from given URL into new audio buffer
b@1608 350 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
b@1608 351
b@1608 352 // Create the audioObject with ID of the new track length;
b@1608 353 audioObjectId = this.audioObjects.length;
b@1608 354 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
b@1608 355
b@1608 356 // AudioObject will get track itself.
b@1608 357 this.audioObjects[audioObjectId].constructTrack(url);
b@1608 358 };
b@1608 359
nickjillings@1620 360 this.newTestPage = function() {
nickjillings@1620 361 this.state = 0;
nickjillings@1620 362 this.audioObjectsReady = false;
nickjillings@1620 363 this.metric.reset();
nickjillings@1620 364 this.audioObjects = [];
nickjillings@1620 365 };
nickjillings@1620 366
nickjillings@1614 367 this.checkAllPlayed = function() {
nickjillings@1614 368 arr = [];
nickjillings@1614 369 for (var id=0; id<this.audioObjects.length; id++) {
nickjillings@1614 370 if (this.audioObjects[id].played == false) {
nickjillings@1614 371 arr.push(this.audioObjects[id].id);
nickjillings@1614 372 }
nickjillings@1614 373 }
nickjillings@1614 374 return arr;
nickjillings@1614 375 };
nickjillings@1614 376
nickjillings@1620 377 this.checkAllReady = function() {
nickjillings@1620 378 var ready = true;
nickjillings@1620 379 for (var i=0; i<this.audioObjects.length; i++) {
nickjillings@1620 380 if (this.audioObjects[i].state == 0) {
nickjillings@1620 381 // Track not ready
nickjillings@1620 382 console.log('WAIT -- audioObject '+i+' not ready yet!');
nickjillings@1620 383 ready = false;
nickjillings@1620 384 };
nickjillings@1620 385 }
nickjillings@1620 386 return ready;
nickjillings@1620 387 };
nickjillings@1620 388
b@1608 389 }
b@1608 390
b@1608 391 function audioObject(id) {
b@1608 392 // The main buffer object with common control nodes to the AudioEngine
b@1608 393
b@1608 394 this.id = id;
b@1608 395 this.state = 0; // 0 - no data, 1 - ready
b@1608 396 this.url = null; // Hold the URL given for the output back to the results.
b@1608 397 this.metric = new metricTracker();
b@1608 398
nickjillings@1614 399 this.played = false;
nickjillings@1614 400
b@1608 401 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
b@1608 402 this.bufferNode = undefined;
b@1608 403 this.outputGain = audioContext.createGain();
b@1608 404
b@1608 405 // Default output gain to be zero
b@1608 406 this.outputGain.gain.value = 0.0;
b@1608 407
b@1608 408 // Connect buffer to the audio graph
b@1608 409 this.outputGain.connect(audioEngineContext.outputGain);
b@1608 410
b@1608 411 // the audiobuffer is not designed for multi-start playback
b@1608 412 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
b@1608 413 this.buffer;
b@1608 414
b@1608 415 this.play = function(startTime) {
b@1608 416 this.bufferNode = audioContext.createBufferSource();
nickjillings@1617 417 this.bufferNode.owner = this;
b@1608 418 this.bufferNode.connect(this.outputGain);
b@1608 419 this.bufferNode.buffer = this.buffer;
b@1608 420 this.bufferNode.loop = audioEngineContext.loopPlayback;
nickjillings@1617 421 if (this.bufferNode.loop == false) {
nickjillings@1617 422 this.bufferNode.onended = function() {
nickjillings@1617 423 this.owner.metric.listening(audioEngineContext.timer.getTestTime());
nickjillings@1620 424 };
nickjillings@1617 425 }
nickjillings@1617 426 this.metric.listening(audioEngineContext.timer.getTestTime());
b@1608 427 this.bufferNode.start(startTime);
nickjillings@1614 428 this.played = true;
b@1608 429 };
b@1608 430
b@1608 431 this.stop = function() {
b@1607 432 if (this.bufferNode != undefined)
b@1607 433 {
b@1607 434 this.bufferNode.stop(0);
b@1607 435 this.bufferNode = undefined;
nickjillings@1617 436 this.metric.listening(audioEngineContext.timer.getTestTime());
b@1607 437 }
b@1608 438 };
b@1608 439
b@1608 440 this.constructTrack = function(url) {
b@1608 441 var request = new XMLHttpRequest();
b@1608 442 this.url = url;
b@1608 443 request.open('GET',url,true);
b@1608 444 request.responseType = 'arraybuffer';
b@1608 445
b@1608 446 var audioObj = this;
b@1608 447
b@1608 448 // Create callback to decode the data asynchronously
b@1608 449 request.onloadend = function() {
b@1608 450 audioContext.decodeAudioData(request.response, function(decodedData) {
b@1608 451 audioObj.buffer = decodedData;
b@1608 452 audioObj.state = 1;
b@1608 453 }, function(){
b@1608 454 // Should only be called if there was an error, but sometimes gets called continuously
b@1608 455 // Check here if the error is genuine
b@1608 456 if (audioObj.state == 0 || audioObj.buffer == undefined) {
b@1608 457 // Genuine error
b@1608 458 console.log('FATAL - Error loading buffer on '+audioObj.id);
b@1608 459 }
b@1608 460 });
b@1608 461 };
b@1608 462 request.send();
b@1608 463 };
b@1608 464
b@1608 465 }
b@1608 466
b@1608 467 function timer()
b@1608 468 {
b@1608 469 /* Timer object used in audioEngine to keep track of session timings
b@1608 470 * Uses the timer of the web audio API, so sample resolution
b@1608 471 */
b@1608 472 this.testStarted = false;
b@1608 473 this.testStartTime = 0;
b@1608 474 this.testDuration = 0;
b@1608 475 this.minimumTestTime = 0; // No minimum test time
b@1608 476 this.startTest = function()
b@1608 477 {
b@1608 478 if (this.testStarted == false)
b@1608 479 {
b@1608 480 this.testStartTime = audioContext.currentTime;
b@1608 481 this.testStarted = true;
b@1608 482 this.updateTestTime();
b@1608 483 audioEngineContext.metric.initialiseTest();
b@1608 484 }
b@1608 485 };
b@1608 486 this.stopTest = function()
b@1608 487 {
b@1608 488 if (this.testStarted)
b@1608 489 {
b@1608 490 this.testDuration = this.getTestTime();
b@1608 491 this.testStarted = false;
b@1608 492 } else {
b@1608 493 console.log('ERR: Test tried to end before beginning');
b@1608 494 }
b@1608 495 };
b@1608 496 this.updateTestTime = function()
b@1608 497 {
b@1608 498 if (this.testStarted)
b@1608 499 {
b@1608 500 this.testDuration = audioContext.currentTime - this.testStartTime;
b@1608 501 }
b@1608 502 };
b@1608 503 this.getTestTime = function()
b@1608 504 {
b@1608 505 this.updateTestTime();
b@1608 506 return this.testDuration;
b@1608 507 };
b@1608 508 }
b@1608 509
b@1608 510 function sessionMetrics(engine)
b@1608 511 {
b@1608 512 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
b@1608 513 */
b@1608 514 this.engine = engine;
b@1608 515 this.lastClicked = -1;
b@1608 516 this.data = -1;
nickjillings@1620 517 this.reset = function() {
nickjillings@1620 518 this.lastClicked = -1;
nickjillings@1620 519 this.data = -1;
nickjillings@1620 520 };
b@1608 521 this.initialiseTest = function(){};
b@1608 522 }
b@1608 523
b@1608 524 function metricTracker()
b@1608 525 {
b@1608 526 /* Custom object to track and collect metric data
b@1608 527 * Used only inside the audioObjects object.
b@1608 528 */
b@1608 529
b@1608 530 this.listenedTimer = 0;
b@1608 531 this.listenStart = 0;
nickjillings@1617 532 this.listenHold = false;
b@1608 533 this.initialPosition = -1;
b@1608 534 this.movementTracker = [];
b@1608 535 this.wasListenedTo = false;
b@1608 536 this.wasMoved = false;
b@1608 537 this.hasComments = false;
b@1608 538
b@1608 539 this.initialised = function(position)
b@1608 540 {
b@1608 541 if (this.initialPosition == -1) {
b@1608 542 this.initialPosition = position;
b@1608 543 }
b@1608 544 };
b@1608 545
b@1608 546 this.moved = function(time,position)
b@1608 547 {
b@1608 548 this.wasMoved = true;
b@1608 549 this.movementTracker[this.movementTracker.length] = [time, position];
b@1608 550 };
b@1608 551
b@1608 552 this.listening = function(time)
b@1608 553 {
nickjillings@1617 554 if (this.listenHold == false)
b@1608 555 {
b@1608 556 this.wasListenedTo = true;
b@1608 557 this.listenStart = time;
nickjillings@1617 558 this.listenHold = true;
b@1608 559 } else {
b@1608 560 this.listenedTimer += (time - this.listenStart);
b@1608 561 this.listenStart = 0;
nickjillings@1617 562 this.listenHold = false;
b@1608 563 }
b@1608 564 };
b@1608 565 }
b@1608 566
b@1608 567 function randomiseOrder(input)
b@1608 568 {
b@1608 569 // This takes an array of information and randomises the order
b@1608 570 var N = input.length;
b@1608 571 var K = N;
b@1608 572 var holdArr = [];
b@1608 573 for (var n=0; n<N; n++)
b@1608 574 {
b@1608 575 // First pick a random number
b@1608 576 var r = Math.random();
b@1608 577 // Multiply and floor by the number of elements left
b@1608 578 r = Math.floor(r*input.length);
b@1608 579 // Pick out that element and delete from the array
b@1608 580 holdArr.push(input.splice(r,1)[0]);
b@1608 581 }
b@1608 582 return holdArr;
b@1608 583 }