annotate core.js @ 1631:91a75ec7a7c0

Feature #1245: Added DateTime node to XML output
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Wed, 27 May 2015 09:46:06 +0100
parents 21381e163787
children ba734075da2d
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@1623 155 console.log("Question: "+ node.textContent);
nickjillings@1623 156 console.log("Question Response: "+ textArea.value);
nickjillings@1622 157 this.responses.appendChild(hold);
nickjillings@1622 158 }
nickjillings@1622 159 }
nickjillings@1622 160 this.currentIndex++;
nickjillings@1622 161 if (this.currentIndex < this.popupOptions.length) {
nickjillings@1622 162 this.postNode();
nickjillings@1622 163 } else {
nickjillings@1622 164 // Reached the end of the popupOptions
nickjillings@1622 165 this.hidePopup();
nickjillings@1622 166 advanceState();
nickjillings@1622 167 }
nickjillings@1622 168 }
nickjillings@1621 169 }
nickjillings@1621 170
nickjillings@1622 171 function advanceState()
nickjillings@1621 172 {
nickjillings@1622 173 console.log(currentState);
nickjillings@1622 174 if (currentState == 'preTest')
nickjillings@1622 175 {
nickjillings@1622 176 // End of pre-test, begin the test
nickjillings@1622 177 preTestQuestions = popup.responses;
nickjillings@1622 178 loadTest(0);
nickjillings@1622 179 } else if (currentState == 'postTest') {
nickjillings@1622 180 postTestQuestions = popup.responses;
nickjillings@1624 181 console.log('ALL COLLECTED!');
nickjillings@1624 182 createProjectSave(projectReturn);
nickjillings@1622 183 }else if (currentState.substr(0,10) == 'testRunPre')
nickjillings@1622 184 {
nickjillings@1622 185 // Start the test
nickjillings@1622 186 var testId = currentState.substr(11,currentState.length-10);
nickjillings@1622 187 currentState = 'testRun-'+testId;
nickjillings@1622 188 currentTestHolder.appendChild(popup.responses);
nickjillings@1622 189 //audioEngineContext.timer.startTest();
nickjillings@1622 190 //audioEngineContext.play();
nickjillings@1622 191 } else if (currentState.substr(0,11) == 'testRunPost')
nickjillings@1622 192 {
nickjillings@1622 193 var testId = currentState.substr(12,currentState.length-11);
nickjillings@1622 194 currentTestHolder.appendChild(popup.responses);
nickjillings@1622 195 testEnded(testId);
nickjillings@1622 196 } else if (currentState.substr(0,7) == 'testRun')
nickjillings@1622 197 {
nickjillings@1622 198 var testId = currentState.substr(8,currentState.length-7);
nickjillings@1622 199 // Check if we have any post tests to perform
nickjillings@1622 200 var postXML = $(testXMLSetups[testId]).find('PostTest')[0];
nickjillings@1622 201 if (postXML == undefined || postXML.childElementCount == 0) {
nickjillings@1622 202 testEnded(testId);
nickjillings@1622 203 }
nickjillings@1622 204 else if (postXML.childElementCount > 0)
nickjillings@1622 205 {
nickjillings@1622 206 currentState = 'testRunPost-'+testId;
nickjillings@1622 207 popup.initState(postXML);
nickjillings@1622 208 }
nickjillings@1621 209 }
nickjillings@1622 210 console.log(currentState);
nickjillings@1621 211 }
nickjillings@1621 212
nickjillings@1622 213 function testEnded(testId)
nickjillings@1621 214 {
nickjillings@1622 215 pageXMLSave(testId);
nickjillings@1622 216 if (testXMLSetups.length-1 > testId)
nickjillings@1622 217 {
nickjillings@1622 218 // Yes we have another test to perform
nickjillings@1622 219 testId = (Number(testId)+1);
nickjillings@1622 220 currentState = 'testRun-'+testId;
nickjillings@1622 221 loadTest(testId);
nickjillings@1622 222 } else {
nickjillings@1622 223 console.log('Testing Completed!');
nickjillings@1622 224 currentState = 'postTest';
nickjillings@1622 225 // Check for any post tests
nickjillings@1622 226 var xmlSetup = projectXML.find('setup');
nickjillings@1622 227 var postTest = xmlSetup.find('PostTest')[0];
nickjillings@1622 228 popup.initState(postTest);
nickjillings@1622 229 }
nickjillings@1621 230 }
nickjillings@1621 231
b@1608 232 function loadProjectSpec(url) {
b@1608 233 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
b@1608 234 // If url is null, request client to upload project XML document
b@1608 235 var r = new XMLHttpRequest();
b@1608 236 r.open('GET',url,true);
b@1608 237 r.onload = function() {
b@1608 238 loadProjectSpecCallback(r.response);
b@1608 239 };
b@1608 240 r.send();
b@1608 241 };
b@1608 242
b@1608 243 function loadProjectSpecCallback(response) {
b@1608 244 // Function called after asynchronous download of XML project specification
b@1608 245 var decode = $.parseXML(response);
b@1608 246 projectXML = $(decode);
b@1608 247
b@1608 248 // Now extract the setup tag
b@1608 249 var xmlSetup = projectXML.find('setup');
b@1608 250 // Detect the interface to use and load the relevant javascripts.
b@1608 251 var interfaceType = xmlSetup[0].attributes['interface'];
b@1608 252 var interfaceJS = document.createElement('script');
b@1608 253 interfaceJS.setAttribute("type","text/javascript");
b@1608 254 if (interfaceType.value == 'APE') {
b@1608 255 interfaceJS.setAttribute("src","ape.js");
b@1608 256
b@1608 257 // APE comes with a css file
b@1608 258 var css = document.createElement('link');
b@1608 259 css.rel = 'stylesheet';
b@1608 260 css.type = 'text/css';
b@1608 261 css.href = 'ape.css';
b@1608 262
b@1608 263 document.getElementsByTagName("head")[0].appendChild(css);
b@1608 264 }
b@1608 265 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
b@1608 266 }
b@1608 267
b@1608 268 function createProjectSave(destURL) {
b@1608 269 // Save the data from interface into XML and send to destURL
b@1608 270 // If destURL is null then download XML in client
b@1608 271 // Now time to render file locally
b@1608 272 var xmlDoc = interfaceXMLSave();
nickjillings@1628 273 var parent = document.createElement("div");
nickjillings@1628 274 parent.appendChild(xmlDoc);
nickjillings@1628 275 var file = [parent.innerHTML];
b@1608 276 if (destURL == "null" || destURL == undefined) {
b@1608 277 var bb = new Blob(file,{type : 'application/xml'});
b@1608 278 var dnlk = window.URL.createObjectURL(bb);
b@1608 279 var a = document.createElement("a");
b@1608 280 a.hidden = '';
b@1608 281 a.href = dnlk;
b@1608 282 a.download = "save.xml";
b@1608 283 a.textContent = "Save File";
b@1608 284
b@1608 285 var submitDiv = document.getElementById('download-point');
b@1608 286 submitDiv.appendChild(a);
nickjillings@1624 287 popup.showPopup();
nickjillings@1624 288 popup.popupContent.innerHTML = null;
nickjillings@1624 289 popup.popupContent.appendChild(submitDiv)
nickjillings@1628 290 } else {
nickjillings@1628 291 var xmlhttp = new XMLHttpRequest;
nickjillings@1628 292 xmlhttp.open("POST",destURL,true);
nickjillings@1628 293 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
nickjillings@1629 294 xmlhttp.onerror = function(){
nickjillings@1629 295 console.log('Error saving file to server! Presenting download locally');
nickjillings@1629 296 createProjectSave(null);
nickjillings@1629 297 };
nickjillings@1628 298 xmlhttp.send(file);
b@1608 299 }
b@1608 300 return submitDiv;
b@1608 301 }
b@1608 302
b@1608 303 function AudioEngine() {
b@1608 304
b@1608 305 // Create two output paths, the main outputGain and fooGain.
b@1608 306 // Output gain is default to 1 and any items for playback route here
b@1608 307 // Foo gain is used for analysis to ensure paths get processed, but are not heard
b@1608 308 // because web audio will optimise and any route which does not go to the destination gets ignored.
b@1608 309 this.outputGain = audioContext.createGain();
b@1608 310 this.fooGain = audioContext.createGain();
b@1608 311 this.fooGain.gain = 0;
b@1608 312
b@1608 313 // Use this to detect playback state: 0 - stopped, 1 - playing
b@1608 314 this.status = 0;
nickjillings@1620 315 this.audioObjectsReady = false;
b@1608 316
b@1608 317 // Connect both gains to output
b@1608 318 this.outputGain.connect(audioContext.destination);
b@1608 319 this.fooGain.connect(audioContext.destination);
b@1608 320
b@1608 321 // Create the timer Object
b@1608 322 this.timer = new timer();
b@1608 323 // Create session metrics
b@1608 324 this.metric = new sessionMetrics(this);
b@1608 325
b@1608 326 this.loopPlayback = false;
b@1608 327
b@1608 328 // Create store for new audioObjects
b@1608 329 this.audioObjects = [];
b@1608 330
nickjillings@1620 331 this.play = function() {
nickjillings@1620 332 // Start the timer and set the audioEngine state to playing (1)
nickjillings@1620 333 if (this.status == 0) {
nickjillings@1620 334 // Check if all audioObjects are ready
nickjillings@1620 335 if (this.audioObjectsReady == false) {
nickjillings@1620 336 this.audioObjectsReady = this.checkAllReady();
nickjillings@1620 337 }
nickjillings@1620 338 if (this.audioObjectsReady == true) {
nickjillings@1620 339 this.timer.startTest();
nickjillings@1620 340 this.status = 1;
nickjillings@1620 341 }
nickjillings@1620 342 }
nickjillings@1620 343 };
b@1608 344
nickjillings@1620 345 this.stop = function() {
nickjillings@1620 346 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
nickjillings@1620 347 if (this.status == 1) {
nickjillings@1620 348 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1620 349 {
nickjillings@1620 350 this.audioObjects[i].stop();
nickjillings@1620 351 }
nickjillings@1620 352 this.status = 0;
nickjillings@1620 353 }
nickjillings@1620 354 };
b@1608 355
b@1608 356
b@1608 357 this.newTrack = function(url) {
b@1608 358 // Pull data from given URL into new audio buffer
b@1608 359 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
b@1608 360
b@1608 361 // Create the audioObject with ID of the new track length;
b@1608 362 audioObjectId = this.audioObjects.length;
b@1608 363 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
b@1608 364
b@1608 365 // AudioObject will get track itself.
b@1608 366 this.audioObjects[audioObjectId].constructTrack(url);
b@1608 367 };
b@1608 368
nickjillings@1620 369 this.newTestPage = function() {
nickjillings@1620 370 this.state = 0;
nickjillings@1620 371 this.audioObjectsReady = false;
nickjillings@1620 372 this.metric.reset();
nickjillings@1620 373 this.audioObjects = [];
nickjillings@1620 374 };
nickjillings@1620 375
nickjillings@1614 376 this.checkAllPlayed = function() {
nickjillings@1614 377 arr = [];
nickjillings@1614 378 for (var id=0; id<this.audioObjects.length; id++) {
nickjillings@1614 379 if (this.audioObjects[id].played == false) {
nickjillings@1614 380 arr.push(this.audioObjects[id].id);
nickjillings@1614 381 }
nickjillings@1614 382 }
nickjillings@1614 383 return arr;
nickjillings@1614 384 };
nickjillings@1614 385
nickjillings@1620 386 this.checkAllReady = function() {
nickjillings@1620 387 var ready = true;
nickjillings@1620 388 for (var i=0; i<this.audioObjects.length; i++) {
nickjillings@1620 389 if (this.audioObjects[i].state == 0) {
nickjillings@1620 390 // Track not ready
nickjillings@1620 391 console.log('WAIT -- audioObject '+i+' not ready yet!');
nickjillings@1620 392 ready = false;
nickjillings@1620 393 };
nickjillings@1620 394 }
nickjillings@1620 395 return ready;
nickjillings@1620 396 };
nickjillings@1620 397
b@1608 398 }
b@1608 399
b@1608 400 function audioObject(id) {
b@1608 401 // The main buffer object with common control nodes to the AudioEngine
b@1608 402
b@1608 403 this.id = id;
b@1608 404 this.state = 0; // 0 - no data, 1 - ready
b@1608 405 this.url = null; // Hold the URL given for the output back to the results.
b@1608 406 this.metric = new metricTracker();
b@1608 407
nickjillings@1614 408 this.played = false;
nickjillings@1614 409
b@1608 410 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
b@1608 411 this.bufferNode = undefined;
b@1608 412 this.outputGain = audioContext.createGain();
b@1608 413
b@1608 414 // Default output gain to be zero
b@1608 415 this.outputGain.gain.value = 0.0;
b@1608 416
b@1608 417 // Connect buffer to the audio graph
b@1608 418 this.outputGain.connect(audioEngineContext.outputGain);
b@1608 419
b@1608 420 // the audiobuffer is not designed for multi-start playback
b@1608 421 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
b@1608 422 this.buffer;
b@1608 423
b@1608 424 this.play = function(startTime) {
b@1608 425 this.bufferNode = audioContext.createBufferSource();
nickjillings@1617 426 this.bufferNode.owner = this;
b@1608 427 this.bufferNode.connect(this.outputGain);
b@1608 428 this.bufferNode.buffer = this.buffer;
b@1608 429 this.bufferNode.loop = audioEngineContext.loopPlayback;
nickjillings@1617 430 if (this.bufferNode.loop == false) {
nickjillings@1617 431 this.bufferNode.onended = function() {
nickjillings@1617 432 this.owner.metric.listening(audioEngineContext.timer.getTestTime());
nickjillings@1620 433 };
nickjillings@1617 434 }
nickjillings@1617 435 this.metric.listening(audioEngineContext.timer.getTestTime());
b@1608 436 this.bufferNode.start(startTime);
nickjillings@1614 437 this.played = true;
b@1608 438 };
b@1608 439
b@1608 440 this.stop = function() {
b@1607 441 if (this.bufferNode != undefined)
b@1607 442 {
b@1607 443 this.bufferNode.stop(0);
b@1607 444 this.bufferNode = undefined;
nickjillings@1617 445 this.metric.listening(audioEngineContext.timer.getTestTime());
b@1607 446 }
b@1608 447 };
b@1608 448
b@1608 449 this.constructTrack = function(url) {
b@1608 450 var request = new XMLHttpRequest();
b@1608 451 this.url = url;
b@1608 452 request.open('GET',url,true);
b@1608 453 request.responseType = 'arraybuffer';
b@1608 454
b@1608 455 var audioObj = this;
b@1608 456
b@1608 457 // Create callback to decode the data asynchronously
b@1608 458 request.onloadend = function() {
b@1608 459 audioContext.decodeAudioData(request.response, function(decodedData) {
b@1608 460 audioObj.buffer = decodedData;
b@1608 461 audioObj.state = 1;
b@1608 462 }, function(){
b@1608 463 // Should only be called if there was an error, but sometimes gets called continuously
b@1608 464 // Check here if the error is genuine
b@1608 465 if (audioObj.state == 0 || audioObj.buffer == undefined) {
b@1608 466 // Genuine error
b@1608 467 console.log('FATAL - Error loading buffer on '+audioObj.id);
b@1608 468 }
b@1608 469 });
b@1608 470 };
b@1608 471 request.send();
b@1608 472 };
b@1608 473
b@1608 474 }
b@1608 475
b@1608 476 function timer()
b@1608 477 {
b@1608 478 /* Timer object used in audioEngine to keep track of session timings
b@1608 479 * Uses the timer of the web audio API, so sample resolution
b@1608 480 */
b@1608 481 this.testStarted = false;
b@1608 482 this.testStartTime = 0;
b@1608 483 this.testDuration = 0;
b@1608 484 this.minimumTestTime = 0; // No minimum test time
b@1608 485 this.startTest = function()
b@1608 486 {
b@1608 487 if (this.testStarted == false)
b@1608 488 {
b@1608 489 this.testStartTime = audioContext.currentTime;
b@1608 490 this.testStarted = true;
b@1608 491 this.updateTestTime();
b@1608 492 audioEngineContext.metric.initialiseTest();
b@1608 493 }
b@1608 494 };
b@1608 495 this.stopTest = function()
b@1608 496 {
b@1608 497 if (this.testStarted)
b@1608 498 {
b@1608 499 this.testDuration = this.getTestTime();
b@1608 500 this.testStarted = false;
b@1608 501 } else {
b@1608 502 console.log('ERR: Test tried to end before beginning');
b@1608 503 }
b@1608 504 };
b@1608 505 this.updateTestTime = function()
b@1608 506 {
b@1608 507 if (this.testStarted)
b@1608 508 {
b@1608 509 this.testDuration = audioContext.currentTime - this.testStartTime;
b@1608 510 }
b@1608 511 };
b@1608 512 this.getTestTime = function()
b@1608 513 {
b@1608 514 this.updateTestTime();
b@1608 515 return this.testDuration;
b@1608 516 };
b@1608 517 }
b@1608 518
b@1608 519 function sessionMetrics(engine)
b@1608 520 {
b@1608 521 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
b@1608 522 */
b@1608 523 this.engine = engine;
b@1608 524 this.lastClicked = -1;
b@1608 525 this.data = -1;
nickjillings@1620 526 this.reset = function() {
nickjillings@1620 527 this.lastClicked = -1;
nickjillings@1620 528 this.data = -1;
nickjillings@1620 529 };
b@1608 530 this.initialiseTest = function(){};
b@1608 531 }
b@1608 532
b@1608 533 function metricTracker()
b@1608 534 {
b@1608 535 /* Custom object to track and collect metric data
b@1608 536 * Used only inside the audioObjects object.
b@1608 537 */
b@1608 538
b@1608 539 this.listenedTimer = 0;
b@1608 540 this.listenStart = 0;
nickjillings@1617 541 this.listenHold = false;
b@1608 542 this.initialPosition = -1;
b@1608 543 this.movementTracker = [];
b@1608 544 this.wasListenedTo = false;
b@1608 545 this.wasMoved = false;
b@1608 546 this.hasComments = false;
b@1608 547
b@1608 548 this.initialised = function(position)
b@1608 549 {
b@1608 550 if (this.initialPosition == -1) {
b@1608 551 this.initialPosition = position;
b@1608 552 }
b@1608 553 };
b@1608 554
b@1608 555 this.moved = function(time,position)
b@1608 556 {
b@1608 557 this.wasMoved = true;
b@1608 558 this.movementTracker[this.movementTracker.length] = [time, position];
b@1608 559 };
b@1608 560
b@1608 561 this.listening = function(time)
b@1608 562 {
nickjillings@1617 563 if (this.listenHold == false)
b@1608 564 {
b@1608 565 this.wasListenedTo = true;
b@1608 566 this.listenStart = time;
nickjillings@1617 567 this.listenHold = true;
b@1608 568 } else {
b@1608 569 this.listenedTimer += (time - this.listenStart);
b@1608 570 this.listenStart = 0;
nickjillings@1617 571 this.listenHold = false;
b@1608 572 }
b@1608 573 };
b@1608 574 }
b@1608 575
b@1608 576 function randomiseOrder(input)
b@1608 577 {
b@1608 578 // This takes an array of information and randomises the order
b@1608 579 var N = input.length;
b@1608 580 var K = N;
b@1608 581 var holdArr = [];
b@1608 582 for (var n=0; n<N; n++)
b@1608 583 {
b@1608 584 // First pick a random number
b@1608 585 var r = Math.random();
b@1608 586 // Multiply and floor by the number of elements left
b@1608 587 r = Math.floor(r*input.length);
b@1608 588 // Pick out that element and delete from the array
b@1608 589 holdArr.push(input.splice(r,1)[0]);
b@1608 590 }
b@1608 591 return holdArr;
nickjillings@1631 592 }
nickjillings@1631 593
nickjillings@1631 594 function returnDateNode()
nickjillings@1631 595 {
nickjillings@1631 596 // Create an XML Node for the Date and Time a test was conducted
nickjillings@1631 597 // Structure is
nickjillings@1631 598 // <datetime>
nickjillings@1631 599 // <date year="##" month="##" day="##">DD/MM/YY</date>
nickjillings@1631 600 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
nickjillings@1631 601 // </datetime>
nickjillings@1631 602 var dateTime = new Date();
nickjillings@1631 603 var year = document.createAttribute('year');
nickjillings@1631 604 var month = document.createAttribute('month');
nickjillings@1631 605 var day = document.createAttribute('day');
nickjillings@1631 606 var hour = document.createAttribute('hour');
nickjillings@1631 607 var minute = document.createAttribute('minute');
nickjillings@1631 608 var secs = document.createAttribute('secs');
nickjillings@1631 609
nickjillings@1631 610 year.nodeValue = dateTime.getFullYear();
nickjillings@1631 611 month.nodeValue = dateTime.getMonth()+1;
nickjillings@1631 612 day.nodeValue = dateTime.getDate();
nickjillings@1631 613 hour.nodeValue = dateTime.getHours();
nickjillings@1631 614 minute.nodeValue = dateTime.getMinutes();
nickjillings@1631 615 secs.nodeValue = dateTime.getSeconds();
nickjillings@1631 616
nickjillings@1631 617 var hold = document.createElement("datetime");
nickjillings@1631 618 var date = document.createElement("date");
nickjillings@1631 619 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
nickjillings@1631 620 var time = document.createElement("time");
nickjillings@1631 621 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
nickjillings@1631 622
nickjillings@1631 623 date.setAttributeNode(year);
nickjillings@1631 624 date.setAttributeNode(month);
nickjillings@1631 625 date.setAttributeNode(day);
nickjillings@1631 626 time.setAttributeNode(hour);
nickjillings@1631 627 time.setAttributeNode(minute);
nickjillings@1631 628 time.setAttributeNode(secs);
nickjillings@1631 629
nickjillings@1631 630 hold.appendChild(date);
nickjillings@1631 631 hold.appendChild(time);
nickjillings@1631 632 return hold
nickjillings@1631 633
nickjillings@1631 634 }