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