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);
|
nickjillings@1633
|
266
|
nickjillings@1633
|
267 // Define window callbacks for interface
|
nickjillings@1633
|
268 window.onresize = function(event){resizeWindow(event);};
|
b@1608
|
269 }
|
b@1608
|
270
|
b@1608
|
271 function createProjectSave(destURL) {
|
b@1608
|
272 // Save the data from interface into XML and send to destURL
|
b@1608
|
273 // If destURL is null then download XML in client
|
b@1608
|
274 // Now time to render file locally
|
b@1608
|
275 var xmlDoc = interfaceXMLSave();
|
nickjillings@1628
|
276 var parent = document.createElement("div");
|
nickjillings@1628
|
277 parent.appendChild(xmlDoc);
|
nickjillings@1628
|
278 var file = [parent.innerHTML];
|
b@1608
|
279 if (destURL == "null" || destURL == undefined) {
|
b@1608
|
280 var bb = new Blob(file,{type : 'application/xml'});
|
b@1608
|
281 var dnlk = window.URL.createObjectURL(bb);
|
b@1608
|
282 var a = document.createElement("a");
|
b@1608
|
283 a.hidden = '';
|
b@1608
|
284 a.href = dnlk;
|
b@1608
|
285 a.download = "save.xml";
|
b@1608
|
286 a.textContent = "Save File";
|
b@1608
|
287
|
b@1608
|
288 var submitDiv = document.getElementById('download-point');
|
b@1608
|
289 submitDiv.appendChild(a);
|
nickjillings@1624
|
290 popup.showPopup();
|
nickjillings@1624
|
291 popup.popupContent.innerHTML = null;
|
nickjillings@1624
|
292 popup.popupContent.appendChild(submitDiv)
|
nickjillings@1628
|
293 } else {
|
nickjillings@1628
|
294 var xmlhttp = new XMLHttpRequest;
|
nickjillings@1628
|
295 xmlhttp.open("POST",destURL,true);
|
nickjillings@1628
|
296 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
|
nickjillings@1629
|
297 xmlhttp.onerror = function(){
|
nickjillings@1629
|
298 console.log('Error saving file to server! Presenting download locally');
|
nickjillings@1629
|
299 createProjectSave(null);
|
nickjillings@1629
|
300 };
|
nickjillings@1628
|
301 xmlhttp.send(file);
|
b@1608
|
302 }
|
b@1608
|
303 return submitDiv;
|
b@1608
|
304 }
|
b@1608
|
305
|
b@1608
|
306 function AudioEngine() {
|
b@1608
|
307
|
b@1608
|
308 // Create two output paths, the main outputGain and fooGain.
|
b@1608
|
309 // Output gain is default to 1 and any items for playback route here
|
b@1608
|
310 // Foo gain is used for analysis to ensure paths get processed, but are not heard
|
b@1608
|
311 // because web audio will optimise and any route which does not go to the destination gets ignored.
|
b@1608
|
312 this.outputGain = audioContext.createGain();
|
b@1608
|
313 this.fooGain = audioContext.createGain();
|
b@1608
|
314 this.fooGain.gain = 0;
|
b@1608
|
315
|
b@1608
|
316 // Use this to detect playback state: 0 - stopped, 1 - playing
|
b@1608
|
317 this.status = 0;
|
nickjillings@1620
|
318 this.audioObjectsReady = false;
|
b@1608
|
319
|
b@1608
|
320 // Connect both gains to output
|
b@1608
|
321 this.outputGain.connect(audioContext.destination);
|
b@1608
|
322 this.fooGain.connect(audioContext.destination);
|
b@1608
|
323
|
b@1608
|
324 // Create the timer Object
|
b@1608
|
325 this.timer = new timer();
|
b@1608
|
326 // Create session metrics
|
b@1608
|
327 this.metric = new sessionMetrics(this);
|
b@1608
|
328
|
b@1608
|
329 this.loopPlayback = false;
|
b@1608
|
330
|
b@1608
|
331 // Create store for new audioObjects
|
b@1608
|
332 this.audioObjects = [];
|
b@1608
|
333
|
nickjillings@1620
|
334 this.play = function() {
|
nickjillings@1620
|
335 // Start the timer and set the audioEngine state to playing (1)
|
nickjillings@1620
|
336 if (this.status == 0) {
|
nickjillings@1620
|
337 // Check if all audioObjects are ready
|
nickjillings@1620
|
338 if (this.audioObjectsReady == false) {
|
nickjillings@1620
|
339 this.audioObjectsReady = this.checkAllReady();
|
nickjillings@1620
|
340 }
|
nickjillings@1620
|
341 if (this.audioObjectsReady == true) {
|
nickjillings@1620
|
342 this.timer.startTest();
|
nickjillings@1620
|
343 this.status = 1;
|
nickjillings@1620
|
344 }
|
nickjillings@1620
|
345 }
|
nickjillings@1620
|
346 };
|
b@1608
|
347
|
nickjillings@1620
|
348 this.stop = function() {
|
nickjillings@1620
|
349 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
|
nickjillings@1620
|
350 if (this.status == 1) {
|
nickjillings@1620
|
351 for (var i=0; i<this.audioObjects.length; i++)
|
nickjillings@1620
|
352 {
|
nickjillings@1620
|
353 this.audioObjects[i].stop();
|
nickjillings@1620
|
354 }
|
nickjillings@1620
|
355 this.status = 0;
|
nickjillings@1620
|
356 }
|
nickjillings@1620
|
357 };
|
b@1608
|
358
|
b@1608
|
359
|
b@1608
|
360 this.newTrack = function(url) {
|
b@1608
|
361 // Pull data from given URL into new audio buffer
|
b@1608
|
362 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
|
b@1608
|
363
|
b@1608
|
364 // Create the audioObject with ID of the new track length;
|
b@1608
|
365 audioObjectId = this.audioObjects.length;
|
b@1608
|
366 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
|
b@1608
|
367
|
b@1608
|
368 // AudioObject will get track itself.
|
b@1608
|
369 this.audioObjects[audioObjectId].constructTrack(url);
|
b@1608
|
370 };
|
b@1608
|
371
|
nickjillings@1620
|
372 this.newTestPage = function() {
|
nickjillings@1620
|
373 this.state = 0;
|
nickjillings@1620
|
374 this.audioObjectsReady = false;
|
nickjillings@1620
|
375 this.metric.reset();
|
nickjillings@1620
|
376 this.audioObjects = [];
|
nickjillings@1620
|
377 };
|
nickjillings@1620
|
378
|
nickjillings@1614
|
379 this.checkAllPlayed = function() {
|
nickjillings@1614
|
380 arr = [];
|
nickjillings@1614
|
381 for (var id=0; id<this.audioObjects.length; id++) {
|
nickjillings@1614
|
382 if (this.audioObjects[id].played == false) {
|
nickjillings@1614
|
383 arr.push(this.audioObjects[id].id);
|
nickjillings@1614
|
384 }
|
nickjillings@1614
|
385 }
|
nickjillings@1614
|
386 return arr;
|
nickjillings@1614
|
387 };
|
nickjillings@1614
|
388
|
nickjillings@1620
|
389 this.checkAllReady = function() {
|
nickjillings@1620
|
390 var ready = true;
|
nickjillings@1620
|
391 for (var i=0; i<this.audioObjects.length; i++) {
|
nickjillings@1620
|
392 if (this.audioObjects[i].state == 0) {
|
nickjillings@1620
|
393 // Track not ready
|
nickjillings@1620
|
394 console.log('WAIT -- audioObject '+i+' not ready yet!');
|
nickjillings@1620
|
395 ready = false;
|
nickjillings@1620
|
396 };
|
nickjillings@1620
|
397 }
|
nickjillings@1620
|
398 return ready;
|
nickjillings@1620
|
399 };
|
nickjillings@1620
|
400
|
b@1608
|
401 }
|
b@1608
|
402
|
b@1608
|
403 function audioObject(id) {
|
b@1608
|
404 // The main buffer object with common control nodes to the AudioEngine
|
b@1608
|
405
|
b@1608
|
406 this.id = id;
|
b@1608
|
407 this.state = 0; // 0 - no data, 1 - ready
|
b@1608
|
408 this.url = null; // Hold the URL given for the output back to the results.
|
b@1608
|
409 this.metric = new metricTracker();
|
b@1608
|
410
|
nickjillings@1614
|
411 this.played = false;
|
nickjillings@1614
|
412
|
b@1608
|
413 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
|
b@1608
|
414 this.bufferNode = undefined;
|
b@1608
|
415 this.outputGain = audioContext.createGain();
|
b@1608
|
416
|
b@1608
|
417 // Default output gain to be zero
|
b@1608
|
418 this.outputGain.gain.value = 0.0;
|
b@1608
|
419
|
b@1608
|
420 // Connect buffer to the audio graph
|
b@1608
|
421 this.outputGain.connect(audioEngineContext.outputGain);
|
b@1608
|
422
|
b@1608
|
423 // the audiobuffer is not designed for multi-start playback
|
b@1608
|
424 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
|
b@1608
|
425 this.buffer;
|
b@1608
|
426
|
b@1608
|
427 this.play = function(startTime) {
|
b@1608
|
428 this.bufferNode = audioContext.createBufferSource();
|
nickjillings@1617
|
429 this.bufferNode.owner = this;
|
b@1608
|
430 this.bufferNode.connect(this.outputGain);
|
b@1608
|
431 this.bufferNode.buffer = this.buffer;
|
b@1608
|
432 this.bufferNode.loop = audioEngineContext.loopPlayback;
|
nickjillings@1617
|
433 if (this.bufferNode.loop == false) {
|
nickjillings@1617
|
434 this.bufferNode.onended = function() {
|
nickjillings@1617
|
435 this.owner.metric.listening(audioEngineContext.timer.getTestTime());
|
nickjillings@1620
|
436 };
|
nickjillings@1617
|
437 }
|
nickjillings@1617
|
438 this.metric.listening(audioEngineContext.timer.getTestTime());
|
b@1608
|
439 this.bufferNode.start(startTime);
|
nickjillings@1614
|
440 this.played = true;
|
b@1608
|
441 };
|
b@1608
|
442
|
b@1608
|
443 this.stop = function() {
|
b@1607
|
444 if (this.bufferNode != undefined)
|
b@1607
|
445 {
|
b@1607
|
446 this.bufferNode.stop(0);
|
b@1607
|
447 this.bufferNode = undefined;
|
nickjillings@1617
|
448 this.metric.listening(audioEngineContext.timer.getTestTime());
|
b@1607
|
449 }
|
b@1608
|
450 };
|
b@1608
|
451
|
b@1608
|
452 this.constructTrack = function(url) {
|
b@1608
|
453 var request = new XMLHttpRequest();
|
b@1608
|
454 this.url = url;
|
b@1608
|
455 request.open('GET',url,true);
|
b@1608
|
456 request.responseType = 'arraybuffer';
|
b@1608
|
457
|
b@1608
|
458 var audioObj = this;
|
b@1608
|
459
|
b@1608
|
460 // Create callback to decode the data asynchronously
|
b@1608
|
461 request.onloadend = function() {
|
b@1608
|
462 audioContext.decodeAudioData(request.response, function(decodedData) {
|
b@1608
|
463 audioObj.buffer = decodedData;
|
b@1608
|
464 audioObj.state = 1;
|
b@1608
|
465 }, function(){
|
b@1608
|
466 // Should only be called if there was an error, but sometimes gets called continuously
|
b@1608
|
467 // Check here if the error is genuine
|
b@1608
|
468 if (audioObj.state == 0 || audioObj.buffer == undefined) {
|
b@1608
|
469 // Genuine error
|
b@1608
|
470 console.log('FATAL - Error loading buffer on '+audioObj.id);
|
b@1608
|
471 }
|
b@1608
|
472 });
|
b@1608
|
473 };
|
b@1608
|
474 request.send();
|
b@1608
|
475 };
|
b@1608
|
476
|
b@1608
|
477 }
|
b@1608
|
478
|
b@1608
|
479 function timer()
|
b@1608
|
480 {
|
b@1608
|
481 /* Timer object used in audioEngine to keep track of session timings
|
b@1608
|
482 * Uses the timer of the web audio API, so sample resolution
|
b@1608
|
483 */
|
b@1608
|
484 this.testStarted = false;
|
b@1608
|
485 this.testStartTime = 0;
|
b@1608
|
486 this.testDuration = 0;
|
b@1608
|
487 this.minimumTestTime = 0; // No minimum test time
|
b@1608
|
488 this.startTest = function()
|
b@1608
|
489 {
|
b@1608
|
490 if (this.testStarted == false)
|
b@1608
|
491 {
|
b@1608
|
492 this.testStartTime = audioContext.currentTime;
|
b@1608
|
493 this.testStarted = true;
|
b@1608
|
494 this.updateTestTime();
|
b@1608
|
495 audioEngineContext.metric.initialiseTest();
|
b@1608
|
496 }
|
b@1608
|
497 };
|
b@1608
|
498 this.stopTest = function()
|
b@1608
|
499 {
|
b@1608
|
500 if (this.testStarted)
|
b@1608
|
501 {
|
b@1608
|
502 this.testDuration = this.getTestTime();
|
b@1608
|
503 this.testStarted = false;
|
b@1608
|
504 } else {
|
b@1608
|
505 console.log('ERR: Test tried to end before beginning');
|
b@1608
|
506 }
|
b@1608
|
507 };
|
b@1608
|
508 this.updateTestTime = function()
|
b@1608
|
509 {
|
b@1608
|
510 if (this.testStarted)
|
b@1608
|
511 {
|
b@1608
|
512 this.testDuration = audioContext.currentTime - this.testStartTime;
|
b@1608
|
513 }
|
b@1608
|
514 };
|
b@1608
|
515 this.getTestTime = function()
|
b@1608
|
516 {
|
b@1608
|
517 this.updateTestTime();
|
b@1608
|
518 return this.testDuration;
|
b@1608
|
519 };
|
b@1608
|
520 }
|
b@1608
|
521
|
b@1608
|
522 function sessionMetrics(engine)
|
b@1608
|
523 {
|
b@1608
|
524 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
|
b@1608
|
525 */
|
b@1608
|
526 this.engine = engine;
|
b@1608
|
527 this.lastClicked = -1;
|
b@1608
|
528 this.data = -1;
|
nickjillings@1620
|
529 this.reset = function() {
|
nickjillings@1620
|
530 this.lastClicked = -1;
|
nickjillings@1620
|
531 this.data = -1;
|
nickjillings@1620
|
532 };
|
b@1608
|
533 this.initialiseTest = function(){};
|
b@1608
|
534 }
|
b@1608
|
535
|
b@1608
|
536 function metricTracker()
|
b@1608
|
537 {
|
b@1608
|
538 /* Custom object to track and collect metric data
|
b@1608
|
539 * Used only inside the audioObjects object.
|
b@1608
|
540 */
|
b@1608
|
541
|
b@1608
|
542 this.listenedTimer = 0;
|
b@1608
|
543 this.listenStart = 0;
|
nickjillings@1617
|
544 this.listenHold = false;
|
b@1608
|
545 this.initialPosition = -1;
|
b@1608
|
546 this.movementTracker = [];
|
b@1608
|
547 this.wasListenedTo = false;
|
b@1608
|
548 this.wasMoved = false;
|
b@1608
|
549 this.hasComments = false;
|
b@1608
|
550
|
b@1608
|
551 this.initialised = function(position)
|
b@1608
|
552 {
|
b@1608
|
553 if (this.initialPosition == -1) {
|
b@1608
|
554 this.initialPosition = position;
|
b@1608
|
555 }
|
b@1608
|
556 };
|
b@1608
|
557
|
b@1608
|
558 this.moved = function(time,position)
|
b@1608
|
559 {
|
b@1608
|
560 this.wasMoved = true;
|
b@1608
|
561 this.movementTracker[this.movementTracker.length] = [time, position];
|
b@1608
|
562 };
|
b@1608
|
563
|
b@1608
|
564 this.listening = function(time)
|
b@1608
|
565 {
|
nickjillings@1617
|
566 if (this.listenHold == false)
|
b@1608
|
567 {
|
b@1608
|
568 this.wasListenedTo = true;
|
b@1608
|
569 this.listenStart = time;
|
nickjillings@1617
|
570 this.listenHold = true;
|
b@1608
|
571 } else {
|
b@1608
|
572 this.listenedTimer += (time - this.listenStart);
|
b@1608
|
573 this.listenStart = 0;
|
nickjillings@1617
|
574 this.listenHold = false;
|
b@1608
|
575 }
|
b@1608
|
576 };
|
b@1608
|
577 }
|
b@1608
|
578
|
b@1608
|
579 function randomiseOrder(input)
|
b@1608
|
580 {
|
b@1608
|
581 // This takes an array of information and randomises the order
|
b@1608
|
582 var N = input.length;
|
b@1608
|
583 var K = N;
|
b@1608
|
584 var holdArr = [];
|
b@1608
|
585 for (var n=0; n<N; n++)
|
b@1608
|
586 {
|
b@1608
|
587 // First pick a random number
|
b@1608
|
588 var r = Math.random();
|
b@1608
|
589 // Multiply and floor by the number of elements left
|
b@1608
|
590 r = Math.floor(r*input.length);
|
b@1608
|
591 // Pick out that element and delete from the array
|
b@1608
|
592 holdArr.push(input.splice(r,1)[0]);
|
b@1608
|
593 }
|
b@1608
|
594 return holdArr;
|
nickjillings@1631
|
595 }
|
nickjillings@1631
|
596
|
nickjillings@1631
|
597 function returnDateNode()
|
nickjillings@1631
|
598 {
|
nickjillings@1631
|
599 // Create an XML Node for the Date and Time a test was conducted
|
nickjillings@1631
|
600 // Structure is
|
nickjillings@1631
|
601 // <datetime>
|
nickjillings@1631
|
602 // <date year="##" month="##" day="##">DD/MM/YY</date>
|
nickjillings@1631
|
603 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
|
nickjillings@1631
|
604 // </datetime>
|
nickjillings@1631
|
605 var dateTime = new Date();
|
nickjillings@1631
|
606 var year = document.createAttribute('year');
|
nickjillings@1631
|
607 var month = document.createAttribute('month');
|
nickjillings@1631
|
608 var day = document.createAttribute('day');
|
nickjillings@1631
|
609 var hour = document.createAttribute('hour');
|
nickjillings@1631
|
610 var minute = document.createAttribute('minute');
|
nickjillings@1631
|
611 var secs = document.createAttribute('secs');
|
nickjillings@1631
|
612
|
nickjillings@1631
|
613 year.nodeValue = dateTime.getFullYear();
|
nickjillings@1631
|
614 month.nodeValue = dateTime.getMonth()+1;
|
nickjillings@1631
|
615 day.nodeValue = dateTime.getDate();
|
nickjillings@1631
|
616 hour.nodeValue = dateTime.getHours();
|
nickjillings@1631
|
617 minute.nodeValue = dateTime.getMinutes();
|
nickjillings@1631
|
618 secs.nodeValue = dateTime.getSeconds();
|
nickjillings@1631
|
619
|
nickjillings@1631
|
620 var hold = document.createElement("datetime");
|
nickjillings@1631
|
621 var date = document.createElement("date");
|
nickjillings@1631
|
622 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
|
nickjillings@1631
|
623 var time = document.createElement("time");
|
nickjillings@1631
|
624 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
|
nickjillings@1631
|
625
|
nickjillings@1631
|
626 date.setAttributeNode(year);
|
nickjillings@1631
|
627 date.setAttributeNode(month);
|
nickjillings@1631
|
628 date.setAttributeNode(day);
|
nickjillings@1631
|
629 time.setAttributeNode(hour);
|
nickjillings@1631
|
630 time.setAttributeNode(minute);
|
nickjillings@1631
|
631 time.setAttributeNode(secs);
|
nickjillings@1631
|
632
|
nickjillings@1631
|
633 hold.appendChild(date);
|
nickjillings@1631
|
634 hold.appendChild(time);
|
nickjillings@1631
|
635 return hold
|
nickjillings@1631
|
636
|
nickjillings@1631
|
637 }
|