annotate core.js @ 701:c76205be5fc1

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