comparison core.js @ 681:ab56aa2fe064

Added Loop
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Sat, 18 Apr 2015 15:36:10 +0100
parents 1e736dc124ab
children 06fbaccf3b58
comparison
equal deleted inserted replaced
680:d7bdfe8bf67e 681:ab56aa2fe064
29 var currentTestHolder; // Hold any intermediate results during test - metrics 29 var currentTestHolder; // Hold any intermediate results during test - metrics
30 var audioEngineContext; // The custome AudioEngine object 30 var audioEngineContext; // The custome AudioEngine object
31 var projectReturn; // Hold the URL for the return 31 var projectReturn; // Hold the URL for the return
32 var preTestQuestions = document.createElement('PreTest'); // Store any pre-test question response 32 var preTestQuestions = document.createElement('PreTest'); // Store any pre-test question response
33 var postTestQuestions = document.createElement('PostTest'); // Store any post-test question response 33 var postTestQuestions = document.createElement('PostTest'); // Store any post-test question response
34
35 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
36 AudioBufferSourceNode.prototype.owner = undefined;
34 37
35 window.onload = function() { 38 window.onload = function() {
36 // Function called once the browser has loaded all files. 39 // Function called once the browser has loaded all files.
37 // This should perform any initial commands such as structure / loading documents 40 // This should perform any initial commands such as structure / loading documents
38 41
124 // Create the timer Object 127 // Create the timer Object
125 this.timer = new timer(); 128 this.timer = new timer();
126 // Create session metrics 129 // Create session metrics
127 this.metric = new sessionMetrics(this); 130 this.metric = new sessionMetrics(this);
128 131
132 this.loopPlayback = false;
133
129 // Create store for new audioObjects 134 // Create store for new audioObjects
130 this.audioObjects = []; 135 this.audioObjects = [];
131 136
132 this.play = function() { 137 this.play = function(){};
133 // Send play command to all playback buffers for synchronised start 138
134 // Also start timer callbacks to detect if playback has finished 139 this.stop = function(){};
135 if (this.status == 0) {
136 this.timer.startTest();
137 // First get current clock
138 var timer = audioContext.currentTime;
139 // Add 3 seconds
140 timer += 3.0;
141
142 // Send play to all tracks
143 for (var i=0; i<this.audioObjects.length; i++)
144 {
145 this.audioObjects[i].play(timer);
146 }
147 this.status = 1;
148 }
149 };
150
151 this.stop = function() {
152 // Send stop and reset command to all playback buffers
153 if (this.status == 1) {
154 for (var i=0; i<this.audioObjects.length; i++)
155 {
156 this.audioObjects[i].stop();
157 }
158 this.status = 0;
159 }
160 };
161
162 this.selectedTrack = function(id) {
163 for (var i=0; i<this.audioObjects.length; i++)
164 {
165 if (id == i) {
166 this.audioObjects[i].outputGain.gain.value = 1.0;
167 } else {
168 this.audioObjects[i].outputGain.gain.value = 0.0;
169 }
170 }
171 };
172 140
173 141
174 this.newTrack = function(url) { 142 this.newTrack = function(url) {
175 // Pull data from given URL into new audio buffer 143 // Pull data from given URL into new audio buffer
176 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin' 144 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
192 this.state = 0; // 0 - no data, 1 - ready 160 this.state = 0; // 0 - no data, 1 - ready
193 this.url = null; // Hold the URL given for the output back to the results. 161 this.url = null; // Hold the URL given for the output back to the results.
194 this.metric = new metricTracker(); 162 this.metric = new metricTracker();
195 163
196 // Create a buffer and external gain control to allow internal patching of effects and volume leveling. 164 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
197 this.bufferNode = audioContext.createBufferSource(); 165 this.bufferNode = undefined;
198 this.outputGain = audioContext.createGain(); 166 this.outputGain = audioContext.createGain();
199 167
200 // Default output gain to be zero 168 // Default output gain to be zero
201 this.outputGain.gain.value = 0.0; 169 this.outputGain.gain.value = 0.0;
202 170
203 // Connect buffer to the audio graph 171 // Connect buffer to the audio graph
204 this.bufferNode.connect(this.outputGain);
205 this.outputGain.connect(audioEngineContext.outputGain); 172 this.outputGain.connect(audioEngineContext.outputGain);
206 173
207 // the audiobuffer is not designed for multi-start playback 174 // the audiobuffer is not designed for multi-start playback
208 // When stopeed, the buffer node is deleted and recreated with the stored buffer. 175 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
209 this.buffer; 176 this.buffer;
210 177
211 this.play = function(startTime) { 178 this.play = function(startTime) {
212 this.bufferNode.start(startTime);
213 };
214
215 this.stop = function() {
216 this.bufferNode.stop(0);
217 this.bufferNode = audioContext.createBufferSource(); 179 this.bufferNode = audioContext.createBufferSource();
218 this.bufferNode.connect(this.outputGain); 180 this.bufferNode.connect(this.outputGain);
219 this.bufferNode.buffer = this.buffer; 181 this.bufferNode.buffer = this.buffer;
220 this.bufferNode.loop = true; 182 this.bufferNode.loop = audioEngineContext.loopPlayback;
183 this.bufferNode.start(startTime);
184 };
185
186 this.stop = function() {
187 this.bufferNode.stop(0);
188 this.bufferNode = undefined;
221 }; 189 };
222 190
223 this.constructTrack = function(url) { 191 this.constructTrack = function(url) {
224 var request = new XMLHttpRequest(); 192 var request = new XMLHttpRequest();
225 this.url = url; 193 this.url = url;
230 198
231 // Create callback to decode the data asynchronously 199 // Create callback to decode the data asynchronously
232 request.onloadend = function() { 200 request.onloadend = function() {
233 audioContext.decodeAudioData(request.response, function(decodedData) { 201 audioContext.decodeAudioData(request.response, function(decodedData) {
234 audioObj.buffer = decodedData; 202 audioObj.buffer = decodedData;
235 audioObj.bufferNode.buffer = audioObj.buffer;
236 audioObj.bufferNode.loop = true;
237 audioObj.state = 1; 203 audioObj.state = 1;
238 }, function(){ 204 }, function(){
239 // Should only be called if there was an error, but sometimes gets called continuously 205 // Should only be called if there was an error, but sometimes gets called continuously
240 // Check here if the error is genuine 206 // Check here if the error is genuine
241 if (audioObj.state == 0 || audioObj.buffer == undefined) { 207 if (audioObj.state == 0 || audioObj.buffer == undefined) {