annotate core.js @ 1662:4310efd6e631

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