annotate core.js @ 959:d6303bdef165

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