BrechtDeMan@938
|
1 /**
|
BrechtDeMan@938
|
2 * core.js
|
BrechtDeMan@938
|
3 *
|
BrechtDeMan@938
|
4 * Main script to run, calls all other core functions and manages loading/store to backend.
|
BrechtDeMan@938
|
5 * Also contains all global variables.
|
BrechtDeMan@938
|
6 */
|
BrechtDeMan@938
|
7
|
BrechtDeMan@938
|
8 /* create the web audio API context and store in audioContext*/
|
BrechtDeMan@938
|
9 var audioContext; // Hold the browser web audio API
|
BrechtDeMan@938
|
10 var projectXML; // Hold the parsed setup XML
|
nicholas@952
|
11 var popup; // Hold the interfacePopup object
|
nicholas@952
|
12 var currentState; // Keep track of the current state (pre/post test, which test, final test? first test?)
|
BrechtDeMan@938
|
13 var testXMLSetups = []; // Hold the parsed test instances
|
BrechtDeMan@938
|
14 var testResultsHolders =[]; // Hold the results from each test for publishing to XML
|
BrechtDeMan@938
|
15 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
|
BrechtDeMan@938
|
16 var currentTestHolder; // Hold any intermediate results during test - metrics
|
BrechtDeMan@938
|
17 var audioEngineContext; // The custome AudioEngine object
|
BrechtDeMan@938
|
18 var projectReturn; // Hold the URL for the return
|
BrechtDeMan@938
|
19 var preTestQuestions = document.createElement('PreTest'); // Store any pre-test question response
|
BrechtDeMan@938
|
20 var postTestQuestions = document.createElement('PostTest'); // Store any post-test question response
|
BrechtDeMan@938
|
21
|
BrechtDeMan@938
|
22 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
|
BrechtDeMan@938
|
23 AudioBufferSourceNode.prototype.owner = undefined;
|
BrechtDeMan@938
|
24
|
BrechtDeMan@938
|
25 window.onload = function() {
|
BrechtDeMan@938
|
26 // Function called once the browser has loaded all files.
|
BrechtDeMan@938
|
27 // This should perform any initial commands such as structure / loading documents
|
BrechtDeMan@938
|
28
|
BrechtDeMan@938
|
29 // Create a web audio API context
|
BrechtDeMan@938
|
30 // Fixed for cross-browser support
|
BrechtDeMan@938
|
31 var AudioContext = window.AudioContext || window.webkitAudioContext;
|
BrechtDeMan@938
|
32 audioContext = new AudioContext;
|
BrechtDeMan@938
|
33
|
BrechtDeMan@938
|
34 // Create the audio engine object
|
BrechtDeMan@938
|
35 audioEngineContext = new AudioEngine();
|
nicholas@952
|
36
|
nicholas@952
|
37 // Create the popup interface object
|
nicholas@952
|
38 popup = new interfacePopup();
|
BrechtDeMan@938
|
39 };
|
BrechtDeMan@938
|
40
|
nicholas@952
|
41 function interfacePopup() {
|
nicholas@952
|
42 // Creates an object to manage the popup
|
nicholas@952
|
43 this.popup = null;
|
nicholas@952
|
44 this.popupContent = null;
|
nicholas@952
|
45 this.popupButton = null;
|
nicholas@952
|
46 this.popupOptions = null;
|
nicholas@952
|
47 this.currentIndex = null;
|
nicholas@952
|
48 this.responses = null;
|
nicholas@952
|
49 this.createPopup = function(){
|
nicholas@952
|
50 // Create popup window interface
|
nicholas@952
|
51 var insertPoint = document.getElementById("topLevelBody");
|
nicholas@952
|
52 var blank = document.createElement('div');
|
nicholas@952
|
53 blank.className = 'testHalt';
|
nicholas@952
|
54
|
nicholas@952
|
55 this.popup = document.createElement('div');
|
nicholas@952
|
56 this.popup.id = 'popupHolder';
|
nicholas@952
|
57 this.popup.className = 'popupHolder';
|
nicholas@952
|
58 this.popup.style.position = 'absolute';
|
nicholas@952
|
59 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
|
nicholas@952
|
60 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
|
nicholas@952
|
61
|
nicholas@952
|
62 this.popupContent = document.createElement('div');
|
nicholas@952
|
63 this.popupContent.id = 'popupContent';
|
nicholas@952
|
64 this.popupContent.style.marginTop = '25px';
|
nicholas@952
|
65 this.popupContent.align = 'center';
|
nicholas@952
|
66 this.popup.appendChild(this.popupContent);
|
nicholas@952
|
67
|
nicholas@952
|
68 this.popupButton = document.createElement('button');
|
nicholas@952
|
69 this.popupButton.className = 'popupButton';
|
nicholas@952
|
70 this.popupButton.innerHTML = 'Next';
|
nicholas@952
|
71 this.popupButton.onclick = function(){popup.buttonClicked();};
|
nicholas@952
|
72 insertPoint.appendChild(this.popup);
|
nicholas@952
|
73 insertPoint.appendChild(blank);
|
nicholas@952
|
74 };
|
nicholas@951
|
75
|
nicholas@952
|
76 this.showPopup = function(){
|
nicholas@952
|
77 if (this.popup == null || this.popup == undefined) {
|
nicholas@952
|
78 this.createPopup();
|
nicholas@952
|
79 }
|
nicholas@952
|
80 this.popup.style.zIndex = 3;
|
nicholas@952
|
81 this.popup.style.visibility = 'visible';
|
nicholas@952
|
82 var blank = document.getElementsByClassName('testHalt')[0];
|
nicholas@952
|
83 blank.style.zIndex = 2;
|
nicholas@952
|
84 blank.style.visibility = 'visible';
|
nicholas@952
|
85 };
|
nicholas@952
|
86
|
nicholas@952
|
87 this.hidePopup = function(){
|
nicholas@952
|
88 this.popup.style.zIndex = -1;
|
nicholas@952
|
89 this.popup.style.visibility = 'hidden';
|
nicholas@952
|
90 var blank = document.getElementsByClassName('testHalt')[0];
|
nicholas@952
|
91 blank.style.zIndex = -2;
|
nicholas@952
|
92 blank.style.visibility = 'hidden';
|
nicholas@952
|
93 };
|
nicholas@952
|
94
|
nicholas@952
|
95 this.postNode = function() {
|
nicholas@952
|
96 // This will take the node from the popupOptions and display it
|
nicholas@952
|
97 var node = this.popupOptions[this.currentIndex];
|
nicholas@952
|
98 this.popupContent.innerHTML = null;
|
nicholas@952
|
99 if (node.nodeName == 'statement') {
|
nicholas@952
|
100 var span = document.createElement('span');
|
nicholas@952
|
101 span.textContent = node.textContent;
|
nicholas@952
|
102 this.popupContent.appendChild(span);
|
nicholas@952
|
103 } else if (node.nodeName == 'question') {
|
nicholas@952
|
104 var span = document.createElement('span');
|
nicholas@952
|
105 span.textContent = node.textContent;
|
nicholas@952
|
106 var textArea = document.createElement('textarea');
|
nicholas@952
|
107 var br = document.createElement('br');
|
nicholas@952
|
108 this.popupContent.appendChild(span);
|
nicholas@952
|
109 this.popupContent.appendChild(br);
|
nicholas@952
|
110 this.popupContent.appendChild(textArea);
|
nicholas@952
|
111 }
|
nicholas@952
|
112 this.popupContent.appendChild(this.popupButton);
|
nicholas@952
|
113 }
|
nicholas@952
|
114
|
nicholas@952
|
115 this.initState = function(node) {
|
nicholas@952
|
116 //Call this with your preTest and postTest nodes when needed to
|
nicholas@952
|
117 // initialise the popup procedure.
|
nicholas@952
|
118 this.popupOptions = $(node).children();
|
nicholas@952
|
119 if (this.popupOptions.length > 0) {
|
nicholas@952
|
120 if (node.nodeName == 'preTest' || node.nodeName == 'PreTest') {
|
nicholas@952
|
121 this.responses = document.createElement('PreTest');
|
nicholas@952
|
122 } else if (node.nodeName == 'postTest' || node.nodeName == 'PostTest') {
|
nicholas@952
|
123 this.responses = document.createElement('PostTest');
|
nicholas@952
|
124 } else {
|
nicholas@952
|
125 console.log ('WARNING - popup node neither pre or post!');
|
nicholas@952
|
126 this.responses = document.createElement('responses');
|
nicholas@952
|
127 }
|
nicholas@952
|
128 this.currentIndex = 0;
|
nicholas@952
|
129 this.showPopup();
|
nicholas@952
|
130 this.postNode();
|
nicholas@952
|
131 }
|
nicholas@952
|
132 }
|
nicholas@952
|
133
|
nicholas@952
|
134 this.buttonClicked = function() {
|
nicholas@952
|
135 // Each time the popup button is clicked!
|
nicholas@952
|
136 var node = this.popupOptions[this.currentIndex];
|
nicholas@952
|
137 if (node.nodeName == 'question') {
|
nicholas@952
|
138 // Must extract the question data
|
nicholas@952
|
139 var mandatory = node.attributes['mandatory'];
|
nicholas@952
|
140 if (mandatory == undefined) {
|
nicholas@952
|
141 mandatory = false;
|
nicholas@952
|
142 } else {
|
nicholas@952
|
143 if (mandatory.value == 'true'){mandatory = true;}
|
nicholas@952
|
144 else {mandatory = false;}
|
nicholas@952
|
145 }
|
nicholas@952
|
146 var textArea = $(popup.popupContent).find('textarea')[0];
|
nicholas@952
|
147 if (mandatory == true && textArea.value.length == 0) {
|
nicholas@952
|
148 alert('This question is mandatory');
|
nicholas@952
|
149 return;
|
nicholas@952
|
150 } else {
|
nicholas@952
|
151 // Save the text content
|
nicholas@952
|
152 var hold = document.createElement('comment');
|
nicholas@952
|
153 hold.id = node.attributes['id'].value;
|
nicholas@952
|
154 hold.innerHTML = textArea.value;
|
nicholas@953
|
155 console.log("Question: "+ node.textContent);
|
nicholas@953
|
156 console.log("Question Response: "+ textArea.value);
|
nicholas@952
|
157 this.responses.appendChild(hold);
|
nicholas@952
|
158 }
|
nicholas@952
|
159 }
|
nicholas@952
|
160 this.currentIndex++;
|
nicholas@952
|
161 if (this.currentIndex < this.popupOptions.length) {
|
nicholas@952
|
162 this.postNode();
|
nicholas@952
|
163 } else {
|
nicholas@952
|
164 // Reached the end of the popupOptions
|
nicholas@952
|
165 this.hidePopup();
|
nicholas@952
|
166 advanceState();
|
nicholas@952
|
167 }
|
nicholas@952
|
168 }
|
nicholas@951
|
169 }
|
nicholas@951
|
170
|
nicholas@952
|
171 function advanceState()
|
nicholas@951
|
172 {
|
nicholas@952
|
173 console.log(currentState);
|
nicholas@952
|
174 if (currentState == 'preTest')
|
nicholas@952
|
175 {
|
nicholas@952
|
176 // End of pre-test, begin the test
|
nicholas@952
|
177 preTestQuestions = popup.responses;
|
nicholas@952
|
178 loadTest(0);
|
nicholas@952
|
179 } else if (currentState == 'postTest') {
|
nicholas@952
|
180 postTestQuestions = popup.responses;
|
nicholas@954
|
181 console.log('ALL COLLECTED!');
|
nicholas@954
|
182 createProjectSave(projectReturn);
|
nicholas@952
|
183 }else if (currentState.substr(0,10) == 'testRunPre')
|
nicholas@952
|
184 {
|
nicholas@952
|
185 // Start the test
|
nicholas@952
|
186 var testId = currentState.substr(11,currentState.length-10);
|
nicholas@952
|
187 currentState = 'testRun-'+testId;
|
nicholas@952
|
188 currentTestHolder.appendChild(popup.responses);
|
nicholas@952
|
189 //audioEngineContext.timer.startTest();
|
nicholas@952
|
190 //audioEngineContext.play();
|
nicholas@952
|
191 } else if (currentState.substr(0,11) == 'testRunPost')
|
nicholas@952
|
192 {
|
nicholas@952
|
193 var testId = currentState.substr(12,currentState.length-11);
|
nicholas@952
|
194 currentTestHolder.appendChild(popup.responses);
|
nicholas@952
|
195 testEnded(testId);
|
nicholas@952
|
196 } else if (currentState.substr(0,7) == 'testRun')
|
nicholas@952
|
197 {
|
nicholas@952
|
198 var testId = currentState.substr(8,currentState.length-7);
|
nicholas@952
|
199 // Check if we have any post tests to perform
|
nicholas@952
|
200 var postXML = $(testXMLSetups[testId]).find('PostTest')[0];
|
nicholas@952
|
201 if (postXML == undefined || postXML.childElementCount == 0) {
|
nicholas@952
|
202 testEnded(testId);
|
nicholas@952
|
203 }
|
nicholas@952
|
204 else if (postXML.childElementCount > 0)
|
nicholas@952
|
205 {
|
nicholas@952
|
206 currentState = 'testRunPost-'+testId;
|
nicholas@952
|
207 popup.initState(postXML);
|
nicholas@952
|
208 }
|
nicholas@951
|
209 }
|
nicholas@952
|
210 console.log(currentState);
|
nicholas@951
|
211 }
|
nicholas@951
|
212
|
nicholas@952
|
213 function testEnded(testId)
|
nicholas@951
|
214 {
|
nicholas@952
|
215 pageXMLSave(testId);
|
nicholas@952
|
216 if (testXMLSetups.length-1 > testId)
|
nicholas@952
|
217 {
|
nicholas@952
|
218 // Yes we have another test to perform
|
nicholas@952
|
219 testId = (Number(testId)+1);
|
nicholas@952
|
220 currentState = 'testRun-'+testId;
|
nicholas@952
|
221 loadTest(testId);
|
nicholas@952
|
222 } else {
|
nicholas@952
|
223 console.log('Testing Completed!');
|
nicholas@952
|
224 currentState = 'postTest';
|
nicholas@952
|
225 // Check for any post tests
|
nicholas@952
|
226 var xmlSetup = projectXML.find('setup');
|
nicholas@952
|
227 var postTest = xmlSetup.find('PostTest')[0];
|
nicholas@952
|
228 popup.initState(postTest);
|
nicholas@952
|
229 }
|
nicholas@951
|
230 }
|
nicholas@951
|
231
|
BrechtDeMan@938
|
232 function loadProjectSpec(url) {
|
BrechtDeMan@938
|
233 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
|
BrechtDeMan@938
|
234 // If url is null, request client to upload project XML document
|
BrechtDeMan@938
|
235 var r = new XMLHttpRequest();
|
BrechtDeMan@938
|
236 r.open('GET',url,true);
|
BrechtDeMan@938
|
237 r.onload = function() {
|
BrechtDeMan@938
|
238 loadProjectSpecCallback(r.response);
|
BrechtDeMan@938
|
239 };
|
BrechtDeMan@938
|
240 r.send();
|
BrechtDeMan@938
|
241 };
|
BrechtDeMan@938
|
242
|
BrechtDeMan@938
|
243 function loadProjectSpecCallback(response) {
|
BrechtDeMan@938
|
244 // Function called after asynchronous download of XML project specification
|
BrechtDeMan@938
|
245 var decode = $.parseXML(response);
|
BrechtDeMan@938
|
246 projectXML = $(decode);
|
BrechtDeMan@938
|
247
|
BrechtDeMan@938
|
248 // Now extract the setup tag
|
BrechtDeMan@938
|
249 var xmlSetup = projectXML.find('setup');
|
BrechtDeMan@938
|
250 // Detect the interface to use and load the relevant javascripts.
|
BrechtDeMan@938
|
251 var interfaceType = xmlSetup[0].attributes['interface'];
|
BrechtDeMan@938
|
252 var interfaceJS = document.createElement('script');
|
BrechtDeMan@938
|
253 interfaceJS.setAttribute("type","text/javascript");
|
BrechtDeMan@938
|
254 if (interfaceType.value == 'APE') {
|
BrechtDeMan@938
|
255 interfaceJS.setAttribute("src","ape.js");
|
BrechtDeMan@938
|
256
|
BrechtDeMan@938
|
257 // APE comes with a css file
|
BrechtDeMan@938
|
258 var css = document.createElement('link');
|
BrechtDeMan@938
|
259 css.rel = 'stylesheet';
|
BrechtDeMan@938
|
260 css.type = 'text/css';
|
BrechtDeMan@938
|
261 css.href = 'ape.css';
|
BrechtDeMan@938
|
262
|
BrechtDeMan@938
|
263 document.getElementsByTagName("head")[0].appendChild(css);
|
BrechtDeMan@938
|
264 }
|
BrechtDeMan@938
|
265 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
|
BrechtDeMan@938
|
266 }
|
BrechtDeMan@938
|
267
|
BrechtDeMan@938
|
268 function createProjectSave(destURL) {
|
BrechtDeMan@938
|
269 // Save the data from interface into XML and send to destURL
|
BrechtDeMan@938
|
270 // If destURL is null then download XML in client
|
BrechtDeMan@938
|
271 // Now time to render file locally
|
BrechtDeMan@938
|
272 var xmlDoc = interfaceXMLSave();
|
BrechtDeMan@938
|
273 if (destURL == "null" || destURL == undefined) {
|
BrechtDeMan@938
|
274 var parent = document.createElement("div");
|
BrechtDeMan@938
|
275 parent.appendChild(xmlDoc);
|
BrechtDeMan@938
|
276 var file = [parent.innerHTML];
|
BrechtDeMan@938
|
277 var bb = new Blob(file,{type : 'application/xml'});
|
BrechtDeMan@938
|
278 var dnlk = window.URL.createObjectURL(bb);
|
BrechtDeMan@938
|
279 var a = document.createElement("a");
|
BrechtDeMan@938
|
280 a.hidden = '';
|
BrechtDeMan@938
|
281 a.href = dnlk;
|
BrechtDeMan@938
|
282 a.download = "save.xml";
|
BrechtDeMan@938
|
283 a.textContent = "Save File";
|
BrechtDeMan@938
|
284
|
BrechtDeMan@938
|
285 var submitDiv = document.getElementById('download-point');
|
BrechtDeMan@938
|
286 submitDiv.appendChild(a);
|
nicholas@954
|
287 popup.showPopup();
|
nicholas@954
|
288 popup.popupContent.innerHTML = null;
|
nicholas@954
|
289 popup.popupContent.appendChild(submitDiv)
|
BrechtDeMan@938
|
290 }
|
BrechtDeMan@938
|
291 return submitDiv;
|
BrechtDeMan@938
|
292 }
|
BrechtDeMan@938
|
293
|
BrechtDeMan@938
|
294 function AudioEngine() {
|
BrechtDeMan@938
|
295
|
BrechtDeMan@938
|
296 // Create two output paths, the main outputGain and fooGain.
|
BrechtDeMan@938
|
297 // Output gain is default to 1 and any items for playback route here
|
BrechtDeMan@938
|
298 // Foo gain is used for analysis to ensure paths get processed, but are not heard
|
BrechtDeMan@938
|
299 // because web audio will optimise and any route which does not go to the destination gets ignored.
|
BrechtDeMan@938
|
300 this.outputGain = audioContext.createGain();
|
BrechtDeMan@938
|
301 this.fooGain = audioContext.createGain();
|
BrechtDeMan@938
|
302 this.fooGain.gain = 0;
|
BrechtDeMan@938
|
303
|
BrechtDeMan@938
|
304 // Use this to detect playback state: 0 - stopped, 1 - playing
|
BrechtDeMan@938
|
305 this.status = 0;
|
n@950
|
306 this.audioObjectsReady = false;
|
BrechtDeMan@938
|
307
|
BrechtDeMan@938
|
308 // Connect both gains to output
|
BrechtDeMan@938
|
309 this.outputGain.connect(audioContext.destination);
|
BrechtDeMan@938
|
310 this.fooGain.connect(audioContext.destination);
|
BrechtDeMan@938
|
311
|
BrechtDeMan@938
|
312 // Create the timer Object
|
BrechtDeMan@938
|
313 this.timer = new timer();
|
BrechtDeMan@938
|
314 // Create session metrics
|
BrechtDeMan@938
|
315 this.metric = new sessionMetrics(this);
|
BrechtDeMan@938
|
316
|
BrechtDeMan@938
|
317 this.loopPlayback = false;
|
BrechtDeMan@938
|
318
|
BrechtDeMan@938
|
319 // Create store for new audioObjects
|
BrechtDeMan@938
|
320 this.audioObjects = [];
|
BrechtDeMan@938
|
321
|
n@950
|
322 this.play = function() {
|
n@950
|
323 // Start the timer and set the audioEngine state to playing (1)
|
n@950
|
324 if (this.status == 0) {
|
n@950
|
325 // Check if all audioObjects are ready
|
n@950
|
326 if (this.audioObjectsReady == false) {
|
n@950
|
327 this.audioObjectsReady = this.checkAllReady();
|
n@950
|
328 }
|
n@950
|
329 if (this.audioObjectsReady == true) {
|
n@950
|
330 this.timer.startTest();
|
n@950
|
331 this.status = 1;
|
n@950
|
332 }
|
n@950
|
333 }
|
n@950
|
334 };
|
BrechtDeMan@938
|
335
|
n@950
|
336 this.stop = function() {
|
n@950
|
337 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
|
n@950
|
338 if (this.status == 1) {
|
n@950
|
339 for (var i=0; i<this.audioObjects.length; i++)
|
n@950
|
340 {
|
n@950
|
341 this.audioObjects[i].stop();
|
n@950
|
342 }
|
n@950
|
343 this.status = 0;
|
n@950
|
344 }
|
n@950
|
345 };
|
BrechtDeMan@938
|
346
|
BrechtDeMan@938
|
347
|
BrechtDeMan@938
|
348 this.newTrack = function(url) {
|
BrechtDeMan@938
|
349 // Pull data from given URL into new audio buffer
|
BrechtDeMan@938
|
350 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
|
BrechtDeMan@938
|
351
|
BrechtDeMan@938
|
352 // Create the audioObject with ID of the new track length;
|
BrechtDeMan@938
|
353 audioObjectId = this.audioObjects.length;
|
BrechtDeMan@938
|
354 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
|
BrechtDeMan@938
|
355
|
BrechtDeMan@938
|
356 // AudioObject will get track itself.
|
BrechtDeMan@938
|
357 this.audioObjects[audioObjectId].constructTrack(url);
|
BrechtDeMan@938
|
358 };
|
BrechtDeMan@938
|
359
|
n@950
|
360 this.newTestPage = function() {
|
n@950
|
361 this.state = 0;
|
n@950
|
362 this.audioObjectsReady = false;
|
n@950
|
363 this.metric.reset();
|
n@950
|
364 this.audioObjects = [];
|
n@950
|
365 };
|
n@950
|
366
|
nicholas@944
|
367 this.checkAllPlayed = function() {
|
nicholas@944
|
368 arr = [];
|
nicholas@944
|
369 for (var id=0; id<this.audioObjects.length; id++) {
|
nicholas@944
|
370 if (this.audioObjects[id].played == false) {
|
nicholas@944
|
371 arr.push(this.audioObjects[id].id);
|
nicholas@944
|
372 }
|
nicholas@944
|
373 }
|
nicholas@944
|
374 return arr;
|
nicholas@944
|
375 };
|
nicholas@944
|
376
|
n@950
|
377 this.checkAllReady = function() {
|
n@950
|
378 var ready = true;
|
n@950
|
379 for (var i=0; i<this.audioObjects.length; i++) {
|
n@950
|
380 if (this.audioObjects[i].state == 0) {
|
n@950
|
381 // Track not ready
|
n@950
|
382 console.log('WAIT -- audioObject '+i+' not ready yet!');
|
n@950
|
383 ready = false;
|
n@950
|
384 };
|
n@950
|
385 }
|
n@950
|
386 return ready;
|
n@950
|
387 };
|
n@950
|
388
|
BrechtDeMan@938
|
389 }
|
BrechtDeMan@938
|
390
|
BrechtDeMan@938
|
391 function audioObject(id) {
|
BrechtDeMan@938
|
392 // The main buffer object with common control nodes to the AudioEngine
|
BrechtDeMan@938
|
393
|
BrechtDeMan@938
|
394 this.id = id;
|
BrechtDeMan@938
|
395 this.state = 0; // 0 - no data, 1 - ready
|
BrechtDeMan@938
|
396 this.url = null; // Hold the URL given for the output back to the results.
|
BrechtDeMan@938
|
397 this.metric = new metricTracker();
|
BrechtDeMan@938
|
398
|
nicholas@944
|
399 this.played = false;
|
nicholas@944
|
400
|
BrechtDeMan@938
|
401 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
|
BrechtDeMan@938
|
402 this.bufferNode = undefined;
|
BrechtDeMan@938
|
403 this.outputGain = audioContext.createGain();
|
BrechtDeMan@938
|
404
|
BrechtDeMan@938
|
405 // Default output gain to be zero
|
BrechtDeMan@938
|
406 this.outputGain.gain.value = 0.0;
|
BrechtDeMan@938
|
407
|
BrechtDeMan@938
|
408 // Connect buffer to the audio graph
|
BrechtDeMan@938
|
409 this.outputGain.connect(audioEngineContext.outputGain);
|
BrechtDeMan@938
|
410
|
BrechtDeMan@938
|
411 // the audiobuffer is not designed for multi-start playback
|
BrechtDeMan@938
|
412 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
|
BrechtDeMan@938
|
413 this.buffer;
|
BrechtDeMan@938
|
414
|
BrechtDeMan@938
|
415 this.play = function(startTime) {
|
BrechtDeMan@938
|
416 this.bufferNode = audioContext.createBufferSource();
|
nicholas@947
|
417 this.bufferNode.owner = this;
|
BrechtDeMan@938
|
418 this.bufferNode.connect(this.outputGain);
|
BrechtDeMan@938
|
419 this.bufferNode.buffer = this.buffer;
|
BrechtDeMan@938
|
420 this.bufferNode.loop = audioEngineContext.loopPlayback;
|
nicholas@947
|
421 if (this.bufferNode.loop == false) {
|
nicholas@947
|
422 this.bufferNode.onended = function() {
|
nicholas@947
|
423 this.owner.metric.listening(audioEngineContext.timer.getTestTime());
|
n@950
|
424 };
|
nicholas@947
|
425 }
|
nicholas@947
|
426 this.metric.listening(audioEngineContext.timer.getTestTime());
|
BrechtDeMan@938
|
427 this.bufferNode.start(startTime);
|
nicholas@944
|
428 this.played = true;
|
BrechtDeMan@938
|
429 };
|
BrechtDeMan@938
|
430
|
BrechtDeMan@938
|
431 this.stop = function() {
|
BrechtDeMan@937
|
432 if (this.bufferNode != undefined)
|
BrechtDeMan@937
|
433 {
|
BrechtDeMan@937
|
434 this.bufferNode.stop(0);
|
BrechtDeMan@937
|
435 this.bufferNode = undefined;
|
nicholas@947
|
436 this.metric.listening(audioEngineContext.timer.getTestTime());
|
BrechtDeMan@937
|
437 }
|
BrechtDeMan@938
|
438 };
|
BrechtDeMan@938
|
439
|
BrechtDeMan@938
|
440 this.constructTrack = function(url) {
|
BrechtDeMan@938
|
441 var request = new XMLHttpRequest();
|
BrechtDeMan@938
|
442 this.url = url;
|
BrechtDeMan@938
|
443 request.open('GET',url,true);
|
BrechtDeMan@938
|
444 request.responseType = 'arraybuffer';
|
BrechtDeMan@938
|
445
|
BrechtDeMan@938
|
446 var audioObj = this;
|
BrechtDeMan@938
|
447
|
BrechtDeMan@938
|
448 // Create callback to decode the data asynchronously
|
BrechtDeMan@938
|
449 request.onloadend = function() {
|
BrechtDeMan@938
|
450 audioContext.decodeAudioData(request.response, function(decodedData) {
|
BrechtDeMan@938
|
451 audioObj.buffer = decodedData;
|
BrechtDeMan@938
|
452 audioObj.state = 1;
|
BrechtDeMan@938
|
453 }, function(){
|
BrechtDeMan@938
|
454 // Should only be called if there was an error, but sometimes gets called continuously
|
BrechtDeMan@938
|
455 // Check here if the error is genuine
|
BrechtDeMan@938
|
456 if (audioObj.state == 0 || audioObj.buffer == undefined) {
|
BrechtDeMan@938
|
457 // Genuine error
|
BrechtDeMan@938
|
458 console.log('FATAL - Error loading buffer on '+audioObj.id);
|
BrechtDeMan@938
|
459 }
|
BrechtDeMan@938
|
460 });
|
BrechtDeMan@938
|
461 };
|
BrechtDeMan@938
|
462 request.send();
|
BrechtDeMan@938
|
463 };
|
BrechtDeMan@938
|
464
|
BrechtDeMan@938
|
465 }
|
BrechtDeMan@938
|
466
|
BrechtDeMan@938
|
467 function timer()
|
BrechtDeMan@938
|
468 {
|
BrechtDeMan@938
|
469 /* Timer object used in audioEngine to keep track of session timings
|
BrechtDeMan@938
|
470 * Uses the timer of the web audio API, so sample resolution
|
BrechtDeMan@938
|
471 */
|
BrechtDeMan@938
|
472 this.testStarted = false;
|
BrechtDeMan@938
|
473 this.testStartTime = 0;
|
BrechtDeMan@938
|
474 this.testDuration = 0;
|
BrechtDeMan@938
|
475 this.minimumTestTime = 0; // No minimum test time
|
BrechtDeMan@938
|
476 this.startTest = function()
|
BrechtDeMan@938
|
477 {
|
BrechtDeMan@938
|
478 if (this.testStarted == false)
|
BrechtDeMan@938
|
479 {
|
BrechtDeMan@938
|
480 this.testStartTime = audioContext.currentTime;
|
BrechtDeMan@938
|
481 this.testStarted = true;
|
BrechtDeMan@938
|
482 this.updateTestTime();
|
BrechtDeMan@938
|
483 audioEngineContext.metric.initialiseTest();
|
BrechtDeMan@938
|
484 }
|
BrechtDeMan@938
|
485 };
|
BrechtDeMan@938
|
486 this.stopTest = function()
|
BrechtDeMan@938
|
487 {
|
BrechtDeMan@938
|
488 if (this.testStarted)
|
BrechtDeMan@938
|
489 {
|
BrechtDeMan@938
|
490 this.testDuration = this.getTestTime();
|
BrechtDeMan@938
|
491 this.testStarted = false;
|
BrechtDeMan@938
|
492 } else {
|
BrechtDeMan@938
|
493 console.log('ERR: Test tried to end before beginning');
|
BrechtDeMan@938
|
494 }
|
BrechtDeMan@938
|
495 };
|
BrechtDeMan@938
|
496 this.updateTestTime = function()
|
BrechtDeMan@938
|
497 {
|
BrechtDeMan@938
|
498 if (this.testStarted)
|
BrechtDeMan@938
|
499 {
|
BrechtDeMan@938
|
500 this.testDuration = audioContext.currentTime - this.testStartTime;
|
BrechtDeMan@938
|
501 }
|
BrechtDeMan@938
|
502 };
|
BrechtDeMan@938
|
503 this.getTestTime = function()
|
BrechtDeMan@938
|
504 {
|
BrechtDeMan@938
|
505 this.updateTestTime();
|
BrechtDeMan@938
|
506 return this.testDuration;
|
BrechtDeMan@938
|
507 };
|
BrechtDeMan@938
|
508 }
|
BrechtDeMan@938
|
509
|
BrechtDeMan@938
|
510 function sessionMetrics(engine)
|
BrechtDeMan@938
|
511 {
|
BrechtDeMan@938
|
512 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
|
BrechtDeMan@938
|
513 */
|
BrechtDeMan@938
|
514 this.engine = engine;
|
BrechtDeMan@938
|
515 this.lastClicked = -1;
|
BrechtDeMan@938
|
516 this.data = -1;
|
n@950
|
517 this.reset = function() {
|
n@950
|
518 this.lastClicked = -1;
|
n@950
|
519 this.data = -1;
|
n@950
|
520 };
|
BrechtDeMan@938
|
521 this.initialiseTest = function(){};
|
BrechtDeMan@938
|
522 }
|
BrechtDeMan@938
|
523
|
BrechtDeMan@938
|
524 function metricTracker()
|
BrechtDeMan@938
|
525 {
|
BrechtDeMan@938
|
526 /* Custom object to track and collect metric data
|
BrechtDeMan@938
|
527 * Used only inside the audioObjects object.
|
BrechtDeMan@938
|
528 */
|
BrechtDeMan@938
|
529
|
BrechtDeMan@938
|
530 this.listenedTimer = 0;
|
BrechtDeMan@938
|
531 this.listenStart = 0;
|
nicholas@947
|
532 this.listenHold = false;
|
BrechtDeMan@938
|
533 this.initialPosition = -1;
|
BrechtDeMan@938
|
534 this.movementTracker = [];
|
BrechtDeMan@938
|
535 this.wasListenedTo = false;
|
BrechtDeMan@938
|
536 this.wasMoved = false;
|
BrechtDeMan@938
|
537 this.hasComments = false;
|
BrechtDeMan@938
|
538
|
BrechtDeMan@938
|
539 this.initialised = function(position)
|
BrechtDeMan@938
|
540 {
|
BrechtDeMan@938
|
541 if (this.initialPosition == -1) {
|
BrechtDeMan@938
|
542 this.initialPosition = position;
|
BrechtDeMan@938
|
543 }
|
BrechtDeMan@938
|
544 };
|
BrechtDeMan@938
|
545
|
BrechtDeMan@938
|
546 this.moved = function(time,position)
|
BrechtDeMan@938
|
547 {
|
BrechtDeMan@938
|
548 this.wasMoved = true;
|
BrechtDeMan@938
|
549 this.movementTracker[this.movementTracker.length] = [time, position];
|
BrechtDeMan@938
|
550 };
|
BrechtDeMan@938
|
551
|
BrechtDeMan@938
|
552 this.listening = function(time)
|
BrechtDeMan@938
|
553 {
|
nicholas@947
|
554 if (this.listenHold == false)
|
BrechtDeMan@938
|
555 {
|
BrechtDeMan@938
|
556 this.wasListenedTo = true;
|
BrechtDeMan@938
|
557 this.listenStart = time;
|
nicholas@947
|
558 this.listenHold = true;
|
BrechtDeMan@938
|
559 } else {
|
BrechtDeMan@938
|
560 this.listenedTimer += (time - this.listenStart);
|
BrechtDeMan@938
|
561 this.listenStart = 0;
|
nicholas@947
|
562 this.listenHold = false;
|
BrechtDeMan@938
|
563 }
|
BrechtDeMan@938
|
564 };
|
BrechtDeMan@938
|
565 }
|
BrechtDeMan@938
|
566
|
BrechtDeMan@938
|
567 function randomiseOrder(input)
|
BrechtDeMan@938
|
568 {
|
BrechtDeMan@938
|
569 // This takes an array of information and randomises the order
|
BrechtDeMan@938
|
570 var N = input.length;
|
BrechtDeMan@938
|
571 var K = N;
|
BrechtDeMan@938
|
572 var holdArr = [];
|
BrechtDeMan@938
|
573 for (var n=0; n<N; n++)
|
BrechtDeMan@938
|
574 {
|
BrechtDeMan@938
|
575 // First pick a random number
|
BrechtDeMan@938
|
576 var r = Math.random();
|
BrechtDeMan@938
|
577 // Multiply and floor by the number of elements left
|
BrechtDeMan@938
|
578 r = Math.floor(r*input.length);
|
BrechtDeMan@938
|
579 // Pick out that element and delete from the array
|
BrechtDeMan@938
|
580 holdArr.push(input.splice(r,1)[0]);
|
BrechtDeMan@938
|
581 }
|
BrechtDeMan@938
|
582 return holdArr;
|
BrechtDeMan@938
|
583 } |