b@1608
|
1 /**
|
b@1608
|
2 * core.js
|
b@1608
|
3 *
|
b@1608
|
4 * Main script to run, calls all other core functions and manages loading/store to backend.
|
b@1608
|
5 * Also contains all global variables.
|
b@1608
|
6 */
|
b@1608
|
7
|
b@1608
|
8 /* create the web audio API context and store in audioContext*/
|
b@1608
|
9 var audioContext; // Hold the browser web audio API
|
b@1608
|
10 var projectXML; // Hold the parsed setup XML
|
b@1608
|
11
|
b@1608
|
12 var testXMLSetups = []; // Hold the parsed test instances
|
b@1608
|
13 var testResultsHolders =[]; // Hold the results from each test for publishing to XML
|
b@1608
|
14 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
|
b@1608
|
15 var currentTestHolder; // Hold any intermediate results during test - metrics
|
b@1608
|
16 var audioEngineContext; // The custome AudioEngine object
|
b@1608
|
17 var projectReturn; // Hold the URL for the return
|
b@1608
|
18 var preTestQuestions = document.createElement('PreTest'); // Store any pre-test question response
|
b@1608
|
19 var postTestQuestions = document.createElement('PostTest'); // Store any post-test question response
|
b@1608
|
20
|
b@1608
|
21 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
|
b@1608
|
22 AudioBufferSourceNode.prototype.owner = undefined;
|
b@1608
|
23
|
b@1608
|
24 window.onload = function() {
|
b@1608
|
25 // Function called once the browser has loaded all files.
|
b@1608
|
26 // This should perform any initial commands such as structure / loading documents
|
b@1608
|
27
|
b@1608
|
28 // Create a web audio API context
|
b@1608
|
29 // Fixed for cross-browser support
|
b@1608
|
30 var AudioContext = window.AudioContext || window.webkitAudioContext;
|
b@1608
|
31 audioContext = new AudioContext;
|
b@1608
|
32
|
b@1608
|
33 // Create the audio engine object
|
b@1608
|
34 audioEngineContext = new AudioEngine();
|
b@1608
|
35 };
|
b@1608
|
36
|
nickjillings@1621
|
37 function createPopup() {
|
nickjillings@1621
|
38 // Create popup window interface
|
nickjillings@1621
|
39 var insertPoint = document.getElementById("topLevelBody");
|
nickjillings@1621
|
40 var blank = document.createElement('div');
|
nickjillings@1621
|
41 blank.className = 'testHalt';
|
nickjillings@1621
|
42
|
nickjillings@1621
|
43 var popupHolder = document.createElement('div');
|
nickjillings@1621
|
44 popupHolder.id = 'popupHolder';
|
nickjillings@1621
|
45 popupHolder.className = 'popupHolder';
|
nickjillings@1621
|
46 popupHolder.style.position = 'absolute';
|
nickjillings@1621
|
47 popupHolder.style.left = (window.innerWidth/2)-250 + 'px';
|
nickjillings@1621
|
48 popupHolder.style.top = (window.innerHeight/2)-125 + 'px';
|
nickjillings@1621
|
49 insertPoint.appendChild(popupHolder);
|
nickjillings@1621
|
50 insertPoint.appendChild(blank);
|
nickjillings@1621
|
51 }
|
nickjillings@1621
|
52
|
nickjillings@1621
|
53 function showPopup()
|
nickjillings@1621
|
54 {
|
nickjillings@1621
|
55 var popupHolder = document.getElementById('popupHolder');
|
nickjillings@1621
|
56 if (popupHolder == null || popupHolder == undefined) {
|
nickjillings@1621
|
57 createPopup();
|
nickjillings@1621
|
58 popupHolder = document.getElementById('popupHolder');
|
nickjillings@1621
|
59 }
|
nickjillings@1621
|
60 popupHolder.style.zIndex = 3;
|
nickjillings@1621
|
61 popupHolder.style.visibility = 'visible';
|
nickjillings@1621
|
62 var blank = document.getElementsByClassName('testHalt')[0];
|
nickjillings@1621
|
63 blank.style.zIndex = 2;
|
nickjillings@1621
|
64 blank.style.visibility = 'visible';
|
nickjillings@1621
|
65 }
|
nickjillings@1621
|
66
|
nickjillings@1621
|
67 function hidePopup()
|
nickjillings@1621
|
68 {
|
nickjillings@1621
|
69 var popupHolder = document.getElementById('popupHolder');
|
nickjillings@1621
|
70 popupHolder.style.zIndex = -1;
|
nickjillings@1621
|
71 popupHolder.style.visibility = 'hidden';
|
nickjillings@1621
|
72 var blank = document.getElementsByClassName('testHalt')[0];
|
nickjillings@1621
|
73 blank.style.zIndex = -2;
|
nickjillings@1621
|
74 blank.style.visibility = 'hidden';
|
nickjillings@1621
|
75 }
|
nickjillings@1621
|
76
|
b@1608
|
77 function loadProjectSpec(url) {
|
b@1608
|
78 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
|
b@1608
|
79 // If url is null, request client to upload project XML document
|
b@1608
|
80 var r = new XMLHttpRequest();
|
b@1608
|
81 r.open('GET',url,true);
|
b@1608
|
82 r.onload = function() {
|
b@1608
|
83 loadProjectSpecCallback(r.response);
|
b@1608
|
84 };
|
b@1608
|
85 r.send();
|
b@1608
|
86 };
|
b@1608
|
87
|
b@1608
|
88 function loadProjectSpecCallback(response) {
|
b@1608
|
89 // Function called after asynchronous download of XML project specification
|
b@1608
|
90 var decode = $.parseXML(response);
|
b@1608
|
91 projectXML = $(decode);
|
b@1608
|
92
|
b@1608
|
93 // Now extract the setup tag
|
b@1608
|
94 var xmlSetup = projectXML.find('setup');
|
b@1608
|
95 // Detect the interface to use and load the relevant javascripts.
|
b@1608
|
96 var interfaceType = xmlSetup[0].attributes['interface'];
|
b@1608
|
97 var interfaceJS = document.createElement('script');
|
b@1608
|
98 interfaceJS.setAttribute("type","text/javascript");
|
b@1608
|
99 if (interfaceType.value == 'APE') {
|
b@1608
|
100 interfaceJS.setAttribute("src","ape.js");
|
b@1608
|
101
|
b@1608
|
102 // APE comes with a css file
|
b@1608
|
103 var css = document.createElement('link');
|
b@1608
|
104 css.rel = 'stylesheet';
|
b@1608
|
105 css.type = 'text/css';
|
b@1608
|
106 css.href = 'ape.css';
|
b@1608
|
107
|
b@1608
|
108 document.getElementsByTagName("head")[0].appendChild(css);
|
b@1608
|
109 }
|
b@1608
|
110 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
|
b@1608
|
111 }
|
b@1608
|
112
|
b@1608
|
113 function createProjectSave(destURL) {
|
b@1608
|
114 // Save the data from interface into XML and send to destURL
|
b@1608
|
115 // If destURL is null then download XML in client
|
b@1608
|
116 // Now time to render file locally
|
b@1608
|
117 var xmlDoc = interfaceXMLSave();
|
b@1608
|
118 if (destURL == "null" || destURL == undefined) {
|
b@1608
|
119 var parent = document.createElement("div");
|
b@1608
|
120 parent.appendChild(xmlDoc);
|
b@1608
|
121 var file = [parent.innerHTML];
|
b@1608
|
122 var bb = new Blob(file,{type : 'application/xml'});
|
b@1608
|
123 var dnlk = window.URL.createObjectURL(bb);
|
b@1608
|
124 var a = document.createElement("a");
|
b@1608
|
125 a.hidden = '';
|
b@1608
|
126 a.href = dnlk;
|
b@1608
|
127 a.download = "save.xml";
|
b@1608
|
128 a.textContent = "Save File";
|
b@1608
|
129
|
b@1608
|
130 var submitDiv = document.getElementById('download-point');
|
b@1608
|
131 submitDiv.appendChild(a);
|
b@1608
|
132 }
|
b@1608
|
133 return submitDiv;
|
b@1608
|
134 }
|
b@1608
|
135
|
b@1608
|
136 function AudioEngine() {
|
b@1608
|
137
|
b@1608
|
138 // Create two output paths, the main outputGain and fooGain.
|
b@1608
|
139 // Output gain is default to 1 and any items for playback route here
|
b@1608
|
140 // Foo gain is used for analysis to ensure paths get processed, but are not heard
|
b@1608
|
141 // because web audio will optimise and any route which does not go to the destination gets ignored.
|
b@1608
|
142 this.outputGain = audioContext.createGain();
|
b@1608
|
143 this.fooGain = audioContext.createGain();
|
b@1608
|
144 this.fooGain.gain = 0;
|
b@1608
|
145
|
b@1608
|
146 // Use this to detect playback state: 0 - stopped, 1 - playing
|
b@1608
|
147 this.status = 0;
|
nickjillings@1620
|
148 this.audioObjectsReady = false;
|
b@1608
|
149
|
b@1608
|
150 // Connect both gains to output
|
b@1608
|
151 this.outputGain.connect(audioContext.destination);
|
b@1608
|
152 this.fooGain.connect(audioContext.destination);
|
b@1608
|
153
|
b@1608
|
154 // Create the timer Object
|
b@1608
|
155 this.timer = new timer();
|
b@1608
|
156 // Create session metrics
|
b@1608
|
157 this.metric = new sessionMetrics(this);
|
b@1608
|
158
|
b@1608
|
159 this.loopPlayback = false;
|
b@1608
|
160
|
b@1608
|
161 // Create store for new audioObjects
|
b@1608
|
162 this.audioObjects = [];
|
b@1608
|
163
|
nickjillings@1620
|
164 this.play = function() {
|
nickjillings@1620
|
165 // Start the timer and set the audioEngine state to playing (1)
|
nickjillings@1620
|
166 if (this.status == 0) {
|
nickjillings@1620
|
167 // Check if all audioObjects are ready
|
nickjillings@1620
|
168 if (this.audioObjectsReady == false) {
|
nickjillings@1620
|
169 this.audioObjectsReady = this.checkAllReady();
|
nickjillings@1620
|
170 }
|
nickjillings@1620
|
171 if (this.audioObjectsReady == true) {
|
nickjillings@1620
|
172 this.timer.startTest();
|
nickjillings@1620
|
173 this.status = 1;
|
nickjillings@1620
|
174 }
|
nickjillings@1620
|
175 }
|
nickjillings@1620
|
176 };
|
b@1608
|
177
|
nickjillings@1620
|
178 this.stop = function() {
|
nickjillings@1620
|
179 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
|
nickjillings@1620
|
180 if (this.status == 1) {
|
nickjillings@1620
|
181 for (var i=0; i<this.audioObjects.length; i++)
|
nickjillings@1620
|
182 {
|
nickjillings@1620
|
183 this.audioObjects[i].stop();
|
nickjillings@1620
|
184 }
|
nickjillings@1620
|
185 this.status = 0;
|
nickjillings@1620
|
186 }
|
nickjillings@1620
|
187 };
|
b@1608
|
188
|
b@1608
|
189
|
b@1608
|
190 this.newTrack = function(url) {
|
b@1608
|
191 // Pull data from given URL into new audio buffer
|
b@1608
|
192 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
|
b@1608
|
193
|
b@1608
|
194 // Create the audioObject with ID of the new track length;
|
b@1608
|
195 audioObjectId = this.audioObjects.length;
|
b@1608
|
196 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
|
b@1608
|
197
|
b@1608
|
198 // AudioObject will get track itself.
|
b@1608
|
199 this.audioObjects[audioObjectId].constructTrack(url);
|
b@1608
|
200 };
|
b@1608
|
201
|
nickjillings@1620
|
202 this.newTestPage = function() {
|
nickjillings@1620
|
203 this.state = 0;
|
nickjillings@1620
|
204 this.audioObjectsReady = false;
|
nickjillings@1620
|
205 this.metric.reset();
|
nickjillings@1620
|
206 this.audioObjects = [];
|
nickjillings@1620
|
207 };
|
nickjillings@1620
|
208
|
nickjillings@1614
|
209 this.checkAllPlayed = function() {
|
nickjillings@1614
|
210 arr = [];
|
nickjillings@1614
|
211 for (var id=0; id<this.audioObjects.length; id++) {
|
nickjillings@1614
|
212 if (this.audioObjects[id].played == false) {
|
nickjillings@1614
|
213 arr.push(this.audioObjects[id].id);
|
nickjillings@1614
|
214 }
|
nickjillings@1614
|
215 }
|
nickjillings@1614
|
216 return arr;
|
nickjillings@1614
|
217 };
|
nickjillings@1614
|
218
|
nickjillings@1620
|
219 this.checkAllReady = function() {
|
nickjillings@1620
|
220 var ready = true;
|
nickjillings@1620
|
221 for (var i=0; i<this.audioObjects.length; i++) {
|
nickjillings@1620
|
222 if (this.audioObjects[i].state == 0) {
|
nickjillings@1620
|
223 // Track not ready
|
nickjillings@1620
|
224 console.log('WAIT -- audioObject '+i+' not ready yet!');
|
nickjillings@1620
|
225 ready = false;
|
nickjillings@1620
|
226 };
|
nickjillings@1620
|
227 }
|
nickjillings@1620
|
228 return ready;
|
nickjillings@1620
|
229 };
|
nickjillings@1620
|
230
|
b@1608
|
231 }
|
b@1608
|
232
|
b@1608
|
233 function audioObject(id) {
|
b@1608
|
234 // The main buffer object with common control nodes to the AudioEngine
|
b@1608
|
235
|
b@1608
|
236 this.id = id;
|
b@1608
|
237 this.state = 0; // 0 - no data, 1 - ready
|
b@1608
|
238 this.url = null; // Hold the URL given for the output back to the results.
|
b@1608
|
239 this.metric = new metricTracker();
|
b@1608
|
240
|
nickjillings@1614
|
241 this.played = false;
|
nickjillings@1614
|
242
|
b@1608
|
243 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
|
b@1608
|
244 this.bufferNode = undefined;
|
b@1608
|
245 this.outputGain = audioContext.createGain();
|
b@1608
|
246
|
b@1608
|
247 // Default output gain to be zero
|
b@1608
|
248 this.outputGain.gain.value = 0.0;
|
b@1608
|
249
|
b@1608
|
250 // Connect buffer to the audio graph
|
b@1608
|
251 this.outputGain.connect(audioEngineContext.outputGain);
|
b@1608
|
252
|
b@1608
|
253 // the audiobuffer is not designed for multi-start playback
|
b@1608
|
254 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
|
b@1608
|
255 this.buffer;
|
b@1608
|
256
|
b@1608
|
257 this.play = function(startTime) {
|
b@1608
|
258 this.bufferNode = audioContext.createBufferSource();
|
nickjillings@1617
|
259 this.bufferNode.owner = this;
|
b@1608
|
260 this.bufferNode.connect(this.outputGain);
|
b@1608
|
261 this.bufferNode.buffer = this.buffer;
|
b@1608
|
262 this.bufferNode.loop = audioEngineContext.loopPlayback;
|
nickjillings@1617
|
263 if (this.bufferNode.loop == false) {
|
nickjillings@1617
|
264 this.bufferNode.onended = function() {
|
nickjillings@1617
|
265 this.owner.metric.listening(audioEngineContext.timer.getTestTime());
|
nickjillings@1620
|
266 };
|
nickjillings@1617
|
267 }
|
nickjillings@1617
|
268 this.metric.listening(audioEngineContext.timer.getTestTime());
|
b@1608
|
269 this.bufferNode.start(startTime);
|
nickjillings@1614
|
270 this.played = true;
|
b@1608
|
271 };
|
b@1608
|
272
|
b@1608
|
273 this.stop = function() {
|
b@1607
|
274 if (this.bufferNode != undefined)
|
b@1607
|
275 {
|
b@1607
|
276 this.bufferNode.stop(0);
|
b@1607
|
277 this.bufferNode = undefined;
|
nickjillings@1617
|
278 this.metric.listening(audioEngineContext.timer.getTestTime());
|
b@1607
|
279 }
|
b@1608
|
280 };
|
b@1608
|
281
|
b@1608
|
282 this.constructTrack = function(url) {
|
b@1608
|
283 var request = new XMLHttpRequest();
|
b@1608
|
284 this.url = url;
|
b@1608
|
285 request.open('GET',url,true);
|
b@1608
|
286 request.responseType = 'arraybuffer';
|
b@1608
|
287
|
b@1608
|
288 var audioObj = this;
|
b@1608
|
289
|
b@1608
|
290 // Create callback to decode the data asynchronously
|
b@1608
|
291 request.onloadend = function() {
|
b@1608
|
292 audioContext.decodeAudioData(request.response, function(decodedData) {
|
b@1608
|
293 audioObj.buffer = decodedData;
|
b@1608
|
294 audioObj.state = 1;
|
b@1608
|
295 }, function(){
|
b@1608
|
296 // Should only be called if there was an error, but sometimes gets called continuously
|
b@1608
|
297 // Check here if the error is genuine
|
b@1608
|
298 if (audioObj.state == 0 || audioObj.buffer == undefined) {
|
b@1608
|
299 // Genuine error
|
b@1608
|
300 console.log('FATAL - Error loading buffer on '+audioObj.id);
|
b@1608
|
301 }
|
b@1608
|
302 });
|
b@1608
|
303 };
|
b@1608
|
304 request.send();
|
b@1608
|
305 };
|
b@1608
|
306
|
b@1608
|
307 }
|
b@1608
|
308
|
b@1608
|
309 function timer()
|
b@1608
|
310 {
|
b@1608
|
311 /* Timer object used in audioEngine to keep track of session timings
|
b@1608
|
312 * Uses the timer of the web audio API, so sample resolution
|
b@1608
|
313 */
|
b@1608
|
314 this.testStarted = false;
|
b@1608
|
315 this.testStartTime = 0;
|
b@1608
|
316 this.testDuration = 0;
|
b@1608
|
317 this.minimumTestTime = 0; // No minimum test time
|
b@1608
|
318 this.startTest = function()
|
b@1608
|
319 {
|
b@1608
|
320 if (this.testStarted == false)
|
b@1608
|
321 {
|
b@1608
|
322 this.testStartTime = audioContext.currentTime;
|
b@1608
|
323 this.testStarted = true;
|
b@1608
|
324 this.updateTestTime();
|
b@1608
|
325 audioEngineContext.metric.initialiseTest();
|
b@1608
|
326 }
|
b@1608
|
327 };
|
b@1608
|
328 this.stopTest = function()
|
b@1608
|
329 {
|
b@1608
|
330 if (this.testStarted)
|
b@1608
|
331 {
|
b@1608
|
332 this.testDuration = this.getTestTime();
|
b@1608
|
333 this.testStarted = false;
|
b@1608
|
334 } else {
|
b@1608
|
335 console.log('ERR: Test tried to end before beginning');
|
b@1608
|
336 }
|
b@1608
|
337 };
|
b@1608
|
338 this.updateTestTime = function()
|
b@1608
|
339 {
|
b@1608
|
340 if (this.testStarted)
|
b@1608
|
341 {
|
b@1608
|
342 this.testDuration = audioContext.currentTime - this.testStartTime;
|
b@1608
|
343 }
|
b@1608
|
344 };
|
b@1608
|
345 this.getTestTime = function()
|
b@1608
|
346 {
|
b@1608
|
347 this.updateTestTime();
|
b@1608
|
348 return this.testDuration;
|
b@1608
|
349 };
|
b@1608
|
350 }
|
b@1608
|
351
|
b@1608
|
352 function sessionMetrics(engine)
|
b@1608
|
353 {
|
b@1608
|
354 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
|
b@1608
|
355 */
|
b@1608
|
356 this.engine = engine;
|
b@1608
|
357 this.lastClicked = -1;
|
b@1608
|
358 this.data = -1;
|
nickjillings@1620
|
359 this.reset = function() {
|
nickjillings@1620
|
360 this.lastClicked = -1;
|
nickjillings@1620
|
361 this.data = -1;
|
nickjillings@1620
|
362 };
|
b@1608
|
363 this.initialiseTest = function(){};
|
b@1608
|
364 }
|
b@1608
|
365
|
b@1608
|
366 function metricTracker()
|
b@1608
|
367 {
|
b@1608
|
368 /* Custom object to track and collect metric data
|
b@1608
|
369 * Used only inside the audioObjects object.
|
b@1608
|
370 */
|
b@1608
|
371
|
b@1608
|
372 this.listenedTimer = 0;
|
b@1608
|
373 this.listenStart = 0;
|
nickjillings@1617
|
374 this.listenHold = false;
|
b@1608
|
375 this.initialPosition = -1;
|
b@1608
|
376 this.movementTracker = [];
|
b@1608
|
377 this.wasListenedTo = false;
|
b@1608
|
378 this.wasMoved = false;
|
b@1608
|
379 this.hasComments = false;
|
b@1608
|
380
|
b@1608
|
381 this.initialised = function(position)
|
b@1608
|
382 {
|
b@1608
|
383 if (this.initialPosition == -1) {
|
b@1608
|
384 this.initialPosition = position;
|
b@1608
|
385 }
|
b@1608
|
386 };
|
b@1608
|
387
|
b@1608
|
388 this.moved = function(time,position)
|
b@1608
|
389 {
|
b@1608
|
390 this.wasMoved = true;
|
b@1608
|
391 this.movementTracker[this.movementTracker.length] = [time, position];
|
b@1608
|
392 };
|
b@1608
|
393
|
b@1608
|
394 this.listening = function(time)
|
b@1608
|
395 {
|
nickjillings@1617
|
396 if (this.listenHold == false)
|
b@1608
|
397 {
|
b@1608
|
398 this.wasListenedTo = true;
|
b@1608
|
399 this.listenStart = time;
|
nickjillings@1617
|
400 this.listenHold = true;
|
b@1608
|
401 } else {
|
b@1608
|
402 this.listenedTimer += (time - this.listenStart);
|
b@1608
|
403 this.listenStart = 0;
|
nickjillings@1617
|
404 this.listenHold = false;
|
b@1608
|
405 }
|
b@1608
|
406 };
|
b@1608
|
407 }
|
b@1608
|
408
|
b@1608
|
409 function randomiseOrder(input)
|
b@1608
|
410 {
|
b@1608
|
411 // This takes an array of information and randomises the order
|
b@1608
|
412 var N = input.length;
|
b@1608
|
413 var K = N;
|
b@1608
|
414 var holdArr = [];
|
b@1608
|
415 for (var n=0; n<N; n++)
|
b@1608
|
416 {
|
b@1608
|
417 // First pick a random number
|
b@1608
|
418 var r = Math.random();
|
b@1608
|
419 // Multiply and floor by the number of elements left
|
b@1608
|
420 r = Math.floor(r*input.length);
|
b@1608
|
421 // Pick out that element and delete from the array
|
b@1608
|
422 holdArr.push(input.splice(r,1)[0]);
|
b@1608
|
423 }
|
b@1608
|
424 return holdArr;
|
b@1608
|
425 } |