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