nickjillings@1571
|
1 /**
|
nickjillings@1571
|
2 * core.js
|
nickjillings@1571
|
3 *
|
nickjillings@1571
|
4 * Main script to run, calls all other core functions and manages loading/store to backend.
|
nickjillings@1571
|
5 * Also contains all global variables.
|
nickjillings@1571
|
6 */
|
nickjillings@1571
|
7
|
nickjillings@1571
|
8 /* create the web audio API context and store in audioContext*/
|
nickjillings@1571
|
9 var audioContext; // Hold the browser web audio API
|
nickjillings@1571
|
10 var projectXML; // Hold the parsed setup XML
|
nickjillings@1571
|
11 var specification;
|
nickjillings@1571
|
12 var interfaceContext;
|
nickjillings@1571
|
13 var popup; // Hold the interfacePopup object
|
nickjillings@1571
|
14 var testState;
|
nickjillings@1571
|
15 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
|
nickjillings@1571
|
16 var audioEngineContext; // The custome AudioEngine object
|
nickjillings@1571
|
17 var projectReturn; // Hold the URL for the return
|
nickjillings@1571
|
18
|
nickjillings@1571
|
19
|
nickjillings@1571
|
20 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
|
nickjillings@1571
|
21 AudioBufferSourceNode.prototype.owner = undefined;
|
nickjillings@1571
|
22
|
nickjillings@1571
|
23 window.onload = function() {
|
nickjillings@1571
|
24 // Function called once the browser has loaded all files.
|
nickjillings@1571
|
25 // This should perform any initial commands such as structure / loading documents
|
nickjillings@1571
|
26
|
nickjillings@1571
|
27 // Create a web audio API context
|
nickjillings@1571
|
28 // Fixed for cross-browser support
|
nickjillings@1571
|
29 var AudioContext = window.AudioContext || window.webkitAudioContext;
|
nickjillings@1571
|
30 audioContext = new AudioContext;
|
nickjillings@1571
|
31
|
nickjillings@1571
|
32 // Create test state
|
nickjillings@1571
|
33 testState = new stateMachine();
|
nickjillings@1571
|
34
|
nickjillings@1571
|
35 // Create the audio engine object
|
nickjillings@1571
|
36 audioEngineContext = new AudioEngine();
|
nickjillings@1571
|
37
|
nickjillings@1571
|
38 // Create the popup interface object
|
nickjillings@1571
|
39 popup = new interfacePopup();
|
nickjillings@1571
|
40
|
nickjillings@1571
|
41 // Create the specification object
|
nickjillings@1571
|
42 specification = new Specification();
|
nickjillings@1571
|
43
|
nickjillings@1571
|
44 // Create the interface object
|
nickjillings@1571
|
45 interfaceContext = new Interface(specification);
|
nickjillings@1571
|
46 };
|
nickjillings@1571
|
47
|
nickjillings@1571
|
48 function interfacePopup() {
|
nickjillings@1571
|
49 // Creates an object to manage the popup
|
nickjillings@1571
|
50 this.popup = null;
|
nickjillings@1571
|
51 this.popupContent = null;
|
nickjillings@1571
|
52 this.popupButton = null;
|
nickjillings@1571
|
53 this.popupOptions = null;
|
nickjillings@1571
|
54 this.currentIndex = null;
|
nickjillings@1571
|
55 this.responses = null;
|
nickjillings@1571
|
56
|
nickjillings@1571
|
57 this.createPopup = function(){
|
nickjillings@1571
|
58 // Create popup window interface
|
nickjillings@1571
|
59 var insertPoint = document.getElementById("topLevelBody");
|
nickjillings@1571
|
60 var blank = document.createElement('div');
|
nickjillings@1571
|
61 blank.className = 'testHalt';
|
nickjillings@1571
|
62
|
nickjillings@1571
|
63 this.popup = document.createElement('div');
|
nickjillings@1571
|
64 this.popup.id = 'popupHolder';
|
nickjillings@1571
|
65 this.popup.className = 'popupHolder';
|
nickjillings@1571
|
66 this.popup.style.position = 'absolute';
|
nickjillings@1571
|
67 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
|
nickjillings@1571
|
68 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
|
nickjillings@1571
|
69
|
nickjillings@1571
|
70 this.popupContent = document.createElement('div');
|
nickjillings@1571
|
71 this.popupContent.id = 'popupContent';
|
nickjillings@1571
|
72 this.popupContent.style.marginTop = '25px';
|
nickjillings@1571
|
73 this.popupContent.align = 'center';
|
nickjillings@1571
|
74 this.popup.appendChild(this.popupContent);
|
nickjillings@1571
|
75
|
nickjillings@1571
|
76 this.popupButton = document.createElement('button');
|
nickjillings@1571
|
77 this.popupButton.className = 'popupButton';
|
nickjillings@1571
|
78 this.popupButton.innerHTML = 'Next';
|
nickjillings@1571
|
79 this.popupButton.onclick = function(){popup.buttonClicked();};
|
nickjillings@1571
|
80 this.popup.style.zIndex = -1;
|
nickjillings@1571
|
81 this.popup.style.visibility = 'hidden';
|
nickjillings@1571
|
82 blank.style.zIndex = -2;
|
nickjillings@1571
|
83 blank.style.visibility = 'hidden';
|
nickjillings@1571
|
84 insertPoint.appendChild(this.popup);
|
nickjillings@1571
|
85 insertPoint.appendChild(blank);
|
nickjillings@1571
|
86 };
|
nickjillings@1571
|
87
|
nickjillings@1571
|
88 this.showPopup = function(){
|
nickjillings@1571
|
89 if (this.popup == null) {
|
nickjillings@1571
|
90 this.createPopup();
|
nickjillings@1571
|
91 }
|
nickjillings@1571
|
92 this.popup.style.zIndex = 3;
|
nickjillings@1571
|
93 this.popup.style.visibility = 'visible';
|
nickjillings@1571
|
94 var blank = document.getElementsByClassName('testHalt')[0];
|
nickjillings@1571
|
95 blank.style.zIndex = 2;
|
nickjillings@1571
|
96 blank.style.visibility = 'visible';
|
nickjillings@1571
|
97 };
|
nickjillings@1571
|
98
|
nickjillings@1571
|
99 this.hidePopup = function(){
|
nickjillings@1571
|
100 this.popup.style.zIndex = -1;
|
nickjillings@1571
|
101 this.popup.style.visibility = 'hidden';
|
nickjillings@1571
|
102 var blank = document.getElementsByClassName('testHalt')[0];
|
nickjillings@1571
|
103 blank.style.zIndex = -2;
|
nickjillings@1571
|
104 blank.style.visibility = 'hidden';
|
nickjillings@1571
|
105 };
|
nickjillings@1571
|
106
|
nickjillings@1571
|
107 this.postNode = function() {
|
nickjillings@1571
|
108 // This will take the node from the popupOptions and display it
|
nickjillings@1571
|
109 var node = this.popupOptions[this.currentIndex];
|
nickjillings@1571
|
110 this.popupContent.innerHTML = null;
|
nickjillings@1571
|
111 if (node.type == 'statement') {
|
nickjillings@1571
|
112 var span = document.createElement('span');
|
nickjillings@1571
|
113 span.textContent = node.statement;
|
nickjillings@1571
|
114 this.popupContent.appendChild(span);
|
nickjillings@1571
|
115 } else if (node.type == 'question') {
|
nickjillings@1571
|
116 var span = document.createElement('span');
|
nickjillings@1571
|
117 span.textContent = node.question;
|
nickjillings@1571
|
118 var textArea = document.createElement('textarea');
|
nickjillings@1571
|
119 switch (node.boxsize) {
|
nickjillings@1571
|
120 case 'small':
|
nickjillings@1571
|
121 textArea.cols = "20";
|
nickjillings@1571
|
122 textArea.rows = "1";
|
nickjillings@1571
|
123 break;
|
nickjillings@1571
|
124 case 'normal':
|
nickjillings@1571
|
125 textArea.cols = "30";
|
nickjillings@1571
|
126 textArea.rows = "2";
|
nickjillings@1571
|
127 break;
|
nickjillings@1571
|
128 case 'large':
|
nickjillings@1571
|
129 textArea.cols = "40";
|
nickjillings@1571
|
130 textArea.rows = "5";
|
nickjillings@1571
|
131 break;
|
nickjillings@1571
|
132 case 'huge':
|
nickjillings@1571
|
133 textArea.cols = "50";
|
nickjillings@1571
|
134 textArea.rows = "10";
|
nickjillings@1571
|
135 break;
|
nickjillings@1571
|
136 }
|
nickjillings@1571
|
137 var br = document.createElement('br');
|
nickjillings@1571
|
138 this.popupContent.appendChild(span);
|
nickjillings@1571
|
139 this.popupContent.appendChild(br);
|
nickjillings@1571
|
140 this.popupContent.appendChild(textArea);
|
nickjillings@1571
|
141 this.popupContent.childNodes[2].focus();
|
nickjillings@1571
|
142 } else if (node.type == 'checkbox') {
|
nickjillings@1571
|
143 var span = document.createElement('span');
|
nickjillings@1571
|
144 span.textContent = node.statement;
|
nickjillings@1571
|
145 this.popupContent.appendChild(span);
|
nickjillings@1571
|
146 var optHold = document.createElement('div');
|
nickjillings@1571
|
147 optHold.id = 'option-holder';
|
nickjillings@1571
|
148 optHold.align = 'left';
|
nickjillings@1571
|
149 for (var i=0; i<node.options.length; i++) {
|
nickjillings@1571
|
150 var option = node.options[i];
|
nickjillings@1571
|
151 var input = document.createElement('input');
|
nickjillings@1571
|
152 input.id = option.id;
|
nickjillings@1571
|
153 input.type = 'checkbox';
|
nickjillings@1571
|
154 var span = document.createElement('span');
|
nickjillings@1571
|
155 span.textContent = option.text;
|
nickjillings@1571
|
156 var hold = document.createElement('div');
|
nickjillings@1571
|
157 hold.setAttribute('name','option');
|
nickjillings@1571
|
158 hold.style.float = 'left';
|
nickjillings@1571
|
159 hold.style.padding = '4px';
|
nickjillings@1571
|
160 hold.appendChild(input);
|
nickjillings@1571
|
161 hold.appendChild(span);
|
nickjillings@1571
|
162 optHold.appendChild(hold);
|
nickjillings@1571
|
163 }
|
nickjillings@1571
|
164 this.popupContent.appendChild(optHold);
|
nickjillings@1571
|
165 } else if (node.type == 'radio') {
|
nickjillings@1571
|
166 var span = document.createElement('span');
|
nickjillings@1571
|
167 span.textContent = node.statement;
|
nickjillings@1571
|
168 this.popupContent.appendChild(span);
|
nickjillings@1571
|
169 var optHold = document.createElement('div');
|
nickjillings@1571
|
170 optHold.id = 'option-holder';
|
nickjillings@1571
|
171 optHold.align = 'none';
|
nickjillings@1571
|
172 optHold.style.float = 'left';
|
nickjillings@1571
|
173 optHold.style.width = "100%";
|
nickjillings@1571
|
174 for (var i=0; i<node.options.length; i++) {
|
nickjillings@1571
|
175 var option = node.options[i];
|
nickjillings@1571
|
176 var input = document.createElement('input');
|
nickjillings@1571
|
177 input.id = option.name;
|
nickjillings@1571
|
178 input.type = 'radio';
|
nickjillings@1571
|
179 input.name = node.id;
|
nickjillings@1571
|
180 var span = document.createElement('span');
|
nickjillings@1571
|
181 span.textContent = option.text;
|
nickjillings@1571
|
182 var hold = document.createElement('div');
|
nickjillings@1571
|
183 hold.setAttribute('name','option');
|
nickjillings@1571
|
184 hold.style.padding = '4px';
|
nickjillings@1571
|
185 hold.appendChild(input);
|
nickjillings@1571
|
186 hold.appendChild(span);
|
nickjillings@1571
|
187 optHold.appendChild(hold);
|
nickjillings@1571
|
188 }
|
nickjillings@1571
|
189 this.popupContent.appendChild(optHold);
|
nickjillings@1571
|
190 }
|
nickjillings@1571
|
191 this.popupContent.appendChild(this.popupButton);
|
nickjillings@1571
|
192 };
|
nickjillings@1571
|
193
|
nickjillings@1571
|
194 this.initState = function(node) {
|
nickjillings@1571
|
195 //Call this with your preTest and postTest nodes when needed to
|
nickjillings@1571
|
196 // initialise the popup procedure.
|
nickjillings@1571
|
197 this.popupOptions = node.options;
|
nickjillings@1571
|
198 if (this.popupOptions.length > 0) {
|
nickjillings@1571
|
199 if (node.type == 'pretest') {
|
nickjillings@1571
|
200 this.responses = document.createElement('PreTest');
|
nickjillings@1571
|
201 } else if (node.type == 'posttest') {
|
nickjillings@1571
|
202 this.responses = document.createElement('PostTest');
|
nickjillings@1571
|
203 } else {
|
nickjillings@1571
|
204 console.log ('WARNING - popup node neither pre or post!');
|
nickjillings@1571
|
205 this.responses = document.createElement('responses');
|
nickjillings@1571
|
206 }
|
nickjillings@1571
|
207 this.currentIndex = 0;
|
nickjillings@1571
|
208 this.showPopup();
|
nickjillings@1571
|
209 this.postNode();
|
nickjillings@1571
|
210 } else {
|
nickjillings@1571
|
211 advanceState();
|
nickjillings@1571
|
212 }
|
nickjillings@1571
|
213 };
|
nickjillings@1571
|
214
|
nickjillings@1571
|
215 this.buttonClicked = function() {
|
nickjillings@1571
|
216 // Each time the popup button is clicked!
|
nickjillings@1571
|
217 var node = this.popupOptions[this.currentIndex];
|
nickjillings@1571
|
218 if (node.type == 'question') {
|
nickjillings@1571
|
219 // Must extract the question data
|
nickjillings@1571
|
220 var textArea = $(popup.popupContent).find('textarea')[0];
|
nickjillings@1571
|
221 if (node.mandatory == true && textArea.value.length == 0) {
|
nickjillings@1571
|
222 alert('This question is mandatory');
|
nickjillings@1571
|
223 return;
|
nickjillings@1571
|
224 } else {
|
nickjillings@1571
|
225 // Save the text content
|
nickjillings@1571
|
226 var hold = document.createElement('comment');
|
nickjillings@1571
|
227 hold.id = node.id;
|
nickjillings@1571
|
228 hold.innerHTML = textArea.value;
|
nickjillings@1571
|
229 console.log("Question: "+ node.textContent);
|
nickjillings@1571
|
230 console.log("Question Response: "+ textArea.value);
|
nickjillings@1571
|
231 this.responses.appendChild(hold);
|
nickjillings@1571
|
232 }
|
nickjillings@1571
|
233 } else if (node.type == 'checkbox') {
|
nickjillings@1571
|
234 // Must extract checkbox data
|
nickjillings@1571
|
235 var optHold = document.getElementById('option-holder');
|
nickjillings@1571
|
236 var hold = document.createElement('checkbox');
|
nickjillings@1571
|
237 console.log("Checkbox: "+ node.statement);
|
nickjillings@1571
|
238 hold.id = node.id;
|
nickjillings@1571
|
239 for (var i=0; i<optHold.childElementCount; i++) {
|
nickjillings@1571
|
240 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
|
nickjillings@1571
|
241 var statement = optHold.childNodes[i].getElementsByTagName('span')[0];
|
nickjillings@1571
|
242 var response = document.createElement('option');
|
nickjillings@1571
|
243 response.setAttribute('id',input.id);
|
nickjillings@1571
|
244 response.setAttribute('checked',input.checked);
|
nickjillings@1571
|
245 hold.appendChild(response);
|
nickjillings@1571
|
246 console.log(input.id +': '+ input.checked);
|
nickjillings@1571
|
247 }
|
nickjillings@1571
|
248 this.responses.appendChild(hold);
|
nickjillings@1571
|
249 } else if (node.type == "radio") {
|
nickjillings@1571
|
250 var optHold = document.getElementById('option-holder');
|
nickjillings@1571
|
251 var hold = document.createElement('radio');
|
nickjillings@1571
|
252 var responseID = null;
|
nickjillings@1571
|
253 var i=0;
|
nickjillings@1571
|
254 while(responseID == null) {
|
nickjillings@1571
|
255 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
|
nickjillings@1571
|
256 if (input.checked == true) {
|
nickjillings@1571
|
257 responseID = i;
|
nickjillings@1571
|
258 }
|
nickjillings@1571
|
259 i++;
|
nickjillings@1571
|
260 }
|
nickjillings@1571
|
261 hold.id = node.id;
|
nickjillings@1571
|
262 hold.setAttribute('name',node.options[responseID].name);
|
nickjillings@1571
|
263 hold.textContent = node.options[responseID].text;
|
nickjillings@1571
|
264 this.responses.appendChild(hold);
|
nickjillings@1571
|
265 }
|
nickjillings@1571
|
266 this.currentIndex++;
|
nickjillings@1571
|
267 if (this.currentIndex < this.popupOptions.length) {
|
nickjillings@1571
|
268 this.postNode();
|
nickjillings@1571
|
269 } else {
|
nickjillings@1571
|
270 // Reached the end of the popupOptions
|
nickjillings@1571
|
271 this.hidePopup();
|
nickjillings@1571
|
272 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) {
|
nickjillings@1571
|
273 testState.stateResults[testState.stateIndex] = this.responses;
|
nickjillings@1571
|
274 } else {
|
nickjillings@1571
|
275 testState.stateResults[testState.stateIndex].appendChild(this.responses);
|
nickjillings@1571
|
276 }
|
nickjillings@1571
|
277 advanceState();
|
nickjillings@1571
|
278 }
|
nickjillings@1571
|
279 };
|
nickjillings@1571
|
280 }
|
nickjillings@1571
|
281
|
nickjillings@1571
|
282 function advanceState()
|
nickjillings@1571
|
283 {
|
nickjillings@1571
|
284 // Just for complete clarity
|
nickjillings@1571
|
285 testState.advanceState();
|
nickjillings@1571
|
286 }
|
nickjillings@1571
|
287
|
nickjillings@1571
|
288 function stateMachine()
|
nickjillings@1571
|
289 {
|
nickjillings@1571
|
290 // Object prototype for tracking and managing the test state
|
nickjillings@1571
|
291 this.stateMap = [];
|
nickjillings@1571
|
292 this.stateIndex = null;
|
nickjillings@1571
|
293 this.currentStateMap = [];
|
nickjillings@1571
|
294 this.currentIndex = null;
|
nickjillings@1571
|
295 this.currentTestId = 0;
|
nickjillings@1571
|
296 this.stateResults = [];
|
nickjillings@1571
|
297 this.timerCallBackHolders = null;
|
nickjillings@1571
|
298 this.initialise = function(){
|
nickjillings@1571
|
299 if (this.stateMap.length > 0) {
|
nickjillings@1571
|
300 if(this.stateIndex != null) {
|
nickjillings@1571
|
301 console.log('NOTE - State already initialise');
|
nickjillings@1571
|
302 }
|
nickjillings@1571
|
303 this.stateIndex = -1;
|
nickjillings@1571
|
304 var that = this;
|
nickjillings@1571
|
305 for (var id=0; id<this.stateMap.length; id++){
|
nickjillings@1571
|
306 var name = this.stateMap[id].type;
|
nickjillings@1571
|
307 var obj = document.createElement(name);
|
nickjillings@1571
|
308 if (name == 'audioHolder') {
|
nickjillings@1571
|
309 obj.id = this.stateMap[id].id;
|
nickjillings@1571
|
310 }
|
nickjillings@1571
|
311 this.stateResults.push(obj);
|
nickjillings@1571
|
312 }
|
nickjillings@1571
|
313 } else {
|
nickjillings@1571
|
314 conolse.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
|
nickjillings@1571
|
315 }
|
nickjillings@1571
|
316 };
|
nickjillings@1571
|
317 this.advanceState = function(){
|
nickjillings@1571
|
318 if (this.stateIndex == null) {
|
nickjillings@1571
|
319 this.initialise();
|
nickjillings@1571
|
320 }
|
nickjillings@1571
|
321 if (this.stateIndex == -1) {
|
nickjillings@1571
|
322 console.log('Starting test...');
|
nickjillings@1571
|
323 }
|
nickjillings@1571
|
324 if (this.currentIndex == null){
|
nickjillings@1571
|
325 if (this.currentStateMap.type == "audioHolder") {
|
nickjillings@1571
|
326 // Save current page
|
nickjillings@1571
|
327 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId);
|
nickjillings@1571
|
328 this.currentTestId++;
|
nickjillings@1571
|
329 }
|
nickjillings@1571
|
330 this.stateIndex++;
|
nickjillings@1571
|
331 if (this.stateIndex >= this.stateMap.length) {
|
nickjillings@1571
|
332 console.log('Test Completed');
|
nickjillings@1571
|
333 createProjectSave(specification.projectReturn);
|
nickjillings@1571
|
334 } else {
|
nickjillings@1571
|
335 this.currentStateMap = this.stateMap[this.stateIndex];
|
nickjillings@1571
|
336 if (this.currentStateMap.type == "audioHolder") {
|
nickjillings@1571
|
337 console.log('Loading test page');
|
nickjillings@1571
|
338 loadTest(this.currentStateMap);
|
nickjillings@1571
|
339 this.initialiseInnerState(this.currentStateMap);
|
nickjillings@1571
|
340 } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") {
|
nickjillings@1571
|
341 if (this.currentStateMap.options.length >= 1) {
|
nickjillings@1571
|
342 popup.initState(this.currentStateMap);
|
nickjillings@1571
|
343 } else {
|
nickjillings@1571
|
344 this.advanceState();
|
nickjillings@1571
|
345 }
|
nickjillings@1571
|
346 } else {
|
nickjillings@1571
|
347 this.advanceState();
|
nickjillings@1571
|
348 }
|
nickjillings@1571
|
349 }
|
nickjillings@1571
|
350 } else {
|
nickjillings@1571
|
351 this.advanceInnerState();
|
nickjillings@1571
|
352 }
|
nickjillings@1571
|
353 };
|
nickjillings@1571
|
354
|
nickjillings@1571
|
355 this.testPageCompleted = function(store, testXML, testId) {
|
nickjillings@1571
|
356 // Function called each time a test page has been completed
|
nickjillings@1571
|
357 // Can be used to over-rule default behaviour
|
nickjillings@1571
|
358
|
nickjillings@1571
|
359 pageXMLSave(store, testXML);
|
nickjillings@1571
|
360 };
|
nickjillings@1571
|
361
|
nickjillings@1571
|
362 this.initialiseInnerState = function(node) {
|
nickjillings@1571
|
363 // Parses the received testXML for pre and post test options
|
nickjillings@1571
|
364 this.currentStateMap = [];
|
nickjillings@1571
|
365 var preTest = node.preTest;
|
nickjillings@1571
|
366 var postTest = node.postTest;
|
nickjillings@1571
|
367 if (preTest == undefined) {preTest = document.createElement("preTest");}
|
nickjillings@1571
|
368 if (postTest == undefined){postTest= document.createElement("postTest");}
|
nickjillings@1571
|
369 this.currentStateMap.push(preTest);
|
nickjillings@1571
|
370 this.currentStateMap.push(node);
|
nickjillings@1571
|
371 this.currentStateMap.push(postTest);
|
nickjillings@1571
|
372 this.currentIndex = -1;
|
nickjillings@1571
|
373 this.advanceInnerState();
|
nickjillings@1571
|
374 };
|
nickjillings@1571
|
375
|
nickjillings@1571
|
376 this.advanceInnerState = function() {
|
nickjillings@1571
|
377 this.currentIndex++;
|
nickjillings@1571
|
378 if (this.currentIndex >= this.currentStateMap.length) {
|
nickjillings@1571
|
379 this.currentIndex = null;
|
nickjillings@1571
|
380 this.currentStateMap = this.stateMap[this.stateIndex];
|
nickjillings@1571
|
381 this.advanceState();
|
nickjillings@1571
|
382 } else {
|
nickjillings@1571
|
383 if (this.currentStateMap[this.currentIndex].type == "audioHolder") {
|
nickjillings@1571
|
384 console.log("Loading test page"+this.currentTestId);
|
nickjillings@1571
|
385 } else if (this.currentStateMap[this.currentIndex].type == "pretest") {
|
nickjillings@1571
|
386 popup.initState(this.currentStateMap[this.currentIndex]);
|
nickjillings@1571
|
387 } else if (this.currentStateMap[this.currentIndex].type == "posttest") {
|
nickjillings@1571
|
388 popup.initState(this.currentStateMap[this.currentIndex]);
|
nickjillings@1571
|
389 } else {
|
nickjillings@1571
|
390 this.advanceInnerState();
|
nickjillings@1571
|
391 }
|
nickjillings@1571
|
392 }
|
nickjillings@1571
|
393 };
|
nickjillings@1571
|
394
|
nickjillings@1571
|
395 this.previousState = function(){};
|
nickjillings@1571
|
396 }
|
nickjillings@1571
|
397
|
nickjillings@1571
|
398 function testEnded(testId)
|
nickjillings@1571
|
399 {
|
nickjillings@1571
|
400 pageXMLSave(testId);
|
nickjillings@1571
|
401 if (testXMLSetups.length-1 > testId)
|
nickjillings@1571
|
402 {
|
nickjillings@1571
|
403 // Yes we have another test to perform
|
nickjillings@1571
|
404 testId = (Number(testId)+1);
|
nickjillings@1571
|
405 currentState = 'testRun-'+testId;
|
nickjillings@1571
|
406 loadTest(testId);
|
nickjillings@1571
|
407 } else {
|
nickjillings@1571
|
408 console.log('Testing Completed!');
|
nickjillings@1571
|
409 currentState = 'postTest';
|
nickjillings@1571
|
410 // Check for any post tests
|
nickjillings@1571
|
411 var xmlSetup = projectXML.find('setup');
|
nickjillings@1571
|
412 var postTest = xmlSetup.find('PostTest')[0];
|
nickjillings@1571
|
413 popup.initState(postTest);
|
nickjillings@1571
|
414 }
|
nickjillings@1571
|
415 }
|
nickjillings@1571
|
416
|
nickjillings@1571
|
417 function loadProjectSpec(url) {
|
nickjillings@1571
|
418 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
|
nickjillings@1571
|
419 // If url is null, request client to upload project XML document
|
nickjillings@1571
|
420 var r = new XMLHttpRequest();
|
nickjillings@1571
|
421 r.open('GET',url,true);
|
nickjillings@1571
|
422 r.onload = function() {
|
nickjillings@1571
|
423 loadProjectSpecCallback(r.response);
|
nickjillings@1571
|
424 };
|
nickjillings@1571
|
425 r.send();
|
nickjillings@1571
|
426 };
|
nickjillings@1571
|
427
|
nickjillings@1571
|
428 function loadProjectSpecCallback(response) {
|
nickjillings@1571
|
429 // Function called after asynchronous download of XML project specification
|
nickjillings@1571
|
430 //var decode = $.parseXML(response);
|
nickjillings@1571
|
431 //projectXML = $(decode);
|
nickjillings@1571
|
432
|
nickjillings@1571
|
433 var parse = new DOMParser();
|
nickjillings@1571
|
434 projectXML = parse.parseFromString(response,'text/xml');
|
nickjillings@1571
|
435
|
nickjillings@1571
|
436 // Build the specification
|
nickjillings@1571
|
437 specification.decode();
|
nickjillings@1571
|
438
|
nickjillings@1571
|
439 testState.stateMap.push(specification.preTest);
|
nickjillings@1571
|
440
|
nickjillings@1571
|
441 // New check if we need to randomise the test order
|
nickjillings@1571
|
442 if (specification.randomiseOrder)
|
nickjillings@1571
|
443 {
|
nickjillings@1571
|
444 specification.audioHolders = randomiseOrder(specification.audioHolders);
|
nickjillings@1571
|
445 }
|
nickjillings@1571
|
446
|
nickjillings@1571
|
447 $(specification.audioHolders).each(function(index,elem){
|
nickjillings@1571
|
448 testState.stateMap.push(elem);
|
nickjillings@1571
|
449 });
|
nickjillings@1571
|
450
|
nickjillings@1571
|
451 testState.stateMap.push(specification.postTest);
|
nickjillings@1571
|
452
|
nickjillings@1571
|
453 // Obtain the metrics enabled
|
nickjillings@1571
|
454 $(specification.metrics).each(function(index,node){
|
nickjillings@1571
|
455 var enabled = node.textContent;
|
nickjillings@1571
|
456 switch(node.enabled)
|
nickjillings@1571
|
457 {
|
nickjillings@1571
|
458 case 'testTimer':
|
nickjillings@1571
|
459 sessionMetrics.prototype.enableTestTimer = true;
|
nickjillings@1571
|
460 break;
|
nickjillings@1571
|
461 case 'elementTimer':
|
nickjillings@1571
|
462 sessionMetrics.prototype.enableElementTimer = true;
|
nickjillings@1571
|
463 break;
|
nickjillings@1571
|
464 case 'elementTracker':
|
nickjillings@1571
|
465 sessionMetrics.prototype.enableElementTracker = true;
|
nickjillings@1571
|
466 break;
|
nickjillings@1571
|
467 case 'elementListenTracker':
|
nickjillings@1571
|
468 sessionMetrics.prototype.enableElementListenTracker = true;
|
nickjillings@1571
|
469 break;
|
nickjillings@1571
|
470 case 'elementInitialPosition':
|
nickjillings@1571
|
471 sessionMetrics.prototype.enableElementInitialPosition = true;
|
nickjillings@1571
|
472 break;
|
nickjillings@1571
|
473 case 'elementFlagListenedTo':
|
nickjillings@1571
|
474 sessionMetrics.prototype.enableFlagListenedTo = true;
|
nickjillings@1571
|
475 break;
|
nickjillings@1571
|
476 case 'elementFlagMoved':
|
nickjillings@1571
|
477 sessionMetrics.prototype.enableFlagMoved = true;
|
nickjillings@1571
|
478 break;
|
nickjillings@1571
|
479 case 'elementFlagComments':
|
nickjillings@1571
|
480 sessionMetrics.prototype.enableFlagComments = true;
|
nickjillings@1571
|
481 break;
|
nickjillings@1571
|
482 }
|
nickjillings@1571
|
483 });
|
nickjillings@1571
|
484
|
nickjillings@1571
|
485
|
nickjillings@1571
|
486
|
nickjillings@1571
|
487 // Detect the interface to use and load the relevant javascripts.
|
nickjillings@1571
|
488 var interfaceJS = document.createElement('script');
|
nickjillings@1571
|
489 interfaceJS.setAttribute("type","text/javascript");
|
nickjillings@1571
|
490 if (specification.interfaceType == 'APE') {
|
nickjillings@1571
|
491 interfaceJS.setAttribute("src","ape.js");
|
nickjillings@1571
|
492
|
nickjillings@1571
|
493 // APE comes with a css file
|
nickjillings@1571
|
494 var css = document.createElement('link');
|
nickjillings@1571
|
495 css.rel = 'stylesheet';
|
nickjillings@1571
|
496 css.type = 'text/css';
|
nickjillings@1571
|
497 css.href = 'ape.css';
|
nickjillings@1571
|
498
|
nickjillings@1571
|
499 document.getElementsByTagName("head")[0].appendChild(css);
|
nickjillings@1571
|
500 }
|
nickjillings@1571
|
501 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
|
nickjillings@1571
|
502
|
nickjillings@1571
|
503 // Define window callbacks for interface
|
nickjillings@1571
|
504 window.onresize = function(event){resizeWindow(event);};
|
nickjillings@1571
|
505 }
|
nickjillings@1571
|
506
|
nickjillings@1571
|
507 function createProjectSave(destURL) {
|
nickjillings@1571
|
508 // Save the data from interface into XML and send to destURL
|
nickjillings@1571
|
509 // If destURL is null then download XML in client
|
nickjillings@1571
|
510 // Now time to render file locally
|
nickjillings@1571
|
511 var xmlDoc = interfaceXMLSave();
|
nickjillings@1571
|
512 var parent = document.createElement("div");
|
nickjillings@1571
|
513 parent.appendChild(xmlDoc);
|
nickjillings@1571
|
514 var file = [parent.innerHTML];
|
nickjillings@1571
|
515 if (destURL == "null" || destURL == undefined) {
|
nickjillings@1571
|
516 var bb = new Blob(file,{type : 'application/xml'});
|
nickjillings@1571
|
517 var dnlk = window.URL.createObjectURL(bb);
|
nickjillings@1571
|
518 var a = document.createElement("a");
|
nickjillings@1571
|
519 a.hidden = '';
|
nickjillings@1571
|
520 a.href = dnlk;
|
nickjillings@1571
|
521 a.download = "save.xml";
|
nickjillings@1571
|
522 a.textContent = "Save File";
|
nickjillings@1571
|
523
|
nickjillings@1571
|
524 popup.showPopup();
|
nickjillings@1571
|
525 popup.popupContent.innerHTML = null;
|
nickjillings@1571
|
526 popup.popupContent.appendChild(a);
|
nickjillings@1571
|
527 } else {
|
nickjillings@1571
|
528 var xmlhttp = new XMLHttpRequest;
|
nickjillings@1571
|
529 xmlhttp.open("POST",destURL,true);
|
nickjillings@1571
|
530 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
|
nickjillings@1571
|
531 xmlhttp.onerror = function(){
|
nickjillings@1571
|
532 console.log('Error saving file to server! Presenting download locally');
|
nickjillings@1571
|
533 createProjectSave(null);
|
nickjillings@1571
|
534 };
|
nickjillings@1571
|
535 xmlhttp.onreadystatechange = function() {
|
nickjillings@1571
|
536 console.log(xmlhttp.status);
|
nickjillings@1571
|
537 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) {
|
nickjillings@1571
|
538 createProjectSave(null);
|
nickjillings@1571
|
539 }
|
nickjillings@1571
|
540 };
|
nickjillings@1571
|
541 xmlhttp.send(file);
|
nickjillings@1571
|
542 }
|
nickjillings@1571
|
543 }
|
nickjillings@1571
|
544
|
nickjillings@1571
|
545 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
|
nickjillings@1571
|
546 function interfaceXMLSave(){
|
nickjillings@1571
|
547 // Create the XML string to be exported with results
|
nickjillings@1571
|
548 var xmlDoc = document.createElement("BrowserEvaluationResult");
|
nickjillings@1571
|
549 xmlDoc.appendChild(returnDateNode());
|
nickjillings@1571
|
550 for (var i=0; i<testState.stateResults.length; i++)
|
nickjillings@1571
|
551 {
|
nickjillings@1571
|
552 xmlDoc.appendChild(testState.stateResults[i]);
|
nickjillings@1571
|
553 }
|
nickjillings@1571
|
554
|
nickjillings@1571
|
555 return xmlDoc;
|
nickjillings@1571
|
556 }
|
nickjillings@1571
|
557
|
nickjillings@1571
|
558 function AudioEngine() {
|
nickjillings@1571
|
559
|
nickjillings@1571
|
560 // Create two output paths, the main outputGain and fooGain.
|
nickjillings@1571
|
561 // Output gain is default to 1 and any items for playback route here
|
nickjillings@1571
|
562 // Foo gain is used for analysis to ensure paths get processed, but are not heard
|
nickjillings@1571
|
563 // because web audio will optimise and any route which does not go to the destination gets ignored.
|
nickjillings@1571
|
564 this.outputGain = audioContext.createGain();
|
nickjillings@1571
|
565 this.fooGain = audioContext.createGain();
|
nickjillings@1571
|
566 this.fooGain.gain = 0;
|
nickjillings@1571
|
567
|
nickjillings@1571
|
568 // Use this to detect playback state: 0 - stopped, 1 - playing
|
nickjillings@1571
|
569 this.status = 0;
|
nickjillings@1571
|
570 this.audioObjectsReady = false;
|
nickjillings@1571
|
571
|
nickjillings@1571
|
572 // Connect both gains to output
|
nickjillings@1571
|
573 this.outputGain.connect(audioContext.destination);
|
nickjillings@1571
|
574 this.fooGain.connect(audioContext.destination);
|
nickjillings@1571
|
575
|
nickjillings@1571
|
576 // Create the timer Object
|
nickjillings@1571
|
577 this.timer = new timer();
|
nickjillings@1571
|
578 // Create session metrics
|
nickjillings@1571
|
579 this.metric = new sessionMetrics(this);
|
nickjillings@1571
|
580
|
nickjillings@1571
|
581 this.loopPlayback = false;
|
nickjillings@1571
|
582
|
nickjillings@1571
|
583 // Create store for new audioObjects
|
nickjillings@1571
|
584 this.audioObjects = [];
|
nickjillings@1571
|
585
|
nickjillings@1571
|
586 this.play = function() {
|
nickjillings@1571
|
587 // Start the timer and set the audioEngine state to playing (1)
|
nickjillings@1571
|
588 if (this.status == 0) {
|
nickjillings@1571
|
589 // Check if all audioObjects are ready
|
nickjillings@1571
|
590 if (this.audioObjectsReady == false) {
|
nickjillings@1571
|
591 this.audioObjectsReady = this.checkAllReady();
|
nickjillings@1571
|
592 }
|
nickjillings@1571
|
593 if (this.audioObjectsReady == true) {
|
nickjillings@1571
|
594 this.timer.startTest();
|
nickjillings@1571
|
595 if (this.loopPlayback) {
|
nickjillings@1571
|
596 for(var i=0; i<this.audioObjects.length; i++) {
|
nickjillings@1571
|
597 this.audioObjects[i].play(this.timer.getTestTime()+1);
|
nickjillings@1571
|
598 }
|
nickjillings@1571
|
599 }
|
nickjillings@1571
|
600 this.status = 1;
|
nickjillings@1571
|
601 }
|
nickjillings@1571
|
602 }
|
nickjillings@1571
|
603 };
|
nickjillings@1571
|
604
|
nickjillings@1571
|
605 this.stop = function() {
|
nickjillings@1571
|
606 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
|
nickjillings@1571
|
607 if (this.status == 1) {
|
nickjillings@1571
|
608 for (var i=0; i<this.audioObjects.length; i++)
|
nickjillings@1571
|
609 {
|
nickjillings@1571
|
610 this.audioObjects[i].stop();
|
nickjillings@1571
|
611 }
|
nickjillings@1571
|
612 this.status = 0;
|
nickjillings@1571
|
613 }
|
nickjillings@1571
|
614 };
|
nickjillings@1571
|
615
|
nickjillings@1571
|
616
|
nickjillings@1571
|
617 this.newTrack = function(element) {
|
nickjillings@1571
|
618 // Pull data from given URL into new audio buffer
|
nickjillings@1571
|
619 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
|
nickjillings@1571
|
620
|
nickjillings@1571
|
621 // Create the audioObject with ID of the new track length;
|
nickjillings@1571
|
622 audioObjectId = this.audioObjects.length;
|
nickjillings@1571
|
623 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
|
nickjillings@1571
|
624
|
nickjillings@1571
|
625 // AudioObject will get track itself.
|
nickjillings@1571
|
626 this.audioObjects[audioObjectId].specification = element;
|
nickjillings@1571
|
627 this.audioObjects[audioObjectId].constructTrack(element.parent.hostURL + element.url);
|
nickjillings@1571
|
628 return this.audioObjects[audioObjectId];
|
nickjillings@1571
|
629 };
|
nickjillings@1571
|
630
|
nickjillings@1571
|
631 this.newTestPage = function() {
|
nickjillings@1571
|
632 this.state = 0;
|
nickjillings@1571
|
633 this.audioObjectsReady = false;
|
nickjillings@1571
|
634 this.metric.reset();
|
nickjillings@1571
|
635 this.audioObjects = [];
|
nickjillings@1571
|
636 };
|
nickjillings@1571
|
637
|
nickjillings@1571
|
638 this.checkAllPlayed = function() {
|
nickjillings@1571
|
639 arr = [];
|
nickjillings@1571
|
640 for (var id=0; id<this.audioObjects.length; id++) {
|
nickjillings@1571
|
641 if (this.audioObjects[id].metric.wasListenedTo == false) {
|
nickjillings@1571
|
642 arr.push(this.audioObjects[id].id);
|
nickjillings@1571
|
643 }
|
nickjillings@1571
|
644 }
|
nickjillings@1571
|
645 return arr;
|
nickjillings@1571
|
646 };
|
nickjillings@1571
|
647
|
nickjillings@1571
|
648 this.checkAllReady = function() {
|
nickjillings@1571
|
649 var ready = true;
|
nickjillings@1571
|
650 for (var i=0; i<this.audioObjects.length; i++) {
|
nickjillings@1571
|
651 if (this.audioObjects[i].state == 0) {
|
nickjillings@1571
|
652 // Track not ready
|
nickjillings@1571
|
653 console.log('WAIT -- audioObject '+i+' not ready yet!');
|
nickjillings@1571
|
654 ready = false;
|
nickjillings@1571
|
655 };
|
nickjillings@1571
|
656 }
|
nickjillings@1571
|
657 return ready;
|
nickjillings@1571
|
658 };
|
nickjillings@1571
|
659
|
nickjillings@1571
|
660 }
|
nickjillings@1571
|
661
|
nickjillings@1571
|
662 function audioObject(id) {
|
nickjillings@1571
|
663 // The main buffer object with common control nodes to the AudioEngine
|
nickjillings@1571
|
664
|
nickjillings@1571
|
665 this.specification;
|
nickjillings@1571
|
666 this.id = id;
|
nickjillings@1571
|
667 this.state = 0; // 0 - no data, 1 - ready
|
nickjillings@1571
|
668 this.url = null; // Hold the URL given for the output back to the results.
|
nickjillings@1571
|
669 this.metric = new metricTracker(this);
|
nickjillings@1571
|
670
|
nickjillings@1571
|
671 // Bindings for GUI
|
nickjillings@1571
|
672 this.interfaceDOM = null;
|
nickjillings@1571
|
673 this.commentDOM = null;
|
nickjillings@1571
|
674
|
nickjillings@1571
|
675 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
|
nickjillings@1571
|
676 this.bufferNode = undefined;
|
nickjillings@1571
|
677 this.outputGain = audioContext.createGain();
|
nickjillings@1571
|
678
|
nickjillings@1571
|
679 // Default output gain to be zero
|
nickjillings@1571
|
680 this.outputGain.gain.value = 0.0;
|
nickjillings@1571
|
681
|
nickjillings@1571
|
682 // Connect buffer to the audio graph
|
nickjillings@1571
|
683 this.outputGain.connect(audioEngineContext.outputGain);
|
nickjillings@1571
|
684
|
nickjillings@1571
|
685 // the audiobuffer is not designed for multi-start playback
|
nickjillings@1571
|
686 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
|
nickjillings@1571
|
687 this.buffer;
|
nickjillings@1571
|
688
|
nickjillings@1571
|
689 this.loopStart = function() {
|
nickjillings@1571
|
690 this.outputGain.gain.value = 1.0;
|
nickjillings@1571
|
691 this.metric.startListening(audioEngineContext.timer.getTestTime());
|
nickjillings@1571
|
692 };
|
nickjillings@1571
|
693
|
nickjillings@1571
|
694 this.loopStop = function() {
|
nickjillings@1571
|
695 if (this.outputGain.gain.value != 0.0) {
|
nickjillings@1571
|
696 this.outputGain.gain.value = 0.0;
|
nickjillings@1571
|
697 this.metric.stopListening(audioEngineContext.timer.getTestTime());
|
nickjillings@1571
|
698 }
|
nickjillings@1571
|
699 };
|
nickjillings@1571
|
700
|
nickjillings@1571
|
701 this.play = function(startTime) {
|
nickjillings@1571
|
702 this.bufferNode = audioContext.createBufferSource();
|
nickjillings@1571
|
703 this.bufferNode.owner = this;
|
nickjillings@1571
|
704 this.bufferNode.connect(this.outputGain);
|
nickjillings@1571
|
705 this.bufferNode.buffer = this.buffer;
|
nickjillings@1571
|
706 this.bufferNode.loop = audioEngineContext.loopPlayback;
|
nickjillings@1571
|
707 this.bufferNode.onended = function() {
|
nickjillings@1571
|
708 // Safari does not like using 'this' to reference the calling object!
|
nickjillings@1571
|
709 event.srcElement.owner.metric.stopListening(audioEngineContext.timer.getTestTime());
|
nickjillings@1571
|
710 };
|
nickjillings@1571
|
711 if (this.bufferNode.loop == false) {
|
nickjillings@1571
|
712 this.metric.startListening(audioEngineContext.timer.getTestTime());
|
nickjillings@1571
|
713 }
|
nickjillings@1571
|
714 this.bufferNode.start(startTime);
|
nickjillings@1571
|
715 };
|
nickjillings@1571
|
716
|
nickjillings@1571
|
717 this.stop = function() {
|
nickjillings@1571
|
718 if (this.bufferNode != undefined)
|
nickjillings@1571
|
719 {
|
nickjillings@1571
|
720 this.bufferNode.stop(0);
|
nickjillings@1571
|
721 this.bufferNode = undefined;
|
nickjillings@1571
|
722 this.metric.stopListening(audioEngineContext.timer.getTestTime());
|
nickjillings@1571
|
723 }
|
nickjillings@1571
|
724 };
|
nickjillings@1571
|
725
|
nickjillings@1571
|
726 this.getCurrentPosition = function() {
|
nickjillings@1571
|
727 var time = audioEngineContext.timer.getTestTime();
|
nickjillings@1571
|
728 if (this.bufferNode != undefined) {
|
nickjillings@1571
|
729 if (this.bufferNode.loop == true) {
|
nickjillings@1571
|
730 if (audioEngineContext.status == 1) {
|
nickjillings@1571
|
731 return time%this.buffer.duration;
|
nickjillings@1571
|
732 } else {
|
nickjillings@1571
|
733 return 0;
|
nickjillings@1571
|
734 }
|
nickjillings@1571
|
735 } else {
|
nickjillings@1571
|
736 if (this.metric.listenHold) {
|
nickjillings@1571
|
737 return time - this.metric.listenStart;
|
nickjillings@1571
|
738 } else {
|
nickjillings@1571
|
739 return 0;
|
nickjillings@1571
|
740 }
|
nickjillings@1571
|
741 }
|
nickjillings@1571
|
742 } else {
|
nickjillings@1571
|
743 return 0;
|
nickjillings@1571
|
744 }
|
nickjillings@1571
|
745 };
|
nickjillings@1571
|
746
|
nickjillings@1571
|
747 this.constructTrack = function(url) {
|
nickjillings@1571
|
748 var request = new XMLHttpRequest();
|
nickjillings@1571
|
749 this.url = url;
|
nickjillings@1571
|
750 request.open('GET',url,true);
|
nickjillings@1571
|
751 request.responseType = 'arraybuffer';
|
nickjillings@1571
|
752
|
nickjillings@1571
|
753 var audioObj = this;
|
nickjillings@1571
|
754
|
nickjillings@1571
|
755 // Create callback to decode the data asynchronously
|
nickjillings@1571
|
756 request.onloadend = function() {
|
nickjillings@1571
|
757 audioContext.decodeAudioData(request.response, function(decodedData) {
|
nickjillings@1571
|
758 audioObj.buffer = decodedData;
|
nickjillings@1571
|
759 audioObj.state = 1;
|
nickjillings@1571
|
760 }, function(){
|
nickjillings@1571
|
761 // Should only be called if there was an error, but sometimes gets called continuously
|
nickjillings@1571
|
762 // Check here if the error is genuine
|
nickjillings@1571
|
763 if (audioObj.state == 0 || audioObj.buffer == undefined) {
|
nickjillings@1571
|
764 // Genuine error
|
nickjillings@1571
|
765 console.log('FATAL - Error loading buffer on '+audioObj.id);
|
nickjillings@1571
|
766 }
|
nickjillings@1571
|
767 });
|
nickjillings@1571
|
768 };
|
nickjillings@1571
|
769 request.send();
|
nickjillings@1571
|
770 };
|
nickjillings@1571
|
771
|
nickjillings@1571
|
772 this.exportXMLDOM = function() {
|
nickjillings@1571
|
773 var root = document.createElement('audioElement');
|
nickjillings@1571
|
774 root.id = this.specification.id;
|
nickjillings@1571
|
775 root.setAttribute('url',this.url);
|
nickjillings@1571
|
776 root.appendChild(this.interfaceDOM.exportXMLDOM());
|
nickjillings@1571
|
777 root.appendChild(this.commentDOM.exportXMLDOM());
|
nickjillings@1571
|
778 root.appendChild(this.metric.exportXMLDOM());
|
nickjillings@1571
|
779 return root;
|
nickjillings@1571
|
780 };
|
nickjillings@1571
|
781 }
|
nickjillings@1571
|
782
|
nickjillings@1571
|
783 function timer()
|
nickjillings@1571
|
784 {
|
nickjillings@1571
|
785 /* Timer object used in audioEngine to keep track of session timings
|
nickjillings@1571
|
786 * Uses the timer of the web audio API, so sample resolution
|
nickjillings@1571
|
787 */
|
nickjillings@1571
|
788 this.testStarted = false;
|
nickjillings@1571
|
789 this.testStartTime = 0;
|
nickjillings@1571
|
790 this.testDuration = 0;
|
nickjillings@1571
|
791 this.minimumTestTime = 0; // No minimum test time
|
nickjillings@1571
|
792 this.startTest = function()
|
nickjillings@1571
|
793 {
|
nickjillings@1571
|
794 if (this.testStarted == false)
|
nickjillings@1571
|
795 {
|
nickjillings@1571
|
796 this.testStartTime = audioContext.currentTime;
|
nickjillings@1571
|
797 this.testStarted = true;
|
nickjillings@1571
|
798 this.updateTestTime();
|
nickjillings@1571
|
799 audioEngineContext.metric.initialiseTest();
|
nickjillings@1571
|
800 }
|
nickjillings@1571
|
801 };
|
nickjillings@1571
|
802 this.stopTest = function()
|
nickjillings@1571
|
803 {
|
nickjillings@1571
|
804 if (this.testStarted)
|
nickjillings@1571
|
805 {
|
nickjillings@1571
|
806 this.testDuration = this.getTestTime();
|
nickjillings@1571
|
807 this.testStarted = false;
|
nickjillings@1571
|
808 } else {
|
nickjillings@1571
|
809 console.log('ERR: Test tried to end before beginning');
|
nickjillings@1571
|
810 }
|
nickjillings@1571
|
811 };
|
nickjillings@1571
|
812 this.updateTestTime = function()
|
nickjillings@1571
|
813 {
|
nickjillings@1571
|
814 if (this.testStarted)
|
nickjillings@1571
|
815 {
|
nickjillings@1571
|
816 this.testDuration = audioContext.currentTime - this.testStartTime;
|
nickjillings@1571
|
817 }
|
nickjillings@1571
|
818 };
|
nickjillings@1571
|
819 this.getTestTime = function()
|
nickjillings@1571
|
820 {
|
nickjillings@1571
|
821 this.updateTestTime();
|
nickjillings@1571
|
822 return this.testDuration;
|
nickjillings@1571
|
823 };
|
nickjillings@1571
|
824 }
|
nickjillings@1571
|
825
|
nickjillings@1571
|
826 function sessionMetrics(engine)
|
nickjillings@1571
|
827 {
|
nickjillings@1571
|
828 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
|
nickjillings@1571
|
829 */
|
nickjillings@1571
|
830 this.engine = engine;
|
nickjillings@1571
|
831 this.lastClicked = -1;
|
nickjillings@1571
|
832 this.data = -1;
|
nickjillings@1571
|
833 this.reset = function() {
|
nickjillings@1571
|
834 this.lastClicked = -1;
|
nickjillings@1571
|
835 this.data = -1;
|
nickjillings@1571
|
836 };
|
nickjillings@1571
|
837 this.initialiseTest = function(){};
|
nickjillings@1571
|
838 }
|
nickjillings@1571
|
839
|
nickjillings@1571
|
840 function metricTracker(caller)
|
nickjillings@1571
|
841 {
|
nickjillings@1571
|
842 /* Custom object to track and collect metric data
|
nickjillings@1571
|
843 * Used only inside the audioObjects object.
|
nickjillings@1571
|
844 */
|
nickjillings@1571
|
845
|
nickjillings@1571
|
846 this.listenedTimer = 0;
|
nickjillings@1571
|
847 this.listenStart = 0;
|
nickjillings@1571
|
848 this.listenHold = false;
|
nickjillings@1571
|
849 this.initialPosition = -1;
|
nickjillings@1571
|
850 this.movementTracker = [];
|
nickjillings@1571
|
851 this.listenTracker =[];
|
nickjillings@1571
|
852 this.wasListenedTo = false;
|
nickjillings@1571
|
853 this.wasMoved = false;
|
nickjillings@1571
|
854 this.hasComments = false;
|
nickjillings@1571
|
855 this.parent = caller;
|
nickjillings@1571
|
856
|
nickjillings@1571
|
857 this.initialised = function(position)
|
nickjillings@1571
|
858 {
|
nickjillings@1571
|
859 if (this.initialPosition == -1) {
|
nickjillings@1571
|
860 this.initialPosition = position;
|
nickjillings@1571
|
861 }
|
nickjillings@1571
|
862 };
|
nickjillings@1571
|
863
|
nickjillings@1571
|
864 this.moved = function(time,position)
|
nickjillings@1571
|
865 {
|
nickjillings@1571
|
866 this.wasMoved = true;
|
nickjillings@1571
|
867 this.movementTracker[this.movementTracker.length] = [time, position];
|
nickjillings@1571
|
868 };
|
nickjillings@1571
|
869
|
nickjillings@1571
|
870 this.startListening = function(time)
|
nickjillings@1571
|
871 {
|
nickjillings@1571
|
872 if (this.listenHold == false)
|
nickjillings@1571
|
873 {
|
nickjillings@1571
|
874 this.wasListenedTo = true;
|
nickjillings@1571
|
875 this.listenStart = time;
|
nickjillings@1571
|
876 this.listenHold = true;
|
nickjillings@1571
|
877
|
nickjillings@1571
|
878 var evnt = document.createElement('event');
|
nickjillings@1571
|
879 var testTime = document.createElement('testTime');
|
nickjillings@1571
|
880 testTime.setAttribute('start',time);
|
nickjillings@1571
|
881 var bufferTime = document.createElement('bufferTime');
|
nickjillings@1571
|
882 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
|
nickjillings@1571
|
883 evnt.appendChild(testTime);
|
nickjillings@1571
|
884 evnt.appendChild(bufferTime);
|
nickjillings@1571
|
885 this.listenTracker.push(evnt);
|
nickjillings@1571
|
886
|
nickjillings@1571
|
887 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
|
nickjillings@1571
|
888 }
|
nickjillings@1571
|
889 };
|
nickjillings@1571
|
890
|
nickjillings@1571
|
891 this.stopListening = function(time)
|
nickjillings@1571
|
892 {
|
nickjillings@1571
|
893 if (this.listenHold == true)
|
nickjillings@1571
|
894 {
|
nickjillings@1571
|
895 var diff = time - this.listenStart;
|
nickjillings@1571
|
896 this.listenedTimer += (diff);
|
nickjillings@1571
|
897 this.listenStart = 0;
|
nickjillings@1571
|
898 this.listenHold = false;
|
nickjillings@1571
|
899
|
nickjillings@1571
|
900 var evnt = this.listenTracker[this.listenTracker.length-1];
|
nickjillings@1571
|
901 var testTime = evnt.getElementsByTagName('testTime')[0];
|
nickjillings@1571
|
902 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
|
nickjillings@1571
|
903 testTime.setAttribute('stop',time);
|
nickjillings@1571
|
904 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
|
nickjillings@1571
|
905 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
|
nickjillings@1571
|
906 }
|
nickjillings@1571
|
907 };
|
nickjillings@1571
|
908
|
nickjillings@1571
|
909 this.exportXMLDOM = function() {
|
nickjillings@1571
|
910 var root = document.createElement('metric');
|
nickjillings@1571
|
911 if (audioEngineContext.metric.enableElementTimer) {
|
nickjillings@1571
|
912 var mElementTimer = document.createElement('metricresult');
|
nickjillings@1571
|
913 mElementTimer.setAttribute('name','enableElementTimer');
|
nickjillings@1571
|
914 mElementTimer.textContent = this.listenedTimer;
|
nickjillings@1571
|
915 root.appendChild(mElementTimer);
|
nickjillings@1571
|
916 }
|
nickjillings@1571
|
917 if (audioEngineContext.metric.enableElementTracker) {
|
nickjillings@1571
|
918 var elementTrackerFull = document.createElement('metricResult');
|
nickjillings@1571
|
919 elementTrackerFull.setAttribute('name','elementTrackerFull');
|
nickjillings@1571
|
920 for (var k=0; k<this.movementTracker.length; k++)
|
nickjillings@1571
|
921 {
|
nickjillings@1571
|
922 var timePos = document.createElement('timePos');
|
nickjillings@1571
|
923 timePos.id = k;
|
nickjillings@1571
|
924 var time = document.createElement('time');
|
nickjillings@1571
|
925 time.textContent = this.movementTracker[k][0];
|
nickjillings@1571
|
926 var position = document.createElement('position');
|
nickjillings@1571
|
927 position.textContent = this.movementTracker[k][1];
|
nickjillings@1571
|
928 timePos.appendChild(time);
|
nickjillings@1571
|
929 timePos.appendChild(position);
|
nickjillings@1571
|
930 elementTrackerFull.appendChild(timePos);
|
nickjillings@1571
|
931 }
|
nickjillings@1571
|
932 root.appendChild(elementTrackerFull);
|
nickjillings@1571
|
933 }
|
nickjillings@1571
|
934 if (audioEngineContext.metric.enableElementListenTracker) {
|
nickjillings@1571
|
935 var elementListenTracker = document.createElement('metricResult');
|
nickjillings@1571
|
936 elementListenTracker.setAttribute('name','elementListenTracker');
|
nickjillings@1571
|
937 for (var k=0; k<this.listenTracker.length; k++) {
|
nickjillings@1571
|
938 elementListenTracker.appendChild(this.listenTracker[k]);
|
nickjillings@1571
|
939 }
|
nickjillings@1571
|
940 root.appendChild(elementListenTracker);
|
nickjillings@1571
|
941 }
|
nickjillings@1571
|
942 if (audioEngineContext.metric.enableElementInitialPosition) {
|
nickjillings@1571
|
943 var elementInitial = document.createElement('metricResult');
|
nickjillings@1571
|
944 elementInitial.setAttribute('name','elementInitialPosition');
|
nickjillings@1571
|
945 elementInitial.textContent = this.initialPosition;
|
nickjillings@1571
|
946 root.appendChild(elementInitial);
|
nickjillings@1571
|
947 }
|
nickjillings@1571
|
948 if (audioEngineContext.metric.enableFlagListenedTo) {
|
nickjillings@1571
|
949 var flagListenedTo = document.createElement('metricResult');
|
nickjillings@1571
|
950 flagListenedTo.setAttribute('name','elementFlagListenedTo');
|
nickjillings@1571
|
951 flagListenedTo.textContent = this.wasListenedTo;
|
nickjillings@1571
|
952 root.appendChild(flagListenedTo);
|
nickjillings@1571
|
953 }
|
nickjillings@1571
|
954 if (audioEngineContext.metric.enableFlagMoved) {
|
nickjillings@1571
|
955 var flagMoved = document.createElement('metricResult');
|
nickjillings@1571
|
956 flagMoved.setAttribute('name','elementFlagMoved');
|
nickjillings@1571
|
957 flagMoved.textContent = this.wasMoved;
|
nickjillings@1571
|
958 root.appendChild(flagMoved);
|
nickjillings@1571
|
959 }
|
nickjillings@1571
|
960 if (audioEngineContext.metric.enableFlagComments) {
|
nickjillings@1571
|
961 var flagComments = document.createElement('metricResult');
|
nickjillings@1571
|
962 flagComments.setAttribute('name','elementFlagComments');
|
nickjillings@1571
|
963 if (this.parent.commentDOM == null)
|
nickjillings@1571
|
964 {flag.textContent = 'false';}
|
nickjillings@1571
|
965 else if (this.parent.commentDOM.textContent.length == 0)
|
nickjillings@1571
|
966 {flag.textContent = 'false';}
|
nickjillings@1571
|
967 else
|
nickjillings@1571
|
968 {flag.textContet = 'true';}
|
nickjillings@1571
|
969 root.appendChild(flagComments);
|
nickjillings@1571
|
970 }
|
nickjillings@1571
|
971
|
nickjillings@1571
|
972 return root;
|
nickjillings@1571
|
973 };
|
nickjillings@1571
|
974 }
|
nickjillings@1571
|
975
|
nickjillings@1571
|
976 function randomiseOrder(input)
|
nickjillings@1571
|
977 {
|
nickjillings@1571
|
978 // This takes an array of information and randomises the order
|
nickjillings@1571
|
979 var N = input.length;
|
nickjillings@1571
|
980 var K = N;
|
nickjillings@1571
|
981 var holdArr = [];
|
nickjillings@1571
|
982 for (var n=0; n<N; n++)
|
nickjillings@1571
|
983 {
|
nickjillings@1571
|
984 // First pick a random number
|
nickjillings@1571
|
985 var r = Math.random();
|
nickjillings@1571
|
986 // Multiply and floor by the number of elements left
|
nickjillings@1571
|
987 r = Math.floor(r*input.length);
|
nickjillings@1571
|
988 // Pick out that element and delete from the array
|
nickjillings@1571
|
989 holdArr.push(input.splice(r,1)[0]);
|
nickjillings@1571
|
990 }
|
nickjillings@1571
|
991 return holdArr;
|
nickjillings@1571
|
992 }
|
nickjillings@1571
|
993
|
nickjillings@1571
|
994 function returnDateNode()
|
nickjillings@1571
|
995 {
|
nickjillings@1571
|
996 // Create an XML Node for the Date and Time a test was conducted
|
nickjillings@1571
|
997 // Structure is
|
nickjillings@1571
|
998 // <datetime>
|
nickjillings@1571
|
999 // <date year="##" month="##" day="##">DD/MM/YY</date>
|
nickjillings@1571
|
1000 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
|
nickjillings@1571
|
1001 // </datetime>
|
nickjillings@1571
|
1002 var dateTime = new Date();
|
nickjillings@1571
|
1003 var year = document.createAttribute('year');
|
nickjillings@1571
|
1004 var month = document.createAttribute('month');
|
nickjillings@1571
|
1005 var day = document.createAttribute('day');
|
nickjillings@1571
|
1006 var hour = document.createAttribute('hour');
|
nickjillings@1571
|
1007 var minute = document.createAttribute('minute');
|
nickjillings@1571
|
1008 var secs = document.createAttribute('secs');
|
nickjillings@1571
|
1009
|
nickjillings@1571
|
1010 year.nodeValue = dateTime.getFullYear();
|
nickjillings@1571
|
1011 month.nodeValue = dateTime.getMonth()+1;
|
nickjillings@1571
|
1012 day.nodeValue = dateTime.getDate();
|
nickjillings@1571
|
1013 hour.nodeValue = dateTime.getHours();
|
nickjillings@1571
|
1014 minute.nodeValue = dateTime.getMinutes();
|
nickjillings@1571
|
1015 secs.nodeValue = dateTime.getSeconds();
|
nickjillings@1571
|
1016
|
nickjillings@1571
|
1017 var hold = document.createElement("datetime");
|
nickjillings@1571
|
1018 var date = document.createElement("date");
|
nickjillings@1571
|
1019 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
|
nickjillings@1571
|
1020 var time = document.createElement("time");
|
nickjillings@1571
|
1021 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
|
nickjillings@1571
|
1022
|
nickjillings@1571
|
1023 date.setAttributeNode(year);
|
nickjillings@1571
|
1024 date.setAttributeNode(month);
|
nickjillings@1571
|
1025 date.setAttributeNode(day);
|
nickjillings@1571
|
1026 time.setAttributeNode(hour);
|
nickjillings@1571
|
1027 time.setAttributeNode(minute);
|
nickjillings@1571
|
1028 time.setAttributeNode(secs);
|
nickjillings@1571
|
1029
|
nickjillings@1571
|
1030 hold.appendChild(date);
|
nickjillings@1571
|
1031 hold.appendChild(time);
|
nickjillings@1571
|
1032 return hold
|
nickjillings@1571
|
1033
|
nickjillings@1571
|
1034 }
|
nickjillings@1571
|
1035
|
nickjillings@1571
|
1036 function testWaitIndicator() {
|
nickjillings@1571
|
1037 if (audioEngineContext.checkAllReady() == false) {
|
nickjillings@1571
|
1038 var hold = document.createElement("div");
|
nickjillings@1571
|
1039 hold.id = "testWaitIndicator";
|
nickjillings@1571
|
1040 hold.className = "indicator-box";
|
nickjillings@1571
|
1041 hold.style.zIndex = 3;
|
nickjillings@1571
|
1042 var span = document.createElement("span");
|
nickjillings@1571
|
1043 span.textContent = "Please wait! Elements still loading";
|
nickjillings@1571
|
1044 hold.appendChild(span);
|
nickjillings@1571
|
1045 var blank = document.createElement('div');
|
nickjillings@1571
|
1046 blank.className = 'testHalt';
|
nickjillings@1571
|
1047 blank.id = "testHaltBlank";
|
nickjillings@1571
|
1048 var body = document.getElementsByTagName('body')[0];
|
nickjillings@1571
|
1049 body.appendChild(hold);
|
nickjillings@1571
|
1050 body.appendChild(blank);
|
nickjillings@1571
|
1051 testWaitTimerIntervalHolder = setInterval(function(){
|
nickjillings@1571
|
1052 var ready = audioEngineContext.checkAllReady();
|
nickjillings@1571
|
1053 if (ready) {
|
nickjillings@1571
|
1054 var elem = document.getElementById('testWaitIndicator');
|
nickjillings@1571
|
1055 var blank = document.getElementById('testHaltBlank');
|
nickjillings@1571
|
1056 var body = document.getElementsByTagName('body')[0];
|
nickjillings@1571
|
1057 body.removeChild(elem);
|
nickjillings@1571
|
1058 body.removeChild(blank);
|
nickjillings@1571
|
1059 clearInterval(testWaitTimerIntervalHolder);
|
nickjillings@1571
|
1060 }
|
nickjillings@1571
|
1061 },500,false);
|
nickjillings@1571
|
1062 }
|
nickjillings@1571
|
1063 }
|
nickjillings@1571
|
1064
|
nickjillings@1571
|
1065 var testWaitTimerIntervalHolder = null;
|
nickjillings@1571
|
1066
|
nickjillings@1571
|
1067 function Specification() {
|
nickjillings@1571
|
1068 // Handles the decoding of the project specification XML into a simple JavaScript Object.
|
nickjillings@1571
|
1069
|
nickjillings@1571
|
1070 this.interfaceType;
|
nickjillings@1571
|
1071 this.projectReturn;
|
nickjillings@1571
|
1072 this.randomiseOrder;
|
nickjillings@1571
|
1073 this.collectMetrics;
|
nickjillings@1571
|
1074 this.preTest;
|
nickjillings@1571
|
1075 this.postTest;
|
nickjillings@1571
|
1076 this.metrics =[];
|
nickjillings@1571
|
1077
|
nickjillings@1571
|
1078 this.audioHolders = [];
|
nickjillings@1571
|
1079
|
nickjillings@1571
|
1080 this.decode = function() {
|
nickjillings@1571
|
1081 // projectXML - DOM Parsed document
|
nickjillings@1571
|
1082 var setupNode = projectXML.getElementsByTagName('setup')[0];
|
nickjillings@1571
|
1083 this.interfaceType = setupNode.getAttribute('interface');
|
nickjillings@1571
|
1084 this.projectReturn = setupNode.getAttribute('projectReturn');
|
nickjillings@1571
|
1085 if (setupNode.getAttribute('randomiseOrder') == "true") {
|
nickjillings@1571
|
1086 this.randomiseOrder = true;
|
nickjillings@1571
|
1087 } else {this.randomiseOrder = false;}
|
nickjillings@1571
|
1088 if (setupNode.getAttribute('collectMetrics') == "true") {
|
nickjillings@1571
|
1089 this.collectMetrics = true;
|
nickjillings@1571
|
1090 } else {this.collectMetrics = false;}
|
nickjillings@1571
|
1091 var metricCollection = setupNode.getElementsByTagName('Metric');
|
nickjillings@1571
|
1092
|
nickjillings@1571
|
1093 this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest'));
|
nickjillings@1571
|
1094 this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest'));
|
nickjillings@1571
|
1095
|
nickjillings@1571
|
1096 if (metricCollection.length > 0) {
|
nickjillings@1571
|
1097 metricCollection = metricCollection[0].getElementsByTagName('metricEnable');
|
nickjillings@1571
|
1098 for (var i=0; i<metricCollection.length; i++) {
|
nickjillings@1571
|
1099 this.metrics.push(new this.metricNode(metricCollection[i].textContent));
|
nickjillings@1571
|
1100 }
|
nickjillings@1571
|
1101 }
|
nickjillings@1571
|
1102
|
nickjillings@1571
|
1103 var audioHolders = projectXML.getElementsByTagName('audioHolder');
|
nickjillings@1571
|
1104 for (var i=0; i<audioHolders.length; i++) {
|
nickjillings@1571
|
1105 this.audioHolders.push(new this.audioHolderNode(this,audioHolders[i]));
|
nickjillings@1571
|
1106 }
|
nickjillings@1571
|
1107
|
nickjillings@1571
|
1108 };
|
nickjillings@1571
|
1109
|
nickjillings@1571
|
1110 this.prepostNode = function(type,Collection) {
|
nickjillings@1571
|
1111 this.type = type;
|
nickjillings@1571
|
1112 this.options = [];
|
nickjillings@1571
|
1113
|
nickjillings@1571
|
1114 this.OptionNode = function(child) {
|
nickjillings@1571
|
1115
|
nickjillings@1571
|
1116 this.childOption = function(element) {
|
nickjillings@1571
|
1117 this.type = 'option';
|
nickjillings@1571
|
1118 this.id = element.id;
|
nickjillings@1571
|
1119 this.name = element.getAttribute('name');
|
nickjillings@1571
|
1120 this.text = element.textContent;
|
nickjillings@1571
|
1121 };
|
nickjillings@1571
|
1122
|
nickjillings@1571
|
1123 this.type = child.nodeName;
|
nickjillings@1571
|
1124 if (child.nodeName == "question") {
|
nickjillings@1571
|
1125 this.id = child.id;
|
nickjillings@1571
|
1126 this.mandatory;
|
nickjillings@1571
|
1127 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;}
|
nickjillings@1571
|
1128 else {this.mandatory = false;}
|
nickjillings@1571
|
1129 this.question = child.textContent;
|
nickjillings@1571
|
1130 if (child.getAttribute('boxsize') == null) {
|
nickjillings@1571
|
1131 this.boxsize = 'normal';
|
nickjillings@1571
|
1132 } else {
|
nickjillings@1571
|
1133 this.boxsize = child.getAttribute('boxsize');
|
nickjillings@1571
|
1134 }
|
nickjillings@1571
|
1135 } else if (child.nodeName == "statement") {
|
nickjillings@1571
|
1136 this.statement = child.textContent;
|
nickjillings@1571
|
1137 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") {
|
nickjillings@1571
|
1138 var element = child.firstElementChild;
|
nickjillings@1571
|
1139 this.id = child.id;
|
nickjillings@1571
|
1140 if (element == null) {
|
nickjillings@1571
|
1141 console.log('Malformed' +child.nodeName+ 'entry');
|
nickjillings@1571
|
1142 this.statement = 'Malformed' +child.nodeName+ 'entry';
|
nickjillings@1571
|
1143 this.type = 'statement';
|
nickjillings@1571
|
1144 } else {
|
nickjillings@1571
|
1145 this.options = [];
|
nickjillings@1571
|
1146 while (element != null) {
|
nickjillings@1571
|
1147 if (element.nodeName == 'statement' && this.statement == undefined){
|
nickjillings@1571
|
1148 this.statement = element.textContent;
|
nickjillings@1571
|
1149 } else if (element.nodeName == 'option') {
|
nickjillings@1571
|
1150 this.options.push(new this.childOption(element));
|
nickjillings@1571
|
1151 }
|
nickjillings@1571
|
1152 element = element.nextElementSibling;
|
nickjillings@1571
|
1153 }
|
nickjillings@1571
|
1154 }
|
nickjillings@1571
|
1155 }
|
nickjillings@1571
|
1156 };
|
nickjillings@1571
|
1157
|
nickjillings@1571
|
1158 // On construction:
|
nickjillings@1571
|
1159 if (Collection.length != 0) {
|
nickjillings@1571
|
1160 Collection = Collection[0];
|
nickjillings@1571
|
1161 if (Collection.childElementCount != 0) {
|
nickjillings@1571
|
1162 var child = Collection.firstElementChild;
|
nickjillings@1571
|
1163 this.options.push(new this.OptionNode(child));
|
nickjillings@1571
|
1164 while (child.nextElementSibling != null) {
|
nickjillings@1571
|
1165 child = child.nextElementSibling;
|
nickjillings@1571
|
1166 this.options.push(new this.OptionNode(child));
|
nickjillings@1571
|
1167 }
|
nickjillings@1571
|
1168 }
|
nickjillings@1571
|
1169 }
|
nickjillings@1571
|
1170 };
|
nickjillings@1571
|
1171
|
nickjillings@1571
|
1172 this.metricNode = function(name) {
|
nickjillings@1571
|
1173 this.enabled = name;
|
nickjillings@1571
|
1174 };
|
nickjillings@1571
|
1175
|
nickjillings@1571
|
1176 this.audioHolderNode = function(parent,xml) {
|
nickjillings@1571
|
1177 this.type = 'audioHolder';
|
nickjillings@1571
|
1178 this.interfaceNode = function(DOM) {
|
nickjillings@1571
|
1179 var title = DOM.getElementsByTagName('title');
|
nickjillings@1571
|
1180 if (title.length == 0) {this.title = null;}
|
nickjillings@1571
|
1181 else {this.title = title[0].textContent;}
|
nickjillings@1571
|
1182
|
nickjillings@1571
|
1183 var scale = DOM.getElementsByTagName('scale');
|
nickjillings@1571
|
1184 this.scale = [];
|
nickjillings@1571
|
1185 for (var i=0; i<scale.length; i++) {
|
nickjillings@1571
|
1186 var arr = [null, null];
|
nickjillings@1571
|
1187 arr[0] = scale[i].getAttribute('position');
|
nickjillings@1571
|
1188 arr[1] = scale[i].textContent;
|
nickjillings@1571
|
1189 this.scale.push(arr);
|
nickjillings@1571
|
1190 }
|
nickjillings@1571
|
1191 };
|
nickjillings@1571
|
1192
|
nickjillings@1571
|
1193 this.audioElementNode = function(parent,xml) {
|
nickjillings@1571
|
1194 this.url = xml.getAttribute('url');
|
nickjillings@1571
|
1195 this.id = xml.id;
|
nickjillings@1571
|
1196 this.parent = parent;
|
nickjillings@1571
|
1197 };
|
nickjillings@1571
|
1198
|
nickjillings@1571
|
1199 this.commentQuestionNode = function(xml) {
|
nickjillings@1571
|
1200 this.childOption = function(element) {
|
nickjillings@1571
|
1201 this.type = 'option';
|
nickjillings@1571
|
1202 this.name = element.getAttribute('name');
|
nickjillings@1571
|
1203 this.text = element.textContent;
|
nickjillings@1571
|
1204 };
|
nickjillings@1571
|
1205 this.id = xml.id;
|
nickjillings@1571
|
1206 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;}
|
nickjillings@1571
|
1207 else {this.mandatory = false;}
|
nickjillings@1571
|
1208 this.type = xml.getAttribute('type');
|
nickjillings@1571
|
1209 if (this.type == undefined) {this.type = 'text';}
|
nickjillings@1571
|
1210 switch (this.type) {
|
nickjillings@1571
|
1211 case 'text':
|
nickjillings@1571
|
1212 this.question = xml.textContent;
|
nickjillings@1571
|
1213 break;
|
nickjillings@1571
|
1214 case 'radio':
|
nickjillings@1571
|
1215 var child = xml.firstElementChild;
|
nickjillings@1571
|
1216 this.options = [];
|
nickjillings@1571
|
1217 while (child != undefined) {
|
nickjillings@1571
|
1218 if (child.nodeName == 'statement' && this.statement == undefined) {
|
nickjillings@1571
|
1219 this.statement = child.textContent;
|
nickjillings@1571
|
1220 } else if (child.nodeName == 'option') {
|
nickjillings@1571
|
1221 this.options.push(new this.childOption(child));
|
nickjillings@1571
|
1222 }
|
nickjillings@1571
|
1223 child = child.nextElementSibling;
|
nickjillings@1571
|
1224 }
|
nickjillings@1571
|
1225 }
|
nickjillings@1571
|
1226 };
|
nickjillings@1571
|
1227
|
nickjillings@1571
|
1228 this.id = xml.id;
|
nickjillings@1571
|
1229 this.hostURL = xml.getAttribute('hostURL');
|
nickjillings@1571
|
1230 this.sampleRate = xml.getAttribute('sampleRate');
|
nickjillings@1571
|
1231 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;}
|
nickjillings@1571
|
1232 else {this.randomiseOrder = false;}
|
nickjillings@1571
|
1233 this.repeatCount = xml.getAttribute('repeatCount');
|
nickjillings@1571
|
1234 if (xml.getAttribute('loop') == 'true') {this.loop = true;}
|
nickjillings@1571
|
1235 else {this.loop == false;}
|
nickjillings@1571
|
1236 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;}
|
nickjillings@1571
|
1237 else {this.elementComments = false;}
|
nickjillings@1571
|
1238
|
nickjillings@1571
|
1239 this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest'));
|
nickjillings@1571
|
1240 this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest'));
|
nickjillings@1571
|
1241
|
nickjillings@1571
|
1242 this.interfaces = [];
|
nickjillings@1571
|
1243 var interfaceDOM = xml.getElementsByTagName('interface');
|
nickjillings@1571
|
1244 for (var i=0; i<interfaceDOM.length; i++) {
|
nickjillings@1571
|
1245 this.interfaces.push(new this.interfaceNode(interfaceDOM[i]));
|
nickjillings@1571
|
1246 }
|
nickjillings@1571
|
1247
|
nickjillings@1571
|
1248 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix');
|
nickjillings@1571
|
1249 if (this.commentBoxPrefix.length != 0) {
|
nickjillings@1571
|
1250 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent;
|
nickjillings@1571
|
1251 } else {
|
nickjillings@1571
|
1252 this.commentBoxPrefix = "Comment on track";
|
nickjillings@1571
|
1253 }
|
nickjillings@1571
|
1254
|
nickjillings@1571
|
1255 this.audioElements =[];
|
nickjillings@1571
|
1256 var audioElementsDOM = xml.getElementsByTagName('audioElements');
|
nickjillings@1571
|
1257 for (var i=0; i<audioElementsDOM.length; i++) {
|
nickjillings@1571
|
1258 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
|
nickjillings@1571
|
1259 }
|
nickjillings@1571
|
1260
|
nickjillings@1571
|
1261 this.commentQuestions = [];
|
nickjillings@1571
|
1262 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion');
|
nickjillings@1571
|
1263 for (var i=0; i<commentQuestionsDOM.length; i++) {
|
nickjillings@1571
|
1264 this.commentQuestions.push(new this.commentQuestionNode(commentQuestionsDOM[i]));
|
nickjillings@1571
|
1265 }
|
nickjillings@1571
|
1266 };
|
nickjillings@1571
|
1267 }
|
nickjillings@1571
|
1268
|
nickjillings@1571
|
1269 function Interface(specificationObject) {
|
nickjillings@1571
|
1270 // This handles the bindings between the interface and the audioEngineContext;
|
nickjillings@1571
|
1271 this.specification = specificationObject;
|
nickjillings@1571
|
1272 this.insertPoint = document.getElementById("topLevelBody");
|
nickjillings@1571
|
1273
|
nickjillings@1571
|
1274 // Bounded by interface!!
|
nickjillings@1571
|
1275 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
|
nickjillings@1571
|
1276 // For example, APE returns the slider position normalised in a <value> tag.
|
nickjillings@1571
|
1277 this.interfaceObjects = [];
|
nickjillings@1571
|
1278 this.interfaceObject = function(){};
|
nickjillings@1571
|
1279
|
nickjillings@1571
|
1280 this.commentBoxes = [];
|
nickjillings@1571
|
1281 this.elementCommentBox = function(audioObject) {
|
nickjillings@1571
|
1282 var element = audioObject.specification;
|
nickjillings@1571
|
1283 this.audioObject = audioObject;
|
nickjillings@1571
|
1284 this.id = audioObject.id;
|
nickjillings@1571
|
1285 var audioHolderObject = audioObject.specification.parent;
|
nickjillings@1571
|
1286 // Create document objects to hold the comment boxes
|
nickjillings@1571
|
1287 this.trackComment = document.createElement('div');
|
nickjillings@1571
|
1288 this.trackComment.className = 'comment-div';
|
nickjillings@1571
|
1289 this.trackComment.id = 'comment-div-'+audioObject.id;
|
nickjillings@1571
|
1290 // Create a string next to each comment asking for a comment
|
nickjillings@1571
|
1291 this.trackString = document.createElement('span');
|
nickjillings@1571
|
1292 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id;
|
nickjillings@1571
|
1293 // Create the HTML5 comment box 'textarea'
|
nickjillings@1571
|
1294 this.trackCommentBox = document.createElement('textarea');
|
nickjillings@1571
|
1295 this.trackCommentBox.rows = '4';
|
nickjillings@1571
|
1296 this.trackCommentBox.cols = '100';
|
nickjillings@1571
|
1297 this.trackCommentBox.name = 'trackComment'+audioObject.id;
|
nickjillings@1571
|
1298 this.trackCommentBox.className = 'trackComment';
|
nickjillings@1571
|
1299 var br = document.createElement('br');
|
nickjillings@1571
|
1300 // Add to the holder.
|
nickjillings@1571
|
1301 this.trackComment.appendChild(this.trackString);
|
nickjillings@1571
|
1302 this.trackComment.appendChild(br);
|
nickjillings@1571
|
1303 this.trackComment.appendChild(this.trackCommentBox);
|
nickjillings@1571
|
1304
|
nickjillings@1571
|
1305 this.exportXMLDOM = function() {
|
nickjillings@1571
|
1306 var root = document.createElement('comment');
|
nickjillings@1571
|
1307 if (this.audioObject.specification.parent.elementComments) {
|
nickjillings@1571
|
1308 var question = document.createElement('question');
|
nickjillings@1571
|
1309 question.textContent = this.trackString.textContent;
|
nickjillings@1571
|
1310 var response = document.createElement('response');
|
nickjillings@1571
|
1311 response.textContent = this.trackCommentBox.value;
|
nickjillings@1571
|
1312 root.appendChild(question);
|
nickjillings@1571
|
1313 root.appendChild(response);
|
nickjillings@1571
|
1314 }
|
nickjillings@1571
|
1315 return root;
|
nickjillings@1571
|
1316 };
|
nickjillings@1571
|
1317 };
|
nickjillings@1571
|
1318
|
nickjillings@1571
|
1319 this.commentQuestions = [];
|
nickjillings@1571
|
1320
|
nickjillings@1571
|
1321 this.commentBox = function(commentQuestion) {
|
nickjillings@1571
|
1322 this.specification = commentQuestion;
|
nickjillings@1571
|
1323 // Create document objects to hold the comment boxes
|
nickjillings@1571
|
1324 this.holder = document.createElement('div');
|
nickjillings@1571
|
1325 this.holder.className = 'comment-div';
|
nickjillings@1571
|
1326 // Create a string next to each comment asking for a comment
|
nickjillings@1571
|
1327 this.string = document.createElement('span');
|
nickjillings@1571
|
1328 this.string.innerHTML = commentQuestion.question;
|
nickjillings@1571
|
1329 // Create the HTML5 comment box 'textarea'
|
nickjillings@1571
|
1330 this.textArea = document.createElement('textarea');
|
nickjillings@1571
|
1331 this.textArea.rows = '4';
|
nickjillings@1571
|
1332 this.textArea.cols = '100';
|
nickjillings@1571
|
1333 this.textArea.className = 'trackComment';
|
nickjillings@1571
|
1334 var br = document.createElement('br');
|
nickjillings@1571
|
1335 // Add to the holder.
|
nickjillings@1571
|
1336 this.holder.appendChild(this.string);
|
nickjillings@1571
|
1337 this.holder.appendChild(br);
|
nickjillings@1571
|
1338 this.holder.appendChild(this.textArea);
|
nickjillings@1571
|
1339
|
nickjillings@1571
|
1340 this.exportXMLDOM = function() {
|
nickjillings@1571
|
1341 var root = document.createElement('comment');
|
nickjillings@1571
|
1342 root.id = this.specification.id;
|
nickjillings@1571
|
1343 root.setAttribute('type',this.specification.type);
|
nickjillings@1571
|
1344 root.textContent = this.textArea.value;
|
nickjillings@1571
|
1345 return root;
|
nickjillings@1571
|
1346 };
|
nickjillings@1571
|
1347 };
|
nickjillings@1571
|
1348
|
nickjillings@1571
|
1349 this.radioBox = function(commentQuestion) {
|
nickjillings@1571
|
1350 this.specification = commentQuestion;
|
nickjillings@1571
|
1351 // Create document objects to hold the comment boxes
|
nickjillings@1571
|
1352 this.holder = document.createElement('div');
|
nickjillings@1571
|
1353 this.holder.className = 'comment-div';
|
nickjillings@1571
|
1354 // Create a string next to each comment asking for a comment
|
nickjillings@1571
|
1355 this.string = document.createElement('span');
|
nickjillings@1571
|
1356 this.string.innerHTML = commentQuestion.statement;
|
nickjillings@1571
|
1357 var br = document.createElement('br');
|
nickjillings@1571
|
1358 // Add to the holder.
|
nickjillings@1571
|
1359 this.holder.appendChild(this.string);
|
nickjillings@1571
|
1360 this.holder.appendChild(br);
|
nickjillings@1571
|
1361 this.options = [];
|
nickjillings@1571
|
1362 this.inputs = document.createElement('div');
|
nickjillings@1571
|
1363 this.span = document.createElement('div');
|
nickjillings@1571
|
1364 this.inputs.align = 'center';
|
nickjillings@1571
|
1365 this.inputs.style.marginLeft = '12px';
|
nickjillings@1571
|
1366 this.span.style.marginLeft = '12px';
|
nickjillings@1571
|
1367 this.span.align = 'center';
|
nickjillings@1571
|
1368 this.span.style.marginTop = '15px';
|
nickjillings@1571
|
1369
|
nickjillings@1571
|
1370 var optCount = commentQuestion.options.length;
|
nickjillings@1571
|
1371 var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px';
|
nickjillings@1571
|
1372 console.log(spanMargin);
|
nickjillings@1571
|
1373 for (var i=0; i<optCount; i++)
|
nickjillings@1571
|
1374 {
|
nickjillings@1571
|
1375 var div = document.createElement('div');
|
nickjillings@1571
|
1376 div.style.width = '100px';
|
nickjillings@1571
|
1377 div.style.float = 'left';
|
nickjillings@1571
|
1378 div.style.marginRight = spanMargin;
|
nickjillings@1571
|
1379 div.style.marginLeft = spanMargin;
|
nickjillings@1571
|
1380 var input = document.createElement('input');
|
nickjillings@1571
|
1381 input.type = 'radio';
|
nickjillings@1571
|
1382 input.name = commentQuestion.id;
|
nickjillings@1571
|
1383 input.setAttribute('setvalue',commentQuestion.options[i].name);
|
nickjillings@1571
|
1384 input.className = 'comment-radio';
|
nickjillings@1571
|
1385 div.appendChild(input);
|
nickjillings@1571
|
1386 this.inputs.appendChild(div);
|
nickjillings@1571
|
1387
|
nickjillings@1571
|
1388
|
nickjillings@1571
|
1389 div = document.createElement('div');
|
nickjillings@1571
|
1390 div.style.width = '100px';
|
nickjillings@1571
|
1391 div.style.float = 'left';
|
nickjillings@1571
|
1392 div.style.marginRight = spanMargin;
|
nickjillings@1571
|
1393 div.style.marginLeft = spanMargin;
|
nickjillings@1571
|
1394 div.align = 'center';
|
nickjillings@1571
|
1395 var span = document.createElement('span');
|
nickjillings@1571
|
1396 span.textContent = commentQuestion.options[i].text;
|
nickjillings@1571
|
1397 span.className = 'comment-radio-span';
|
nickjillings@1571
|
1398 div.appendChild(span);
|
nickjillings@1571
|
1399 this.span.appendChild(div);
|
nickjillings@1571
|
1400 this.options.push(input);
|
nickjillings@1571
|
1401 }
|
nickjillings@1571
|
1402 this.holder.appendChild(this.span);
|
nickjillings@1571
|
1403 this.holder.appendChild(this.inputs);
|
nickjillings@1571
|
1404
|
nickjillings@1571
|
1405 this.exportXMLDOM = function() {
|
nickjillings@1571
|
1406 var root = document.createElement('comment');
|
nickjillings@1571
|
1407 root.id = this.specification.id;
|
nickjillings@1571
|
1408 root.setAttribute('type',this.specification.type);
|
nickjillings@1571
|
1409 var question = document.createElement('question');
|
nickjillings@1571
|
1410 question.textContent = this.string.textContent;
|
nickjillings@1571
|
1411 var response = document.createElement('response');
|
nickjillings@1571
|
1412 var i=0;
|
nickjillings@1571
|
1413 while(this.options[i].checked == false) {
|
nickjillings@1571
|
1414 i++;
|
nickjillings@1571
|
1415 if (i >= this.options.length) {
|
nickjillings@1571
|
1416 break;
|
nickjillings@1571
|
1417 }
|
nickjillings@1571
|
1418 }
|
nickjillings@1571
|
1419 if (i >= this.options.length) {
|
nickjillings@1571
|
1420 response.textContent = 'null';
|
nickjillings@1571
|
1421 } else {
|
nickjillings@1571
|
1422 response.textContent = this.options[i].getAttribute('setvalue');
|
nickjillings@1571
|
1423 response.setAttribute('number',i);
|
nickjillings@1571
|
1424 }
|
nickjillings@1571
|
1425 root.appendChild(question);
|
nickjillings@1571
|
1426 root.appendChild(response);
|
nickjillings@1571
|
1427 return root;
|
nickjillings@1571
|
1428 };
|
nickjillings@1571
|
1429 };
|
nickjillings@1571
|
1430
|
nickjillings@1571
|
1431
|
nickjillings@1571
|
1432 this.createCommentBox = function(audioObject) {
|
nickjillings@1571
|
1433 var node = new this.elementCommentBox(audioObject);
|
nickjillings@1571
|
1434 this.commentBoxes.push(node);
|
nickjillings@1571
|
1435 audioObject.commentDOM = node;
|
nickjillings@1571
|
1436 return node;
|
nickjillings@1571
|
1437 };
|
nickjillings@1571
|
1438
|
nickjillings@1571
|
1439 this.sortCommentBoxes = function() {
|
nickjillings@1571
|
1440 var holder = [];
|
nickjillings@1571
|
1441 while (this.commentBoxes.length > 0) {
|
nickjillings@1571
|
1442 var node = this.commentBoxes.pop(0);
|
nickjillings@1571
|
1443 holder[node.id] = node;
|
nickjillings@1571
|
1444 }
|
nickjillings@1571
|
1445 this.commentBoxes = holder;
|
nickjillings@1571
|
1446 };
|
nickjillings@1571
|
1447
|
nickjillings@1571
|
1448 this.showCommentBoxes = function(inject, sort) {
|
nickjillings@1571
|
1449 if (sort) {interfaceContext.sortCommentBoxes();}
|
nickjillings@1571
|
1450 for (var i=0; i<interfaceContext.commentBoxes.length; i++) {
|
nickjillings@1571
|
1451 inject.appendChild(this.commentBoxes[i].trackComment);
|
nickjillings@1571
|
1452 }
|
nickjillings@1571
|
1453 };
|
nickjillings@1571
|
1454
|
nickjillings@1571
|
1455 this.createCommentQuestion = function(element) {
|
nickjillings@1571
|
1456 var node;
|
nickjillings@1571
|
1457 if (element.type == 'text') {
|
nickjillings@1571
|
1458 node = new this.commentBox(element);
|
nickjillings@1571
|
1459 } else if (element.type == 'radio') {
|
nickjillings@1571
|
1460 node = new this.radioBox(element);
|
nickjillings@1571
|
1461 }
|
nickjillings@1571
|
1462 this.commentQuestions.push(node);
|
nickjillings@1571
|
1463 return node;
|
nickjillings@1571
|
1464 };
|
nickjillings@1571
|
1465 }
|
nickjillings@1571
|
1466
|