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@1624
|
181 console.log('ALL COLLECTED!');
|
nickjillings@1624
|
182 createProjectSave(projectReturn);
|
nickjillings@1622
|
183 }else if (currentState.substr(0,10) == 'testRunPre')
|
nickjillings@1622
|
184 {
|
nickjillings@1622
|
185 // Start the test
|
nickjillings@1622
|
186 var testId = currentState.substr(11,currentState.length-10);
|
nickjillings@1622
|
187 currentState = 'testRun-'+testId;
|
nickjillings@1622
|
188 currentTestHolder.appendChild(popup.responses);
|
nickjillings@1622
|
189 //audioEngineContext.timer.startTest();
|
nickjillings@1622
|
190 //audioEngineContext.play();
|
nickjillings@1622
|
191 } else if (currentState.substr(0,11) == 'testRunPost')
|
nickjillings@1622
|
192 {
|
nickjillings@1622
|
193 var testId = currentState.substr(12,currentState.length-11);
|
nickjillings@1622
|
194 currentTestHolder.appendChild(popup.responses);
|
nickjillings@1622
|
195 testEnded(testId);
|
nickjillings@1622
|
196 } else if (currentState.substr(0,7) == 'testRun')
|
nickjillings@1622
|
197 {
|
nickjillings@1622
|
198 var testId = currentState.substr(8,currentState.length-7);
|
nickjillings@1622
|
199 // Check if we have any post tests to perform
|
nickjillings@1622
|
200 var postXML = $(testXMLSetups[testId]).find('PostTest')[0];
|
nickjillings@1622
|
201 if (postXML == undefined || postXML.childElementCount == 0) {
|
nickjillings@1622
|
202 testEnded(testId);
|
nickjillings@1622
|
203 }
|
nickjillings@1622
|
204 else if (postXML.childElementCount > 0)
|
nickjillings@1622
|
205 {
|
nickjillings@1622
|
206 currentState = 'testRunPost-'+testId;
|
nickjillings@1622
|
207 popup.initState(postXML);
|
nickjillings@1622
|
208 }
|
nickjillings@1621
|
209 }
|
nickjillings@1622
|
210 console.log(currentState);
|
nickjillings@1621
|
211 }
|
nickjillings@1621
|
212
|
nickjillings@1622
|
213 function testEnded(testId)
|
nickjillings@1621
|
214 {
|
nickjillings@1622
|
215 pageXMLSave(testId);
|
nickjillings@1622
|
216 if (testXMLSetups.length-1 > testId)
|
nickjillings@1622
|
217 {
|
nickjillings@1622
|
218 // Yes we have another test to perform
|
nickjillings@1622
|
219 testId = (Number(testId)+1);
|
nickjillings@1622
|
220 currentState = 'testRun-'+testId;
|
nickjillings@1622
|
221 loadTest(testId);
|
nickjillings@1622
|
222 } else {
|
nickjillings@1622
|
223 console.log('Testing Completed!');
|
nickjillings@1622
|
224 currentState = 'postTest';
|
nickjillings@1622
|
225 // Check for any post tests
|
nickjillings@1622
|
226 var xmlSetup = projectXML.find('setup');
|
nickjillings@1622
|
227 var postTest = xmlSetup.find('PostTest')[0];
|
nickjillings@1622
|
228 popup.initState(postTest);
|
nickjillings@1622
|
229 }
|
nickjillings@1621
|
230 }
|
nickjillings@1621
|
231
|
b@1608
|
232 function loadProjectSpec(url) {
|
b@1608
|
233 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
|
b@1608
|
234 // If url is null, request client to upload project XML document
|
b@1608
|
235 var r = new XMLHttpRequest();
|
b@1608
|
236 r.open('GET',url,true);
|
b@1608
|
237 r.onload = function() {
|
b@1608
|
238 loadProjectSpecCallback(r.response);
|
b@1608
|
239 };
|
b@1608
|
240 r.send();
|
b@1608
|
241 };
|
b@1608
|
242
|
b@1608
|
243 function loadProjectSpecCallback(response) {
|
b@1608
|
244 // Function called after asynchronous download of XML project specification
|
b@1608
|
245 var decode = $.parseXML(response);
|
b@1608
|
246 projectXML = $(decode);
|
b@1608
|
247
|
b@1608
|
248 // Now extract the setup tag
|
b@1608
|
249 var xmlSetup = projectXML.find('setup');
|
b@1608
|
250 // Detect the interface to use and load the relevant javascripts.
|
b@1608
|
251 var interfaceType = xmlSetup[0].attributes['interface'];
|
b@1608
|
252 var interfaceJS = document.createElement('script');
|
b@1608
|
253 interfaceJS.setAttribute("type","text/javascript");
|
b@1608
|
254 if (interfaceType.value == 'APE') {
|
b@1608
|
255 interfaceJS.setAttribute("src","ape.js");
|
b@1608
|
256
|
b@1608
|
257 // APE comes with a css file
|
b@1608
|
258 var css = document.createElement('link');
|
b@1608
|
259 css.rel = 'stylesheet';
|
b@1608
|
260 css.type = 'text/css';
|
b@1608
|
261 css.href = 'ape.css';
|
b@1608
|
262
|
b@1608
|
263 document.getElementsByTagName("head")[0].appendChild(css);
|
b@1608
|
264 }
|
b@1608
|
265 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
|
b@1608
|
266 }
|
b@1608
|
267
|
b@1608
|
268 function createProjectSave(destURL) {
|
b@1608
|
269 // Save the data from interface into XML and send to destURL
|
b@1608
|
270 // If destURL is null then download XML in client
|
b@1608
|
271 // Now time to render file locally
|
b@1608
|
272 var xmlDoc = interfaceXMLSave();
|
nickjillings@1628
|
273 var parent = document.createElement("div");
|
nickjillings@1628
|
274 parent.appendChild(xmlDoc);
|
nickjillings@1628
|
275 var file = [parent.innerHTML];
|
b@1608
|
276 if (destURL == "null" || destURL == undefined) {
|
b@1608
|
277 var bb = new Blob(file,{type : 'application/xml'});
|
b@1608
|
278 var dnlk = window.URL.createObjectURL(bb);
|
b@1608
|
279 var a = document.createElement("a");
|
b@1608
|
280 a.hidden = '';
|
b@1608
|
281 a.href = dnlk;
|
b@1608
|
282 a.download = "save.xml";
|
b@1608
|
283 a.textContent = "Save File";
|
b@1608
|
284
|
b@1608
|
285 var submitDiv = document.getElementById('download-point');
|
b@1608
|
286 submitDiv.appendChild(a);
|
nickjillings@1624
|
287 popup.showPopup();
|
nickjillings@1624
|
288 popup.popupContent.innerHTML = null;
|
nickjillings@1624
|
289 popup.popupContent.appendChild(submitDiv)
|
nickjillings@1628
|
290 } else {
|
nickjillings@1628
|
291 var xmlhttp = new XMLHttpRequest;
|
nickjillings@1628
|
292 xmlhttp.open("POST",destURL,true);
|
nickjillings@1628
|
293 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
|
nickjillings@1628
|
294 xmlhttp.send(file);
|
b@1608
|
295 }
|
b@1608
|
296 return submitDiv;
|
b@1608
|
297 }
|
b@1608
|
298
|
b@1608
|
299 function AudioEngine() {
|
b@1608
|
300
|
b@1608
|
301 // Create two output paths, the main outputGain and fooGain.
|
b@1608
|
302 // Output gain is default to 1 and any items for playback route here
|
b@1608
|
303 // Foo gain is used for analysis to ensure paths get processed, but are not heard
|
b@1608
|
304 // because web audio will optimise and any route which does not go to the destination gets ignored.
|
b@1608
|
305 this.outputGain = audioContext.createGain();
|
b@1608
|
306 this.fooGain = audioContext.createGain();
|
b@1608
|
307 this.fooGain.gain = 0;
|
b@1608
|
308
|
b@1608
|
309 // Use this to detect playback state: 0 - stopped, 1 - playing
|
b@1608
|
310 this.status = 0;
|
nickjillings@1620
|
311 this.audioObjectsReady = false;
|
b@1608
|
312
|
b@1608
|
313 // Connect both gains to output
|
b@1608
|
314 this.outputGain.connect(audioContext.destination);
|
b@1608
|
315 this.fooGain.connect(audioContext.destination);
|
b@1608
|
316
|
b@1608
|
317 // Create the timer Object
|
b@1608
|
318 this.timer = new timer();
|
b@1608
|
319 // Create session metrics
|
b@1608
|
320 this.metric = new sessionMetrics(this);
|
b@1608
|
321
|
b@1608
|
322 this.loopPlayback = false;
|
b@1608
|
323
|
b@1608
|
324 // Create store for new audioObjects
|
b@1608
|
325 this.audioObjects = [];
|
b@1608
|
326
|
nickjillings@1620
|
327 this.play = function() {
|
nickjillings@1620
|
328 // Start the timer and set the audioEngine state to playing (1)
|
nickjillings@1620
|
329 if (this.status == 0) {
|
nickjillings@1620
|
330 // Check if all audioObjects are ready
|
nickjillings@1620
|
331 if (this.audioObjectsReady == false) {
|
nickjillings@1620
|
332 this.audioObjectsReady = this.checkAllReady();
|
nickjillings@1620
|
333 }
|
nickjillings@1620
|
334 if (this.audioObjectsReady == true) {
|
nickjillings@1620
|
335 this.timer.startTest();
|
nickjillings@1620
|
336 this.status = 1;
|
nickjillings@1620
|
337 }
|
nickjillings@1620
|
338 }
|
nickjillings@1620
|
339 };
|
b@1608
|
340
|
nickjillings@1620
|
341 this.stop = function() {
|
nickjillings@1620
|
342 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
|
nickjillings@1620
|
343 if (this.status == 1) {
|
nickjillings@1620
|
344 for (var i=0; i<this.audioObjects.length; i++)
|
nickjillings@1620
|
345 {
|
nickjillings@1620
|
346 this.audioObjects[i].stop();
|
nickjillings@1620
|
347 }
|
nickjillings@1620
|
348 this.status = 0;
|
nickjillings@1620
|
349 }
|
nickjillings@1620
|
350 };
|
b@1608
|
351
|
b@1608
|
352
|
b@1608
|
353 this.newTrack = function(url) {
|
b@1608
|
354 // Pull data from given URL into new audio buffer
|
b@1608
|
355 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
|
b@1608
|
356
|
b@1608
|
357 // Create the audioObject with ID of the new track length;
|
b@1608
|
358 audioObjectId = this.audioObjects.length;
|
b@1608
|
359 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
|
b@1608
|
360
|
b@1608
|
361 // AudioObject will get track itself.
|
b@1608
|
362 this.audioObjects[audioObjectId].constructTrack(url);
|
b@1608
|
363 };
|
b@1608
|
364
|
nickjillings@1620
|
365 this.newTestPage = function() {
|
nickjillings@1620
|
366 this.state = 0;
|
nickjillings@1620
|
367 this.audioObjectsReady = false;
|
nickjillings@1620
|
368 this.metric.reset();
|
nickjillings@1620
|
369 this.audioObjects = [];
|
nickjillings@1620
|
370 };
|
nickjillings@1620
|
371
|
nickjillings@1614
|
372 this.checkAllPlayed = function() {
|
nickjillings@1614
|
373 arr = [];
|
nickjillings@1614
|
374 for (var id=0; id<this.audioObjects.length; id++) {
|
nickjillings@1614
|
375 if (this.audioObjects[id].played == false) {
|
nickjillings@1614
|
376 arr.push(this.audioObjects[id].id);
|
nickjillings@1614
|
377 }
|
nickjillings@1614
|
378 }
|
nickjillings@1614
|
379 return arr;
|
nickjillings@1614
|
380 };
|
nickjillings@1614
|
381
|
nickjillings@1620
|
382 this.checkAllReady = function() {
|
nickjillings@1620
|
383 var ready = true;
|
nickjillings@1620
|
384 for (var i=0; i<this.audioObjects.length; i++) {
|
nickjillings@1620
|
385 if (this.audioObjects[i].state == 0) {
|
nickjillings@1620
|
386 // Track not ready
|
nickjillings@1620
|
387 console.log('WAIT -- audioObject '+i+' not ready yet!');
|
nickjillings@1620
|
388 ready = false;
|
nickjillings@1620
|
389 };
|
nickjillings@1620
|
390 }
|
nickjillings@1620
|
391 return ready;
|
nickjillings@1620
|
392 };
|
nickjillings@1620
|
393
|
b@1608
|
394 }
|
b@1608
|
395
|
b@1608
|
396 function audioObject(id) {
|
b@1608
|
397 // The main buffer object with common control nodes to the AudioEngine
|
b@1608
|
398
|
b@1608
|
399 this.id = id;
|
b@1608
|
400 this.state = 0; // 0 - no data, 1 - ready
|
b@1608
|
401 this.url = null; // Hold the URL given for the output back to the results.
|
b@1608
|
402 this.metric = new metricTracker();
|
b@1608
|
403
|
nickjillings@1614
|
404 this.played = false;
|
nickjillings@1614
|
405
|
b@1608
|
406 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
|
b@1608
|
407 this.bufferNode = undefined;
|
b@1608
|
408 this.outputGain = audioContext.createGain();
|
b@1608
|
409
|
b@1608
|
410 // Default output gain to be zero
|
b@1608
|
411 this.outputGain.gain.value = 0.0;
|
b@1608
|
412
|
b@1608
|
413 // Connect buffer to the audio graph
|
b@1608
|
414 this.outputGain.connect(audioEngineContext.outputGain);
|
b@1608
|
415
|
b@1608
|
416 // the audiobuffer is not designed for multi-start playback
|
b@1608
|
417 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
|
b@1608
|
418 this.buffer;
|
b@1608
|
419
|
b@1608
|
420 this.play = function(startTime) {
|
b@1608
|
421 this.bufferNode = audioContext.createBufferSource();
|
nickjillings@1617
|
422 this.bufferNode.owner = this;
|
b@1608
|
423 this.bufferNode.connect(this.outputGain);
|
b@1608
|
424 this.bufferNode.buffer = this.buffer;
|
b@1608
|
425 this.bufferNode.loop = audioEngineContext.loopPlayback;
|
nickjillings@1617
|
426 if (this.bufferNode.loop == false) {
|
nickjillings@1617
|
427 this.bufferNode.onended = function() {
|
nickjillings@1617
|
428 this.owner.metric.listening(audioEngineContext.timer.getTestTime());
|
nickjillings@1620
|
429 };
|
nickjillings@1617
|
430 }
|
nickjillings@1617
|
431 this.metric.listening(audioEngineContext.timer.getTestTime());
|
b@1608
|
432 this.bufferNode.start(startTime);
|
nickjillings@1614
|
433 this.played = true;
|
b@1608
|
434 };
|
b@1608
|
435
|
b@1608
|
436 this.stop = function() {
|
b@1607
|
437 if (this.bufferNode != undefined)
|
b@1607
|
438 {
|
b@1607
|
439 this.bufferNode.stop(0);
|
b@1607
|
440 this.bufferNode = undefined;
|
nickjillings@1617
|
441 this.metric.listening(audioEngineContext.timer.getTestTime());
|
b@1607
|
442 }
|
b@1608
|
443 };
|
b@1608
|
444
|
b@1608
|
445 this.constructTrack = function(url) {
|
b@1608
|
446 var request = new XMLHttpRequest();
|
b@1608
|
447 this.url = url;
|
b@1608
|
448 request.open('GET',url,true);
|
b@1608
|
449 request.responseType = 'arraybuffer';
|
b@1608
|
450
|
b@1608
|
451 var audioObj = this;
|
b@1608
|
452
|
b@1608
|
453 // Create callback to decode the data asynchronously
|
b@1608
|
454 request.onloadend = function() {
|
b@1608
|
455 audioContext.decodeAudioData(request.response, function(decodedData) {
|
b@1608
|
456 audioObj.buffer = decodedData;
|
b@1608
|
457 audioObj.state = 1;
|
b@1608
|
458 }, function(){
|
b@1608
|
459 // Should only be called if there was an error, but sometimes gets called continuously
|
b@1608
|
460 // Check here if the error is genuine
|
b@1608
|
461 if (audioObj.state == 0 || audioObj.buffer == undefined) {
|
b@1608
|
462 // Genuine error
|
b@1608
|
463 console.log('FATAL - Error loading buffer on '+audioObj.id);
|
b@1608
|
464 }
|
b@1608
|
465 });
|
b@1608
|
466 };
|
b@1608
|
467 request.send();
|
b@1608
|
468 };
|
b@1608
|
469
|
b@1608
|
470 }
|
b@1608
|
471
|
b@1608
|
472 function timer()
|
b@1608
|
473 {
|
b@1608
|
474 /* Timer object used in audioEngine to keep track of session timings
|
b@1608
|
475 * Uses the timer of the web audio API, so sample resolution
|
b@1608
|
476 */
|
b@1608
|
477 this.testStarted = false;
|
b@1608
|
478 this.testStartTime = 0;
|
b@1608
|
479 this.testDuration = 0;
|
b@1608
|
480 this.minimumTestTime = 0; // No minimum test time
|
b@1608
|
481 this.startTest = function()
|
b@1608
|
482 {
|
b@1608
|
483 if (this.testStarted == false)
|
b@1608
|
484 {
|
b@1608
|
485 this.testStartTime = audioContext.currentTime;
|
b@1608
|
486 this.testStarted = true;
|
b@1608
|
487 this.updateTestTime();
|
b@1608
|
488 audioEngineContext.metric.initialiseTest();
|
b@1608
|
489 }
|
b@1608
|
490 };
|
b@1608
|
491 this.stopTest = function()
|
b@1608
|
492 {
|
b@1608
|
493 if (this.testStarted)
|
b@1608
|
494 {
|
b@1608
|
495 this.testDuration = this.getTestTime();
|
b@1608
|
496 this.testStarted = false;
|
b@1608
|
497 } else {
|
b@1608
|
498 console.log('ERR: Test tried to end before beginning');
|
b@1608
|
499 }
|
b@1608
|
500 };
|
b@1608
|
501 this.updateTestTime = function()
|
b@1608
|
502 {
|
b@1608
|
503 if (this.testStarted)
|
b@1608
|
504 {
|
b@1608
|
505 this.testDuration = audioContext.currentTime - this.testStartTime;
|
b@1608
|
506 }
|
b@1608
|
507 };
|
b@1608
|
508 this.getTestTime = function()
|
b@1608
|
509 {
|
b@1608
|
510 this.updateTestTime();
|
b@1608
|
511 return this.testDuration;
|
b@1608
|
512 };
|
b@1608
|
513 }
|
b@1608
|
514
|
b@1608
|
515 function sessionMetrics(engine)
|
b@1608
|
516 {
|
b@1608
|
517 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
|
b@1608
|
518 */
|
b@1608
|
519 this.engine = engine;
|
b@1608
|
520 this.lastClicked = -1;
|
b@1608
|
521 this.data = -1;
|
nickjillings@1620
|
522 this.reset = function() {
|
nickjillings@1620
|
523 this.lastClicked = -1;
|
nickjillings@1620
|
524 this.data = -1;
|
nickjillings@1620
|
525 };
|
b@1608
|
526 this.initialiseTest = function(){};
|
b@1608
|
527 }
|
b@1608
|
528
|
b@1608
|
529 function metricTracker()
|
b@1608
|
530 {
|
b@1608
|
531 /* Custom object to track and collect metric data
|
b@1608
|
532 * Used only inside the audioObjects object.
|
b@1608
|
533 */
|
b@1608
|
534
|
b@1608
|
535 this.listenedTimer = 0;
|
b@1608
|
536 this.listenStart = 0;
|
nickjillings@1617
|
537 this.listenHold = false;
|
b@1608
|
538 this.initialPosition = -1;
|
b@1608
|
539 this.movementTracker = [];
|
b@1608
|
540 this.wasListenedTo = false;
|
b@1608
|
541 this.wasMoved = false;
|
b@1608
|
542 this.hasComments = false;
|
b@1608
|
543
|
b@1608
|
544 this.initialised = function(position)
|
b@1608
|
545 {
|
b@1608
|
546 if (this.initialPosition == -1) {
|
b@1608
|
547 this.initialPosition = position;
|
b@1608
|
548 }
|
b@1608
|
549 };
|
b@1608
|
550
|
b@1608
|
551 this.moved = function(time,position)
|
b@1608
|
552 {
|
b@1608
|
553 this.wasMoved = true;
|
b@1608
|
554 this.movementTracker[this.movementTracker.length] = [time, position];
|
b@1608
|
555 };
|
b@1608
|
556
|
b@1608
|
557 this.listening = function(time)
|
b@1608
|
558 {
|
nickjillings@1617
|
559 if (this.listenHold == false)
|
b@1608
|
560 {
|
b@1608
|
561 this.wasListenedTo = true;
|
b@1608
|
562 this.listenStart = time;
|
nickjillings@1617
|
563 this.listenHold = true;
|
b@1608
|
564 } else {
|
b@1608
|
565 this.listenedTimer += (time - this.listenStart);
|
b@1608
|
566 this.listenStart = 0;
|
nickjillings@1617
|
567 this.listenHold = false;
|
b@1608
|
568 }
|
b@1608
|
569 };
|
b@1608
|
570 }
|
b@1608
|
571
|
b@1608
|
572 function randomiseOrder(input)
|
b@1608
|
573 {
|
b@1608
|
574 // This takes an array of information and randomises the order
|
b@1608
|
575 var N = input.length;
|
b@1608
|
576 var K = N;
|
b@1608
|
577 var holdArr = [];
|
b@1608
|
578 for (var n=0; n<N; n++)
|
b@1608
|
579 {
|
b@1608
|
580 // First pick a random number
|
b@1608
|
581 var r = Math.random();
|
b@1608
|
582 // Multiply and floor by the number of elements left
|
b@1608
|
583 r = Math.floor(r*input.length);
|
b@1608
|
584 // Pick out that element and delete from the array
|
b@1608
|
585 holdArr.push(input.splice(r,1)[0]);
|
b@1608
|
586 }
|
b@1608
|
587 return holdArr;
|
b@1608
|
588 } |