annotate core.js @ 1617:ed5b4a9b266a

Possible fix for Bug #1238. audioObject metric collection now controlled by the audioObjects themselves for timer information. Lastclicked and sliderPlayed functions no longer used.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Sun, 24 May 2015 11:33:04 +0100
parents 4f3ddd805ff3
children 05e7edb032b9
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
b@1608 11
b@1608 12 var testXMLSetups = []; // Hold the parsed test instances
b@1608 13 var testResultsHolders =[]; // Hold the results from each test for publishing to XML
b@1608 14 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
b@1608 15 var currentTestHolder; // Hold any intermediate results during test - metrics
b@1608 16 var audioEngineContext; // The custome AudioEngine object
b@1608 17 var projectReturn; // Hold the URL for the return
b@1608 18 var preTestQuestions = document.createElement('PreTest'); // Store any pre-test question response
b@1608 19 var postTestQuestions = document.createElement('PostTest'); // Store any post-test question response
b@1608 20
b@1608 21 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
b@1608 22 AudioBufferSourceNode.prototype.owner = undefined;
b@1608 23
b@1608 24 window.onload = function() {
b@1608 25 // Function called once the browser has loaded all files.
b@1608 26 // This should perform any initial commands such as structure / loading documents
b@1608 27
b@1608 28 // Create a web audio API context
b@1608 29 // Fixed for cross-browser support
b@1608 30 var AudioContext = window.AudioContext || window.webkitAudioContext;
b@1608 31 audioContext = new AudioContext;
b@1608 32
b@1608 33 // Create the audio engine object
b@1608 34 audioEngineContext = new AudioEngine();
b@1608 35 };
b@1608 36
b@1608 37 function loadProjectSpec(url) {
b@1608 38 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
b@1608 39 // If url is null, request client to upload project XML document
b@1608 40 var r = new XMLHttpRequest();
b@1608 41 r.open('GET',url,true);
b@1608 42 r.onload = function() {
b@1608 43 loadProjectSpecCallback(r.response);
b@1608 44 };
b@1608 45 r.send();
b@1608 46 };
b@1608 47
b@1608 48 function loadProjectSpecCallback(response) {
b@1608 49 // Function called after asynchronous download of XML project specification
b@1608 50 var decode = $.parseXML(response);
b@1608 51 projectXML = $(decode);
b@1608 52
b@1608 53 // Now extract the setup tag
b@1608 54 var xmlSetup = projectXML.find('setup');
b@1608 55 // Detect the interface to use and load the relevant javascripts.
b@1608 56 var interfaceType = xmlSetup[0].attributes['interface'];
b@1608 57 var interfaceJS = document.createElement('script');
b@1608 58 interfaceJS.setAttribute("type","text/javascript");
b@1608 59 if (interfaceType.value == 'APE') {
b@1608 60 interfaceJS.setAttribute("src","ape.js");
b@1608 61
b@1608 62 // APE comes with a css file
b@1608 63 var css = document.createElement('link');
b@1608 64 css.rel = 'stylesheet';
b@1608 65 css.type = 'text/css';
b@1608 66 css.href = 'ape.css';
b@1608 67
b@1608 68 document.getElementsByTagName("head")[0].appendChild(css);
b@1608 69 }
b@1608 70 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
b@1608 71 }
b@1608 72
b@1608 73 function createProjectSave(destURL) {
b@1608 74 // Save the data from interface into XML and send to destURL
b@1608 75 // If destURL is null then download XML in client
b@1608 76 // Now time to render file locally
b@1608 77 var xmlDoc = interfaceXMLSave();
b@1608 78 if (destURL == "null" || destURL == undefined) {
b@1608 79 var parent = document.createElement("div");
b@1608 80 parent.appendChild(xmlDoc);
b@1608 81 var file = [parent.innerHTML];
b@1608 82 var bb = new Blob(file,{type : 'application/xml'});
b@1608 83 var dnlk = window.URL.createObjectURL(bb);
b@1608 84 var a = document.createElement("a");
b@1608 85 a.hidden = '';
b@1608 86 a.href = dnlk;
b@1608 87 a.download = "save.xml";
b@1608 88 a.textContent = "Save File";
b@1608 89
b@1608 90 var submitDiv = document.getElementById('download-point');
b@1608 91 submitDiv.appendChild(a);
b@1608 92 }
b@1608 93 return submitDiv;
b@1608 94 }
b@1608 95
b@1608 96 function AudioEngine() {
b@1608 97
b@1608 98 // Create two output paths, the main outputGain and fooGain.
b@1608 99 // Output gain is default to 1 and any items for playback route here
b@1608 100 // Foo gain is used for analysis to ensure paths get processed, but are not heard
b@1608 101 // because web audio will optimise and any route which does not go to the destination gets ignored.
b@1608 102 this.outputGain = audioContext.createGain();
b@1608 103 this.fooGain = audioContext.createGain();
b@1608 104 this.fooGain.gain = 0;
b@1608 105
b@1608 106 // Use this to detect playback state: 0 - stopped, 1 - playing
b@1608 107 this.status = 0;
b@1608 108
b@1608 109 // Connect both gains to output
b@1608 110 this.outputGain.connect(audioContext.destination);
b@1608 111 this.fooGain.connect(audioContext.destination);
b@1608 112
b@1608 113 // Create the timer Object
b@1608 114 this.timer = new timer();
b@1608 115 // Create session metrics
b@1608 116 this.metric = new sessionMetrics(this);
b@1608 117
b@1608 118 this.loopPlayback = false;
b@1608 119
b@1608 120 // Create store for new audioObjects
b@1608 121 this.audioObjects = [];
b@1608 122
b@1608 123 this.play = function(){};
b@1608 124
b@1608 125 this.stop = function(){};
b@1608 126
b@1608 127
b@1608 128 this.newTrack = function(url) {
b@1608 129 // Pull data from given URL into new audio buffer
b@1608 130 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
b@1608 131
b@1608 132 // Create the audioObject with ID of the new track length;
b@1608 133 audioObjectId = this.audioObjects.length;
b@1608 134 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
b@1608 135
b@1608 136 // AudioObject will get track itself.
b@1608 137 this.audioObjects[audioObjectId].constructTrack(url);
b@1608 138 };
b@1608 139
nickjillings@1614 140 this.checkAllPlayed = function() {
nickjillings@1614 141 arr = [];
nickjillings@1614 142 for (var id=0; id<this.audioObjects.length; id++) {
nickjillings@1614 143 if (this.audioObjects[id].played == false) {
nickjillings@1614 144 arr.push(this.audioObjects[id].id);
nickjillings@1614 145 }
nickjillings@1614 146 }
nickjillings@1614 147 return arr;
nickjillings@1614 148 };
nickjillings@1614 149
b@1608 150 }
b@1608 151
b@1608 152 function audioObject(id) {
b@1608 153 // The main buffer object with common control nodes to the AudioEngine
b@1608 154
b@1608 155 this.id = id;
b@1608 156 this.state = 0; // 0 - no data, 1 - ready
b@1608 157 this.url = null; // Hold the URL given for the output back to the results.
b@1608 158 this.metric = new metricTracker();
b@1608 159
nickjillings@1614 160 this.played = false;
nickjillings@1614 161
b@1608 162 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
b@1608 163 this.bufferNode = undefined;
b@1608 164 this.outputGain = audioContext.createGain();
b@1608 165
b@1608 166 // Default output gain to be zero
b@1608 167 this.outputGain.gain.value = 0.0;
b@1608 168
b@1608 169 // Connect buffer to the audio graph
b@1608 170 this.outputGain.connect(audioEngineContext.outputGain);
b@1608 171
b@1608 172 // the audiobuffer is not designed for multi-start playback
b@1608 173 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
b@1608 174 this.buffer;
b@1608 175
b@1608 176 this.play = function(startTime) {
b@1608 177 this.bufferNode = audioContext.createBufferSource();
nickjillings@1617 178 this.bufferNode.owner = this;
b@1608 179 this.bufferNode.connect(this.outputGain);
b@1608 180 this.bufferNode.buffer = this.buffer;
b@1608 181 this.bufferNode.loop = audioEngineContext.loopPlayback;
nickjillings@1617 182 if (this.bufferNode.loop == false) {
nickjillings@1617 183 this.bufferNode.onended = function() {
nickjillings@1617 184 this.owner.metric.listening(audioEngineContext.timer.getTestTime());
nickjillings@1617 185 }
nickjillings@1617 186 }
nickjillings@1617 187 this.metric.listening(audioEngineContext.timer.getTestTime());
b@1608 188 this.bufferNode.start(startTime);
nickjillings@1614 189 this.played = true;
b@1608 190 };
b@1608 191
b@1608 192 this.stop = function() {
b@1607 193 if (this.bufferNode != undefined)
b@1607 194 {
b@1607 195 this.bufferNode.stop(0);
b@1607 196 this.bufferNode = undefined;
nickjillings@1617 197 this.metric.listening(audioEngineContext.timer.getTestTime());
b@1607 198 }
b@1608 199 };
b@1608 200
b@1608 201 this.constructTrack = function(url) {
b@1608 202 var request = new XMLHttpRequest();
b@1608 203 this.url = url;
b@1608 204 request.open('GET',url,true);
b@1608 205 request.responseType = 'arraybuffer';
b@1608 206
b@1608 207 var audioObj = this;
b@1608 208
b@1608 209 // Create callback to decode the data asynchronously
b@1608 210 request.onloadend = function() {
b@1608 211 audioContext.decodeAudioData(request.response, function(decodedData) {
b@1608 212 audioObj.buffer = decodedData;
b@1608 213 audioObj.state = 1;
b@1608 214 }, function(){
b@1608 215 // Should only be called if there was an error, but sometimes gets called continuously
b@1608 216 // Check here if the error is genuine
b@1608 217 if (audioObj.state == 0 || audioObj.buffer == undefined) {
b@1608 218 // Genuine error
b@1608 219 console.log('FATAL - Error loading buffer on '+audioObj.id);
b@1608 220 }
b@1608 221 });
b@1608 222 };
b@1608 223 request.send();
b@1608 224 };
b@1608 225
b@1608 226 }
b@1608 227
b@1608 228 function timer()
b@1608 229 {
b@1608 230 /* Timer object used in audioEngine to keep track of session timings
b@1608 231 * Uses the timer of the web audio API, so sample resolution
b@1608 232 */
b@1608 233 this.testStarted = false;
b@1608 234 this.testStartTime = 0;
b@1608 235 this.testDuration = 0;
b@1608 236 this.minimumTestTime = 0; // No minimum test time
b@1608 237 this.startTest = function()
b@1608 238 {
b@1608 239 if (this.testStarted == false)
b@1608 240 {
b@1608 241 this.testStartTime = audioContext.currentTime;
b@1608 242 this.testStarted = true;
b@1608 243 this.updateTestTime();
b@1608 244 audioEngineContext.metric.initialiseTest();
b@1608 245 }
b@1608 246 };
b@1608 247 this.stopTest = function()
b@1608 248 {
b@1608 249 if (this.testStarted)
b@1608 250 {
b@1608 251 this.testDuration = this.getTestTime();
b@1608 252 this.testStarted = false;
b@1608 253 } else {
b@1608 254 console.log('ERR: Test tried to end before beginning');
b@1608 255 }
b@1608 256 };
b@1608 257 this.updateTestTime = function()
b@1608 258 {
b@1608 259 if (this.testStarted)
b@1608 260 {
b@1608 261 this.testDuration = audioContext.currentTime - this.testStartTime;
b@1608 262 }
b@1608 263 };
b@1608 264 this.getTestTime = function()
b@1608 265 {
b@1608 266 this.updateTestTime();
b@1608 267 return this.testDuration;
b@1608 268 };
b@1608 269 }
b@1608 270
b@1608 271 function sessionMetrics(engine)
b@1608 272 {
b@1608 273 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
b@1608 274 */
b@1608 275 this.engine = engine;
b@1608 276 this.lastClicked = -1;
b@1608 277 this.data = -1;
b@1608 278 this.initialiseTest = function(){};
b@1608 279 }
b@1608 280
b@1608 281 function metricTracker()
b@1608 282 {
b@1608 283 /* Custom object to track and collect metric data
b@1608 284 * Used only inside the audioObjects object.
b@1608 285 */
b@1608 286
b@1608 287 this.listenedTimer = 0;
b@1608 288 this.listenStart = 0;
nickjillings@1617 289 this.listenHold = false;
b@1608 290 this.initialPosition = -1;
b@1608 291 this.movementTracker = [];
b@1608 292 this.wasListenedTo = false;
b@1608 293 this.wasMoved = false;
b@1608 294 this.hasComments = false;
b@1608 295
b@1608 296 this.initialised = function(position)
b@1608 297 {
b@1608 298 if (this.initialPosition == -1) {
b@1608 299 this.initialPosition = position;
b@1608 300 }
b@1608 301 };
b@1608 302
b@1608 303 this.moved = function(time,position)
b@1608 304 {
b@1608 305 this.wasMoved = true;
b@1608 306 this.movementTracker[this.movementTracker.length] = [time, position];
b@1608 307 };
b@1608 308
b@1608 309 this.listening = function(time)
b@1608 310 {
nickjillings@1617 311 if (this.listenHold == false)
b@1608 312 {
b@1608 313 this.wasListenedTo = true;
b@1608 314 this.listenStart = time;
nickjillings@1617 315 this.listenHold = true;
b@1608 316 } else {
b@1608 317 this.listenedTimer += (time - this.listenStart);
b@1608 318 this.listenStart = 0;
nickjillings@1617 319 this.listenHold = false;
b@1608 320 }
b@1608 321 };
b@1608 322 }
b@1608 323
b@1608 324 function randomiseOrder(input)
b@1608 325 {
b@1608 326 // This takes an array of information and randomises the order
b@1608 327 var N = input.length;
b@1608 328 var K = N;
b@1608 329 var holdArr = [];
b@1608 330 for (var n=0; n<N; n++)
b@1608 331 {
b@1608 332 // First pick a random number
b@1608 333 var r = Math.random();
b@1608 334 // Multiply and floor by the number of elements left
b@1608 335 r = Math.floor(r*input.length);
b@1608 336 // Pick out that element and delete from the array
b@1608 337 holdArr.push(input.splice(r,1)[0]);
b@1608 338 }
b@1608 339 return holdArr;
b@1608 340 }