nicholas@1
|
1 /**
|
nicholas@1
|
2 * core.js
|
nicholas@1
|
3 *
|
nicholas@1
|
4 * Main script to run, calls all other core functions and manages loading/store to backend.
|
nicholas@1
|
5 * Also contains all global variables.
|
nicholas@1
|
6 */
|
nicholas@1
|
7
|
nicholas@1
|
8 /* create the web audio API context and store in audioContext*/
|
n@657
|
9 var audioContext; // Hold the browser web audio API
|
n@657
|
10 var projectXML; // Hold the parsed setup XML
|
nicholas@952
|
11 var popup; // Hold the interfacePopup object
|
nicholas@964
|
12 var testState;
|
nicholas@952
|
13 var currentState; // Keep track of the current state (pre/post test, which test, final test? first test?)
|
n@669
|
14 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
|
n@657
|
15 var audioEngineContext; // The custome AudioEngine object
|
n@657
|
16 var projectReturn; // Hold the URL for the return
|
n@1007
|
17
|
n@656
|
18
|
n@681
|
19 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
|
n@681
|
20 AudioBufferSourceNode.prototype.owner = undefined;
|
nicholas@1
|
21
|
nicholas@1
|
22 window.onload = function() {
|
nicholas@1
|
23 // Function called once the browser has loaded all files.
|
nicholas@1
|
24 // This should perform any initial commands such as structure / loading documents
|
nicholas@1
|
25
|
nicholas@1
|
26 // Create a web audio API context
|
nicholas@705
|
27 // Fixed for cross-browser support
|
nicholas@705
|
28 var AudioContext = window.AudioContext || window.webkitAudioContext;
|
nicholas@7
|
29 audioContext = new AudioContext;
|
nicholas@1
|
30
|
nicholas@964
|
31 // Create test state
|
nicholas@964
|
32 testState = new stateMachine();
|
nicholas@964
|
33
|
nicholas@1
|
34 // Create the audio engine object
|
nicholas@1
|
35 audioEngineContext = new AudioEngine();
|
nicholas@952
|
36
|
nicholas@952
|
37 // Create the popup interface object
|
nicholas@952
|
38 popup = new interfacePopup();
|
n@701
|
39 };
|
nicholas@1
|
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);
|
n@1009
|
111 this.popupContent.childNodes[2].focus();
|
nicholas@952
|
112 }
|
nicholas@952
|
113 this.popupContent.appendChild(this.popupButton);
|
n@1009
|
114 };
|
nicholas@952
|
115
|
nicholas@952
|
116 this.initState = function(node) {
|
nicholas@952
|
117 //Call this with your preTest and postTest nodes when needed to
|
nicholas@952
|
118 // initialise the popup procedure.
|
nicholas@952
|
119 this.popupOptions = $(node).children();
|
nicholas@952
|
120 if (this.popupOptions.length > 0) {
|
nicholas@952
|
121 if (node.nodeName == 'preTest' || node.nodeName == 'PreTest') {
|
nicholas@952
|
122 this.responses = document.createElement('PreTest');
|
nicholas@952
|
123 } else if (node.nodeName == 'postTest' || node.nodeName == 'PostTest') {
|
nicholas@952
|
124 this.responses = document.createElement('PostTest');
|
nicholas@952
|
125 } else {
|
nicholas@952
|
126 console.log ('WARNING - popup node neither pre or post!');
|
nicholas@952
|
127 this.responses = document.createElement('responses');
|
nicholas@952
|
128 }
|
nicholas@952
|
129 this.currentIndex = 0;
|
nicholas@952
|
130 this.showPopup();
|
nicholas@952
|
131 this.postNode();
|
nicholas@952
|
132 }
|
n@1009
|
133 };
|
nicholas@952
|
134
|
nicholas@952
|
135 this.buttonClicked = function() {
|
nicholas@952
|
136 // Each time the popup button is clicked!
|
nicholas@952
|
137 var node = this.popupOptions[this.currentIndex];
|
nicholas@952
|
138 if (node.nodeName == 'question') {
|
nicholas@952
|
139 // Must extract the question data
|
nicholas@952
|
140 var mandatory = node.attributes['mandatory'];
|
nicholas@952
|
141 if (mandatory == undefined) {
|
nicholas@952
|
142 mandatory = false;
|
nicholas@952
|
143 } else {
|
nicholas@952
|
144 if (mandatory.value == 'true'){mandatory = true;}
|
nicholas@952
|
145 else {mandatory = false;}
|
nicholas@952
|
146 }
|
nicholas@952
|
147 var textArea = $(popup.popupContent).find('textarea')[0];
|
nicholas@952
|
148 if (mandatory == true && textArea.value.length == 0) {
|
nicholas@952
|
149 alert('This question is mandatory');
|
nicholas@952
|
150 return;
|
nicholas@952
|
151 } else {
|
nicholas@952
|
152 // Save the text content
|
nicholas@952
|
153 var hold = document.createElement('comment');
|
nicholas@952
|
154 hold.id = node.attributes['id'].value;
|
nicholas@952
|
155 hold.innerHTML = textArea.value;
|
nicholas@953
|
156 console.log("Question: "+ node.textContent);
|
nicholas@953
|
157 console.log("Question Response: "+ textArea.value);
|
nicholas@952
|
158 this.responses.appendChild(hold);
|
nicholas@952
|
159 }
|
nicholas@952
|
160 }
|
nicholas@952
|
161 this.currentIndex++;
|
nicholas@952
|
162 if (this.currentIndex < this.popupOptions.length) {
|
nicholas@952
|
163 this.postNode();
|
nicholas@952
|
164 } else {
|
nicholas@952
|
165 // Reached the end of the popupOptions
|
nicholas@952
|
166 this.hidePopup();
|
nicholas@964
|
167 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) {
|
nicholas@964
|
168 testState.stateResults[testState.stateIndex] = this.responses;
|
nicholas@964
|
169 } else {
|
nicholas@964
|
170 testState.stateResults[testState.stateIndex].appendChild(this.responses);
|
nicholas@964
|
171 }
|
nicholas@952
|
172 advanceState();
|
nicholas@952
|
173 }
|
n@1009
|
174 };
|
nicholas@951
|
175 }
|
nicholas@951
|
176
|
nicholas@952
|
177 function advanceState()
|
nicholas@951
|
178 {
|
nicholas@964
|
179 // Just for complete clarity
|
nicholas@964
|
180 testState.advanceState();
|
nicholas@964
|
181 }
|
nicholas@964
|
182
|
nicholas@964
|
183 function stateMachine()
|
nicholas@964
|
184 {
|
nicholas@964
|
185 // Object prototype for tracking and managing the test state
|
nicholas@964
|
186 this.stateMap = [];
|
nicholas@964
|
187 this.stateIndex = null;
|
nicholas@964
|
188 this.currentStateMap = [];
|
nicholas@964
|
189 this.currentIndex = null;
|
nicholas@964
|
190 this.currentTestId = 0;
|
nicholas@964
|
191 this.stateResults = [];
|
nicholas@934
|
192 this.timerCallBackHolders = null;
|
nicholas@964
|
193 this.initialise = function(){
|
nicholas@964
|
194 if (this.stateMap.length > 0) {
|
nicholas@964
|
195 if(this.stateIndex != null) {
|
nicholas@964
|
196 console.log('NOTE - State already initialise');
|
nicholas@964
|
197 }
|
nicholas@964
|
198 this.stateIndex = -1;
|
nicholas@964
|
199 var that = this;
|
nicholas@964
|
200 for (var id=0; id<this.stateMap.length; id++){
|
nicholas@964
|
201 var name = this.stateMap[id].nodeName;
|
nicholas@964
|
202 var obj = document.createElement(name);
|
n@930
|
203 if (name == "audioHolder") {
|
n@930
|
204 obj.id = this.stateMap[id].id;
|
n@930
|
205 }
|
nicholas@964
|
206 this.stateResults.push(obj);
|
nicholas@964
|
207 }
|
nicholas@964
|
208 } else {
|
nicholas@964
|
209 conolse.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
|
nicholas@952
|
210 }
|
nicholas@964
|
211 };
|
nicholas@964
|
212 this.advanceState = function(){
|
nicholas@964
|
213 if (this.stateIndex == null) {
|
nicholas@964
|
214 this.initialise();
|
nicholas@964
|
215 }
|
nicholas@964
|
216 if (this.stateIndex == -1) {
|
nicholas@964
|
217 console.log('Starting test...');
|
nicholas@964
|
218 }
|
nicholas@964
|
219 if (this.currentIndex == null){
|
nicholas@964
|
220 if (this.currentStateMap.nodeName == "audioHolder") {
|
nicholas@964
|
221 // Save current page
|
nicholas@964
|
222 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId);
|
nicholas@964
|
223 this.currentTestId++;
|
nicholas@964
|
224 }
|
nicholas@964
|
225 this.stateIndex++;
|
nicholas@964
|
226 if (this.stateIndex >= this.stateMap.length) {
|
nicholas@964
|
227 console.log('Test Completed');
|
nicholas@964
|
228 createProjectSave(projectReturn);
|
nicholas@964
|
229 } else {
|
nicholas@964
|
230 this.currentStateMap = this.stateMap[this.stateIndex];
|
nicholas@964
|
231 if (this.currentStateMap.nodeName == "audioHolder") {
|
nicholas@964
|
232 console.log('Loading test page');
|
nicholas@964
|
233 loadTest(this.currentStateMap);
|
nicholas@964
|
234 this.initialiseInnerState(this.currentStateMap);
|
nicholas@964
|
235 } else if (this.currentStateMap.nodeName == "PreTest" || this.currentStateMap.nodeName == "PostTest") {
|
nicholas@964
|
236 if (this.currentStateMap.childElementCount >= 1) {
|
nicholas@964
|
237 popup.initState(this.currentStateMap);
|
nicholas@964
|
238 } else {
|
nicholas@964
|
239 this.advanceState();
|
nicholas@964
|
240 }
|
nicholas@964
|
241 } else {
|
nicholas@964
|
242 this.advanceState();
|
nicholas@964
|
243 }
|
nicholas@964
|
244 }
|
nicholas@964
|
245 } else {
|
nicholas@964
|
246 this.advanceInnerState();
|
nicholas@964
|
247 }
|
nicholas@964
|
248 };
|
nicholas@964
|
249
|
nicholas@964
|
250 this.testPageCompleted = function(store, testXML, testId) {
|
nicholas@964
|
251 // Function called each time a test page has been completed
|
nicholas@964
|
252 // Can be used to over-rule default behaviour
|
nicholas@964
|
253
|
nicholas@964
|
254 pageXMLSave(store, testXML, testId);
|
nicholas@964
|
255 }
|
nicholas@964
|
256
|
nicholas@964
|
257 this.initialiseInnerState = function(testXML) {
|
nicholas@964
|
258 // Parses the received testXML for pre and post test options
|
nicholas@964
|
259 this.currentStateMap = [];
|
nicholas@964
|
260 var preTest = $(testXML).find('PreTest')[0];
|
nicholas@964
|
261 var postTest = $(testXML).find('PostTest')[0];
|
nicholas@964
|
262 if (preTest == undefined) {preTest = document.createElement("preTest");}
|
nicholas@964
|
263 if (postTest == undefined){postTest= document.createElement("postTest");}
|
nicholas@964
|
264 this.currentStateMap.push(preTest);
|
nicholas@964
|
265 this.currentStateMap.push(testXML);
|
nicholas@964
|
266 this.currentStateMap.push(postTest);
|
nicholas@964
|
267 this.currentIndex = -1;
|
nicholas@964
|
268 this.advanceInnerState();
|
nicholas@964
|
269 }
|
nicholas@964
|
270
|
nicholas@964
|
271 this.advanceInnerState = function() {
|
nicholas@964
|
272 this.currentIndex++;
|
nicholas@964
|
273 if (this.currentIndex >= this.currentStateMap.length) {
|
nicholas@964
|
274 this.currentIndex = null;
|
nicholas@964
|
275 this.currentStateMap = this.stateMap[this.stateIndex];
|
nicholas@964
|
276 this.advanceState();
|
nicholas@964
|
277 } else {
|
nicholas@964
|
278 if (this.currentStateMap[this.currentIndex].nodeName == "audioHolder") {
|
nicholas@964
|
279 console.log("Loading test page"+this.currentTestId);
|
nicholas@964
|
280 } else if (this.currentStateMap[this.currentIndex].nodeName == "PreTest") {
|
nicholas@964
|
281 popup.initState(this.currentStateMap[this.currentIndex]);
|
nicholas@964
|
282 } else if (this.currentStateMap[this.currentIndex].nodeName == "PostTest") {
|
nicholas@964
|
283 popup.initState(this.currentStateMap[this.currentIndex]);
|
nicholas@964
|
284 } else {
|
nicholas@964
|
285 this.advanceInnerState();
|
nicholas@964
|
286 }
|
nicholas@952
|
287 }
|
nicholas@951
|
288 }
|
nicholas@964
|
289
|
nicholas@964
|
290 this.previousState = function(){};
|
nicholas@951
|
291 }
|
nicholas@951
|
292
|
nicholas@952
|
293 function testEnded(testId)
|
nicholas@951
|
294 {
|
nicholas@952
|
295 pageXMLSave(testId);
|
nicholas@952
|
296 if (testXMLSetups.length-1 > testId)
|
nicholas@952
|
297 {
|
nicholas@952
|
298 // Yes we have another test to perform
|
nicholas@952
|
299 testId = (Number(testId)+1);
|
nicholas@952
|
300 currentState = 'testRun-'+testId;
|
nicholas@952
|
301 loadTest(testId);
|
nicholas@952
|
302 } else {
|
nicholas@952
|
303 console.log('Testing Completed!');
|
nicholas@952
|
304 currentState = 'postTest';
|
nicholas@952
|
305 // Check for any post tests
|
nicholas@952
|
306 var xmlSetup = projectXML.find('setup');
|
nicholas@952
|
307 var postTest = xmlSetup.find('PostTest')[0];
|
nicholas@952
|
308 popup.initState(postTest);
|
nicholas@952
|
309 }
|
nicholas@951
|
310 }
|
nicholas@951
|
311
|
nicholas@1
|
312 function loadProjectSpec(url) {
|
nicholas@1
|
313 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
|
nicholas@1
|
314 // If url is null, request client to upload project XML document
|
nicholas@2
|
315 var r = new XMLHttpRequest();
|
nicholas@2
|
316 r.open('GET',url,true);
|
nicholas@2
|
317 r.onload = function() {
|
nicholas@2
|
318 loadProjectSpecCallback(r.response);
|
n@701
|
319 };
|
nicholas@2
|
320 r.send();
|
n@701
|
321 };
|
nicholas@2
|
322
|
nicholas@2
|
323 function loadProjectSpecCallback(response) {
|
nicholas@2
|
324 // Function called after asynchronous download of XML project specification
|
nicholas@2
|
325 var decode = $.parseXML(response);
|
nicholas@2
|
326 projectXML = $(decode);
|
nicholas@2
|
327
|
nicholas@2
|
328 // Now extract the setup tag
|
nicholas@2
|
329 var xmlSetup = projectXML.find('setup');
|
n@701
|
330 // Detect the interface to use and load the relevant javascripts.
|
nicholas@2
|
331 var interfaceType = xmlSetup[0].attributes['interface'];
|
nicholas@2
|
332 var interfaceJS = document.createElement('script');
|
nicholas@2
|
333 interfaceJS.setAttribute("type","text/javascript");
|
nicholas@2
|
334 if (interfaceType.value == 'APE') {
|
nicholas@2
|
335 interfaceJS.setAttribute("src","ape.js");
|
n@657
|
336
|
n@657
|
337 // APE comes with a css file
|
n@657
|
338 var css = document.createElement('link');
|
n@657
|
339 css.rel = 'stylesheet';
|
n@657
|
340 css.type = 'text/css';
|
n@657
|
341 css.href = 'ape.css';
|
n@657
|
342
|
n@657
|
343 document.getElementsByTagName("head")[0].appendChild(css);
|
nicholas@2
|
344 }
|
nicholas@2
|
345 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
|
n@963
|
346
|
n@963
|
347 // Define window callbacks for interface
|
n@963
|
348 window.onresize = function(event){resizeWindow(event);};
|
nicholas@1
|
349 }
|
nicholas@1
|
350
|
nicholas@1
|
351 function createProjectSave(destURL) {
|
nicholas@1
|
352 // Save the data from interface into XML and send to destURL
|
nicholas@1
|
353 // If destURL is null then download XML in client
|
nicholas@7
|
354 // Now time to render file locally
|
nicholas@7
|
355 var xmlDoc = interfaceXMLSave();
|
nicholas@958
|
356 var parent = document.createElement("div");
|
nicholas@958
|
357 parent.appendChild(xmlDoc);
|
nicholas@958
|
358 var file = [parent.innerHTML];
|
nicholas@7
|
359 if (destURL == "null" || destURL == undefined) {
|
nicholas@7
|
360 var bb = new Blob(file,{type : 'application/xml'});
|
nicholas@7
|
361 var dnlk = window.URL.createObjectURL(bb);
|
nicholas@7
|
362 var a = document.createElement("a");
|
nicholas@7
|
363 a.hidden = '';
|
nicholas@7
|
364 a.href = dnlk;
|
nicholas@7
|
365 a.download = "save.xml";
|
nicholas@7
|
366 a.textContent = "Save File";
|
nicholas@7
|
367
|
nicholas@7
|
368 var submitDiv = document.getElementById('download-point');
|
nicholas@7
|
369 submitDiv.appendChild(a);
|
nicholas@954
|
370 popup.showPopup();
|
nicholas@954
|
371 popup.popupContent.innerHTML = null;
|
nicholas@954
|
372 popup.popupContent.appendChild(submitDiv)
|
nicholas@958
|
373 } else {
|
nicholas@958
|
374 var xmlhttp = new XMLHttpRequest;
|
nicholas@958
|
375 xmlhttp.open("POST",destURL,true);
|
nicholas@958
|
376 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
|
n@959
|
377 xmlhttp.onerror = function(){
|
n@959
|
378 console.log('Error saving file to server! Presenting download locally');
|
n@959
|
379 createProjectSave(null);
|
n@959
|
380 };
|
nicholas@958
|
381 xmlhttp.send(file);
|
n@1007
|
382 if (xmlhttp.status == 404) {
|
n@1007
|
383 createProjectSave(null);
|
n@1007
|
384 }
|
nicholas@7
|
385 }
|
n@667
|
386 return submitDiv;
|
nicholas@1
|
387 }
|
nicholas@1
|
388
|
nicholas@964
|
389 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
|
nicholas@964
|
390 function interfaceXMLSave(){
|
nicholas@964
|
391 // Create the XML string to be exported with results
|
nicholas@964
|
392 var xmlDoc = document.createElement("BrowserEvaluationResult");
|
nicholas@964
|
393 xmlDoc.appendChild(returnDateNode());
|
nicholas@964
|
394 for (var i=0; i<testState.stateResults.length; i++)
|
nicholas@964
|
395 {
|
nicholas@964
|
396 xmlDoc.appendChild(testState.stateResults[i]);
|
nicholas@964
|
397 }
|
nicholas@964
|
398
|
nicholas@964
|
399 return xmlDoc;
|
nicholas@964
|
400 }
|
nicholas@964
|
401
|
nicholas@1
|
402 function AudioEngine() {
|
nicholas@1
|
403
|
nicholas@1
|
404 // Create two output paths, the main outputGain and fooGain.
|
nicholas@1
|
405 // Output gain is default to 1 and any items for playback route here
|
nicholas@1
|
406 // Foo gain is used for analysis to ensure paths get processed, but are not heard
|
nicholas@1
|
407 // because web audio will optimise and any route which does not go to the destination gets ignored.
|
nicholas@1
|
408 this.outputGain = audioContext.createGain();
|
nicholas@1
|
409 this.fooGain = audioContext.createGain();
|
nicholas@1
|
410 this.fooGain.gain = 0;
|
nicholas@1
|
411
|
nicholas@7
|
412 // Use this to detect playback state: 0 - stopped, 1 - playing
|
nicholas@7
|
413 this.status = 0;
|
n@950
|
414 this.audioObjectsReady = false;
|
nicholas@7
|
415
|
nicholas@1
|
416 // Connect both gains to output
|
nicholas@1
|
417 this.outputGain.connect(audioContext.destination);
|
nicholas@1
|
418 this.fooGain.connect(audioContext.destination);
|
nicholas@1
|
419
|
n@673
|
420 // Create the timer Object
|
n@673
|
421 this.timer = new timer();
|
n@673
|
422 // Create session metrics
|
n@673
|
423 this.metric = new sessionMetrics(this);
|
n@673
|
424
|
n@681
|
425 this.loopPlayback = false;
|
n@681
|
426
|
nicholas@1
|
427 // Create store for new audioObjects
|
nicholas@1
|
428 this.audioObjects = [];
|
nicholas@1
|
429
|
n@950
|
430 this.play = function() {
|
n@950
|
431 // Start the timer and set the audioEngine state to playing (1)
|
n@950
|
432 if (this.status == 0) {
|
n@950
|
433 // Check if all audioObjects are ready
|
n@950
|
434 if (this.audioObjectsReady == false) {
|
n@950
|
435 this.audioObjectsReady = this.checkAllReady();
|
n@950
|
436 }
|
n@950
|
437 if (this.audioObjectsReady == true) {
|
n@950
|
438 this.timer.startTest();
|
nicholas@966
|
439 if (this.loopPlayback) {
|
nicholas@966
|
440 for(var i=0; i<this.audioObjects.length; i++) {
|
nicholas@966
|
441 this.audioObjects[i].play(this.timer.getTestTime()+1);
|
nicholas@966
|
442 }
|
nicholas@966
|
443 }
|
n@950
|
444 this.status = 1;
|
n@950
|
445 }
|
n@950
|
446 }
|
n@950
|
447 };
|
nicholas@1
|
448
|
n@950
|
449 this.stop = function() {
|
n@950
|
450 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
|
n@950
|
451 if (this.status == 1) {
|
n@950
|
452 for (var i=0; i<this.audioObjects.length; i++)
|
n@950
|
453 {
|
n@950
|
454 this.audioObjects[i].stop();
|
n@950
|
455 }
|
n@950
|
456 this.status = 0;
|
n@950
|
457 }
|
n@950
|
458 };
|
nicholas@8
|
459
|
nicholas@8
|
460
|
nicholas@1
|
461 this.newTrack = function(url) {
|
nicholas@1
|
462 // Pull data from given URL into new audio buffer
|
nicholas@1
|
463 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
|
nicholas@7
|
464
|
nicholas@1
|
465 // Create the audioObject with ID of the new track length;
|
n@673
|
466 audioObjectId = this.audioObjects.length;
|
nicholas@1
|
467 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
|
nicholas@7
|
468
|
nicholas@7
|
469 // AudioObject will get track itself.
|
nicholas@7
|
470 this.audioObjects[audioObjectId].constructTrack(url);
|
n@701
|
471 };
|
nicholas@1
|
472
|
n@950
|
473 this.newTestPage = function() {
|
n@950
|
474 this.state = 0;
|
n@950
|
475 this.audioObjectsReady = false;
|
n@950
|
476 this.metric.reset();
|
n@950
|
477 this.audioObjects = [];
|
n@950
|
478 };
|
n@950
|
479
|
nicholas@944
|
480 this.checkAllPlayed = function() {
|
nicholas@944
|
481 arr = [];
|
nicholas@944
|
482 for (var id=0; id<this.audioObjects.length; id++) {
|
nicholas@1002
|
483 if (this.audioObjects[id].metric.wasListenedTo == false) {
|
nicholas@944
|
484 arr.push(this.audioObjects[id].id);
|
nicholas@944
|
485 }
|
nicholas@944
|
486 }
|
nicholas@944
|
487 return arr;
|
nicholas@944
|
488 };
|
nicholas@944
|
489
|
n@950
|
490 this.checkAllReady = function() {
|
n@950
|
491 var ready = true;
|
n@950
|
492 for (var i=0; i<this.audioObjects.length; i++) {
|
n@950
|
493 if (this.audioObjects[i].state == 0) {
|
n@950
|
494 // Track not ready
|
n@950
|
495 console.log('WAIT -- audioObject '+i+' not ready yet!');
|
n@950
|
496 ready = false;
|
n@950
|
497 };
|
n@950
|
498 }
|
n@950
|
499 return ready;
|
n@950
|
500 };
|
n@950
|
501
|
nicholas@1
|
502 }
|
nicholas@1
|
503
|
nicholas@1
|
504 function audioObject(id) {
|
nicholas@1
|
505 // The main buffer object with common control nodes to the AudioEngine
|
nicholas@1
|
506
|
nicholas@1
|
507 this.id = id;
|
nicholas@1
|
508 this.state = 0; // 0 - no data, 1 - ready
|
n@708
|
509 this.url = null; // Hold the URL given for the output back to the results.
|
n@932
|
510 this.metric = new metricTracker(this);
|
nicholas@1
|
511
|
nicholas@1
|
512 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
|
n@681
|
513 this.bufferNode = undefined;
|
nicholas@1
|
514 this.outputGain = audioContext.createGain();
|
nicholas@1
|
515
|
nicholas@8
|
516 // Default output gain to be zero
|
nicholas@8
|
517 this.outputGain.gain.value = 0.0;
|
nicholas@8
|
518
|
nicholas@1
|
519 // Connect buffer to the audio graph
|
nicholas@1
|
520 this.outputGain.connect(audioEngineContext.outputGain);
|
nicholas@1
|
521
|
nicholas@1
|
522 // the audiobuffer is not designed for multi-start playback
|
nicholas@1
|
523 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
|
nicholas@1
|
524 this.buffer;
|
BrechtDeMan@969
|
525
|
nicholas@967
|
526 this.loopStart = function() {
|
nicholas@967
|
527 this.outputGain.gain.value = 1.0;
|
nicholas@967
|
528 this.metric.startListening(audioEngineContext.timer.getTestTime());
|
nicholas@967
|
529 }
|
nicholas@967
|
530
|
nicholas@967
|
531 this.loopStop = function() {
|
nicholas@967
|
532 if (this.outputGain.gain.value != 0.0) {
|
nicholas@967
|
533 this.outputGain.gain.value = 0.0;
|
nicholas@967
|
534 this.metric.stopListening(audioEngineContext.timer.getTestTime());
|
nicholas@967
|
535 }
|
nicholas@967
|
536 }
|
nicholas@967
|
537
|
nicholas@1
|
538 this.play = function(startTime) {
|
n@681
|
539 this.bufferNode = audioContext.createBufferSource();
|
nicholas@947
|
540 this.bufferNode.owner = this;
|
n@681
|
541 this.bufferNode.connect(this.outputGain);
|
n@681
|
542 this.bufferNode.buffer = this.buffer;
|
n@681
|
543 this.bufferNode.loop = audioEngineContext.loopPlayback;
|
nicholas@967
|
544 this.bufferNode.onended = function() {
|
nicholas@1002
|
545 // Safari does not like using 'this' to reference the calling object!
|
nicholas@1003
|
546 event.srcElement.owner.metric.stopListening(audioEngineContext.timer.getTestTime());
|
nicholas@1002
|
547 };
|
nicholas@947
|
548 if (this.bufferNode.loop == false) {
|
nicholas@967
|
549 this.metric.startListening(audioEngineContext.timer.getTestTime());
|
nicholas@947
|
550 }
|
nicholas@1
|
551 this.bufferNode.start(startTime);
|
n@701
|
552 };
|
nicholas@1
|
553
|
nicholas@1
|
554 this.stop = function() {
|
n@996
|
555 if (this.bufferNode != undefined)
|
n@996
|
556 {
|
n@996
|
557 this.bufferNode.stop(0);
|
n@996
|
558 this.bufferNode = undefined;
|
nicholas@967
|
559 this.metric.stopListening(audioEngineContext.timer.getTestTime());
|
n@996
|
560 }
|
n@701
|
561 };
|
nicholas@8
|
562
|
nicholas@7
|
563 this.constructTrack = function(url) {
|
nicholas@7
|
564 var request = new XMLHttpRequest();
|
n@708
|
565 this.url = url;
|
nicholas@7
|
566 request.open('GET',url,true);
|
nicholas@7
|
567 request.responseType = 'arraybuffer';
|
nicholas@7
|
568
|
nicholas@7
|
569 var audioObj = this;
|
nicholas@7
|
570
|
nicholas@7
|
571 // Create callback to decode the data asynchronously
|
nicholas@7
|
572 request.onloadend = function() {
|
nicholas@7
|
573 audioContext.decodeAudioData(request.response, function(decodedData) {
|
nicholas@7
|
574 audioObj.buffer = decodedData;
|
nicholas@7
|
575 audioObj.state = 1;
|
nicholas@7
|
576 }, function(){
|
nicholas@7
|
577 // Should only be called if there was an error, but sometimes gets called continuously
|
nicholas@7
|
578 // Check here if the error is genuine
|
nicholas@7
|
579 if (audioObj.state == 0 || audioObj.buffer == undefined) {
|
nicholas@7
|
580 // Genuine error
|
nicholas@7
|
581 console.log('FATAL - Error loading buffer on '+audioObj.id);
|
nicholas@7
|
582 }
|
nicholas@7
|
583 });
|
n@701
|
584 };
|
nicholas@7
|
585 request.send();
|
n@701
|
586 };
|
nicholas@7
|
587
|
n@673
|
588 }
|
n@673
|
589
|
n@673
|
590 function timer()
|
n@673
|
591 {
|
n@673
|
592 /* Timer object used in audioEngine to keep track of session timings
|
n@673
|
593 * Uses the timer of the web audio API, so sample resolution
|
n@673
|
594 */
|
n@673
|
595 this.testStarted = false;
|
n@673
|
596 this.testStartTime = 0;
|
n@673
|
597 this.testDuration = 0;
|
n@673
|
598 this.minimumTestTime = 0; // No minimum test time
|
n@673
|
599 this.startTest = function()
|
n@673
|
600 {
|
n@673
|
601 if (this.testStarted == false)
|
n@673
|
602 {
|
n@673
|
603 this.testStartTime = audioContext.currentTime;
|
n@673
|
604 this.testStarted = true;
|
n@673
|
605 this.updateTestTime();
|
n@676
|
606 audioEngineContext.metric.initialiseTest();
|
n@673
|
607 }
|
n@673
|
608 };
|
n@673
|
609 this.stopTest = function()
|
n@673
|
610 {
|
n@673
|
611 if (this.testStarted)
|
n@673
|
612 {
|
n@673
|
613 this.testDuration = this.getTestTime();
|
n@673
|
614 this.testStarted = false;
|
n@673
|
615 } else {
|
n@673
|
616 console.log('ERR: Test tried to end before beginning');
|
n@673
|
617 }
|
n@673
|
618 };
|
n@673
|
619 this.updateTestTime = function()
|
n@673
|
620 {
|
n@673
|
621 if (this.testStarted)
|
n@673
|
622 {
|
n@673
|
623 this.testDuration = audioContext.currentTime - this.testStartTime;
|
n@673
|
624 }
|
n@673
|
625 };
|
n@673
|
626 this.getTestTime = function()
|
n@673
|
627 {
|
n@673
|
628 this.updateTestTime();
|
n@673
|
629 return this.testDuration;
|
n@673
|
630 };
|
n@673
|
631 }
|
n@673
|
632
|
n@673
|
633 function sessionMetrics(engine)
|
n@673
|
634 {
|
n@673
|
635 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
|
n@673
|
636 */
|
n@673
|
637 this.engine = engine;
|
n@673
|
638 this.lastClicked = -1;
|
n@673
|
639 this.data = -1;
|
n@950
|
640 this.reset = function() {
|
n@950
|
641 this.lastClicked = -1;
|
n@950
|
642 this.data = -1;
|
n@950
|
643 };
|
n@676
|
644 this.initialiseTest = function(){};
|
n@673
|
645 }
|
n@673
|
646
|
n@932
|
647 function metricTracker(caller)
|
n@673
|
648 {
|
n@673
|
649 /* Custom object to track and collect metric data
|
n@673
|
650 * Used only inside the audioObjects object.
|
n@673
|
651 */
|
n@673
|
652
|
n@673
|
653 this.listenedTimer = 0;
|
n@673
|
654 this.listenStart = 0;
|
nicholas@947
|
655 this.listenHold = false;
|
n@675
|
656 this.initialPosition = -1;
|
n@673
|
657 this.movementTracker = [];
|
n@673
|
658 this.wasListenedTo = false;
|
n@673
|
659 this.wasMoved = false;
|
n@673
|
660 this.hasComments = false;
|
n@932
|
661 this.parent = caller;
|
n@673
|
662
|
n@673
|
663 this.initialised = function(position)
|
n@673
|
664 {
|
n@675
|
665 if (this.initialPosition == -1) {
|
n@675
|
666 this.initialPosition = position;
|
n@675
|
667 }
|
n@673
|
668 };
|
n@673
|
669
|
n@673
|
670 this.moved = function(time,position)
|
n@673
|
671 {
|
n@673
|
672 this.wasMoved = true;
|
n@673
|
673 this.movementTracker[this.movementTracker.length] = [time, position];
|
n@673
|
674 };
|
n@673
|
675
|
nicholas@967
|
676 this.startListening = function(time)
|
n@673
|
677 {
|
nicholas@947
|
678 if (this.listenHold == false)
|
n@673
|
679 {
|
n@673
|
680 this.wasListenedTo = true;
|
n@673
|
681 this.listenStart = time;
|
nicholas@947
|
682 this.listenHold = true;
|
n@932
|
683 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
|
n@932
|
684 }
|
n@932
|
685 };
|
nicholas@967
|
686
|
nicholas@967
|
687 this.stopListening = function(time)
|
nicholas@967
|
688 {
|
nicholas@967
|
689 if (this.listenHold == true)
|
nicholas@967
|
690 {
|
n@673
|
691 this.listenedTimer += (time - this.listenStart);
|
n@673
|
692 this.listenStart = 0;
|
nicholas@947
|
693 this.listenHold = false;
|
n@673
|
694 }
|
n@673
|
695 };
|
n@678
|
696 }
|
n@678
|
697
|
n@678
|
698 function randomiseOrder(input)
|
n@678
|
699 {
|
n@678
|
700 // This takes an array of information and randomises the order
|
n@678
|
701 var N = input.length;
|
n@678
|
702 var K = N;
|
n@678
|
703 var holdArr = [];
|
n@678
|
704 for (var n=0; n<N; n++)
|
n@678
|
705 {
|
n@678
|
706 // First pick a random number
|
n@678
|
707 var r = Math.random();
|
n@678
|
708 // Multiply and floor by the number of elements left
|
n@678
|
709 r = Math.floor(r*input.length);
|
n@678
|
710 // Pick out that element and delete from the array
|
n@678
|
711 holdArr.push(input.splice(r,1)[0]);
|
n@678
|
712 }
|
n@678
|
713 return holdArr;
|
n@961
|
714 }
|
n@961
|
715
|
n@961
|
716 function returnDateNode()
|
n@961
|
717 {
|
n@961
|
718 // Create an XML Node for the Date and Time a test was conducted
|
n@961
|
719 // Structure is
|
n@961
|
720 // <datetime>
|
n@961
|
721 // <date year="##" month="##" day="##">DD/MM/YY</date>
|
n@961
|
722 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
|
n@961
|
723 // </datetime>
|
n@961
|
724 var dateTime = new Date();
|
n@961
|
725 var year = document.createAttribute('year');
|
n@961
|
726 var month = document.createAttribute('month');
|
n@961
|
727 var day = document.createAttribute('day');
|
n@961
|
728 var hour = document.createAttribute('hour');
|
n@961
|
729 var minute = document.createAttribute('minute');
|
n@961
|
730 var secs = document.createAttribute('secs');
|
n@961
|
731
|
n@961
|
732 year.nodeValue = dateTime.getFullYear();
|
n@961
|
733 month.nodeValue = dateTime.getMonth()+1;
|
n@961
|
734 day.nodeValue = dateTime.getDate();
|
n@961
|
735 hour.nodeValue = dateTime.getHours();
|
n@961
|
736 minute.nodeValue = dateTime.getMinutes();
|
n@961
|
737 secs.nodeValue = dateTime.getSeconds();
|
n@961
|
738
|
n@961
|
739 var hold = document.createElement("datetime");
|
n@961
|
740 var date = document.createElement("date");
|
n@961
|
741 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
|
n@961
|
742 var time = document.createElement("time");
|
n@961
|
743 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
|
n@961
|
744
|
n@961
|
745 date.setAttributeNode(year);
|
n@961
|
746 date.setAttributeNode(month);
|
n@961
|
747 date.setAttributeNode(day);
|
n@961
|
748 time.setAttributeNode(hour);
|
n@961
|
749 time.setAttributeNode(minute);
|
n@961
|
750 time.setAttributeNode(secs);
|
n@961
|
751
|
n@961
|
752 hold.appendChild(date);
|
n@961
|
753 hold.appendChild(time);
|
n@961
|
754 return hold
|
n@961
|
755
|
nicholas@934
|
756 }
|
nicholas@934
|
757
|
nicholas@934
|
758 function testWaitIndicator() {
|
n@1007
|
759 if (audioEngineContext.checkAllReady() == false) {
|
n@1007
|
760 var hold = document.createElement("div");
|
n@1007
|
761 hold.id = "testWaitIndicator";
|
n@1007
|
762 hold.className = "indicator-box";
|
n@1007
|
763 var span = document.createElement("span");
|
n@1007
|
764 span.textContent = "Please wait! Elements still loading";
|
n@1007
|
765 hold.appendChild(span);
|
n@1007
|
766 var body = document.getElementsByTagName('body')[0];
|
n@1007
|
767 body.appendChild(hold);
|
n@1007
|
768 testWaitTimerIntervalHolder = setInterval(function(){
|
n@1007
|
769 var ready = audioEngineContext.checkAllReady();
|
n@1007
|
770 if (ready) {
|
n@1007
|
771 var elem = document.getElementById('testWaitIndicator');
|
n@1007
|
772 var body = document.getElementsByTagName('body')[0];
|
n@1007
|
773 body.removeChild(elem);
|
n@1007
|
774 clearInterval(testWaitTimerIntervalHolder);
|
n@1007
|
775 }
|
n@1007
|
776 },500,false);
|
n@1007
|
777 }
|
nicholas@934
|
778 }
|
nicholas@934
|
779
|
n@1007
|
780 var testWaitTimerIntervalHolder = null;
|