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