annotate core.js @ 90:e100556bd44f

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