annotate core.js @ 32:2b9bcac771a8 Dev_main

Added dev-main branch warning at top of files
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 10 Apr 2015 10:25:52 +0100
parents b826d3640725
children c61a92659541
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
n@32 8
n@32 9 /*
n@32 10 *
n@32 11 * WARNING!!!
n@32 12 *
n@32 13 * YOU ARE VIEWING THE DEV VERSION. THERE IS NO GUARANTEE THIS WILL BE FULLY FUNCTIONAL
n@32 14 *
n@32 15 * WARNING!!!
n@32 16 *
n@32 17 */
n@32 18
n@32 19
n@32 20
n@32 21
nicholas@1 22 /* create the web audio API context and store in audioContext*/
nicholas@1 23 var audioContext;
nicholas@2 24 var projectXML;
nicholas@1 25 var audioEngineContext;
nicholas@7 26 var projectReturn;
n@23 27 var preTestQuestions = document.createElement('PreTest');
n@23 28 var postTestQuestions = document.createElement('PostTest');
nicholas@1 29
nicholas@1 30 window.onload = function() {
nicholas@1 31 // Function called once the browser has loaded all files.
nicholas@1 32 // This should perform any initial commands such as structure / loading documents
nicholas@1 33
nicholas@1 34 // Create a web audio API context
nicholas@21 35 // Fixed for cross-browser support
nicholas@21 36 var AudioContext = window.AudioContext || window.webkitAudioContext;
nicholas@7 37 audioContext = new AudioContext;
nicholas@1 38
nicholas@1 39 // Create the audio engine object
nicholas@1 40 audioEngineContext = new AudioEngine();
n@16 41 };
nicholas@1 42
nicholas@1 43 function loadProjectSpec(url) {
nicholas@1 44 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
nicholas@1 45 // If url is null, request client to upload project XML document
nicholas@2 46 var r = new XMLHttpRequest();
nicholas@2 47 r.open('GET',url,true);
nicholas@2 48 r.onload = function() {
nicholas@2 49 loadProjectSpecCallback(r.response);
n@16 50 };
nicholas@2 51 r.send();
n@16 52 };
nicholas@2 53
nicholas@2 54 function loadProjectSpecCallback(response) {
nicholas@2 55 // Function called after asynchronous download of XML project specification
nicholas@2 56 var decode = $.parseXML(response);
nicholas@2 57 projectXML = $(decode);
nicholas@2 58
nicholas@2 59 // Now extract the setup tag
nicholas@2 60 var xmlSetup = projectXML.find('setup');
n@16 61 // Detect the interface to use and load the relevant javascripts.
nicholas@2 62 var interfaceType = xmlSetup[0].attributes['interface'];
nicholas@2 63 var interfaceJS = document.createElement('script');
nicholas@2 64 interfaceJS.setAttribute("type","text/javascript");
nicholas@2 65 if (interfaceType.value == 'APE') {
nicholas@2 66 interfaceJS.setAttribute("src","ape.js");
nicholas@2 67 }
nicholas@2 68 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
nicholas@1 69 }
nicholas@1 70
nicholas@1 71 function createProjectSave(destURL) {
nicholas@1 72 // Save the data from interface into XML and send to destURL
nicholas@1 73 // If destURL is null then download XML in client
nicholas@7 74 // Now time to render file locally
nicholas@7 75 var xmlDoc = interfaceXMLSave();
nicholas@7 76 if (destURL == "null" || destURL == undefined) {
nicholas@7 77 var parent = document.createElement("div");
nicholas@7 78 parent.appendChild(xmlDoc);
nicholas@7 79 var file = [parent.innerHTML];
nicholas@7 80 var bb = new Blob(file,{type : 'application/xml'});
nicholas@7 81 var dnlk = window.URL.createObjectURL(bb);
nicholas@7 82 var a = document.createElement("a");
nicholas@7 83 a.hidden = '';
nicholas@7 84 a.href = dnlk;
nicholas@7 85 a.download = "save.xml";
nicholas@7 86 a.textContent = "Save File";
nicholas@7 87
nicholas@7 88 var submitDiv = document.getElementById('download-point');
nicholas@7 89 submitDiv.appendChild(a);
nicholas@7 90 }
nicholas@1 91 }
nicholas@1 92
nicholas@1 93 function AudioEngine() {
nicholas@1 94
nicholas@1 95 // Create two output paths, the main outputGain and fooGain.
nicholas@1 96 // Output gain is default to 1 and any items for playback route here
nicholas@1 97 // Foo gain is used for analysis to ensure paths get processed, but are not heard
nicholas@1 98 // because web audio will optimise and any route which does not go to the destination gets ignored.
nicholas@1 99 this.outputGain = audioContext.createGain();
nicholas@1 100 this.fooGain = audioContext.createGain();
nicholas@1 101 this.fooGain.gain = 0;
nicholas@1 102
nicholas@7 103 // Use this to detect playback state: 0 - stopped, 1 - playing
nicholas@7 104 this.status = 0;
nicholas@7 105
nicholas@1 106 // Connect both gains to output
nicholas@1 107 this.outputGain.connect(audioContext.destination);
nicholas@1 108 this.fooGain.connect(audioContext.destination);
nicholas@1 109
nicholas@1 110 // Create store for new audioObjects
nicholas@1 111 this.audioObjects = [];
nicholas@1 112
nicholas@1 113 this.play = function() {
nicholas@1 114 // Send play command to all playback buffers for synchronised start
nicholas@1 115 // Also start timer callbacks to detect if playback has finished
nicholas@7 116 if (this.status == 0) {
nicholas@7 117 // First get current clock
nicholas@7 118 var timer = audioContext.currentTime;
nicholas@7 119 // Add 3 seconds
nicholas@7 120 timer += 3.0;
nicholas@7 121
nicholas@7 122 // Send play to all tracks
nicholas@7 123 for (var i=0; i<this.audioObjects.length; i++)
nicholas@7 124 {
nicholas@7 125 this.audioObjects[i].play(timer);
nicholas@7 126 }
nicholas@7 127 this.status = 1;
nicholas@7 128 }
n@16 129 };
nicholas@1 130
nicholas@1 131 this.stop = function() {
nicholas@1 132 // Send stop and reset command to all playback buffers
nicholas@7 133 if (this.status == 1) {
nicholas@7 134 for (var i=0; i<this.audioObjects.length; i++)
nicholas@7 135 {
nicholas@7 136 this.audioObjects[i].stop();
nicholas@7 137 }
nicholas@7 138 this.status = 0;
nicholas@7 139 }
n@16 140 };
nicholas@1 141
nicholas@8 142 this.selectedTrack = function(id) {
nicholas@8 143 for (var i=0; i<this.audioObjects.length; i++)
nicholas@8 144 {
nicholas@8 145 if (id == i) {
nicholas@8 146 this.audioObjects[i].outputGain.gain.value = 1.0;
nicholas@8 147 } else {
nicholas@8 148 this.audioObjects[i].outputGain.gain.value = 0.0;
nicholas@8 149 }
nicholas@8 150 }
n@16 151 };
nicholas@8 152
nicholas@8 153
nicholas@1 154 this.newTrack = function(url) {
nicholas@1 155 // Pull data from given URL into new audio buffer
nicholas@1 156 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
nicholas@7 157
nicholas@1 158 // Create the audioObject with ID of the new track length;
nicholas@1 159 audioObjectId = this.audioObjects.length
nicholas@1 160 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
nicholas@7 161
nicholas@7 162 // AudioObject will get track itself.
nicholas@7 163 this.audioObjects[audioObjectId].constructTrack(url);
n@16 164 };
nicholas@1 165
nicholas@1 166 }
nicholas@1 167
nicholas@1 168 function audioObject(id) {
nicholas@1 169 // The main buffer object with common control nodes to the AudioEngine
nicholas@1 170
nicholas@1 171 this.id = id;
nicholas@1 172 this.state = 0; // 0 - no data, 1 - ready
n@24 173 this.url = null; // Hold the URL given for the output back to the results.
nicholas@1 174
nicholas@1 175 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
nicholas@1 176 this.bufferNode = audioContext.createBufferSource();
nicholas@1 177 this.outputGain = audioContext.createGain();
nicholas@1 178
nicholas@8 179 // Default output gain to be zero
nicholas@8 180 this.outputGain.gain.value = 0.0;
nicholas@8 181
nicholas@1 182 // Connect buffer to the audio graph
nicholas@1 183 this.bufferNode.connect(this.outputGain);
nicholas@1 184 this.outputGain.connect(audioEngineContext.outputGain);
nicholas@1 185
nicholas@1 186 // the audiobuffer is not designed for multi-start playback
nicholas@1 187 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
nicholas@1 188 this.buffer;
nicholas@1 189
nicholas@1 190 this.play = function(startTime) {
nicholas@1 191 this.bufferNode.start(startTime);
n@16 192 };
nicholas@1 193
nicholas@1 194 this.stop = function() {
nicholas@1 195 this.bufferNode.stop(0);
nicholas@1 196 this.bufferNode = audioContext.createBufferSource();
nicholas@1 197 this.bufferNode.connect(this.outputGain);
nicholas@1 198 this.bufferNode.buffer = this.buffer;
nicholas@7 199 this.bufferNode.loop = true;
n@16 200 };
nicholas@8 201
nicholas@7 202 this.constructTrack = function(url) {
nicholas@7 203 var request = new XMLHttpRequest();
n@24 204 this.url = url;
nicholas@7 205 request.open('GET',url,true);
nicholas@7 206 request.responseType = 'arraybuffer';
nicholas@7 207
nicholas@7 208 var audioObj = this;
nicholas@7 209
nicholas@7 210 // Create callback to decode the data asynchronously
nicholas@7 211 request.onloadend = function() {
nicholas@7 212 audioContext.decodeAudioData(request.response, function(decodedData) {
nicholas@7 213 audioObj.buffer = decodedData;
nicholas@7 214 audioObj.bufferNode.buffer = audioObj.buffer;
nicholas@7 215 audioObj.bufferNode.loop = true;
nicholas@7 216 audioObj.state = 1;
nicholas@7 217 }, function(){
nicholas@7 218 // Should only be called if there was an error, but sometimes gets called continuously
nicholas@7 219 // Check here if the error is genuine
nicholas@7 220 if (audioObj.state == 0 || audioObj.buffer == undefined) {
nicholas@7 221 // Genuine error
nicholas@7 222 console.log('FATAL - Error loading buffer on '+audioObj.id);
nicholas@7 223 }
nicholas@7 224 });
n@16 225 };
nicholas@7 226 request.send();
n@16 227 };
nicholas@7 228
nicholas@7 229 }