nicholas@2224
|
1 /**
|
nicholas@2224
|
2 * core.js
|
nicholas@2224
|
3 *
|
nicholas@2224
|
4 * Main script to run, calls all other core functions and manages loading/store to backend.
|
nicholas@2224
|
5 * Also contains all global variables.
|
nicholas@2224
|
6 */
|
nicholas@2224
|
7
|
nicholas@2224
|
8 /* create the web audio API context and store in audioContext*/
|
nicholas@2224
|
9 var audioContext; // Hold the browser web audio API
|
nicholas@2224
|
10 var projectXML; // Hold the parsed setup XML
|
nicholas@2224
|
11 var schemaXSD; // Hold the parsed schema XSD
|
nicholas@2224
|
12 var specification;
|
nicholas@2224
|
13 var interfaceContext;
|
nicholas@2224
|
14 var storage;
|
nicholas@2224
|
15 var popup; // Hold the interfacePopup object
|
nicholas@2224
|
16 var testState;
|
nicholas@2224
|
17 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
|
nicholas@2224
|
18 var audioEngineContext; // The custome AudioEngine object
|
nicholas@2329
|
19 var gReturnURL;
|
giuliomoro@2337
|
20 var gSaveFilenamePrefix;
|
nicholas@2224
|
21
|
nicholas@2224
|
22
|
nicholas@2224
|
23 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
|
nicholas@2224
|
24 AudioBufferSourceNode.prototype.owner = undefined;
|
nicholas@2224
|
25 // Add a prototype to the bufferSourceNode to hold when the object was given a play command
|
nicholas@2224
|
26 AudioBufferSourceNode.prototype.playbackStartTime = undefined;
|
nicholas@2224
|
27 // Add a prototype to the bufferNode to hold the desired LINEAR gain
|
nicholas@2224
|
28 AudioBuffer.prototype.playbackGain = undefined;
|
nicholas@2224
|
29 // Add a prototype to the bufferNode to hold the computed LUFS loudness
|
nicholas@2224
|
30 AudioBuffer.prototype.lufs = undefined;
|
nicholas@2224
|
31
|
nicholas@2224
|
32 // Convert relative URLs into absolutes
|
nicholas@2224
|
33 function escapeHTML(s) {
|
nicholas@2224
|
34 return s.split('&').join('&').split('<').join('<').split('"').join('"');
|
nicholas@2224
|
35 }
|
nicholas@2224
|
36 function qualifyURL(url) {
|
nicholas@2224
|
37 var el= document.createElement('div');
|
nicholas@2224
|
38 el.innerHTML= '<a href="'+escapeHTML(url)+'">x</a>';
|
nicholas@2224
|
39 return el.firstChild.href;
|
nicholas@2224
|
40 }
|
nicholas@2224
|
41
|
nicholas@2224
|
42 // Firefox does not have an XMLDocument.prototype.getElementsByName
|
nicholas@2224
|
43 // and there is no searchAll style command, this custom function will
|
nicholas@2224
|
44 // search all children recusrively for the name. Used for XSD where all
|
nicholas@2224
|
45 // element nodes must have a name and therefore can pull the schema node
|
nicholas@2224
|
46 XMLDocument.prototype.getAllElementsByName = function(name)
|
nicholas@2224
|
47 {
|
nicholas@2224
|
48 name = String(name);
|
nicholas@2224
|
49 var selected = this.documentElement.getAllElementsByName(name);
|
nicholas@2224
|
50 return selected;
|
nicholas@2224
|
51 }
|
nicholas@2224
|
52
|
nicholas@2224
|
53 Element.prototype.getAllElementsByName = function(name)
|
nicholas@2224
|
54 {
|
nicholas@2224
|
55 name = String(name);
|
nicholas@2224
|
56 var selected = [];
|
nicholas@2224
|
57 var node = this.firstElementChild;
|
nicholas@2224
|
58 while(node != null)
|
nicholas@2224
|
59 {
|
nicholas@2224
|
60 if (node.getAttribute('name') == name)
|
nicholas@2224
|
61 {
|
nicholas@2224
|
62 selected.push(node);
|
nicholas@2224
|
63 }
|
nicholas@2224
|
64 if (node.childElementCount > 0)
|
nicholas@2224
|
65 {
|
nicholas@2224
|
66 selected = selected.concat(node.getAllElementsByName(name));
|
nicholas@2224
|
67 }
|
nicholas@2224
|
68 node = node.nextElementSibling;
|
nicholas@2224
|
69 }
|
nicholas@2224
|
70 return selected;
|
nicholas@2224
|
71 }
|
nicholas@2224
|
72
|
nicholas@2224
|
73 XMLDocument.prototype.getAllElementsByTagName = function(name)
|
nicholas@2224
|
74 {
|
nicholas@2224
|
75 name = String(name);
|
nicholas@2224
|
76 var selected = this.documentElement.getAllElementsByTagName(name);
|
nicholas@2224
|
77 return selected;
|
nicholas@2224
|
78 }
|
nicholas@2224
|
79
|
nicholas@2224
|
80 Element.prototype.getAllElementsByTagName = function(name)
|
nicholas@2224
|
81 {
|
nicholas@2224
|
82 name = String(name);
|
nicholas@2224
|
83 var selected = [];
|
nicholas@2224
|
84 var node = this.firstElementChild;
|
nicholas@2224
|
85 while(node != null)
|
nicholas@2224
|
86 {
|
nicholas@2224
|
87 if (node.nodeName == name)
|
nicholas@2224
|
88 {
|
nicholas@2224
|
89 selected.push(node);
|
nicholas@2224
|
90 }
|
nicholas@2224
|
91 if (node.childElementCount > 0)
|
nicholas@2224
|
92 {
|
nicholas@2224
|
93 selected = selected.concat(node.getAllElementsByTagName(name));
|
nicholas@2224
|
94 }
|
nicholas@2224
|
95 node = node.nextElementSibling;
|
nicholas@2224
|
96 }
|
nicholas@2224
|
97 return selected;
|
nicholas@2224
|
98 }
|
nicholas@2224
|
99
|
nicholas@2224
|
100 // Firefox does not have an XMLDocument.prototype.getElementsByName
|
nicholas@2224
|
101 if (typeof XMLDocument.prototype.getElementsByName != "function") {
|
nicholas@2224
|
102 XMLDocument.prototype.getElementsByName = function(name)
|
nicholas@2224
|
103 {
|
nicholas@2224
|
104 name = String(name);
|
nicholas@2224
|
105 var node = this.documentElement.firstElementChild;
|
nicholas@2224
|
106 var selected = [];
|
nicholas@2224
|
107 while(node != null)
|
nicholas@2224
|
108 {
|
nicholas@2224
|
109 if (node.getAttribute('name') == name)
|
nicholas@2224
|
110 {
|
nicholas@2224
|
111 selected.push(node);
|
nicholas@2224
|
112 }
|
nicholas@2224
|
113 node = node.nextElementSibling;
|
nicholas@2224
|
114 }
|
nicholas@2224
|
115 return selected;
|
nicholas@2224
|
116 }
|
nicholas@2224
|
117 }
|
nicholas@2224
|
118
|
nicholas@2401
|
119 var check_dependancies = function() {
|
nicholas@2401
|
120 // This will check for the data dependancies
|
nicholas@2401
|
121 if (typeof(jQuery) != "function") {return false;}
|
nicholas@2401
|
122 if (typeof(Specification) != "function") {return false;}
|
nicholas@2401
|
123 if (typeof(calculateLoudness) != "function") {return false;}
|
nicholas@2401
|
124 if (typeof(WAVE) != "function") {return false;}
|
nicholas@2401
|
125 if (typeof(validateXML) != "function") {return false;}
|
nicholas@2401
|
126 return true;
|
nicholas@2401
|
127 }
|
nicholas@2401
|
128
|
nicholas@2384
|
129 var onload = function() {
|
nicholas@2224
|
130 // Function called once the browser has loaded all files.
|
nicholas@2224
|
131 // This should perform any initial commands such as structure / loading documents
|
nicholas@2224
|
132
|
nicholas@2224
|
133 // Create a web audio API context
|
nicholas@2224
|
134 // Fixed for cross-browser support
|
nicholas@2224
|
135 var AudioContext = window.AudioContext || window.webkitAudioContext;
|
nicholas@2224
|
136 audioContext = new AudioContext;
|
nicholas@2224
|
137
|
nicholas@2224
|
138 // Create test state
|
nicholas@2224
|
139 testState = new stateMachine();
|
nicholas@2224
|
140
|
nicholas@2224
|
141 // Create the popup interface object
|
nicholas@2224
|
142 popup = new interfacePopup();
|
nicholas@2224
|
143
|
nicholas@2224
|
144 // Create the specification object
|
nicholas@2224
|
145 specification = new Specification();
|
nicholas@2224
|
146
|
nicholas@2224
|
147 // Create the interface object
|
nicholas@2224
|
148 interfaceContext = new Interface(specification);
|
nicholas@2224
|
149
|
nicholas@2224
|
150 // Create the storage object
|
nicholas@2224
|
151 storage = new Storage();
|
nicholas@2224
|
152 // Define window callbacks for interface
|
nicholas@2224
|
153 window.onresize = function(event){interfaceContext.resizeWindow(event);};
|
nicholas@2319
|
154
|
nicholas@2319
|
155 if (window.location.search.length != 0)
|
nicholas@2319
|
156 {
|
nicholas@2319
|
157 var search = window.location.search.split('?')[1];
|
nicholas@2319
|
158 // Now split the requests into pairs
|
nicholas@2319
|
159 var searchQueries = search.split('&');
|
giuliomoro@2331
|
160
|
nicholas@2319
|
161 for (var i in searchQueries)
|
nicholas@2319
|
162 {
|
giuliomoro@2331
|
163 // Split each key-value pair
|
nicholas@2319
|
164 searchQueries[i] = searchQueries[i].split('=');
|
giuliomoro@2331
|
165 var key = searchQueries[i][0];
|
giuliomoro@2331
|
166 var value = decodeURIComponent(searchQueries[i][1]);
|
giuliomoro@2331
|
167 switch(key) {
|
giuliomoro@2331
|
168 case "url":
|
giuliomoro@2331
|
169 url = value;
|
giuliomoro@2331
|
170 break;
|
giuliomoro@2331
|
171 case "returnURL":
|
giuliomoro@2331
|
172 gReturnURL = value;
|
giuliomoro@2331
|
173 break;
|
giuliomoro@2337
|
174 case "saveFilenamePrefix":
|
giuliomoro@2337
|
175 gSaveFilenamePrefix = value;
|
giuliomoro@2337
|
176 break;
|
nicholas@2319
|
177 }
|
nicholas@2319
|
178 }
|
nicholas@2319
|
179 loadProjectSpec(url);
|
nicholas@2319
|
180 window.onbeforeunload = function() {
|
nicholas@2319
|
181 return "Please only leave this page once you have completed the tests. Are you sure you have completed all testing?";
|
nicholas@2319
|
182 };
|
nicholas@2319
|
183 }
|
nicholas@2360
|
184 interfaceContext.lightbox.resize();
|
nicholas@2224
|
185 };
|
nicholas@2224
|
186
|
nicholas@2224
|
187 function loadProjectSpec(url) {
|
nicholas@2224
|
188 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
|
nicholas@2224
|
189 // If url is null, request client to upload project XML document
|
nicholas@2224
|
190 var xmlhttp = new XMLHttpRequest();
|
nicholas@2224
|
191 xmlhttp.open("GET",'xml/test-schema.xsd',true);
|
nicholas@2224
|
192 xmlhttp.onload = function()
|
nicholas@2224
|
193 {
|
nicholas@2224
|
194 schemaXSD = xmlhttp.response;
|
nicholas@2224
|
195 var parse = new DOMParser();
|
nicholas@2224
|
196 specification.schema = parse.parseFromString(xmlhttp.response,'text/xml');
|
nicholas@2224
|
197 var r = new XMLHttpRequest();
|
nicholas@2224
|
198 r.open('GET',url,true);
|
nicholas@2224
|
199 r.onload = function() {
|
nicholas@2224
|
200 loadProjectSpecCallback(r.response);
|
nicholas@2224
|
201 };
|
nicholas@2224
|
202 r.onerror = function() {
|
nicholas@2224
|
203 document.getElementsByTagName('body')[0].innerHTML = null;
|
nicholas@2224
|
204 var msg = document.createElement("h3");
|
nicholas@2224
|
205 msg.textContent = "FATAL ERROR";
|
nicholas@2224
|
206 var span = document.createElement("p");
|
nicholas@2224
|
207 span.textContent = "There was an error when loading your XML file. Please check your path in the URL. After the path to this page, there should be '?url=path/to/your/file.xml'. Check the spelling of your filename as well. If you are still having issues, check the log of the python server or your webserver distribution for 404 codes for your file.";
|
nicholas@2224
|
208 document.getElementsByTagName('body')[0].appendChild(msg);
|
nicholas@2224
|
209 document.getElementsByTagName('body')[0].appendChild(span);
|
nicholas@2224
|
210 }
|
nicholas@2224
|
211 r.send();
|
nicholas@2224
|
212 };
|
nicholas@2224
|
213 xmlhttp.send();
|
nicholas@2224
|
214 };
|
nicholas@2224
|
215
|
nicholas@2224
|
216 function loadProjectSpecCallback(response) {
|
nicholas@2224
|
217 // Function called after asynchronous download of XML project specification
|
nicholas@2224
|
218 //var decode = $.parseXML(response);
|
nicholas@2224
|
219 //projectXML = $(decode);
|
nicholas@2224
|
220
|
nicholas@2224
|
221 // Check if XML is new or a resumption
|
nicholas@2224
|
222 var parse = new DOMParser();
|
nicholas@2224
|
223 var responseDocument = parse.parseFromString(response,'text/xml');
|
nicholas@2224
|
224 var errorNode = responseDocument.getElementsByTagName('parsererror');
|
nicholas@2224
|
225 if (errorNode.length >= 1)
|
nicholas@2224
|
226 {
|
nicholas@2224
|
227 var msg = document.createElement("h3");
|
nicholas@2224
|
228 msg.textContent = "FATAL ERROR";
|
nicholas@2224
|
229 var span = document.createElement("span");
|
nicholas@2224
|
230 span.textContent = "The XML parser returned the following errors when decoding your XML file";
|
nicholas@2224
|
231 document.getElementsByTagName('body')[0].innerHTML = null;
|
nicholas@2224
|
232 document.getElementsByTagName('body')[0].appendChild(msg);
|
nicholas@2224
|
233 document.getElementsByTagName('body')[0].appendChild(span);
|
nicholas@2224
|
234 document.getElementsByTagName('body')[0].appendChild(errorNode[0]);
|
nicholas@2224
|
235 return;
|
nicholas@2224
|
236 }
|
nicholas@2247
|
237 if (responseDocument == undefined || responseDocument.firstChild == undefined) {
|
nicholas@2224
|
238 var msg = document.createElement("h3");
|
nicholas@2224
|
239 msg.textContent = "FATAL ERROR";
|
nicholas@2224
|
240 var span = document.createElement("span");
|
nicholas@2224
|
241 span.textContent = "The project XML was not decoded properly, try refreshing your browser and clearing caches. If the problem persists, contact the test creator.";
|
nicholas@2224
|
242 document.getElementsByTagName('body')[0].innerHTML = null;
|
nicholas@2224
|
243 document.getElementsByTagName('body')[0].appendChild(msg);
|
nicholas@2224
|
244 document.getElementsByTagName('body')[0].appendChild(span);
|
nicholas@2224
|
245 return;
|
nicholas@2224
|
246 }
|
nicholas@2247
|
247 if (responseDocument.firstChild.nodeName == "waet") {
|
nicholas@2224
|
248 // document is a specification
|
nicholas@2224
|
249
|
nicholas@2224
|
250 // Perform XML schema validation
|
nicholas@2224
|
251 var Module = {
|
nicholas@2224
|
252 xml: response,
|
nicholas@2224
|
253 schema: schemaXSD,
|
nicholas@2224
|
254 arguments:["--noout", "--schema", 'test-schema.xsd','document.xml']
|
nicholas@2224
|
255 };
|
nicholas@2224
|
256 projectXML = responseDocument;
|
nicholas@2224
|
257 var xmllint = validateXML(Module);
|
nicholas@2224
|
258 console.log(xmllint);
|
nicholas@2224
|
259 if(xmllint != 'document.xml validates\n')
|
nicholas@2224
|
260 {
|
nicholas@2224
|
261 document.getElementsByTagName('body')[0].innerHTML = null;
|
nicholas@2224
|
262 var msg = document.createElement("h3");
|
nicholas@2224
|
263 msg.textContent = "FATAL ERROR";
|
nicholas@2224
|
264 var span = document.createElement("h4");
|
nicholas@2224
|
265 span.textContent = "The XML validator returned the following errors when decoding your XML file";
|
nicholas@2224
|
266 document.getElementsByTagName('body')[0].appendChild(msg);
|
nicholas@2224
|
267 document.getElementsByTagName('body')[0].appendChild(span);
|
nicholas@2224
|
268 xmllint = xmllint.split('\n');
|
nicholas@2224
|
269 for (var i in xmllint)
|
nicholas@2224
|
270 {
|
nicholas@2224
|
271 document.getElementsByTagName('body')[0].appendChild(document.createElement('br'));
|
nicholas@2224
|
272 var span = document.createElement("span");
|
nicholas@2224
|
273 span.textContent = xmllint[i];
|
nicholas@2224
|
274 document.getElementsByTagName('body')[0].appendChild(span);
|
nicholas@2224
|
275 }
|
nicholas@2224
|
276 return;
|
nicholas@2224
|
277 }
|
nicholas@2224
|
278 // Build the specification
|
nicholas@2224
|
279 specification.decode(projectXML);
|
nicholas@2224
|
280 // Generate the session-key
|
nicholas@2224
|
281 storage.initialise();
|
nicholas@2224
|
282
|
nicholas@2247
|
283 } else if (responseDocument.firstChild.nodeName == "waetresult") {
|
nicholas@2224
|
284 // document is a result
|
nicholas@2224
|
285 projectXML = document.implementation.createDocument(null,"waet");
|
nicholas@2294
|
286 projectXML.firstChild.appendChild(responseDocument.getElementsByTagName('waet')[0].getElementsByTagName("setup")[0].cloneNode(true));
|
nicholas@2294
|
287 var child = responseDocument.firstChild.firstChild;
|
nicholas@2224
|
288 while (child != null) {
|
nicholas@2224
|
289 if (child.nodeName == "survey") {
|
nicholas@2224
|
290 // One of the global survey elements
|
nicholas@2224
|
291 if (child.getAttribute("state") == "complete") {
|
nicholas@2224
|
292 // We need to remove this survey from <setup>
|
nicholas@2224
|
293 var location = child.getAttribute("location");
|
nicholas@2224
|
294 var globalSurveys = projectXML.getElementsByTagName("setup")[0].getElementsByTagName("survey")[0];
|
nicholas@2224
|
295 while(globalSurveys != null) {
|
nicholas@2224
|
296 if (location == "pre" || location == "before") {
|
nicholas@2224
|
297 if (globalSurveys.getAttribute("location") == "pre" || globalSurveys.getAttribute("location") == "before") {
|
nicholas@2224
|
298 projectXML.getElementsByTagName("setup")[0].removeChild(globalSurveys);
|
nicholas@2224
|
299 break;
|
nicholas@2224
|
300 }
|
nicholas@2224
|
301 } else {
|
nicholas@2224
|
302 if (globalSurveys.getAttribute("location") == "post" || globalSurveys.getAttribute("location") == "after") {
|
nicholas@2224
|
303 projectXML.getElementsByTagName("setup")[0].removeChild(globalSurveys);
|
nicholas@2224
|
304 break;
|
nicholas@2224
|
305 }
|
nicholas@2224
|
306 }
|
nicholas@2224
|
307 globalSurveys = globalSurveys.nextElementSibling;
|
nicholas@2224
|
308 }
|
nicholas@2224
|
309 } else {
|
nicholas@2224
|
310 // We need to complete this, so it must be regenerated by store
|
nicholas@2224
|
311 var copy = child;
|
nicholas@2224
|
312 child = child.previousElementSibling;
|
nicholas@2294
|
313 responseDocument.firstChild.removeChild(copy);
|
nicholas@2224
|
314 }
|
nicholas@2224
|
315 } else if (child.nodeName == "page") {
|
nicholas@2224
|
316 if (child.getAttribute("state") == "empty") {
|
nicholas@2224
|
317 // We need to complete this page
|
nicholas@2294
|
318 projectXML.firstChild.appendChild(responseDocument.getElementById(child.getAttribute("ref")).cloneNode(true));
|
nicholas@2224
|
319 var copy = child;
|
nicholas@2224
|
320 child = child.previousElementSibling;
|
nicholas@2294
|
321 responseDocument.firstChild.removeChild(copy);
|
nicholas@2224
|
322 }
|
nicholas@2224
|
323 }
|
nicholas@2224
|
324 child = child.nextElementSibling;
|
nicholas@2224
|
325 }
|
nicholas@2224
|
326 // Build the specification
|
nicholas@2224
|
327 specification.decode(projectXML);
|
nicholas@2224
|
328 // Use the original
|
nicholas@2224
|
329 storage.initialise(responseDocument);
|
nicholas@2224
|
330 }
|
nicholas@2224
|
331 /// CHECK FOR SAMPLE RATE COMPATIBILITY
|
nicholas@2224
|
332 if (specification.sampleRate != undefined) {
|
nicholas@2224
|
333 if (Number(specification.sampleRate) != audioContext.sampleRate) {
|
nicholas@2224
|
334 var errStr = 'Sample rates do not match! Requested '+Number(specification.sampleRate)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
|
nicholas@2361
|
335 interfaceContext.lightbox.post("Error",errStr);
|
nicholas@2224
|
336 return;
|
nicholas@2224
|
337 }
|
nicholas@2224
|
338 }
|
nicholas@2224
|
339
|
nicholas@2224
|
340 // Detect the interface to use and load the relevant javascripts.
|
nicholas@2224
|
341 var interfaceJS = document.createElement('script');
|
nicholas@2224
|
342 interfaceJS.setAttribute("type","text/javascript");
|
nicholas@2224
|
343 switch(specification.interface)
|
nicholas@2224
|
344 {
|
nicholas@2224
|
345 case "APE":
|
nicholas@2224
|
346 interfaceJS.setAttribute("src","interfaces/ape.js");
|
nicholas@2224
|
347
|
nicholas@2224
|
348 // APE comes with a css file
|
nicholas@2224
|
349 var css = document.createElement('link');
|
nicholas@2224
|
350 css.rel = 'stylesheet';
|
nicholas@2224
|
351 css.type = 'text/css';
|
nicholas@2224
|
352 css.href = 'interfaces/ape.css';
|
nicholas@2224
|
353
|
nicholas@2224
|
354 document.getElementsByTagName("head")[0].appendChild(css);
|
nicholas@2224
|
355 break;
|
nicholas@2224
|
356
|
nicholas@2224
|
357 case "MUSHRA":
|
nicholas@2224
|
358 interfaceJS.setAttribute("src","interfaces/mushra.js");
|
nicholas@2224
|
359
|
nicholas@2224
|
360 // MUSHRA comes with a css file
|
nicholas@2224
|
361 var css = document.createElement('link');
|
nicholas@2224
|
362 css.rel = 'stylesheet';
|
nicholas@2224
|
363 css.type = 'text/css';
|
nicholas@2224
|
364 css.href = 'interfaces/mushra.css';
|
nicholas@2224
|
365
|
nicholas@2224
|
366 document.getElementsByTagName("head")[0].appendChild(css);
|
nicholas@2224
|
367 break;
|
nicholas@2224
|
368
|
nicholas@2224
|
369 case "AB":
|
nicholas@2224
|
370 interfaceJS.setAttribute("src","interfaces/AB.js");
|
nicholas@2224
|
371
|
nicholas@2224
|
372 // AB comes with a css file
|
nicholas@2224
|
373 var css = document.createElement('link');
|
nicholas@2224
|
374 css.rel = 'stylesheet';
|
nicholas@2224
|
375 css.type = 'text/css';
|
nicholas@2224
|
376 css.href = 'interfaces/AB.css';
|
nicholas@2224
|
377
|
nicholas@2224
|
378 document.getElementsByTagName("head")[0].appendChild(css);
|
nicholas@2224
|
379 break;
|
nicholas@2224
|
380
|
nicholas@2224
|
381 case "ABX":
|
nicholas@2224
|
382 interfaceJS.setAttribute("src","interfaces/ABX.js");
|
nicholas@2224
|
383
|
nicholas@2224
|
384 // AB comes with a css file
|
nicholas@2224
|
385 var css = document.createElement('link');
|
nicholas@2224
|
386 css.rel = 'stylesheet';
|
nicholas@2224
|
387 css.type = 'text/css';
|
nicholas@2224
|
388 css.href = 'interfaces/ABX.css';
|
nicholas@2224
|
389
|
nicholas@2224
|
390 document.getElementsByTagName("head")[0].appendChild(css);
|
nicholas@2224
|
391 break;
|
nicholas@2224
|
392
|
nicholas@2224
|
393 case "Bipolar":
|
nicholas@2224
|
394 case "ACR":
|
nicholas@2224
|
395 case "DCR":
|
nicholas@2224
|
396 case "CCR":
|
nicholas@2224
|
397 case "ABC":
|
nicholas@2224
|
398 // Above enumerate to horizontal sliders
|
nicholas@2224
|
399 interfaceJS.setAttribute("src","interfaces/horizontal-sliders.js");
|
nicholas@2224
|
400
|
nicholas@2224
|
401 // horizontal-sliders comes with a css file
|
nicholas@2224
|
402 var css = document.createElement('link');
|
nicholas@2224
|
403 css.rel = 'stylesheet';
|
nicholas@2224
|
404 css.type = 'text/css';
|
nicholas@2224
|
405 css.href = 'interfaces/horizontal-sliders.css';
|
nicholas@2224
|
406
|
nicholas@2224
|
407 document.getElementsByTagName("head")[0].appendChild(css);
|
nicholas@2224
|
408 break;
|
nicholas@2224
|
409 case "discrete":
|
nicholas@2224
|
410 case "likert":
|
nicholas@2224
|
411 // Above enumerate to horizontal discrete radios
|
nicholas@2224
|
412 interfaceJS.setAttribute("src","interfaces/discrete.js");
|
nicholas@2224
|
413
|
nicholas@2224
|
414 // horizontal-sliders comes with a css file
|
nicholas@2224
|
415 var css = document.createElement('link');
|
nicholas@2224
|
416 css.rel = 'stylesheet';
|
nicholas@2224
|
417 css.type = 'text/css';
|
nicholas@2224
|
418 css.href = 'interfaces/discrete.css';
|
nicholas@2224
|
419
|
nicholas@2224
|
420 document.getElementsByTagName("head")[0].appendChild(css);
|
nicholas@2224
|
421 break;
|
nicholas@2479
|
422 case "timeline":
|
nicholas@2479
|
423 interfaceJS.setAttribute("src","interfaces/timeline.js");
|
nicholas@2479
|
424 var css = document.createElement('link');
|
nicholas@2479
|
425 css.rel = 'stylesheet';
|
nicholas@2479
|
426 css.type = 'text/css';
|
nicholas@2479
|
427 css.href = 'interfaces/timeline.css';
|
nicholas@2479
|
428
|
nicholas@2479
|
429 document.getElementsByTagName("head")[0].appendChild(css);
|
nicholas@2479
|
430 break;
|
nicholas@2224
|
431 }
|
nicholas@2224
|
432 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
|
nicholas@2329
|
433
|
nicholas@2329
|
434 if (gReturnURL != undefined) {
|
nicholas@2329
|
435 console.log("returnURL Overide from "+specification.returnURL+" to "+gReturnURL);
|
nicholas@2329
|
436 specification.returnURL = gReturnURL;
|
nicholas@2329
|
437 }
|
giuliomoro@2337
|
438 if (gSaveFilenamePrefix != undefined){
|
giuliomoro@2337
|
439 specification.saveFilenamePrefix = gSaveFilenamePrefix;
|
giuliomoro@2337
|
440 }
|
nicholas@2224
|
441
|
nicholas@2224
|
442 // Create the audio engine object
|
nicholas@2224
|
443 audioEngineContext = new AudioEngine(specification);
|
nicholas@2224
|
444 }
|
nicholas@2224
|
445
|
nicholas@2224
|
446 function createProjectSave(destURL) {
|
nicholas@2224
|
447 // Clear the window.onbeforeunload
|
nicholas@2224
|
448 window.onbeforeunload = null;
|
nicholas@2224
|
449 // Save the data from interface into XML and send to destURL
|
nicholas@2224
|
450 // If destURL is null then download XML in client
|
nicholas@2224
|
451 // Now time to render file locally
|
nicholas@2224
|
452 var xmlDoc = interfaceXMLSave();
|
nicholas@2224
|
453 var parent = document.createElement("div");
|
nicholas@2224
|
454 parent.appendChild(xmlDoc);
|
nicholas@2224
|
455 var file = [parent.innerHTML];
|
nicholas@2224
|
456 if (destURL == "local") {
|
nicholas@2224
|
457 var bb = new Blob(file,{type : 'application/xml'});
|
nicholas@2224
|
458 var dnlk = window.URL.createObjectURL(bb);
|
nicholas@2224
|
459 var a = document.createElement("a");
|
nicholas@2224
|
460 a.hidden = '';
|
nicholas@2224
|
461 a.href = dnlk;
|
nicholas@2224
|
462 a.download = "save.xml";
|
nicholas@2224
|
463 a.textContent = "Save File";
|
nicholas@2224
|
464
|
nicholas@2224
|
465 popup.showPopup();
|
giuliomoro@2299
|
466 popup.popupContent.innerHTML = "<span>Please save the file below to give to your test supervisor</span><br>";
|
nicholas@2224
|
467 popup.popupContent.appendChild(a);
|
nicholas@2224
|
468 } else {
|
giuliomoro@2337
|
469 var saveUrlSuffix = "";
|
giuliomoro@2337
|
470 var saveFilenamePrefix = specification.saveFilenamePrefix;
|
giuliomoro@2337
|
471 if(typeof(saveFilenamePrefix) === "string" && saveFilenamePrefix.length > 0){
|
giuliomoro@2337
|
472 saveUrlSuffix = "&saveFilenamePrefix="+saveFilenamePrefix;
|
giuliomoro@2337
|
473 }
|
giuliomoro@2337
|
474 var projectReturn = "";
|
giuliomoro@2337
|
475 if (typeof specification.projectReturn == "string") {
|
giuliomoro@2337
|
476 if (specification.projectReturn.substr(0,4) == "http") {
|
giuliomoro@2337
|
477 projectReturn = specification.projectReturn;
|
giuliomoro@2337
|
478 }
|
giuliomoro@2337
|
479 }
|
giuliomoro@2337
|
480 var saveURL = projectReturn+"php/save.php?key="+storage.SessionKey.key+saveUrlSuffix;
|
nicholas@2224
|
481 var xmlhttp = new XMLHttpRequest;
|
giuliomoro@2337
|
482 xmlhttp.open("POST", saveURL, true);
|
nicholas@2224
|
483 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
|
nicholas@2224
|
484 xmlhttp.onerror = function(){
|
nicholas@2224
|
485 console.log('Error saving file to server! Presenting download locally');
|
nicholas@2224
|
486 createProjectSave("local");
|
nicholas@2224
|
487 };
|
nicholas@2224
|
488 xmlhttp.onload = function() {
|
nicholas@2224
|
489 console.log(xmlhttp);
|
nicholas@2224
|
490 if (this.status >= 300) {
|
nicholas@2224
|
491 console.log("WARNING - Could not update at this time");
|
nicholas@2224
|
492 createProjectSave("local");
|
nicholas@2224
|
493 } else {
|
nicholas@2224
|
494 var parser = new DOMParser();
|
nicholas@2224
|
495 var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml");
|
nicholas@2224
|
496 var response = xmlDoc.getElementsByTagName('response')[0];
|
nicholas@2224
|
497 if (response.getAttribute("state") == "OK") {
|
nicholas@2303
|
498 window.onbeforeunload = undefined;
|
nicholas@2224
|
499 var file = response.getElementsByTagName("file")[0];
|
nicholas@2224
|
500 console.log("Save: OK, written "+file.getAttribute("bytes")+"B");
|
giuliomoro@2335
|
501 if (typeof specification.returnURL == "string" && specification.returnURL.length > 0) {
|
nicholas@2329
|
502 window.location = specification.returnURL;
|
nicholas@2303
|
503 } else {
|
nicholas@2303
|
504 popup.popupContent.textContent = specification.exitText;
|
nicholas@2303
|
505 }
|
nicholas@2224
|
506 } else {
|
nicholas@2224
|
507 var message = response.getElementsByTagName("message");
|
nicholas@2224
|
508 console.log("Save: Error! "+message.textContent);
|
nicholas@2224
|
509 createProjectSave("local");
|
nicholas@2224
|
510 }
|
nicholas@2224
|
511 }
|
nicholas@2224
|
512 };
|
nicholas@2224
|
513 xmlhttp.send(file);
|
nicholas@2224
|
514 popup.showPopup();
|
nicholas@2224
|
515 popup.popupContent.innerHTML = null;
|
nicholas@2224
|
516 popup.popupContent.textContent = "Submitting. Please Wait";
|
giuliomoro@2325
|
517 if(typeof(popup.hideNextButton) === "function"){
|
giuliomoro@2325
|
518 popup.hideNextButton();
|
giuliomoro@2325
|
519 }
|
giuliomoro@2325
|
520 if(typeof(popup.hidePreviousButton) === "function"){
|
giuliomoro@2325
|
521 popup.hidePreviousButton();
|
giuliomoro@2325
|
522 }
|
nicholas@2224
|
523 }
|
nicholas@2224
|
524 }
|
nicholas@2224
|
525
|
nicholas@2224
|
526 function errorSessionDump(msg){
|
nicholas@2224
|
527 // Create the partial interface XML save
|
nicholas@2224
|
528 // Include error node with message on why the dump occured
|
nicholas@2224
|
529 popup.showPopup();
|
nicholas@2224
|
530 popup.popupContent.innerHTML = null;
|
nicholas@2224
|
531 var err = document.createElement('error');
|
nicholas@2224
|
532 var parent = document.createElement("div");
|
nicholas@2224
|
533 if (typeof msg === "object")
|
nicholas@2224
|
534 {
|
nicholas@2224
|
535 err.appendChild(msg);
|
nicholas@2224
|
536 popup.popupContent.appendChild(msg);
|
nicholas@2224
|
537
|
nicholas@2224
|
538 } else {
|
nicholas@2224
|
539 err.textContent = msg;
|
nicholas@2224
|
540 popup.popupContent.innerHTML = "ERROR : "+msg;
|
nicholas@2224
|
541 }
|
nicholas@2224
|
542 var xmlDoc = interfaceXMLSave();
|
nicholas@2224
|
543 xmlDoc.appendChild(err);
|
nicholas@2224
|
544 parent.appendChild(xmlDoc);
|
nicholas@2224
|
545 var file = [parent.innerHTML];
|
nicholas@2224
|
546 var bb = new Blob(file,{type : 'application/xml'});
|
nicholas@2224
|
547 var dnlk = window.URL.createObjectURL(bb);
|
nicholas@2224
|
548 var a = document.createElement("a");
|
nicholas@2224
|
549 a.hidden = '';
|
nicholas@2224
|
550 a.href = dnlk;
|
nicholas@2224
|
551 a.download = "save.xml";
|
nicholas@2224
|
552 a.textContent = "Save File";
|
nicholas@2224
|
553
|
nicholas@2224
|
554
|
nicholas@2224
|
555
|
nicholas@2224
|
556 popup.popupContent.appendChild(a);
|
nicholas@2224
|
557 }
|
nicholas@2224
|
558
|
nicholas@2224
|
559 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
|
nicholas@2224
|
560 function interfaceXMLSave(){
|
nicholas@2224
|
561 // Create the XML string to be exported with results
|
nicholas@2224
|
562 return storage.finish();
|
nicholas@2224
|
563 }
|
nicholas@2224
|
564
|
nicholas@2224
|
565 function linearToDecibel(gain)
|
nicholas@2224
|
566 {
|
nicholas@2224
|
567 return 20.0*Math.log10(gain);
|
nicholas@2224
|
568 }
|
nicholas@2224
|
569
|
nicholas@2224
|
570 function decibelToLinear(gain)
|
nicholas@2224
|
571 {
|
nicholas@2224
|
572 return Math.pow(10,gain/20.0);
|
nicholas@2224
|
573 }
|
nicholas@2224
|
574
|
nicholas@2224
|
575 function secondsToSamples(time,fs) {
|
nicholas@2224
|
576 return Math.round(time*fs);
|
nicholas@2224
|
577 }
|
nicholas@2224
|
578
|
nicholas@2224
|
579 function samplesToSeconds(samples,fs) {
|
nicholas@2224
|
580 return samples / fs;
|
nicholas@2224
|
581 }
|
nicholas@2224
|
582
|
nicholas@2224
|
583 function randomString(length) {
|
nicholas@2376
|
584 var str = ""
|
nicholas@2376
|
585 for (var i=0; i<length; i+=2) {
|
nicholas@2376
|
586 var num = Math.floor(Math.random()*1295);
|
nicholas@2376
|
587 str += num.toString(36);
|
nicholas@2376
|
588 }
|
nicholas@2376
|
589 return str;
|
nicholas@2376
|
590 //return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
|
nicholas@2224
|
591 }
|
nicholas@2224
|
592
|
nicholas@2224
|
593 function randomiseOrder(input)
|
nicholas@2224
|
594 {
|
nicholas@2224
|
595 // This takes an array of information and randomises the order
|
nicholas@2224
|
596 var N = input.length;
|
nicholas@2224
|
597
|
nicholas@2224
|
598 var inputSequence = []; // For safety purposes: keep track of randomisation
|
nicholas@2224
|
599 for (var counter = 0; counter < N; ++counter)
|
nicholas@2224
|
600 inputSequence.push(counter) // Fill array
|
nicholas@2224
|
601 var inputSequenceClone = inputSequence.slice(0);
|
nicholas@2224
|
602
|
nicholas@2224
|
603 var holdArr = [];
|
nicholas@2224
|
604 var outputSequence = [];
|
nicholas@2224
|
605 for (var n=0; n<N; n++)
|
nicholas@2224
|
606 {
|
nicholas@2224
|
607 // First pick a random number
|
nicholas@2224
|
608 var r = Math.random();
|
nicholas@2224
|
609 // Multiply and floor by the number of elements left
|
nicholas@2224
|
610 r = Math.floor(r*input.length);
|
nicholas@2224
|
611 // Pick out that element and delete from the array
|
nicholas@2224
|
612 holdArr.push(input.splice(r,1)[0]);
|
nicholas@2224
|
613 // Do the same with sequence
|
nicholas@2224
|
614 outputSequence.push(inputSequence.splice(r,1)[0]);
|
nicholas@2224
|
615 }
|
nicholas@2224
|
616 console.log(inputSequenceClone.toString()); // print original array to console
|
nicholas@2224
|
617 console.log(outputSequence.toString()); // print randomised array to console
|
nicholas@2224
|
618 return holdArr;
|
nicholas@2224
|
619 }
|
nicholas@2224
|
620
|
nicholas@2224
|
621 function randomSubArray(array,num) {
|
nicholas@2224
|
622 if (num > array.length) {
|
nicholas@2224
|
623 num = array.length;
|
nicholas@2224
|
624 }
|
nicholas@2224
|
625 var ret = [];
|
nicholas@2224
|
626 while (num > 0) {
|
nicholas@2224
|
627 var index = Math.floor(Math.random() * array.length);
|
nicholas@2224
|
628 ret.push( array.splice(index,1)[0] );
|
nicholas@2224
|
629 num--;
|
nicholas@2224
|
630 }
|
nicholas@2224
|
631 return ret;
|
nicholas@2224
|
632 }
|
nicholas@2224
|
633
|
nicholas@2224
|
634 function interfacePopup() {
|
nicholas@2224
|
635 // Creates an object to manage the popup
|
nicholas@2224
|
636 this.popup = null;
|
nicholas@2224
|
637 this.popupContent = null;
|
nicholas@2224
|
638 this.popupTitle = null;
|
nicholas@2224
|
639 this.popupResponse = null;
|
nicholas@2224
|
640 this.buttonProceed = null;
|
nicholas@2224
|
641 this.buttonPrevious = null;
|
nicholas@2224
|
642 this.popupOptions = null;
|
nicholas@2224
|
643 this.currentIndex = null;
|
nicholas@2224
|
644 this.node = null;
|
nicholas@2224
|
645 this.store = null;
|
nicholas@2224
|
646 $(window).keypress(function(e){
|
nicholas@2224
|
647 if (e.keyCode == 13 && popup.popup.style.visibility == 'visible')
|
nicholas@2224
|
648 {
|
nicholas@2224
|
649 console.log(e);
|
nicholas@2224
|
650 popup.buttonProceed.onclick();
|
nicholas@2224
|
651 e.preventDefault();
|
nicholas@2224
|
652 }
|
nicholas@2224
|
653 });
|
nicholas@2224
|
654
|
nicholas@2224
|
655 this.createPopup = function(){
|
nicholas@2224
|
656 // Create popup window interface
|
nicholas@2224
|
657 var insertPoint = document.getElementById("topLevelBody");
|
nicholas@2224
|
658
|
nicholas@2224
|
659 this.popup = document.getElementById('popupHolder');
|
nicholas@2224
|
660 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
|
nicholas@2224
|
661 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
|
nicholas@2224
|
662
|
nicholas@2224
|
663 this.popupContent = document.getElementById('popupContent');
|
nicholas@2224
|
664
|
nicholas@2224
|
665 this.popupTitle = document.getElementById('popupTitle');
|
nicholas@2224
|
666
|
nicholas@2224
|
667 this.popupResponse = document.getElementById('popupResponse');
|
nicholas@2224
|
668
|
nicholas@2224
|
669 this.buttonProceed = document.getElementById('popup-proceed');
|
nicholas@2224
|
670 this.buttonProceed.onclick = function(){popup.proceedClicked();};
|
nicholas@2224
|
671
|
nicholas@2224
|
672 this.buttonPrevious = document.getElementById('popup-previous');
|
nicholas@2224
|
673 this.buttonPrevious.onclick = function(){popup.previousClick();};
|
nicholas@2224
|
674
|
nicholas@2224
|
675 this.hidePopup();
|
nicholas@2224
|
676 this.popup.style.visibility = 'hidden';
|
nicholas@2224
|
677 };
|
nicholas@2224
|
678
|
nicholas@2224
|
679 this.showPopup = function(){
|
nicholas@2224
|
680 if (this.popup == null) {
|
nicholas@2224
|
681 this.createPopup();
|
nicholas@2224
|
682 }
|
nicholas@2224
|
683 this.popup.style.visibility = 'visible';
|
nicholas@2224
|
684 var blank = document.getElementsByClassName('testHalt')[0];
|
nicholas@2224
|
685 blank.style.visibility = 'visible';
|
nicholas@2224
|
686 this.popupResponse.style.left="0%";
|
nicholas@2224
|
687 };
|
nicholas@2224
|
688
|
nicholas@2224
|
689 this.hidePopup = function(){
|
nicholas@2224
|
690 if (this.popup) {
|
nicholas@2224
|
691 this.popup.style.visibility = 'hidden';
|
nicholas@2224
|
692 var blank = document.getElementsByClassName('testHalt')[0];
|
nicholas@2224
|
693 blank.style.visibility = 'hidden';
|
nicholas@2224
|
694 this.buttonPrevious.style.visibility = 'inherit';
|
nicholas@2224
|
695 }
|
nicholas@2224
|
696 };
|
nicholas@2224
|
697
|
nicholas@2224
|
698 this.postNode = function() {
|
nicholas@2224
|
699 // This will take the node from the popupOptions and display it
|
nicholas@2224
|
700 var node = this.popupOptions[this.currentIndex];
|
nicholas@2379
|
701 this.popupResponse.innerHTML = "";
|
nicholas@2224
|
702 this.popupTitle.textContent = node.specification.statement;
|
nicholas@2224
|
703 if (node.specification.type == 'question') {
|
nicholas@2224
|
704 var textArea = document.createElement('textarea');
|
nicholas@2224
|
705 switch (node.specification.boxsize) {
|
nicholas@2224
|
706 case 'small':
|
nicholas@2224
|
707 textArea.cols = "20";
|
nicholas@2224
|
708 textArea.rows = "1";
|
nicholas@2224
|
709 break;
|
nicholas@2224
|
710 case 'normal':
|
nicholas@2224
|
711 textArea.cols = "30";
|
nicholas@2224
|
712 textArea.rows = "2";
|
nicholas@2224
|
713 break;
|
nicholas@2224
|
714 case 'large':
|
nicholas@2224
|
715 textArea.cols = "40";
|
nicholas@2224
|
716 textArea.rows = "5";
|
nicholas@2224
|
717 break;
|
nicholas@2224
|
718 case 'huge':
|
nicholas@2224
|
719 textArea.cols = "50";
|
nicholas@2224
|
720 textArea.rows = "10";
|
nicholas@2224
|
721 break;
|
nicholas@2224
|
722 }
|
nicholas@2224
|
723 if (node.response == undefined) {
|
nicholas@2224
|
724 node.response = "";
|
nicholas@2224
|
725 } else {
|
nicholas@2224
|
726 textArea.value = node.response;
|
nicholas@2224
|
727 }
|
nicholas@2224
|
728 this.popupResponse.appendChild(textArea);
|
nicholas@2224
|
729 textArea.focus();
|
nicholas@2224
|
730 this.popupResponse.style.textAlign="center";
|
nicholas@2224
|
731 this.popupResponse.style.left="0%";
|
nicholas@2224
|
732 } else if (node.specification.type == 'checkbox') {
|
nicholas@2224
|
733 if (node.response == undefined) {
|
nicholas@2224
|
734 node.response = Array(node.specification.options.length);
|
nicholas@2224
|
735 }
|
nicholas@2224
|
736 var index = 0;
|
nicholas@2346
|
737 var table = document.createElement("table");
|
nicholas@2346
|
738 table.className = "popup-option-list";
|
nicholas@2346
|
739 table.border = "0";
|
nicholas@2224
|
740 for (var option of node.specification.options) {
|
nicholas@2346
|
741 var tr = document.createElement("tr");
|
nicholas@2346
|
742 table.appendChild(tr);
|
nicholas@2346
|
743 var td = document.createElement("td");
|
nicholas@2346
|
744 tr.appendChild(td);
|
nicholas@2346
|
745 var input = document.createElement('input');
|
nicholas@2346
|
746 input.id = option.name;
|
nicholas@2224
|
747 input.type = 'checkbox';
|
nicholas@2346
|
748 td.appendChild(input);
|
nicholas@2346
|
749
|
nicholas@2346
|
750 td = document.createElement("td");
|
nicholas@2346
|
751 tr.appendChild(td);
|
nicholas@2224
|
752 var span = document.createElement('span');
|
nicholas@2224
|
753 span.textContent = option.text;
|
nicholas@2346
|
754 td.appendChild(span);
|
nicholas@2346
|
755 var tr = document.createElement('div');
|
nicholas@2346
|
756 tr.setAttribute('name','option');
|
nicholas@2346
|
757 tr.className = "popup-option-checbox";
|
nicholas@2224
|
758 if (node.response[index] != undefined){
|
nicholas@2224
|
759 if (node.response[index].checked == true) {
|
nicholas@2224
|
760 input.checked = "true";
|
nicholas@2224
|
761 }
|
nicholas@2224
|
762 }
|
nicholas@2347
|
763 index++;
|
nicholas@2346
|
764 }
|
nicholas@2346
|
765 this.popupResponse.appendChild(table);
|
nicholas@2224
|
766 } else if (node.specification.type == 'radio') {
|
nicholas@2224
|
767 if (node.response == undefined) {
|
nicholas@2224
|
768 node.response = {name: "", text: ""};
|
nicholas@2224
|
769 }
|
nicholas@2224
|
770 var index = 0;
|
nicholas@2346
|
771 var table = document.createElement("table");
|
nicholas@2346
|
772 table.className = "popup-option-list";
|
nicholas@2346
|
773 table.border = "0";
|
nicholas@2224
|
774 for (var option of node.specification.options) {
|
nicholas@2346
|
775 var tr = document.createElement("tr");
|
nicholas@2346
|
776 table.appendChild(tr);
|
nicholas@2346
|
777 var td = document.createElement("td");
|
nicholas@2346
|
778 tr.appendChild(td);
|
nicholas@2346
|
779 var input = document.createElement('input');
|
nicholas@2346
|
780 input.id = option.name;
|
nicholas@2224
|
781 input.type = 'radio';
|
nicholas@2346
|
782 input.name = node.specification.id;
|
nicholas@2346
|
783 td.appendChild(input);
|
nicholas@2346
|
784
|
nicholas@2346
|
785 td = document.createElement("td");
|
nicholas@2346
|
786 tr.appendChild(td);
|
nicholas@2224
|
787 var span = document.createElement('span');
|
nicholas@2224
|
788 span.textContent = option.text;
|
nicholas@2346
|
789 td.appendChild(span);
|
nicholas@2346
|
790 var tr = document.createElement('div');
|
nicholas@2346
|
791 tr.setAttribute('name','option');
|
nicholas@2346
|
792 tr.className = "popup-option-checbox";
|
nicholas@2346
|
793 if (node.response[index] != undefined){
|
nicholas@2346
|
794 if (node.response[index].checked == true) {
|
nicholas@2346
|
795 input.checked = "true";
|
nicholas@2346
|
796 }
|
nicholas@2224
|
797 }
|
nicholas@2347
|
798 index++;
|
nicholas@2346
|
799 }
|
nicholas@2346
|
800 this.popupResponse.appendChild(table);
|
nicholas@2224
|
801 } else if (node.specification.type == 'number') {
|
nicholas@2224
|
802 var input = document.createElement('input');
|
nicholas@2224
|
803 input.type = 'textarea';
|
nicholas@2224
|
804 if (node.min != null) {input.min = node.specification.min;}
|
nicholas@2224
|
805 if (node.max != null) {input.max = node.specification.max;}
|
nicholas@2224
|
806 if (node.step != null) {input.step = node.specification.step;}
|
nicholas@2224
|
807 if (node.response != undefined) {
|
nicholas@2224
|
808 input.value = node.response;
|
nicholas@2224
|
809 }
|
nicholas@2224
|
810 this.popupResponse.appendChild(input);
|
nicholas@2224
|
811 this.popupResponse.style.textAlign="center";
|
nicholas@2224
|
812 this.popupResponse.style.left="0%";
|
nicholas@2491
|
813 } else if (node.specification.type == "video") {
|
nicholas@2491
|
814 var video = document.createElement("video");
|
nicholas@2491
|
815 video.src = node.specification.url;
|
nicholas@2491
|
816 this.popupResponse.appendChild(video);
|
nicholas@2491
|
817 } else if (node.specification.type == "youtube") {
|
nicholas@2491
|
818 var iframe = document.createElement("iframe");
|
nicholas@2491
|
819 iframe.className = "youtube";
|
nicholas@2491
|
820 iframe.src = node.specification.url;
|
nicholas@2491
|
821 this.popupResponse.appendChild(iframe);
|
nicholas@2491
|
822 }
|
nicholas@2224
|
823 if(this.currentIndex+1 == this.popupOptions.length) {
|
nicholas@2224
|
824 if (this.node.location == "pre") {
|
nicholas@2224
|
825 this.buttonProceed.textContent = 'Start';
|
nicholas@2224
|
826 } else {
|
nicholas@2224
|
827 this.buttonProceed.textContent = 'Submit';
|
nicholas@2224
|
828 }
|
nicholas@2224
|
829 } else {
|
nicholas@2224
|
830 this.buttonProceed.textContent = 'Next';
|
nicholas@2224
|
831 }
|
nicholas@2224
|
832 if(this.currentIndex > 0)
|
nicholas@2224
|
833 this.buttonPrevious.style.visibility = 'visible';
|
nicholas@2224
|
834 else
|
nicholas@2224
|
835 this.buttonPrevious.style.visibility = 'hidden';
|
nicholas@2224
|
836 };
|
nicholas@2224
|
837
|
nicholas@2224
|
838 this.initState = function(node,store) {
|
nicholas@2224
|
839 //Call this with your preTest and postTest nodes when needed to
|
nicholas@2224
|
840 // initialise the popup procedure.
|
nicholas@2224
|
841 if (node.options.length > 0) {
|
nicholas@2224
|
842 this.popupOptions = [];
|
nicholas@2224
|
843 this.node = node;
|
nicholas@2224
|
844 this.store = store;
|
nicholas@2224
|
845 for (var opt of node.options)
|
nicholas@2224
|
846 {
|
nicholas@2224
|
847 this.popupOptions.push({
|
nicholas@2224
|
848 specification: opt,
|
nicholas@2224
|
849 response: null
|
nicholas@2224
|
850 });
|
nicholas@2224
|
851 }
|
nicholas@2224
|
852 this.currentIndex = 0;
|
nicholas@2224
|
853 this.showPopup();
|
nicholas@2224
|
854 this.postNode();
|
nicholas@2224
|
855 } else {
|
nicholas@2224
|
856 advanceState();
|
nicholas@2224
|
857 }
|
nicholas@2224
|
858 };
|
nicholas@2224
|
859
|
nicholas@2224
|
860 this.proceedClicked = function() {
|
nicholas@2224
|
861 // Each time the popup button is clicked!
|
nicholas@2224
|
862 if (testState.stateIndex == 0 && specification.calibration) {
|
nicholas@2224
|
863 interfaceContext.calibrationModuleObject.collect();
|
nicholas@2224
|
864 advanceState();
|
nicholas@2224
|
865 return;
|
nicholas@2224
|
866 }
|
nicholas@2224
|
867 var node = this.popupOptions[this.currentIndex];
|
nicholas@2224
|
868 if (node.specification.type == 'question') {
|
nicholas@2224
|
869 // Must extract the question data
|
nicholas@2224
|
870 var textArea = $(popup.popupContent).find('textarea')[0];
|
nicholas@2224
|
871 if (node.specification.mandatory == true && textArea.value.length == 0) {
|
b@2372
|
872 interfaceContext.lightbox.post("Error","This question is mandatory");
|
nicholas@2224
|
873 return;
|
nicholas@2224
|
874 } else {
|
nicholas@2224
|
875 // Save the text content
|
nicholas@2224
|
876 console.log("Question: "+ node.specification.statement);
|
nicholas@2224
|
877 console.log("Question Response: "+ textArea.value);
|
nicholas@2224
|
878 node.response = textArea.value;
|
nicholas@2224
|
879 }
|
nicholas@2464
|
880 // Perform the conditional
|
nicholas@2464
|
881 for (var condition of node.specification.conditions) {
|
nicholas@2464
|
882 var pass = false;
|
nicholas@2464
|
883 switch(condition.check) {
|
nicholas@2464
|
884 case "equals":
|
nicholas@2464
|
885 if (textArea.value == condition.value) {
|
nicholas@2464
|
886 pass = true;
|
nicholas@2464
|
887 }
|
nicholas@2464
|
888 break;
|
nicholas@2464
|
889 case "greaterThan":
|
nicholas@2464
|
890 case "lessThan":
|
nicholas@2464
|
891 console.log("Survey Element of type 'question' cannot interpret greaterThan/lessThan conditions. IGNORING");
|
nicholas@2464
|
892 break;
|
nicholas@2464
|
893 case "contains":
|
nicholas@2464
|
894 if (textArea.value.includes(condition.value)) {
|
nicholas@2464
|
895 pass = true;
|
nicholas@2464
|
896 }
|
nicholas@2464
|
897 break;
|
nicholas@2464
|
898 }
|
nicholas@2464
|
899 var jumpID;
|
nicholas@2464
|
900 if (pass) {
|
nicholas@2464
|
901 jumpID = condition.jumpToOnPass;
|
nicholas@2464
|
902 } else {
|
nicholas@2464
|
903 jumpID = condition.jumpToOnFail;
|
nicholas@2464
|
904 }
|
nicholas@2464
|
905 if (jumpID != undefined) {
|
nicholas@2464
|
906 var index = this.popupOptions.findIndex(function(item,index,element){
|
nicholas@2464
|
907 if (item.specification.id == jumpID) {return true;} else {return false;}
|
nicholas@2464
|
908 },this);
|
nicholas@2464
|
909 this.currentIndex = index-1;
|
nicholas@2464
|
910 break;
|
nicholas@2464
|
911 }
|
nicholas@2464
|
912 }
|
nicholas@2224
|
913 } else if (node.specification.type == 'checkbox') {
|
nicholas@2224
|
914 // Must extract checkbox data
|
nicholas@2224
|
915 console.log("Checkbox: "+ node.specification.statement);
|
nicholas@2224
|
916 var inputs = this.popupResponse.getElementsByTagName('input');
|
nicholas@2224
|
917 node.response = [];
|
nicholas@2224
|
918 for (var i=0; i<node.specification.options.length; i++) {
|
nicholas@2224
|
919 node.response.push({
|
nicholas@2224
|
920 name: node.specification.options[i].name,
|
nicholas@2224
|
921 text: node.specification.options[i].text,
|
nicholas@2224
|
922 checked: inputs[i].checked
|
nicholas@2224
|
923 });
|
nicholas@2224
|
924 console.log(node.specification.options[i].name+": "+ inputs[i].checked);
|
nicholas@2224
|
925 }
|
nicholas@2464
|
926 // Perform the conditional
|
nicholas@2464
|
927 for (var condition of node.specification.conditions) {
|
nicholas@2464
|
928 var pass = false;
|
nicholas@2464
|
929 switch(condition.check) {
|
nicholas@2464
|
930 case "equals":
|
nicholas@2464
|
931 case "greaterThan":
|
nicholas@2464
|
932 case "lessThan":
|
nicholas@2464
|
933 console.log("Survey Element of type 'checkbox' cannot interpret equals/greaterThan/lessThan conditions. IGNORING");
|
nicholas@2464
|
934 break;
|
nicholas@2464
|
935 case "contains":
|
nicholas@2464
|
936 for (var response of node.response) {
|
nicholas@2464
|
937 if (response.name == condition.value && response.checked) {
|
nicholas@2464
|
938 pass = true;
|
nicholas@2464
|
939 break;
|
nicholas@2464
|
940 }
|
nicholas@2464
|
941 }
|
nicholas@2464
|
942 break;
|
nicholas@2464
|
943 }
|
nicholas@2464
|
944 var jumpID;
|
nicholas@2464
|
945 if (pass) {
|
nicholas@2464
|
946 jumpID = condition.jumpToOnPass;
|
nicholas@2464
|
947 } else {
|
nicholas@2464
|
948 jumpID = condition.jumpToOnFail;
|
nicholas@2464
|
949 }
|
nicholas@2464
|
950 if (jumpID != undefined) {
|
nicholas@2464
|
951 var index = this.popupOptions.findIndex(function(item,index,element){
|
nicholas@2464
|
952 if (item.specification.id == jumpID) {return true;} else {return false;}
|
nicholas@2464
|
953 },this);
|
nicholas@2464
|
954 this.currentIndex = index-1;
|
nicholas@2464
|
955 break;
|
nicholas@2464
|
956 }
|
nicholas@2464
|
957 }
|
nicholas@2224
|
958 } else if (node.specification.type == "radio") {
|
nicholas@2224
|
959 var optHold = this.popupResponse;
|
nicholas@2224
|
960 console.log("Radio: "+ node.specification.statement);
|
nicholas@2224
|
961 node.response = null;
|
nicholas@2224
|
962 var i=0;
|
nicholas@2224
|
963 var inputs = optHold.getElementsByTagName('input');
|
nicholas@2224
|
964 while(node.response == null) {
|
nicholas@2224
|
965 if (i == inputs.length)
|
nicholas@2224
|
966 {
|
nicholas@2224
|
967 if (node.specification.mandatory == true)
|
nicholas@2224
|
968 {
|
nicholas@2361
|
969 interfaceContext.lightbox.post("Error","Please select one option");
|
nicholas@2316
|
970 return;
|
nicholas@2224
|
971 }
|
nicholas@2316
|
972 break;
|
nicholas@2224
|
973 }
|
nicholas@2224
|
974 if (inputs[i].checked == true) {
|
nicholas@2224
|
975 node.response = node.specification.options[i];
|
nicholas@2224
|
976 console.log("Selected: "+ node.specification.options[i].name);
|
nicholas@2224
|
977 }
|
nicholas@2224
|
978 i++;
|
nicholas@2224
|
979 }
|
nicholas@2464
|
980 // Perform the conditional
|
nicholas@2464
|
981 for (var condition of node.specification.conditions) {
|
nicholas@2464
|
982 var pass = false;
|
nicholas@2464
|
983 switch(condition.check) {
|
nicholas@2464
|
984 case "contains":
|
nicholas@2464
|
985 case "greaterThan":
|
nicholas@2464
|
986 case "lessThan":
|
nicholas@2464
|
987 console.log("Survey Element of type 'radio' cannot interpret contains/greaterThan/lessThan conditions. IGNORING");
|
nicholas@2464
|
988 break;
|
nicholas@2464
|
989 case "equals":
|
nicholas@2464
|
990 if (node.response == condition.value) {
|
nicholas@2464
|
991 pass = true;
|
nicholas@2464
|
992 }
|
nicholas@2464
|
993 break;
|
nicholas@2464
|
994 }
|
nicholas@2464
|
995 var jumpID;
|
nicholas@2464
|
996 if (pass) {
|
nicholas@2464
|
997 jumpID = condition.jumpToOnPass;
|
nicholas@2464
|
998 } else {
|
nicholas@2464
|
999 jumpID = condition.jumpToOnFail;
|
nicholas@2464
|
1000 }
|
nicholas@2464
|
1001 if (jumpID != undefined) {
|
nicholas@2464
|
1002 var index = this.popupOptions.findIndex(function(item,index,element){
|
nicholas@2464
|
1003 if (item.specification.id == jumpID) {return true;} else {return false;}
|
nicholas@2464
|
1004 },this);
|
nicholas@2464
|
1005 this.currentIndex = index-1;
|
nicholas@2464
|
1006 break;
|
nicholas@2464
|
1007 }
|
nicholas@2464
|
1008 }
|
nicholas@2224
|
1009 } else if (node.specification.type == "number") {
|
nicholas@2224
|
1010 var input = this.popupContent.getElementsByTagName('input')[0];
|
nicholas@2224
|
1011 if (node.mandatory == true && input.value.length == 0) {
|
nicholas@2361
|
1012 interfaceContext.lightbox.post("Error",'This question is mandatory. Please enter a number');
|
nicholas@2224
|
1013 return;
|
nicholas@2224
|
1014 }
|
nicholas@2224
|
1015 var enteredNumber = Number(input.value);
|
nicholas@2224
|
1016 if (isNaN(enteredNumber)) {
|
nicholas@2361
|
1017 interfaceContext.lightbox.post("Error",'Please enter a valid number');
|
nicholas@2224
|
1018 return;
|
nicholas@2224
|
1019 }
|
nicholas@2224
|
1020 if (enteredNumber < node.min && node.min != null) {
|
nicholas@2361
|
1021 interfaceContext.lightbox.post("Error",'Number is below the minimum value of '+node.min);
|
nicholas@2224
|
1022 return;
|
nicholas@2224
|
1023 }
|
nicholas@2224
|
1024 if (enteredNumber > node.max && node.max != null) {
|
nicholas@2361
|
1025 interfaceContext.lightbox.post("Error",'Number is above the maximum value of '+node.max);
|
nicholas@2224
|
1026 return;
|
nicholas@2224
|
1027 }
|
nicholas@2224
|
1028 node.response = input.value;
|
nicholas@2464
|
1029 // Perform the conditional
|
nicholas@2464
|
1030 for (var condition of node.specification.conditions) {
|
nicholas@2464
|
1031 var pass = false;
|
nicholas@2464
|
1032 switch(condition.check) {
|
nicholas@2464
|
1033 case "contains":
|
nicholas@2464
|
1034 console.log("Survey Element of type 'number' cannot interpret contains conditions. IGNORING");
|
nicholas@2464
|
1035 break;
|
nicholas@2464
|
1036 case "greaterThan":
|
nicholas@2464
|
1037 if (node.response > Number(condition.value)) {
|
nicholas@2464
|
1038 pass = true;
|
nicholas@2464
|
1039 }
|
nicholas@2464
|
1040 break;
|
nicholas@2464
|
1041 case "lessThan":
|
nicholas@2464
|
1042 if (node.response < Number(condition.value)) {
|
nicholas@2464
|
1043 pass = true;
|
nicholas@2464
|
1044 }
|
nicholas@2464
|
1045 break;
|
nicholas@2464
|
1046 case "equals":
|
nicholas@2464
|
1047 if (node.response == condition.value) {
|
nicholas@2464
|
1048 pass = true;
|
nicholas@2464
|
1049 }
|
nicholas@2464
|
1050 break;
|
nicholas@2464
|
1051 }
|
nicholas@2464
|
1052 var jumpID;
|
nicholas@2464
|
1053 if (pass) {
|
nicholas@2464
|
1054 jumpID = condition.jumpToOnPass;
|
nicholas@2464
|
1055 } else {
|
nicholas@2464
|
1056 jumpID = condition.jumpToOnFail;
|
nicholas@2464
|
1057 }
|
nicholas@2464
|
1058 if (jumpID != undefined) {
|
nicholas@2464
|
1059 var index = this.popupOptions.findIndex(function(item,index,element){
|
nicholas@2464
|
1060 if (item.specification.id == jumpID) {return true;} else {return false;}
|
nicholas@2464
|
1061 },this);
|
nicholas@2464
|
1062 this.currentIndex = index-1;
|
nicholas@2464
|
1063 break;
|
nicholas@2464
|
1064 }
|
nicholas@2464
|
1065 }
|
nicholas@2224
|
1066 }
|
nicholas@2224
|
1067 this.currentIndex++;
|
nicholas@2224
|
1068 if (this.currentIndex < this.popupOptions.length) {
|
nicholas@2224
|
1069 this.postNode();
|
nicholas@2224
|
1070 } else {
|
nicholas@2224
|
1071 // Reached the end of the popupOptions
|
nicholas@2492
|
1072 this.popupContent.innerHTML = "";
|
nicholas@2224
|
1073 this.hidePopup();
|
nicholas@2224
|
1074 for (var node of this.popupOptions)
|
nicholas@2224
|
1075 {
|
nicholas@2224
|
1076 this.store.postResult(node);
|
nicholas@2224
|
1077 }
|
nicholas@2224
|
1078 this.store.complete();
|
nicholas@2224
|
1079 advanceState();
|
nicholas@2224
|
1080 }
|
nicholas@2224
|
1081 };
|
nicholas@2224
|
1082
|
nicholas@2224
|
1083 this.previousClick = function() {
|
nicholas@2224
|
1084 // Triggered when the 'Back' button is clicked in the survey
|
nicholas@2224
|
1085 if (this.currentIndex > 0) {
|
nicholas@2224
|
1086 this.currentIndex--;
|
nicholas@2224
|
1087 this.postNode();
|
nicholas@2224
|
1088 }
|
nicholas@2224
|
1089 };
|
nicholas@2224
|
1090
|
nicholas@2224
|
1091 this.resize = function(event)
|
nicholas@2224
|
1092 {
|
nicholas@2224
|
1093 // Called on window resize;
|
nicholas@2224
|
1094 if (this.popup != null) {
|
nicholas@2224
|
1095 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
|
nicholas@2224
|
1096 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
|
nicholas@2224
|
1097 var blank = document.getElementsByClassName('testHalt')[0];
|
nicholas@2224
|
1098 blank.style.width = window.innerWidth;
|
nicholas@2224
|
1099 blank.style.height = window.innerHeight;
|
nicholas@2224
|
1100 }
|
nicholas@2224
|
1101 };
|
nicholas@2224
|
1102 this.hideNextButton = function() {
|
nicholas@2224
|
1103 this.buttonProceed.style.visibility = "hidden";
|
nicholas@2224
|
1104 }
|
nicholas@2224
|
1105 this.hidePreviousButton = function() {
|
nicholas@2224
|
1106 this.buttonPrevious.style.visibility = "hidden";
|
nicholas@2224
|
1107 }
|
nicholas@2224
|
1108 this.showNextButton = function() {
|
nicholas@2224
|
1109 this.buttonProceed.style.visibility = "visible";
|
nicholas@2224
|
1110 }
|
nicholas@2224
|
1111 this.showPreviousButton = function() {
|
nicholas@2224
|
1112 this.buttonPrevious.style.visibility = "visible";
|
nicholas@2224
|
1113 }
|
nicholas@2224
|
1114 }
|
nicholas@2224
|
1115
|
nicholas@2224
|
1116 function advanceState()
|
nicholas@2224
|
1117 {
|
nicholas@2224
|
1118 // Just for complete clarity
|
nicholas@2224
|
1119 testState.advanceState();
|
nicholas@2224
|
1120 }
|
nicholas@2224
|
1121
|
nicholas@2224
|
1122 function stateMachine()
|
nicholas@2224
|
1123 {
|
nicholas@2224
|
1124 // Object prototype for tracking and managing the test state
|
nicholas@2224
|
1125 this.stateMap = [];
|
nicholas@2224
|
1126 this.preTestSurvey = null;
|
nicholas@2224
|
1127 this.postTestSurvey = null;
|
nicholas@2224
|
1128 this.stateIndex = null;
|
nicholas@2224
|
1129 this.currentStateMap = null;
|
nicholas@2224
|
1130 this.currentStatePosition = null;
|
nicholas@2224
|
1131 this.currentStore = null;
|
nicholas@2224
|
1132 this.initialise = function(){
|
nicholas@2224
|
1133
|
nicholas@2224
|
1134 // Get the data from Specification
|
nicholas@2224
|
1135 var pagePool = [];
|
nicholas@2224
|
1136 var pageInclude = [];
|
nicholas@2224
|
1137 for (var page of specification.pages)
|
nicholas@2224
|
1138 {
|
nicholas@2224
|
1139 if (page.alwaysInclude) {
|
nicholas@2224
|
1140 pageInclude.push(page);
|
nicholas@2224
|
1141 } else {
|
nicholas@2224
|
1142 pagePool.push(page);
|
nicholas@2224
|
1143 }
|
nicholas@2224
|
1144 }
|
nicholas@2224
|
1145
|
nicholas@2224
|
1146 // Find how many are left to get
|
nicholas@2224
|
1147 var numPages = specification.poolSize;
|
nicholas@2224
|
1148 if (numPages > pagePool.length) {
|
nicholas@2224
|
1149 console.log("WARNING - You have specified more pages in <setup poolSize> than you have created!!");
|
nicholas@2224
|
1150 numPages = specification.pages.length;
|
nicholas@2224
|
1151 }
|
nicholas@2224
|
1152 if (specification.poolSize == 0) {
|
nicholas@2224
|
1153 numPages = specification.pages.length;
|
nicholas@2224
|
1154 }
|
nicholas@2224
|
1155 numPages -= pageInclude.length;
|
nicholas@2224
|
1156
|
nicholas@2224
|
1157 if (numPages > 0) {
|
nicholas@2224
|
1158 // Go find the rest of the pages from the pool
|
nicholas@2224
|
1159 var subarr = null;
|
nicholas@2224
|
1160 if (specification.randomiseOrder) {
|
nicholas@2224
|
1161 // Append a random sub-array
|
nicholas@2224
|
1162 subarr = randomSubArray(pagePool,numPages);
|
nicholas@2224
|
1163 } else {
|
nicholas@2224
|
1164 // Append the matching number
|
nicholas@2224
|
1165 subarr = pagePool.slice(0,numPages);
|
nicholas@2224
|
1166 }
|
nicholas@2224
|
1167 pageInclude = pageInclude.concat(subarr);
|
nicholas@2224
|
1168 }
|
nicholas@2224
|
1169
|
nicholas@2224
|
1170 // We now have our selected pages in pageInclude array
|
nicholas@2224
|
1171 if (specification.randomiseOrder)
|
nicholas@2224
|
1172 {
|
nicholas@2224
|
1173 pageInclude = randomiseOrder(pageInclude);
|
nicholas@2224
|
1174 }
|
nicholas@2224
|
1175 for (var i=0; i<pageInclude.length; i++)
|
nicholas@2224
|
1176 {
|
nicholas@2224
|
1177 pageInclude[i].presentedId = i;
|
nicholas@2224
|
1178 this.stateMap.push(pageInclude[i]);
|
nicholas@2224
|
1179 // For each selected page, we must get the sub pool
|
nicholas@2224
|
1180 if (pageInclude[i].poolSize != 0 && pageInclude[i].poolSize != pageInclude[i].audioElements.length) {
|
nicholas@2224
|
1181 var elemInclude = [];
|
nicholas@2224
|
1182 var elemPool = [];
|
nicholas@2224
|
1183 for (var elem of pageInclude[i].audioElements) {
|
nicholas@2224
|
1184 if (elem.include || elem.type != "normal") {
|
nicholas@2224
|
1185 elemInclude.push(elem);
|
nicholas@2224
|
1186 } else {
|
nicholas@2224
|
1187 elemPool.push(elem);
|
nicholas@2224
|
1188 }
|
nicholas@2224
|
1189 }
|
nicholas@2224
|
1190 var numElems = pageInclude[i].poolSize - elemInclude.length;
|
nicholas@2224
|
1191 pageInclude[i].audioElements = elemInclude.concat(randomSubArray(elemPool,numElems));
|
nicholas@2224
|
1192 }
|
nicholas@2224
|
1193 storage.createTestPageStore(pageInclude[i]);
|
nicholas@2224
|
1194 audioEngineContext.loadPageData(pageInclude[i]);
|
nicholas@2224
|
1195 }
|
nicholas@2224
|
1196
|
nicholas@2224
|
1197 if (specification.preTest != null) {this.preTestSurvey = specification.preTest;}
|
nicholas@2224
|
1198 if (specification.postTest != null) {this.postTestSurvey = specification.postTest;}
|
nicholas@2224
|
1199
|
nicholas@2224
|
1200 if (this.stateMap.length > 0) {
|
nicholas@2224
|
1201 if(this.stateIndex != null) {
|
nicholas@2224
|
1202 console.log('NOTE - State already initialise');
|
nicholas@2224
|
1203 }
|
nicholas@2224
|
1204 this.stateIndex = -2;
|
nicholas@2224
|
1205 console.log('Starting test...');
|
nicholas@2224
|
1206 } else {
|
nicholas@2224
|
1207 console.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
|
nicholas@2224
|
1208 }
|
nicholas@2224
|
1209 };
|
nicholas@2224
|
1210 this.advanceState = function(){
|
nicholas@2224
|
1211 if (this.stateIndex == null) {
|
nicholas@2224
|
1212 this.initialise();
|
nicholas@2224
|
1213 }
|
nicholas@2357
|
1214 if (this.stateIndex > -2) {
|
nicholas@2357
|
1215 storage.update();
|
nicholas@2357
|
1216 }
|
nicholas@2224
|
1217 if (this.stateIndex == -2) {
|
nicholas@2224
|
1218 this.stateIndex++;
|
nicholas@2224
|
1219 if (this.preTestSurvey != null)
|
nicholas@2224
|
1220 {
|
nicholas@2224
|
1221 popup.initState(this.preTestSurvey,storage.globalPreTest);
|
nicholas@2224
|
1222 } else {
|
nicholas@2224
|
1223 this.advanceState();
|
nicholas@2224
|
1224 }
|
nicholas@2224
|
1225 } else if (this.stateIndex == -1) {
|
nicholas@2224
|
1226 this.stateIndex++;
|
nicholas@2224
|
1227 if (specification.calibration) {
|
nicholas@2224
|
1228 popup.showPopup();
|
nicholas@2224
|
1229 popup.popupTitle.textContent = "Calibration. Set the levels so all tones are of equal amplitude. Move your mouse over the sliders to hear the tones. The red slider is the reference tone";
|
nicholas@2224
|
1230 interfaceContext.calibrationModuleObject = new interfaceContext.calibrationModule();
|
nicholas@2224
|
1231 interfaceContext.calibrationModuleObject.build(popup.popupResponse);
|
nicholas@2224
|
1232 popup.hidePreviousButton();
|
nicholas@2224
|
1233 } else {
|
nicholas@2224
|
1234 this.advanceState();
|
nicholas@2224
|
1235 }
|
nicholas@2224
|
1236 }
|
nicholas@2224
|
1237 else if (this.stateIndex == this.stateMap.length)
|
nicholas@2224
|
1238 {
|
nicholas@2224
|
1239 // All test pages complete, post test
|
nicholas@2224
|
1240 console.log('Ending test ...');
|
nicholas@2224
|
1241 this.stateIndex++;
|
nicholas@2224
|
1242 if (this.postTestSurvey == null) {
|
nicholas@2224
|
1243 this.advanceState();
|
nicholas@2224
|
1244 } else {
|
nicholas@2224
|
1245 popup.initState(this.postTestSurvey,storage.globalPostTest);
|
nicholas@2224
|
1246 }
|
nicholas@2224
|
1247 } else if (this.stateIndex > this.stateMap.length)
|
nicholas@2224
|
1248 {
|
nicholas@2224
|
1249 createProjectSave(specification.projectReturn);
|
nicholas@2224
|
1250 }
|
nicholas@2224
|
1251 else
|
nicholas@2224
|
1252 {
|
nicholas@2224
|
1253 popup.hidePopup();
|
nicholas@2224
|
1254 if (this.currentStateMap == null)
|
nicholas@2224
|
1255 {
|
nicholas@2224
|
1256 this.currentStateMap = this.stateMap[this.stateIndex];
|
nicholas@2349
|
1257 // Find and extract the outside reference
|
nicholas@2399
|
1258 var elements = [], ref = [];
|
nicholas@2399
|
1259 var elem;
|
nicholas@2399
|
1260 while(elem = this.currentStateMap.audioElements.pop())
|
nicholas@2399
|
1261 {
|
nicholas@2399
|
1262 if (elem.type == "outside-reference") {
|
nicholas@2399
|
1263 ref.push(elem);
|
nicholas@2399
|
1264 }
|
nicholas@2399
|
1265 else {
|
nicholas@2399
|
1266 elements.push(elem);
|
nicholas@2399
|
1267 }
|
nicholas@2349
|
1268 }
|
nicholas@2443
|
1269 elements = elements.reverse();
|
nicholas@2224
|
1270 if (this.currentStateMap.randomiseOrder)
|
nicholas@2224
|
1271 {
|
nicholas@2399
|
1272 elements = randomiseOrder(elements);
|
nicholas@2224
|
1273 }
|
nicholas@2399
|
1274 this.currentStateMap.audioElements = elements.concat(ref);
|
nicholas@2399
|
1275
|
nicholas@2224
|
1276 this.currentStore = storage.testPages[this.stateIndex];
|
nicholas@2224
|
1277 if (this.currentStateMap.preTest != null)
|
nicholas@2224
|
1278 {
|
nicholas@2224
|
1279 this.currentStatePosition = 'pre';
|
nicholas@2224
|
1280 popup.initState(this.currentStateMap.preTest,storage.testPages[this.stateIndex].preTest);
|
nicholas@2224
|
1281 } else {
|
nicholas@2224
|
1282 this.currentStatePosition = 'test';
|
nicholas@2224
|
1283 }
|
nicholas@2224
|
1284 interfaceContext.newPage(this.currentStateMap,storage.testPages[this.stateIndex]);
|
nicholas@2224
|
1285 return;
|
nicholas@2224
|
1286 }
|
nicholas@2224
|
1287 switch(this.currentStatePosition)
|
nicholas@2224
|
1288 {
|
nicholas@2224
|
1289 case 'pre':
|
nicholas@2224
|
1290 this.currentStatePosition = 'test';
|
nicholas@2224
|
1291 break;
|
nicholas@2224
|
1292 case 'test':
|
nicholas@2224
|
1293 this.currentStatePosition = 'post';
|
nicholas@2224
|
1294 // Save the data
|
nicholas@2224
|
1295 this.testPageCompleted();
|
nicholas@2224
|
1296 if (this.currentStateMap.postTest == null)
|
nicholas@2224
|
1297 {
|
nicholas@2224
|
1298 this.advanceState();
|
nicholas@2224
|
1299 return;
|
nicholas@2224
|
1300 } else {
|
nicholas@2224
|
1301 popup.initState(this.currentStateMap.postTest,storage.testPages[this.stateIndex].postTest);
|
nicholas@2224
|
1302 }
|
nicholas@2224
|
1303 break;
|
nicholas@2224
|
1304 case 'post':
|
nicholas@2224
|
1305 this.stateIndex++;
|
nicholas@2224
|
1306 this.currentStateMap = null;
|
nicholas@2224
|
1307 this.advanceState();
|
nicholas@2224
|
1308 break;
|
nicholas@2224
|
1309 };
|
nicholas@2224
|
1310 }
|
nicholas@2224
|
1311 };
|
nicholas@2224
|
1312
|
nicholas@2224
|
1313 this.testPageCompleted = function() {
|
nicholas@2224
|
1314 // Function called each time a test page has been completed
|
nicholas@2224
|
1315 var storePoint = storage.testPages[this.stateIndex];
|
nicholas@2224
|
1316 // First get the test metric
|
nicholas@2224
|
1317
|
nicholas@2224
|
1318 var metric = storePoint.XMLDOM.getElementsByTagName('metric')[0];
|
nicholas@2224
|
1319 if (audioEngineContext.metric.enableTestTimer)
|
nicholas@2224
|
1320 {
|
nicholas@2224
|
1321 var testTime = storePoint.parent.document.createElement('metricresult');
|
nicholas@2224
|
1322 testTime.id = 'testTime';
|
nicholas@2224
|
1323 testTime.textContent = audioEngineContext.timer.testDuration;
|
nicholas@2224
|
1324 metric.appendChild(testTime);
|
nicholas@2224
|
1325 }
|
nicholas@2224
|
1326
|
nicholas@2224
|
1327 var audioObjects = audioEngineContext.audioObjects;
|
nicholas@2224
|
1328 for (var ao of audioEngineContext.audioObjects)
|
nicholas@2224
|
1329 {
|
nicholas@2224
|
1330 ao.exportXMLDOM();
|
nicholas@2224
|
1331 }
|
nicholas@2224
|
1332 for (var element of interfaceContext.commentQuestions)
|
nicholas@2224
|
1333 {
|
nicholas@2224
|
1334 element.exportXMLDOM(storePoint);
|
nicholas@2224
|
1335 }
|
nicholas@2224
|
1336 pageXMLSave(storePoint.XMLDOM, this.currentStateMap);
|
nicholas@2224
|
1337 storePoint.complete();
|
nicholas@2224
|
1338 };
|
nicholas@2310
|
1339
|
nicholas@2310
|
1340 this.getCurrentTestPage = function() {
|
nicholas@2310
|
1341 if (this.stateIndex >= 0 && this.stateIndex< this.stateMap.length) {
|
nicholas@2310
|
1342 return this.currentStateMap;
|
nicholas@2310
|
1343 } else {
|
nicholas@2310
|
1344 return null;
|
nicholas@2310
|
1345 }
|
nicholas@2310
|
1346 }
|
nicholas@2312
|
1347 this.getCurrentTestPageStore = function() {
|
nicholas@2312
|
1348 if (this.stateIndex >= 0 && this.stateIndex< this.stateMap.length) {
|
nicholas@2312
|
1349 return this.currentStore;
|
nicholas@2312
|
1350 } else {
|
nicholas@2312
|
1351 return null;
|
nicholas@2312
|
1352 }
|
nicholas@2312
|
1353 }
|
nicholas@2224
|
1354 }
|
nicholas@2224
|
1355
|
nicholas@2224
|
1356 function AudioEngine(specification) {
|
nicholas@2224
|
1357
|
nicholas@2224
|
1358 // Create two output paths, the main outputGain and fooGain.
|
nicholas@2224
|
1359 // Output gain is default to 1 and any items for playback route here
|
nicholas@2224
|
1360 // Foo gain is used for analysis to ensure paths get processed, but are not heard
|
nicholas@2224
|
1361 // because web audio will optimise and any route which does not go to the destination gets ignored.
|
nicholas@2224
|
1362 this.outputGain = audioContext.createGain();
|
nicholas@2224
|
1363 this.fooGain = audioContext.createGain();
|
nicholas@2224
|
1364 this.fooGain.gain = 0;
|
nicholas@2224
|
1365
|
nicholas@2224
|
1366 // Use this to detect playback state: 0 - stopped, 1 - playing
|
nicholas@2224
|
1367 this.status = 0;
|
nicholas@2224
|
1368
|
nicholas@2224
|
1369 // Connect both gains to output
|
nicholas@2224
|
1370 this.outputGain.connect(audioContext.destination);
|
nicholas@2224
|
1371 this.fooGain.connect(audioContext.destination);
|
nicholas@2224
|
1372
|
nicholas@2224
|
1373 // Create the timer Object
|
nicholas@2224
|
1374 this.timer = new timer();
|
nicholas@2224
|
1375 // Create session metrics
|
nicholas@2224
|
1376 this.metric = new sessionMetrics(this,specification);
|
nicholas@2224
|
1377
|
nicholas@2224
|
1378 this.loopPlayback = false;
|
nicholas@2351
|
1379 this.synchPlayback = false;
|
nicholas@2351
|
1380 this.pageSpecification = null;
|
nicholas@2224
|
1381
|
nicholas@2224
|
1382 this.pageStore = null;
|
nicholas@2224
|
1383
|
nicholas@2224
|
1384 // Create store for new audioObjects
|
nicholas@2224
|
1385 this.audioObjects = [];
|
nicholas@2224
|
1386
|
nicholas@2224
|
1387 this.buffers = [];
|
nicholas@2224
|
1388 this.bufferObj = function()
|
nicholas@2224
|
1389 {
|
nicholas@2224
|
1390 this.url = null;
|
nicholas@2224
|
1391 this.buffer = null;
|
nicholas@2224
|
1392 this.xmlRequest = new XMLHttpRequest();
|
nicholas@2224
|
1393 this.xmlRequest.parent = this;
|
nicholas@2224
|
1394 this.users = [];
|
nicholas@2224
|
1395 this.progress = 0;
|
nicholas@2224
|
1396 this.status = 0;
|
nicholas@2224
|
1397 this.ready = function()
|
nicholas@2224
|
1398 {
|
nicholas@2224
|
1399 if (this.status >= 2)
|
nicholas@2224
|
1400 {
|
nicholas@2224
|
1401 this.status = 3;
|
nicholas@2224
|
1402 }
|
nicholas@2224
|
1403 for (var i=0; i<this.users.length; i++)
|
nicholas@2224
|
1404 {
|
nicholas@2224
|
1405 this.users[i].state = 1;
|
nicholas@2224
|
1406 if (this.users[i].interfaceDOM != null)
|
nicholas@2224
|
1407 {
|
nicholas@2224
|
1408 this.users[i].bufferLoaded(this);
|
nicholas@2224
|
1409 }
|
nicholas@2224
|
1410 }
|
nicholas@2224
|
1411 };
|
nicholas@2224
|
1412 this.getMedia = function(url) {
|
nicholas@2224
|
1413 this.url = url;
|
nicholas@2224
|
1414 this.xmlRequest.open('GET',this.url,true);
|
nicholas@2224
|
1415 this.xmlRequest.responseType = 'arraybuffer';
|
nicholas@2224
|
1416
|
nicholas@2224
|
1417 var bufferObj = this;
|
nicholas@2224
|
1418
|
nicholas@2224
|
1419 // Create callback to decode the data asynchronously
|
nicholas@2224
|
1420 this.xmlRequest.onloadend = function() {
|
nicholas@2224
|
1421 // Use inbuilt WAVE decoder first
|
nicholas@2224
|
1422 if (this.status == -1) {return;}
|
nicholas@2224
|
1423 var waveObj = new WAVE();
|
nicholas@2403
|
1424 audioContext.decodeAudioData(bufferObj.xmlRequest.response, function(decodedData) {
|
nicholas@2224
|
1425 bufferObj.buffer = decodedData;
|
nicholas@2244
|
1426 bufferObj.status = 2;
|
nicholas@2244
|
1427 calculateLoudness(bufferObj,"I");
|
nicholas@2403
|
1428 }, function(e) {
|
nicholas@2403
|
1429 var waveObj = new WAVE();
|
nicholas@2403
|
1430 if (waveObj.open(bufferObj.xmlRequest.response) == 0)
|
nicholas@2403
|
1431 {
|
nicholas@2403
|
1432 bufferObj.buffer = audioContext.createBuffer(waveObj.num_channels,waveObj.num_samples,waveObj.sample_rate);
|
nicholas@2403
|
1433 for (var c=0; c<waveObj.num_channels; c++)
|
nicholas@2403
|
1434 {
|
nicholas@2403
|
1435 var buffer_ptr = bufferObj.buffer.getChannelData(c);
|
nicholas@2403
|
1436 for (var n=0; n<waveObj.num_samples; n++)
|
nicholas@2224
|
1437 {
|
nicholas@2403
|
1438 buffer_ptr[n] = waveObj.decoded_data[c][n];
|
nicholas@2224
|
1439 }
|
nicholas@2224
|
1440 }
|
nicholas@2403
|
1441
|
nicholas@2403
|
1442 delete waveObj;
|
nicholas@2403
|
1443 }
|
nicholas@2403
|
1444 if (bufferObj.buffer != undefined)
|
nicholas@2403
|
1445 {
|
nicholas@2403
|
1446 bufferObj.status = 2;
|
nicholas@2403
|
1447 calculateLoudness(bufferObj,"I");
|
nicholas@2403
|
1448 }
|
nicholas@2403
|
1449 });
|
nicholas@2224
|
1450 };
|
nicholas@2224
|
1451
|
nicholas@2224
|
1452 // Create callback for any error in loading
|
nicholas@2224
|
1453 this.xmlRequest.onerror = function() {
|
nicholas@2224
|
1454 this.parent.status = -1;
|
nicholas@2224
|
1455 for (var i=0; i<this.parent.users.length; i++)
|
nicholas@2224
|
1456 {
|
nicholas@2224
|
1457 this.parent.users[i].state = -1;
|
nicholas@2224
|
1458 if (this.parent.users[i].interfaceDOM != null)
|
nicholas@2224
|
1459 {
|
nicholas@2224
|
1460 this.parent.users[i].bufferLoaded(this);
|
nicholas@2224
|
1461 }
|
nicholas@2224
|
1462 }
|
n@2425
|
1463 interfaceContext.lightbox.post("Error","Could not load resource "+this.parent.url);
|
nicholas@2224
|
1464 }
|
nicholas@2224
|
1465
|
nicholas@2224
|
1466 this.progress = 0;
|
nicholas@2224
|
1467 this.progressCallback = function(event){
|
nicholas@2224
|
1468 if (event.lengthComputable)
|
nicholas@2224
|
1469 {
|
nicholas@2224
|
1470 this.parent.progress = event.loaded / event.total;
|
nicholas@2224
|
1471 for (var i=0; i<this.parent.users.length; i++)
|
nicholas@2224
|
1472 {
|
nicholas@2224
|
1473 if(this.parent.users[i].interfaceDOM != null)
|
nicholas@2224
|
1474 {
|
nicholas@2224
|
1475 if (typeof this.parent.users[i].interfaceDOM.updateLoading === "function")
|
nicholas@2224
|
1476 {
|
nicholas@2224
|
1477 this.parent.users[i].interfaceDOM.updateLoading(this.parent.progress*100);
|
nicholas@2224
|
1478 }
|
nicholas@2224
|
1479 }
|
nicholas@2224
|
1480 }
|
nicholas@2224
|
1481 }
|
nicholas@2224
|
1482 };
|
nicholas@2224
|
1483 this.xmlRequest.addEventListener("progress", this.progressCallback);
|
nicholas@2224
|
1484 this.status = 1;
|
nicholas@2224
|
1485 this.xmlRequest.send();
|
nicholas@2224
|
1486 };
|
nicholas@2224
|
1487
|
nicholas@2224
|
1488 this.registerAudioObject = function(audioObject)
|
nicholas@2224
|
1489 {
|
nicholas@2224
|
1490 // Called by an audioObject to register to the buffer for use
|
nicholas@2224
|
1491 // First check if already in the register pool
|
nicholas@2224
|
1492 for (var objects of this.users)
|
nicholas@2224
|
1493 {
|
nicholas@2224
|
1494 if (audioObject.id == objects.id){return 0;}
|
nicholas@2224
|
1495 }
|
nicholas@2224
|
1496 this.users.push(audioObject);
|
nicholas@2224
|
1497 if (this.status == 3 || this.status == -1)
|
nicholas@2224
|
1498 {
|
nicholas@2224
|
1499 // The buffer is already ready, trigger bufferLoaded
|
nicholas@2224
|
1500 audioObject.bufferLoaded(this);
|
nicholas@2224
|
1501 }
|
nicholas@2224
|
1502 };
|
nicholas@2224
|
1503
|
nicholas@2224
|
1504 this.copyBuffer = function(preSilenceTime,postSilenceTime) {
|
nicholas@2224
|
1505 // Copies the entire bufferObj.
|
nicholas@2224
|
1506 if (preSilenceTime == undefined) {preSilenceTime = 0;}
|
nicholas@2224
|
1507 if (postSilenceTime == undefined) {postSilenceTime = 0;}
|
nicholas@2224
|
1508 var preSilenceSamples = secondsToSamples(preSilenceTime,this.buffer.sampleRate);
|
nicholas@2224
|
1509 var postSilenceSamples = secondsToSamples(postSilenceTime,this.buffer.sampleRate);
|
nicholas@2224
|
1510 var newLength = this.buffer.length+preSilenceSamples+postSilenceSamples;
|
nicholas@2460
|
1511 var copybuffer = audioContext.createBuffer(this.buffer.numberOfChannels, newLength, this.buffer.sampleRate);
|
nicholas@2224
|
1512 // Now we can use some efficient background copy schemes if we are just padding the end
|
nicholas@2460
|
1513 if (preSilenceSamples == 0 && typeof copybuffer.copyToChannel == "function") {
|
nicholas@2224
|
1514 for (var c=0; c<this.buffer.numberOfChannels; c++) {
|
nicholas@2460
|
1515 copybuffer.copyToChannel(this.buffer.getChannelData(c),c);
|
nicholas@2224
|
1516 }
|
nicholas@2224
|
1517 } else {
|
nicholas@2224
|
1518 for (var c=0; c<this.buffer.numberOfChannels; c++) {
|
nicholas@2224
|
1519 var src = this.buffer.getChannelData(c);
|
nicholas@2460
|
1520 var dst = copybuffer.getChannelData(c);
|
nicholas@2224
|
1521 for (var n=0; n<src.length; n++)
|
nicholas@2224
|
1522 dst[n+preSilenceSamples] = src[n];
|
nicholas@2224
|
1523 }
|
nicholas@2224
|
1524 }
|
nicholas@2224
|
1525 // Copy in the rest of the buffer information
|
nicholas@2460
|
1526 copybuffer.lufs = this.buffer.lufs;
|
nicholas@2460
|
1527 copybuffer.playbackGain = this.buffer.playbackGain;
|
nicholas@2460
|
1528 return copybuffer;
|
nicholas@2460
|
1529 }
|
nicholas@2460
|
1530
|
nicholas@2460
|
1531 this.cropBuffer = function(startTime, stopTime) {
|
nicholas@2460
|
1532 // Copy and return the cropped buffer
|
nicholas@2460
|
1533 var start_sample = Math.floor(startTime*this.buffer.sampleRate);
|
nicholas@2460
|
1534 var stop_sample = Math.floor(stopTime*this.buffer.sampleRate);
|
nicholas@2460
|
1535 var newLength = stop_sample - start_sample;
|
nicholas@2460
|
1536 var copybuffer = audioContext.createBuffer(this.buffer.numberOfChannels, newLength, this.buffer.sampleRate);
|
nicholas@2460
|
1537 // Now we can use some efficient background copy schemes if we are just padding the end
|
nicholas@2460
|
1538 for (var c=0; c<this.buffer.numberOfChannels; c++) {
|
nicholas@2460
|
1539 var buffer = this.buffer.getChannelData(c);
|
nicholas@2460
|
1540 var sub_frame = buffer.subarray(start_sample,stop_sample);
|
nicholas@2460
|
1541 if (typeof copybuffer.copyToChannel == "function") {
|
nicholas@2460
|
1542 copybuffer.copyToChannel(sub_frame,c);
|
nicholas@2460
|
1543 } else {
|
nicholas@2460
|
1544 var dst = copybuffer.getChannelData(c);
|
nicholas@2460
|
1545 for (var n=0; n<newLength; n++)
|
nicholas@2460
|
1546 dst[n] = src[n+start_sample];
|
nicholas@2460
|
1547 }
|
nicholas@2460
|
1548 }
|
nicholas@2460
|
1549 return copybuffer;
|
nicholas@2224
|
1550 }
|
nicholas@2224
|
1551 };
|
nicholas@2224
|
1552
|
nicholas@2224
|
1553 this.loadPageData = function(page) {
|
nicholas@2224
|
1554 // Load the URL from pages
|
nicholas@2224
|
1555 for (var element of page.audioElements) {
|
nicholas@2224
|
1556 var URL = page.hostURL + element.url;
|
nicholas@2224
|
1557 var buffer = null;
|
nicholas@2224
|
1558 for (var buffObj of this.buffers) {
|
nicholas@2224
|
1559 if (URL == buffObj.url) {
|
nicholas@2224
|
1560 buffer = buffObj;
|
nicholas@2224
|
1561 break;
|
nicholas@2224
|
1562 }
|
nicholas@2224
|
1563 }
|
nicholas@2224
|
1564 if (buffer == null) {
|
nicholas@2224
|
1565 buffer = new this.bufferObj();
|
nicholas@2224
|
1566 buffer.getMedia(URL);
|
nicholas@2224
|
1567 this.buffers.push(buffer);
|
nicholas@2224
|
1568 }
|
nicholas@2224
|
1569 }
|
nicholas@2224
|
1570 };
|
nicholas@2224
|
1571
|
nicholas@2224
|
1572 this.play = function(id) {
|
nicholas@2224
|
1573 // Start the timer and set the audioEngine state to playing (1)
|
nicholas@2460
|
1574 if (this.status == 0) {
|
nicholas@2224
|
1575 // Check if all audioObjects are ready
|
nicholas@2460
|
1576 this.bufferReady(id);
|
nicholas@2224
|
1577 }
|
nicholas@2224
|
1578 else
|
nicholas@2224
|
1579 {
|
nicholas@2224
|
1580 this.status = 1;
|
nicholas@2224
|
1581 }
|
nicholas@2224
|
1582 if (this.status== 1) {
|
nicholas@2224
|
1583 this.timer.startTest();
|
nicholas@2224
|
1584 if (id == undefined) {
|
nicholas@2224
|
1585 id = -1;
|
nicholas@2224
|
1586 console.error('FATAL - Passed id was undefined - AudioEngineContext.play(id)');
|
nicholas@2224
|
1587 return;
|
nicholas@2224
|
1588 } else {
|
nicholas@2224
|
1589 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]);
|
nicholas@2224
|
1590 }
|
nicholas@2351
|
1591 if (this.synchPlayback && this.loopPlayback) {
|
nicholas@2351
|
1592 // Traditional looped playback
|
nicholas@2224
|
1593 var setTime = audioContext.currentTime+specification.crossFade;
|
nicholas@2224
|
1594 for (var i=0; i<this.audioObjects.length; i++)
|
nicholas@2224
|
1595 {
|
nicholas@2224
|
1596 this.audioObjects[i].play(audioContext.currentTime);
|
nicholas@2224
|
1597 if (id == i) {
|
nicholas@2224
|
1598 this.audioObjects[i].loopStart(setTime);
|
nicholas@2224
|
1599 } else {
|
nicholas@2224
|
1600 this.audioObjects[i].loopStop(setTime);
|
nicholas@2224
|
1601 }
|
nicholas@2224
|
1602 }
|
nicholas@2351
|
1603 } else {
|
nicholas@2224
|
1604 var setTime = audioContext.currentTime+specification.crossFade;
|
nicholas@2224
|
1605 for (var i=0; i<this.audioObjects.length; i++)
|
nicholas@2224
|
1606 {
|
nicholas@2224
|
1607 if (i != id) {
|
nicholas@2224
|
1608 this.audioObjects[i].stop(setTime);
|
nicholas@2224
|
1609 } else if (i == id) {
|
nicholas@2224
|
1610 this.audioObjects[id].play(setTime);
|
nicholas@2224
|
1611 }
|
nicholas@2224
|
1612 }
|
nicholas@2224
|
1613 }
|
nicholas@2224
|
1614 interfaceContext.playhead.start();
|
nicholas@2224
|
1615 }
|
nicholas@2224
|
1616 };
|
nicholas@2224
|
1617
|
nicholas@2224
|
1618 this.stop = function() {
|
nicholas@2224
|
1619 // Send stop and reset command to all playback buffers
|
nicholas@2224
|
1620 if (this.status == 1) {
|
nicholas@2224
|
1621 var setTime = audioContext.currentTime+0.1;
|
nicholas@2224
|
1622 for (var i=0; i<this.audioObjects.length; i++)
|
nicholas@2224
|
1623 {
|
nicholas@2224
|
1624 this.audioObjects[i].stop(setTime);
|
nicholas@2224
|
1625 }
|
nicholas@2224
|
1626 interfaceContext.playhead.stop();
|
nicholas@2224
|
1627 }
|
nicholas@2224
|
1628 };
|
nicholas@2224
|
1629
|
nicholas@2224
|
1630 this.newTrack = function(element) {
|
nicholas@2224
|
1631 // Pull data from given URL into new audio buffer
|
nicholas@2224
|
1632 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
|
nicholas@2224
|
1633
|
nicholas@2224
|
1634 // Create the audioObject with ID of the new track length;
|
nicholas@2224
|
1635 audioObjectId = this.audioObjects.length;
|
nicholas@2224
|
1636 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
|
nicholas@2224
|
1637
|
nicholas@2224
|
1638 // Check if audioObject buffer is currently stored by full URL
|
nicholas@2224
|
1639 var URL = testState.currentStateMap.hostURL + element.url;
|
nicholas@2224
|
1640 var buffer = null;
|
nicholas@2224
|
1641 for (var i=0; i<this.buffers.length; i++)
|
nicholas@2224
|
1642 {
|
nicholas@2224
|
1643 if (URL == this.buffers[i].url)
|
nicholas@2224
|
1644 {
|
nicholas@2224
|
1645 buffer = this.buffers[i];
|
nicholas@2224
|
1646 break;
|
nicholas@2224
|
1647 }
|
nicholas@2224
|
1648 }
|
nicholas@2224
|
1649 if (buffer == null)
|
nicholas@2224
|
1650 {
|
nicholas@2224
|
1651 console.log("[WARN]: Buffer was not loaded in pre-test! "+URL);
|
nicholas@2224
|
1652 buffer = new this.bufferObj();
|
nicholas@2224
|
1653 this.buffers.push(buffer);
|
nicholas@2224
|
1654 buffer.getMedia(URL);
|
nicholas@2224
|
1655 }
|
nicholas@2224
|
1656 this.audioObjects[audioObjectId].specification = element;
|
nicholas@2224
|
1657 this.audioObjects[audioObjectId].url = URL;
|
nicholas@2224
|
1658 // Obtain store node
|
nicholas@2224
|
1659 var aeNodes = this.pageStore.XMLDOM.getElementsByTagName('audioelement');
|
nicholas@2224
|
1660 for (var i=0; i<aeNodes.length; i++)
|
nicholas@2224
|
1661 {
|
nicholas@2224
|
1662 if(aeNodes[i].getAttribute("ref") == element.id)
|
nicholas@2224
|
1663 {
|
nicholas@2224
|
1664 this.audioObjects[audioObjectId].storeDOM = aeNodes[i];
|
nicholas@2224
|
1665 break;
|
nicholas@2224
|
1666 }
|
nicholas@2224
|
1667 }
|
nicholas@2224
|
1668 buffer.registerAudioObject(this.audioObjects[audioObjectId]);
|
nicholas@2224
|
1669 return this.audioObjects[audioObjectId];
|
nicholas@2224
|
1670 };
|
nicholas@2224
|
1671
|
nicholas@2224
|
1672 this.newTestPage = function(audioHolderObject,store) {
|
nicholas@2224
|
1673 this.pageStore = store;
|
nicholas@2351
|
1674 this.pageSpecification = audioHolderObject;
|
nicholas@2224
|
1675 this.status = 0;
|
nicholas@2224
|
1676 this.audioObjectsReady = false;
|
nicholas@2224
|
1677 this.metric.reset();
|
nicholas@2224
|
1678 for (var i=0; i < this.buffers.length; i++)
|
nicholas@2224
|
1679 {
|
nicholas@2224
|
1680 this.buffers[i].users = [];
|
nicholas@2224
|
1681 }
|
nicholas@2224
|
1682 this.audioObjects = [];
|
nicholas@2224
|
1683 this.timer = new timer();
|
nicholas@2224
|
1684 this.loopPlayback = audioHolderObject.loop;
|
nicholas@2351
|
1685 this.synchPlayback = audioHolderObject.synchronous;
|
nicholas@2224
|
1686 };
|
nicholas@2224
|
1687
|
nicholas@2224
|
1688 this.checkAllPlayed = function() {
|
nicholas@2224
|
1689 arr = [];
|
nicholas@2224
|
1690 for (var id=0; id<this.audioObjects.length; id++) {
|
nicholas@2224
|
1691 if (this.audioObjects[id].metric.wasListenedTo == false) {
|
nicholas@2224
|
1692 arr.push(this.audioObjects[id].id);
|
nicholas@2224
|
1693 }
|
nicholas@2224
|
1694 }
|
nicholas@2224
|
1695 return arr;
|
nicholas@2224
|
1696 };
|
nicholas@2224
|
1697
|
nicholas@2224
|
1698 this.checkAllReady = function() {
|
nicholas@2224
|
1699 var ready = true;
|
nicholas@2224
|
1700 for (var i=0; i<this.audioObjects.length; i++) {
|
nicholas@2224
|
1701 if (this.audioObjects[i].state == 0) {
|
nicholas@2224
|
1702 // Track not ready
|
nicholas@2224
|
1703 console.log('WAIT -- audioObject '+i+' not ready yet!');
|
nicholas@2224
|
1704 ready = false;
|
nicholas@2224
|
1705 };
|
nicholas@2224
|
1706 }
|
nicholas@2224
|
1707 return ready;
|
nicholas@2224
|
1708 };
|
nicholas@2224
|
1709
|
nicholas@2224
|
1710 this.setSynchronousLoop = function() {
|
nicholas@2224
|
1711 // Pads the signals so they are all exactly the same length
|
nicholas@2224
|
1712 // Get the length of the longest signal.
|
nicholas@2224
|
1713 var length = 0;
|
nicholas@2224
|
1714 var maxId;
|
nicholas@2224
|
1715 for (var i=0; i<this.audioObjects.length; i++)
|
nicholas@2224
|
1716 {
|
nicholas@2224
|
1717 if (length < this.audioObjects[i].buffer.buffer.length)
|
nicholas@2224
|
1718 {
|
nicholas@2224
|
1719 length = this.audioObjects[i].buffer.buffer.length;
|
nicholas@2224
|
1720 maxId = i;
|
nicholas@2224
|
1721 }
|
nicholas@2224
|
1722 }
|
nicholas@2224
|
1723 // Extract the audio and zero-pad
|
nicholas@2224
|
1724 for (var ao of this.audioObjects)
|
nicholas@2224
|
1725 {
|
nicholas@2224
|
1726 var lengthDiff = length - ao.buffer.buffer.length;
|
nicholas@2224
|
1727 ao.buffer = ao.buffer.copyBuffer(0,samplesToSeconds(lengthDiff,ao.buffer.buffer.sampleRate));
|
nicholas@2224
|
1728 }
|
nicholas@2224
|
1729 };
|
nicholas@2224
|
1730
|
nicholas@2460
|
1731 this.bufferReady = function(id) {
|
nicholas@2460
|
1732 if(this.checkAllReady()) {
|
nicholas@2460
|
1733 if (this.synchPlayback){this.setSynchronousLoop();}
|
nicholas@2460
|
1734 this.status = 1;
|
nicholas@2460
|
1735 return true;
|
nicholas@2460
|
1736 }
|
nicholas@2460
|
1737 return false;
|
nicholas@2460
|
1738 }
|
nicholas@2460
|
1739
|
nicholas@2224
|
1740 this.exportXML = function()
|
nicholas@2224
|
1741 {
|
nicholas@2224
|
1742
|
nicholas@2224
|
1743 };
|
nicholas@2224
|
1744
|
nicholas@2224
|
1745 }
|
nicholas@2224
|
1746
|
nicholas@2224
|
1747 function audioObject(id) {
|
nicholas@2224
|
1748 // The main buffer object with common control nodes to the AudioEngine
|
nicholas@2224
|
1749
|
nicholas@2224
|
1750 this.specification;
|
nicholas@2224
|
1751 this.id = id;
|
nicholas@2224
|
1752 this.state = 0; // 0 - no data, 1 - ready
|
nicholas@2224
|
1753 this.url = null; // Hold the URL given for the output back to the results.
|
nicholas@2224
|
1754 this.metric = new metricTracker(this);
|
nicholas@2224
|
1755 this.storeDOM = null;
|
nicholas@2224
|
1756
|
nicholas@2224
|
1757 // Bindings for GUI
|
nicholas@2224
|
1758 this.interfaceDOM = null;
|
nicholas@2224
|
1759 this.commentDOM = null;
|
nicholas@2224
|
1760
|
nicholas@2224
|
1761 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
|
nicholas@2224
|
1762 this.bufferNode = undefined;
|
nicholas@2224
|
1763 this.outputGain = audioContext.createGain();
|
nicholas@2224
|
1764
|
nicholas@2224
|
1765 this.onplayGain = 1.0;
|
nicholas@2224
|
1766
|
nicholas@2224
|
1767 // Connect buffer to the audio graph
|
nicholas@2224
|
1768 this.outputGain.connect(audioEngineContext.outputGain);
|
nicholas@2224
|
1769
|
nicholas@2224
|
1770 // the audiobuffer is not designed for multi-start playback
|
nicholas@2224
|
1771 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
|
nicholas@2224
|
1772 this.buffer;
|
nicholas@2224
|
1773
|
nicholas@2224
|
1774 this.bufferLoaded = function(callee)
|
nicholas@2224
|
1775 {
|
nicholas@2224
|
1776 // Called by the associated buffer when it has finished loading, will then 'bind' the buffer to the
|
nicholas@2224
|
1777 // audioObject and trigger the interfaceDOM.enable() function for user feedback
|
nicholas@2224
|
1778 if (callee.status == -1) {
|
nicholas@2224
|
1779 // ERROR
|
nicholas@2224
|
1780 this.state = -1;
|
nicholas@2224
|
1781 if (this.interfaceDOM != null) {this.interfaceDOM.error();}
|
nicholas@2224
|
1782 this.buffer = callee;
|
nicholas@2224
|
1783 return;
|
nicholas@2224
|
1784 }
|
nicholas@2224
|
1785 this.buffer = callee;
|
nicholas@2224
|
1786 var preSilenceTime = this.specification.preSilence || this.specification.parent.preSilence || specification.preSilence || 0.0;
|
nicholas@2224
|
1787 var postSilenceTime = this.specification.postSilence || this.specification.parent.postSilence || specification.postSilence || 0.0;
|
nicholas@2460
|
1788 var startTime = this.specification.startTime;
|
nicholas@2460
|
1789 var stopTime = this.specification.stopTime;
|
nicholas@2460
|
1790 var copybuffer = new callee.constructor();
|
nicholas@2460
|
1791 if (isFinite(startTime) || isFinite(stopTime)) {
|
nicholas@2460
|
1792 copybuffer.buffer = callee.cropBuffer(startTime,stopTime);
|
nicholas@2460
|
1793 }
|
nicholas@2224
|
1794 if (preSilenceTime != 0 || postSilenceTime != 0) {
|
nicholas@2460
|
1795 if (copybuffer.buffer == undefined) {
|
nicholas@2460
|
1796 copybuffer.buffer = callee.copyBuffer(preSilenceTime,postSilenceTime);
|
nicholas@2460
|
1797 } else {
|
nicholas@2460
|
1798 copybuffer.buffer = copybuffer.copyBuffer(preSilenceTime,postSilenceTime);
|
nicholas@2460
|
1799 }
|
nicholas@2224
|
1800 }
|
nicholas@2460
|
1801
|
nicholas@2224
|
1802 var targetLUFS = this.specification.parent.loudness || specification.loudness;
|
nicholas@2460
|
1803 if (typeof targetLUFS === "number" && isFinite(targetLUFS))
|
nicholas@2224
|
1804 {
|
nicholas@2224
|
1805 this.buffer.buffer.playbackGain = decibelToLinear(targetLUFS - this.buffer.buffer.lufs);
|
nicholas@2224
|
1806 } else {
|
nicholas@2224
|
1807 this.buffer.buffer.playbackGain = 1.0;
|
nicholas@2224
|
1808 }
|
nicholas@2224
|
1809 if (this.interfaceDOM != null) {
|
nicholas@2224
|
1810 this.interfaceDOM.enable();
|
nicholas@2224
|
1811 }
|
n@2433
|
1812 this.onplayGain = decibelToLinear(this.specification.gain)*(this.buffer.buffer.playbackGain||1.0);
|
nicholas@2224
|
1813 this.storeDOM.setAttribute('playGain',linearToDecibel(this.onplayGain));
|
nicholas@2460
|
1814 this.state = 1;
|
nicholas@2460
|
1815 audioEngineContext.bufferReady(id);
|
nicholas@2224
|
1816 };
|
nicholas@2224
|
1817
|
nicholas@2224
|
1818 this.bindInterface = function(interfaceObject)
|
nicholas@2224
|
1819 {
|
nicholas@2224
|
1820 this.interfaceDOM = interfaceObject;
|
nicholas@2224
|
1821 this.metric.initialise(interfaceObject.getValue());
|
nicholas@2224
|
1822 if (this.state == 1)
|
nicholas@2224
|
1823 {
|
nicholas@2224
|
1824 this.interfaceDOM.enable();
|
nicholas@2224
|
1825 } else if (this.state == -1) {
|
nicholas@2224
|
1826 // ERROR
|
nicholas@2224
|
1827 this.interfaceDOM.error();
|
nicholas@2224
|
1828 return;
|
nicholas@2224
|
1829 }
|
nicholas@2224
|
1830 this.storeDOM.setAttribute('presentedId',interfaceObject.getPresentedId());
|
nicholas@2224
|
1831 };
|
nicholas@2224
|
1832
|
nicholas@2224
|
1833 this.loopStart = function(setTime) {
|
nicholas@2224
|
1834 this.outputGain.gain.linearRampToValueAtTime(this.onplayGain,setTime);
|
nicholas@2224
|
1835 this.metric.startListening(audioEngineContext.timer.getTestTime());
|
nicholas@2224
|
1836 this.interfaceDOM.startPlayback();
|
nicholas@2224
|
1837 };
|
nicholas@2224
|
1838
|
nicholas@2224
|
1839 this.loopStop = function(setTime) {
|
nicholas@2224
|
1840 if (this.outputGain.gain.value != 0.0) {
|
nicholas@2224
|
1841 this.outputGain.gain.linearRampToValueAtTime(0.0,setTime);
|
nicholas@2224
|
1842 this.metric.stopListening(audioEngineContext.timer.getTestTime());
|
nicholas@2224
|
1843 }
|
nicholas@2224
|
1844 this.interfaceDOM.stopPlayback();
|
nicholas@2224
|
1845 };
|
nicholas@2224
|
1846
|
nicholas@2224
|
1847 this.play = function(startTime) {
|
nicholas@2224
|
1848 if (this.bufferNode == undefined && this.buffer.buffer != undefined) {
|
nicholas@2224
|
1849 this.bufferNode = audioContext.createBufferSource();
|
nicholas@2224
|
1850 this.bufferNode.owner = this;
|
nicholas@2224
|
1851 this.bufferNode.connect(this.outputGain);
|
nicholas@2224
|
1852 this.bufferNode.buffer = this.buffer.buffer;
|
nicholas@2224
|
1853 this.bufferNode.loop = audioEngineContext.loopPlayback;
|
nicholas@2224
|
1854 this.bufferNode.onended = function(event) {
|
nicholas@2224
|
1855 // Safari does not like using 'this' to reference the calling object!
|
nicholas@2224
|
1856 //event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition());
|
nicholas@2224
|
1857 if (event.currentTarget != null) {
|
nicholas@2224
|
1858 event.currentTarget.owner.stop(audioContext.currentTime+1);
|
nicholas@2224
|
1859 }
|
nicholas@2224
|
1860 };
|
nicholas@2351
|
1861 if (!audioEngineContext.loopPlayback || !audioEngineContext.synchPlayback) {
|
nicholas@2224
|
1862 this.metric.startListening(audioEngineContext.timer.getTestTime());
|
n@2434
|
1863 this.outputGain.gain.setValueAtTime(this.onplayGain,0.0);
|
nicholas@2224
|
1864 this.interfaceDOM.startPlayback();
|
nicholas@2224
|
1865 } else {
|
nicholas@2224
|
1866 this.outputGain.gain.setValueAtTime(0.0,startTime);
|
nicholas@2224
|
1867 }
|
nicholas@2408
|
1868 this.bufferNode.start(startTime,this.specification.startTime || 0, this.specification.stopTime-this.specification.startTime || this.buffer.buffer.duration);
|
nicholas@2224
|
1869 this.bufferNode.playbackStartTime = audioEngineContext.timer.getTestTime();
|
nicholas@2224
|
1870 }
|
nicholas@2224
|
1871 };
|
nicholas@2224
|
1872
|
nicholas@2224
|
1873 this.stop = function(stopTime) {
|
nicholas@2224
|
1874 this.outputGain.gain.cancelScheduledValues(audioContext.currentTime);
|
nicholas@2224
|
1875 if (this.bufferNode != undefined)
|
nicholas@2224
|
1876 {
|
nicholas@2224
|
1877 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition());
|
nicholas@2224
|
1878 this.bufferNode.stop(stopTime);
|
nicholas@2224
|
1879 this.bufferNode = undefined;
|
nicholas@2224
|
1880 }
|
nicholas@2224
|
1881 this.outputGain.gain.value = 0.0;
|
nicholas@2224
|
1882 this.interfaceDOM.stopPlayback();
|
nicholas@2224
|
1883 };
|
nicholas@2224
|
1884
|
nicholas@2224
|
1885 this.getCurrentPosition = function() {
|
nicholas@2224
|
1886 var time = audioEngineContext.timer.getTestTime();
|
nicholas@2224
|
1887 if (this.bufferNode != undefined) {
|
nicholas@2224
|
1888 var position = (time - this.bufferNode.playbackStartTime)%this.buffer.buffer.duration;
|
nicholas@2224
|
1889 if (isNaN(position)){return 0;}
|
nicholas@2224
|
1890 return position;
|
nicholas@2224
|
1891 } else {
|
nicholas@2224
|
1892 return 0;
|
nicholas@2224
|
1893 }
|
nicholas@2224
|
1894 };
|
nicholas@2224
|
1895
|
nicholas@2224
|
1896 this.exportXMLDOM = function() {
|
nicholas@2224
|
1897 var file = storage.document.createElement('file');
|
nicholas@2224
|
1898 file.setAttribute('sampleRate',this.buffer.buffer.sampleRate);
|
nicholas@2224
|
1899 file.setAttribute('channels',this.buffer.buffer.numberOfChannels);
|
nicholas@2224
|
1900 file.setAttribute('sampleCount',this.buffer.buffer.length);
|
nicholas@2224
|
1901 file.setAttribute('duration',this.buffer.buffer.duration);
|
nicholas@2224
|
1902 this.storeDOM.appendChild(file);
|
nicholas@2224
|
1903 if (this.specification.type != 'outside-reference') {
|
nicholas@2224
|
1904 var interfaceXML = this.interfaceDOM.exportXMLDOM(this);
|
nicholas@2224
|
1905 if (interfaceXML != null)
|
nicholas@2224
|
1906 {
|
nicholas@2224
|
1907 if (interfaceXML.length == undefined) {
|
nicholas@2224
|
1908 this.storeDOM.appendChild(interfaceXML);
|
nicholas@2224
|
1909 } else {
|
nicholas@2224
|
1910 for (var i=0; i<interfaceXML.length; i++)
|
nicholas@2224
|
1911 {
|
nicholas@2224
|
1912 this.storeDOM.appendChild(interfaceXML[i]);
|
nicholas@2224
|
1913 }
|
nicholas@2224
|
1914 }
|
nicholas@2224
|
1915 }
|
nicholas@2224
|
1916 if (this.commentDOM != null) {
|
nicholas@2224
|
1917 this.storeDOM.appendChild(this.commentDOM.exportXMLDOM(this));
|
nicholas@2224
|
1918 }
|
nicholas@2224
|
1919 }
|
nicholas@2224
|
1920 var nodes = this.metric.exportXMLDOM();
|
nicholas@2224
|
1921 var mroot = this.storeDOM.getElementsByTagName('metric')[0];
|
nicholas@2224
|
1922 for (var i=0; i<nodes.length; i++)
|
nicholas@2224
|
1923 {
|
nicholas@2224
|
1924 mroot.appendChild(nodes[i]);
|
nicholas@2224
|
1925 }
|
nicholas@2224
|
1926 };
|
nicholas@2224
|
1927 }
|
nicholas@2224
|
1928
|
nicholas@2224
|
1929 function timer()
|
nicholas@2224
|
1930 {
|
nicholas@2224
|
1931 /* Timer object used in audioEngine to keep track of session timings
|
nicholas@2224
|
1932 * Uses the timer of the web audio API, so sample resolution
|
nicholas@2224
|
1933 */
|
nicholas@2224
|
1934 this.testStarted = false;
|
nicholas@2224
|
1935 this.testStartTime = 0;
|
nicholas@2224
|
1936 this.testDuration = 0;
|
nicholas@2224
|
1937 this.minimumTestTime = 0; // No minimum test time
|
nicholas@2224
|
1938 this.startTest = function()
|
nicholas@2224
|
1939 {
|
nicholas@2224
|
1940 if (this.testStarted == false)
|
nicholas@2224
|
1941 {
|
nicholas@2224
|
1942 this.testStartTime = audioContext.currentTime;
|
nicholas@2224
|
1943 this.testStarted = true;
|
nicholas@2224
|
1944 this.updateTestTime();
|
nicholas@2224
|
1945 audioEngineContext.metric.initialiseTest();
|
nicholas@2224
|
1946 }
|
nicholas@2224
|
1947 };
|
nicholas@2224
|
1948 this.stopTest = function()
|
nicholas@2224
|
1949 {
|
nicholas@2224
|
1950 if (this.testStarted)
|
nicholas@2224
|
1951 {
|
nicholas@2224
|
1952 this.testDuration = this.getTestTime();
|
nicholas@2224
|
1953 this.testStarted = false;
|
nicholas@2224
|
1954 } else {
|
nicholas@2224
|
1955 console.log('ERR: Test tried to end before beginning');
|
nicholas@2224
|
1956 }
|
nicholas@2224
|
1957 };
|
nicholas@2224
|
1958 this.updateTestTime = function()
|
nicholas@2224
|
1959 {
|
nicholas@2224
|
1960 if (this.testStarted)
|
nicholas@2224
|
1961 {
|
nicholas@2224
|
1962 this.testDuration = audioContext.currentTime - this.testStartTime;
|
nicholas@2224
|
1963 }
|
nicholas@2224
|
1964 };
|
nicholas@2224
|
1965 this.getTestTime = function()
|
nicholas@2224
|
1966 {
|
nicholas@2224
|
1967 this.updateTestTime();
|
nicholas@2224
|
1968 return this.testDuration;
|
nicholas@2224
|
1969 };
|
nicholas@2224
|
1970 }
|
nicholas@2224
|
1971
|
nicholas@2224
|
1972 function sessionMetrics(engine,specification)
|
nicholas@2224
|
1973 {
|
nicholas@2224
|
1974 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
|
nicholas@2224
|
1975 */
|
nicholas@2224
|
1976 this.engine = engine;
|
nicholas@2224
|
1977 this.lastClicked = -1;
|
nicholas@2224
|
1978 this.data = -1;
|
nicholas@2224
|
1979 this.reset = function() {
|
nicholas@2224
|
1980 this.lastClicked = -1;
|
nicholas@2224
|
1981 this.data = -1;
|
nicholas@2224
|
1982 };
|
nicholas@2224
|
1983
|
nicholas@2224
|
1984 this.enableElementInitialPosition = false;
|
nicholas@2224
|
1985 this.enableElementListenTracker = false;
|
nicholas@2224
|
1986 this.enableElementTimer = false;
|
nicholas@2224
|
1987 this.enableElementTracker = false;
|
nicholas@2224
|
1988 this.enableFlagListenedTo = false;
|
nicholas@2224
|
1989 this.enableFlagMoved = false;
|
nicholas@2224
|
1990 this.enableTestTimer = false;
|
nicholas@2224
|
1991 // Obtain the metrics enabled
|
nicholas@2224
|
1992 for (var i=0; i<specification.metrics.enabled.length; i++)
|
nicholas@2224
|
1993 {
|
nicholas@2224
|
1994 var node = specification.metrics.enabled[i];
|
nicholas@2224
|
1995 switch(node)
|
nicholas@2224
|
1996 {
|
nicholas@2224
|
1997 case 'testTimer':
|
nicholas@2224
|
1998 this.enableTestTimer = true;
|
nicholas@2224
|
1999 break;
|
nicholas@2224
|
2000 case 'elementTimer':
|
nicholas@2224
|
2001 this.enableElementTimer = true;
|
nicholas@2224
|
2002 break;
|
nicholas@2224
|
2003 case 'elementTracker':
|
nicholas@2224
|
2004 this.enableElementTracker = true;
|
nicholas@2224
|
2005 break;
|
nicholas@2224
|
2006 case 'elementListenTracker':
|
nicholas@2224
|
2007 this.enableElementListenTracker = true;
|
nicholas@2224
|
2008 break;
|
nicholas@2224
|
2009 case 'elementInitialPosition':
|
nicholas@2224
|
2010 this.enableElementInitialPosition = true;
|
nicholas@2224
|
2011 break;
|
nicholas@2224
|
2012 case 'elementFlagListenedTo':
|
nicholas@2224
|
2013 this.enableFlagListenedTo = true;
|
nicholas@2224
|
2014 break;
|
nicholas@2224
|
2015 case 'elementFlagMoved':
|
nicholas@2224
|
2016 this.enableFlagMoved = true;
|
nicholas@2224
|
2017 break;
|
nicholas@2224
|
2018 case 'elementFlagComments':
|
nicholas@2224
|
2019 this.enableFlagComments = true;
|
nicholas@2224
|
2020 break;
|
nicholas@2224
|
2021 }
|
nicholas@2224
|
2022 }
|
nicholas@2224
|
2023 this.initialiseTest = function(){};
|
nicholas@2224
|
2024 }
|
nicholas@2224
|
2025
|
nicholas@2224
|
2026 function metricTracker(caller)
|
nicholas@2224
|
2027 {
|
nicholas@2224
|
2028 /* Custom object to track and collect metric data
|
nicholas@2224
|
2029 * Used only inside the audioObjects object.
|
nicholas@2224
|
2030 */
|
nicholas@2224
|
2031
|
nicholas@2224
|
2032 this.listenedTimer = 0;
|
nicholas@2224
|
2033 this.listenStart = 0;
|
nicholas@2224
|
2034 this.listenHold = false;
|
nicholas@2224
|
2035 this.initialPosition = -1;
|
nicholas@2224
|
2036 this.movementTracker = [];
|
nicholas@2224
|
2037 this.listenTracker =[];
|
nicholas@2224
|
2038 this.wasListenedTo = false;
|
nicholas@2224
|
2039 this.wasMoved = false;
|
nicholas@2224
|
2040 this.hasComments = false;
|
nicholas@2224
|
2041 this.parent = caller;
|
nicholas@2224
|
2042
|
nicholas@2224
|
2043 this.initialise = function(position)
|
nicholas@2224
|
2044 {
|
nicholas@2224
|
2045 if (this.initialPosition == -1) {
|
nicholas@2224
|
2046 this.initialPosition = position;
|
nicholas@2224
|
2047 this.moved(0,position);
|
nicholas@2224
|
2048 }
|
nicholas@2224
|
2049 };
|
nicholas@2224
|
2050
|
nicholas@2224
|
2051 this.moved = function(time,position)
|
nicholas@2224
|
2052 {
|
nicholas@2224
|
2053 if (time > 0) {this.wasMoved = true;}
|
nicholas@2224
|
2054 this.movementTracker[this.movementTracker.length] = [time, position];
|
nicholas@2224
|
2055 };
|
nicholas@2224
|
2056
|
nicholas@2224
|
2057 this.startListening = function(time)
|
nicholas@2224
|
2058 {
|
nicholas@2224
|
2059 if (this.listenHold == false)
|
nicholas@2224
|
2060 {
|
nicholas@2224
|
2061 this.wasListenedTo = true;
|
nicholas@2224
|
2062 this.listenStart = time;
|
nicholas@2224
|
2063 this.listenHold = true;
|
nicholas@2224
|
2064
|
nicholas@2224
|
2065 var evnt = document.createElement('event');
|
nicholas@2224
|
2066 var testTime = document.createElement('testTime');
|
nicholas@2224
|
2067 testTime.setAttribute('start',time);
|
nicholas@2224
|
2068 var bufferTime = document.createElement('bufferTime');
|
nicholas@2224
|
2069 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
|
nicholas@2224
|
2070 evnt.appendChild(testTime);
|
nicholas@2224
|
2071 evnt.appendChild(bufferTime);
|
nicholas@2224
|
2072 this.listenTracker.push(evnt);
|
nicholas@2224
|
2073
|
nicholas@2224
|
2074 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
|
nicholas@2224
|
2075 }
|
nicholas@2224
|
2076 };
|
nicholas@2224
|
2077
|
nicholas@2224
|
2078 this.stopListening = function(time,bufferStopTime)
|
nicholas@2224
|
2079 {
|
nicholas@2224
|
2080 if (this.listenHold == true)
|
nicholas@2224
|
2081 {
|
nicholas@2224
|
2082 var diff = time - this.listenStart;
|
nicholas@2224
|
2083 this.listenedTimer += (diff);
|
nicholas@2224
|
2084 this.listenStart = 0;
|
nicholas@2224
|
2085 this.listenHold = false;
|
nicholas@2224
|
2086
|
nicholas@2224
|
2087 var evnt = this.listenTracker[this.listenTracker.length-1];
|
nicholas@2224
|
2088 var testTime = evnt.getElementsByTagName('testTime')[0];
|
nicholas@2224
|
2089 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
|
nicholas@2224
|
2090 testTime.setAttribute('stop',time);
|
nicholas@2224
|
2091 if (bufferStopTime == undefined) {
|
nicholas@2224
|
2092 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
|
nicholas@2224
|
2093 } else {
|
nicholas@2224
|
2094 bufferTime.setAttribute('stop',bufferStopTime);
|
nicholas@2224
|
2095 }
|
nicholas@2224
|
2096 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
|
nicholas@2224
|
2097 }
|
nicholas@2224
|
2098 };
|
nicholas@2224
|
2099
|
nicholas@2224
|
2100 this.exportXMLDOM = function() {
|
nicholas@2224
|
2101 var storeDOM = [];
|
nicholas@2224
|
2102 if (audioEngineContext.metric.enableElementTimer) {
|
nicholas@2224
|
2103 var mElementTimer = storage.document.createElement('metricresult');
|
nicholas@2224
|
2104 mElementTimer.setAttribute('name','enableElementTimer');
|
nicholas@2224
|
2105 mElementTimer.textContent = this.listenedTimer;
|
nicholas@2224
|
2106 storeDOM.push(mElementTimer);
|
nicholas@2224
|
2107 }
|
nicholas@2224
|
2108 if (audioEngineContext.metric.enableElementTracker) {
|
b@2281
|
2109 var elementTrackerFull = storage.document.createElement('metricresult');
|
nicholas@2224
|
2110 elementTrackerFull.setAttribute('name','elementTrackerFull');
|
nicholas@2224
|
2111 for (var k=0; k<this.movementTracker.length; k++)
|
nicholas@2224
|
2112 {
|
nicholas@2224
|
2113 var timePos = storage.document.createElement('movement');
|
nicholas@2224
|
2114 timePos.setAttribute("time",this.movementTracker[k][0]);
|
nicholas@2224
|
2115 timePos.setAttribute("value",this.movementTracker[k][1]);
|
nicholas@2224
|
2116 elementTrackerFull.appendChild(timePos);
|
nicholas@2224
|
2117 }
|
nicholas@2224
|
2118 storeDOM.push(elementTrackerFull);
|
nicholas@2224
|
2119 }
|
nicholas@2224
|
2120 if (audioEngineContext.metric.enableElementListenTracker) {
|
b@2281
|
2121 var elementListenTracker = storage.document.createElement('metricresult');
|
nicholas@2224
|
2122 elementListenTracker.setAttribute('name','elementListenTracker');
|
nicholas@2224
|
2123 for (var k=0; k<this.listenTracker.length; k++) {
|
nicholas@2224
|
2124 elementListenTracker.appendChild(this.listenTracker[k]);
|
nicholas@2224
|
2125 }
|
nicholas@2224
|
2126 storeDOM.push(elementListenTracker);
|
nicholas@2224
|
2127 }
|
nicholas@2224
|
2128 if (audioEngineContext.metric.enableElementInitialPosition) {
|
b@2281
|
2129 var elementInitial = storage.document.createElement('metricresult');
|
nicholas@2224
|
2130 elementInitial.setAttribute('name','elementInitialPosition');
|
nicholas@2224
|
2131 elementInitial.textContent = this.initialPosition;
|
nicholas@2224
|
2132 storeDOM.push(elementInitial);
|
nicholas@2224
|
2133 }
|
nicholas@2224
|
2134 if (audioEngineContext.metric.enableFlagListenedTo) {
|
b@2281
|
2135 var flagListenedTo = storage.document.createElement('metricresult');
|
nicholas@2224
|
2136 flagListenedTo.setAttribute('name','elementFlagListenedTo');
|
nicholas@2224
|
2137 flagListenedTo.textContent = this.wasListenedTo;
|
nicholas@2224
|
2138 storeDOM.push(flagListenedTo);
|
nicholas@2224
|
2139 }
|
nicholas@2224
|
2140 if (audioEngineContext.metric.enableFlagMoved) {
|
b@2281
|
2141 var flagMoved = storage.document.createElement('metricresult');
|
nicholas@2224
|
2142 flagMoved.setAttribute('name','elementFlagMoved');
|
nicholas@2224
|
2143 flagMoved.textContent = this.wasMoved;
|
nicholas@2224
|
2144 storeDOM.push(flagMoved);
|
nicholas@2224
|
2145 }
|
nicholas@2224
|
2146 if (audioEngineContext.metric.enableFlagComments) {
|
b@2281
|
2147 var flagComments = storage.document.createElement('metricresult');
|
nicholas@2224
|
2148 flagComments.setAttribute('name','elementFlagComments');
|
nicholas@2224
|
2149 if (this.parent.commentDOM == null)
|
nicholas@2224
|
2150 {flag.textContent = 'false';}
|
nicholas@2224
|
2151 else if (this.parent.commentDOM.textContent.length == 0)
|
nicholas@2224
|
2152 {flag.textContent = 'false';}
|
nicholas@2224
|
2153 else
|
nicholas@2224
|
2154 {flag.textContet = 'true';}
|
nicholas@2224
|
2155 storeDOM.push(flagComments);
|
nicholas@2224
|
2156 }
|
nicholas@2224
|
2157 return storeDOM;
|
nicholas@2224
|
2158 };
|
nicholas@2224
|
2159 }
|
nicholas@2224
|
2160
|
nicholas@2224
|
2161 function Interface(specificationObject) {
|
nicholas@2224
|
2162 // This handles the bindings between the interface and the audioEngineContext;
|
nicholas@2224
|
2163 this.specification = specificationObject;
|
nicholas@2224
|
2164 this.insertPoint = document.getElementById("topLevelBody");
|
nicholas@2224
|
2165
|
nicholas@2224
|
2166 this.newPage = function(audioHolderObject,store)
|
nicholas@2224
|
2167 {
|
nicholas@2224
|
2168 audioEngineContext.newTestPage(audioHolderObject,store);
|
nicholas@2224
|
2169 interfaceContext.commentBoxes.deleteCommentBoxes();
|
nicholas@2224
|
2170 interfaceContext.deleteCommentQuestions();
|
nicholas@2224
|
2171 loadTest(audioHolderObject,store);
|
nicholas@2224
|
2172 };
|
nicholas@2224
|
2173
|
nicholas@2224
|
2174 // Bounded by interface!!
|
nicholas@2224
|
2175 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
|
nicholas@2224
|
2176 // For example, APE returns the slider position normalised in a <value> tag.
|
nicholas@2224
|
2177 this.interfaceObjects = [];
|
nicholas@2224
|
2178 this.interfaceObject = function(){};
|
nicholas@2224
|
2179
|
nicholas@2224
|
2180 this.resizeWindow = function(event)
|
nicholas@2224
|
2181 {
|
nicholas@2224
|
2182 popup.resize(event);
|
nicholas@2352
|
2183 this.volume.resize();
|
nicholas@2360
|
2184 this.lightbox.resize();
|
nicholas@2224
|
2185 for(var i=0; i<this.commentBoxes.length; i++)
|
nicholas@2224
|
2186 {this.commentBoxes[i].resize();}
|
nicholas@2224
|
2187 for(var i=0; i<this.commentQuestions.length; i++)
|
nicholas@2224
|
2188 {this.commentQuestions[i].resize();}
|
nicholas@2224
|
2189 try
|
nicholas@2224
|
2190 {
|
nicholas@2224
|
2191 resizeWindow(event);
|
nicholas@2224
|
2192 }
|
nicholas@2224
|
2193 catch(err)
|
nicholas@2224
|
2194 {
|
nicholas@2224
|
2195 console.log("Warning - Interface does not have Resize option");
|
nicholas@2224
|
2196 console.log(err);
|
nicholas@2224
|
2197 }
|
nicholas@2224
|
2198 };
|
nicholas@2224
|
2199
|
nicholas@2224
|
2200 this.returnNavigator = function()
|
nicholas@2224
|
2201 {
|
nicholas@2224
|
2202 var node = storage.document.createElement("navigator");
|
nicholas@2224
|
2203 var platform = storage.document.createElement("platform");
|
nicholas@2224
|
2204 platform.textContent = navigator.platform;
|
nicholas@2224
|
2205 var vendor = storage.document.createElement("vendor");
|
nicholas@2224
|
2206 vendor.textContent = navigator.vendor;
|
nicholas@2224
|
2207 var userAgent = storage.document.createElement("uagent");
|
nicholas@2224
|
2208 userAgent.textContent = navigator.userAgent;
|
nicholas@2224
|
2209 var screen = storage.document.createElement("window");
|
nicholas@2224
|
2210 screen.setAttribute('innerWidth',window.innerWidth);
|
nicholas@2224
|
2211 screen.setAttribute('innerHeight',window.innerHeight);
|
nicholas@2224
|
2212 node.appendChild(platform);
|
nicholas@2224
|
2213 node.appendChild(vendor);
|
nicholas@2224
|
2214 node.appendChild(userAgent);
|
nicholas@2224
|
2215 node.appendChild(screen);
|
nicholas@2224
|
2216 return node;
|
nicholas@2224
|
2217 };
|
nicholas@2224
|
2218
|
nicholas@2224
|
2219 this.returnDateNode = function()
|
nicholas@2224
|
2220 {
|
nicholas@2224
|
2221 // Create an XML Node for the Date and Time a test was conducted
|
nicholas@2224
|
2222 // Structure is
|
nicholas@2224
|
2223 // <datetime>
|
nicholas@2224
|
2224 // <date year="##" month="##" day="##">DD/MM/YY</date>
|
nicholas@2224
|
2225 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
|
nicholas@2224
|
2226 // </datetime>
|
nicholas@2224
|
2227 var dateTime = new Date();
|
nicholas@2224
|
2228 var hold = storage.document.createElement("datetime");
|
nicholas@2224
|
2229 var date = storage.document.createElement("date");
|
nicholas@2224
|
2230 var time = storage.document.createElement("time");
|
nicholas@2224
|
2231 date.setAttribute('year',dateTime.getFullYear());
|
nicholas@2224
|
2232 date.setAttribute('month',dateTime.getMonth()+1);
|
nicholas@2224
|
2233 date.setAttribute('day',dateTime.getDate());
|
nicholas@2224
|
2234 time.setAttribute('hour',dateTime.getHours());
|
nicholas@2224
|
2235 time.setAttribute('minute',dateTime.getMinutes());
|
nicholas@2224
|
2236 time.setAttribute('secs',dateTime.getSeconds());
|
nicholas@2224
|
2237
|
nicholas@2224
|
2238 hold.appendChild(date);
|
nicholas@2224
|
2239 hold.appendChild(time);
|
nicholas@2224
|
2240 return hold;
|
nicholas@2224
|
2241
|
nicholas@2224
|
2242 }
|
nicholas@2360
|
2243
|
nicholas@2360
|
2244 this.lightbox = {
|
nicholas@2360
|
2245 parent: this,
|
nicholas@2360
|
2246 root: document.createElement("div"),
|
nicholas@2360
|
2247 content: document.createElement("div"),
|
nicholas@2360
|
2248 accept: document.createElement("button"),
|
nicholas@2360
|
2249 blanker: document.createElement("div"),
|
nicholas@2360
|
2250 post: function(type,message) {
|
nicholas@2360
|
2251 switch(type) {
|
nicholas@2360
|
2252 case "Error":
|
nicholas@2360
|
2253 this.content.className = "lightbox-error";
|
nicholas@2360
|
2254 break;
|
nicholas@2360
|
2255 case "Warning":
|
nicholas@2360
|
2256 this.content.className = "lightbox-warning";
|
nicholas@2360
|
2257 break;
|
nicholas@2360
|
2258 default:
|
nicholas@2360
|
2259 this.content.className = "lightbox-message";
|
nicholas@2360
|
2260 break;
|
nicholas@2360
|
2261 }
|
nicholas@2360
|
2262 var msg = document.createElement("p");
|
nicholas@2360
|
2263 msg.textContent = message;
|
nicholas@2360
|
2264 this.content.appendChild(msg);
|
nicholas@2360
|
2265 this.show();
|
nicholas@2360
|
2266 },
|
nicholas@2360
|
2267 show: function() {
|
nicholas@2360
|
2268 this.root.style.visibility = "visible";
|
nicholas@2360
|
2269 this.blanker.style.visibility = "visible";
|
nicholas@2360
|
2270 },
|
nicholas@2360
|
2271 clear: function() {
|
nicholas@2360
|
2272 this.root.style.visibility = "";
|
nicholas@2360
|
2273 this.blanker.style.visibility = "";
|
nicholas@2360
|
2274 this.content.textContent = "";
|
nicholas@2360
|
2275 },
|
nicholas@2360
|
2276 handleEvent: function(event) {
|
nicholas@2360
|
2277 if (event.currentTarget == this.accept) {
|
nicholas@2360
|
2278 this.clear();
|
nicholas@2360
|
2279 }
|
nicholas@2360
|
2280 },
|
nicholas@2360
|
2281 resize: function(event) {
|
nicholas@2360
|
2282 this.root.style.left = (window.innerWidth/2)-250 + 'px';
|
nicholas@2360
|
2283 }
|
nicholas@2360
|
2284 }
|
nicholas@2360
|
2285
|
nicholas@2360
|
2286 this.lightbox.root.appendChild(this.lightbox.content);
|
nicholas@2360
|
2287 this.lightbox.root.appendChild(this.lightbox.accept);
|
nicholas@2360
|
2288 this.lightbox.root.className = "popupHolder";
|
nicholas@2360
|
2289 this.lightbox.root.id = "lightbox-root";
|
nicholas@2360
|
2290 this.lightbox.accept.className = "popupButton";
|
nicholas@2360
|
2291 this.lightbox.accept.style.bottom = "10px";
|
nicholas@2360
|
2292 this.lightbox.accept.textContent = "OK";
|
nicholas@2360
|
2293 this.lightbox.accept.style.left = "237.5px";
|
nicholas@2360
|
2294 this.lightbox.accept.addEventListener("click",this.lightbox);
|
nicholas@2360
|
2295 this.lightbox.blanker.className = "testHalt";
|
nicholas@2360
|
2296 this.lightbox.blanker.id = "lightbox-blanker";
|
nicholas@2360
|
2297 document.getElementsByTagName("body")[0].appendChild(this.lightbox.root);
|
nicholas@2360
|
2298 document.getElementsByTagName("body")[0].appendChild(this.lightbox.blanker);
|
nicholas@2224
|
2299
|
nicholas@2224
|
2300 this.commentBoxes = new function() {
|
nicholas@2224
|
2301 this.boxes = [];
|
nicholas@2224
|
2302 this.injectPoint = null;
|
nicholas@2224
|
2303 this.elementCommentBox = function(audioObject) {
|
nicholas@2224
|
2304 var element = audioObject.specification;
|
nicholas@2224
|
2305 this.audioObject = audioObject;
|
nicholas@2224
|
2306 this.id = audioObject.id;
|
nicholas@2224
|
2307 var audioHolderObject = audioObject.specification.parent;
|
nicholas@2224
|
2308 // Create document objects to hold the comment boxes
|
nicholas@2224
|
2309 this.trackComment = document.createElement('div');
|
nicholas@2224
|
2310 this.trackComment.className = 'comment-div';
|
nicholas@2224
|
2311 this.trackComment.id = 'comment-div-'+audioObject.id;
|
nicholas@2224
|
2312 // Create a string next to each comment asking for a comment
|
nicholas@2224
|
2313 this.trackString = document.createElement('span');
|
nicholas@2224
|
2314 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.interfaceDOM.getPresentedId();
|
nicholas@2224
|
2315 // Create the HTML5 comment box 'textarea'
|
nicholas@2224
|
2316 this.trackCommentBox = document.createElement('textarea');
|
nicholas@2224
|
2317 this.trackCommentBox.rows = '4';
|
nicholas@2224
|
2318 this.trackCommentBox.cols = '100';
|
nicholas@2224
|
2319 this.trackCommentBox.name = 'trackComment'+audioObject.id;
|
nicholas@2224
|
2320 this.trackCommentBox.className = 'trackComment';
|
nicholas@2224
|
2321 var br = document.createElement('br');
|
nicholas@2224
|
2322 // Add to the holder.
|
nicholas@2224
|
2323 this.trackComment.appendChild(this.trackString);
|
nicholas@2224
|
2324 this.trackComment.appendChild(br);
|
nicholas@2224
|
2325 this.trackComment.appendChild(this.trackCommentBox);
|
nicholas@2224
|
2326
|
nicholas@2224
|
2327 this.exportXMLDOM = function() {
|
nicholas@2224
|
2328 var root = document.createElement('comment');
|
nicholas@2224
|
2329 var question = document.createElement('question');
|
nicholas@2224
|
2330 question.textContent = this.trackString.textContent;
|
nicholas@2224
|
2331 var response = document.createElement('response');
|
nicholas@2224
|
2332 response.textContent = this.trackCommentBox.value;
|
nicholas@2224
|
2333 console.log("Comment frag-"+this.id+": "+response.textContent);
|
nicholas@2224
|
2334 root.appendChild(question);
|
nicholas@2224
|
2335 root.appendChild(response);
|
nicholas@2224
|
2336 return root;
|
nicholas@2224
|
2337 };
|
nicholas@2224
|
2338 this.resize = function()
|
nicholas@2224
|
2339 {
|
nicholas@2224
|
2340 var boxwidth = (window.innerWidth-100)/2;
|
nicholas@2224
|
2341 if (boxwidth >= 600)
|
nicholas@2224
|
2342 {
|
nicholas@2224
|
2343 boxwidth = 600;
|
nicholas@2224
|
2344 }
|
nicholas@2224
|
2345 else if (boxwidth < 400)
|
nicholas@2224
|
2346 {
|
nicholas@2224
|
2347 boxwidth = 400;
|
nicholas@2224
|
2348 }
|
nicholas@2224
|
2349 this.trackComment.style.width = boxwidth+"px";
|
nicholas@2224
|
2350 this.trackCommentBox.style.width = boxwidth-6+"px";
|
nicholas@2224
|
2351 };
|
nicholas@2224
|
2352 this.resize();
|
nicholas@2224
|
2353 };
|
nicholas@2224
|
2354 this.createCommentBox = function(audioObject) {
|
nicholas@2224
|
2355 var node = new this.elementCommentBox(audioObject);
|
nicholas@2224
|
2356 this.boxes.push(node);
|
nicholas@2224
|
2357 audioObject.commentDOM = node;
|
nicholas@2224
|
2358 return node;
|
nicholas@2224
|
2359 };
|
nicholas@2224
|
2360 this.sortCommentBoxes = function() {
|
nicholas@2224
|
2361 this.boxes.sort(function(a,b){return a.id - b.id;});
|
nicholas@2224
|
2362 };
|
nicholas@2224
|
2363
|
nicholas@2224
|
2364 this.showCommentBoxes = function(inject, sort) {
|
nicholas@2224
|
2365 this.injectPoint = inject;
|
nicholas@2224
|
2366 if (sort) {this.sortCommentBoxes();}
|
nicholas@2224
|
2367 for (var box of this.boxes) {
|
nicholas@2224
|
2368 inject.appendChild(box.trackComment);
|
nicholas@2224
|
2369 }
|
nicholas@2224
|
2370 };
|
nicholas@2224
|
2371
|
nicholas@2224
|
2372 this.deleteCommentBoxes = function() {
|
nicholas@2224
|
2373 if (this.injectPoint != null) {
|
nicholas@2224
|
2374 for (var box of this.boxes) {
|
nicholas@2224
|
2375 this.injectPoint.removeChild(box.trackComment);
|
nicholas@2224
|
2376 }
|
nicholas@2224
|
2377 this.injectPoint = null;
|
nicholas@2224
|
2378 }
|
nicholas@2224
|
2379 this.boxes = [];
|
nicholas@2224
|
2380 };
|
nicholas@2224
|
2381 }
|
nicholas@2224
|
2382
|
nicholas@2224
|
2383 this.commentQuestions = [];
|
nicholas@2224
|
2384
|
nicholas@2224
|
2385 this.commentBox = function(commentQuestion) {
|
nicholas@2224
|
2386 this.specification = commentQuestion;
|
nicholas@2224
|
2387 // Create document objects to hold the comment boxes
|
nicholas@2224
|
2388 this.holder = document.createElement('div');
|
nicholas@2224
|
2389 this.holder.className = 'comment-div';
|
nicholas@2224
|
2390 // Create a string next to each comment asking for a comment
|
nicholas@2224
|
2391 this.string = document.createElement('span');
|
nicholas@2224
|
2392 this.string.innerHTML = commentQuestion.statement;
|
nicholas@2224
|
2393 // Create the HTML5 comment box 'textarea'
|
nicholas@2224
|
2394 this.textArea = document.createElement('textarea');
|
nicholas@2224
|
2395 this.textArea.rows = '4';
|
nicholas@2224
|
2396 this.textArea.cols = '100';
|
nicholas@2224
|
2397 this.textArea.className = 'trackComment';
|
nicholas@2224
|
2398 var br = document.createElement('br');
|
nicholas@2224
|
2399 // Add to the holder.
|
nicholas@2224
|
2400 this.holder.appendChild(this.string);
|
nicholas@2224
|
2401 this.holder.appendChild(br);
|
nicholas@2224
|
2402 this.holder.appendChild(this.textArea);
|
nicholas@2224
|
2403
|
nicholas@2224
|
2404 this.exportXMLDOM = function(storePoint) {
|
nicholas@2224
|
2405 var root = storePoint.parent.document.createElement('comment');
|
nicholas@2224
|
2406 root.id = this.specification.id;
|
nicholas@2224
|
2407 root.setAttribute('type',this.specification.type);
|
nicholas@2224
|
2408 console.log("Question: "+this.string.textContent);
|
nicholas@2224
|
2409 console.log("Response: "+root.textContent);
|
nicholas@2224
|
2410 var question = storePoint.parent.document.createElement('question');
|
nicholas@2224
|
2411 question.textContent = this.string.textContent;
|
nicholas@2224
|
2412 var response = storePoint.parent.document.createElement('response');
|
nicholas@2224
|
2413 response.textContent = this.textArea.value;
|
nicholas@2224
|
2414 root.appendChild(question);
|
nicholas@2224
|
2415 root.appendChild(response);
|
nicholas@2224
|
2416 storePoint.XMLDOM.appendChild(root);
|
nicholas@2224
|
2417 return root;
|
nicholas@2224
|
2418 };
|
nicholas@2224
|
2419 this.resize = function()
|
nicholas@2224
|
2420 {
|
nicholas@2224
|
2421 var boxwidth = (window.innerWidth-100)/2;
|
nicholas@2224
|
2422 if (boxwidth >= 600)
|
nicholas@2224
|
2423 {
|
nicholas@2224
|
2424 boxwidth = 600;
|
nicholas@2224
|
2425 }
|
nicholas@2224
|
2426 else if (boxwidth < 400)
|
nicholas@2224
|
2427 {
|
nicholas@2224
|
2428 boxwidth = 400;
|
nicholas@2224
|
2429 }
|
nicholas@2224
|
2430 this.holder.style.width = boxwidth+"px";
|
nicholas@2224
|
2431 this.textArea.style.width = boxwidth-6+"px";
|
nicholas@2224
|
2432 };
|
nicholas@2224
|
2433 this.resize();
|
nicholas@2224
|
2434 };
|
nicholas@2224
|
2435
|
nicholas@2224
|
2436 this.radioBox = function(commentQuestion) {
|
nicholas@2224
|
2437 this.specification = commentQuestion;
|
nicholas@2224
|
2438 // Create document objects to hold the comment boxes
|
nicholas@2224
|
2439 this.holder = document.createElement('div');
|
nicholas@2224
|
2440 this.holder.className = 'comment-div';
|
nicholas@2224
|
2441 // Create a string next to each comment asking for a comment
|
nicholas@2224
|
2442 this.string = document.createElement('span');
|
nicholas@2224
|
2443 this.string.innerHTML = commentQuestion.statement;
|
nicholas@2224
|
2444 var br = document.createElement('br');
|
nicholas@2224
|
2445 // Add to the holder.
|
nicholas@2224
|
2446 this.holder.appendChild(this.string);
|
nicholas@2224
|
2447 this.holder.appendChild(br);
|
nicholas@2224
|
2448 this.options = [];
|
nicholas@2224
|
2449 this.inputs = document.createElement('div');
|
nicholas@2224
|
2450 this.span = document.createElement('div');
|
nicholas@2224
|
2451 this.inputs.align = 'center';
|
nicholas@2224
|
2452 this.inputs.style.marginLeft = '12px';
|
nicholas@2294
|
2453 this.inputs.className = "comment-radio-inputs-holder";
|
nicholas@2224
|
2454 this.span.style.marginLeft = '12px';
|
nicholas@2224
|
2455 this.span.align = 'center';
|
nicholas@2224
|
2456 this.span.style.marginTop = '15px';
|
nicholas@2294
|
2457 this.span.className = "comment-radio-span-holder";
|
nicholas@2224
|
2458
|
nicholas@2224
|
2459 var optCount = commentQuestion.options.length;
|
nicholas@2224
|
2460 for (var optNode of commentQuestion.options)
|
nicholas@2224
|
2461 {
|
nicholas@2224
|
2462 var div = document.createElement('div');
|
nicholas@2224
|
2463 div.style.width = '80px';
|
nicholas@2224
|
2464 div.style.float = 'left';
|
nicholas@2224
|
2465 var input = document.createElement('input');
|
nicholas@2224
|
2466 input.type = 'radio';
|
nicholas@2224
|
2467 input.name = commentQuestion.id;
|
nicholas@2224
|
2468 input.setAttribute('setvalue',optNode.name);
|
nicholas@2224
|
2469 input.className = 'comment-radio';
|
nicholas@2224
|
2470 div.appendChild(input);
|
nicholas@2224
|
2471 this.inputs.appendChild(div);
|
nicholas@2224
|
2472
|
nicholas@2224
|
2473
|
nicholas@2224
|
2474 div = document.createElement('div');
|
nicholas@2224
|
2475 div.style.width = '80px';
|
nicholas@2224
|
2476 div.style.float = 'left';
|
nicholas@2224
|
2477 div.align = 'center';
|
nicholas@2224
|
2478 var span = document.createElement('span');
|
nicholas@2224
|
2479 span.textContent = optNode.text;
|
nicholas@2224
|
2480 span.className = 'comment-radio-span';
|
nicholas@2224
|
2481 div.appendChild(span);
|
nicholas@2224
|
2482 this.span.appendChild(div);
|
nicholas@2224
|
2483 this.options.push(input);
|
nicholas@2224
|
2484 }
|
nicholas@2224
|
2485 this.holder.appendChild(this.span);
|
nicholas@2224
|
2486 this.holder.appendChild(this.inputs);
|
nicholas@2224
|
2487
|
nicholas@2224
|
2488 this.exportXMLDOM = function(storePoint) {
|
nicholas@2224
|
2489 var root = storePoint.parent.document.createElement('comment');
|
nicholas@2224
|
2490 root.id = this.specification.id;
|
nicholas@2224
|
2491 root.setAttribute('type',this.specification.type);
|
nicholas@2224
|
2492 var question = document.createElement('question');
|
nicholas@2224
|
2493 question.textContent = this.string.textContent;
|
nicholas@2224
|
2494 var response = document.createElement('response');
|
nicholas@2224
|
2495 var i=0;
|
nicholas@2224
|
2496 while(this.options[i].checked == false) {
|
nicholas@2224
|
2497 i++;
|
nicholas@2224
|
2498 if (i >= this.options.length) {
|
nicholas@2224
|
2499 break;
|
nicholas@2224
|
2500 }
|
nicholas@2224
|
2501 }
|
nicholas@2224
|
2502 if (i >= this.options.length) {
|
nicholas@2224
|
2503 response.textContent = 'null';
|
nicholas@2224
|
2504 } else {
|
nicholas@2224
|
2505 response.textContent = this.options[i].getAttribute('setvalue');
|
nicholas@2224
|
2506 response.setAttribute('number',i);
|
nicholas@2224
|
2507 }
|
nicholas@2224
|
2508 console.log('Comment: '+question.textContent);
|
nicholas@2224
|
2509 console.log('Response: '+response.textContent);
|
nicholas@2224
|
2510 root.appendChild(question);
|
nicholas@2224
|
2511 root.appendChild(response);
|
nicholas@2224
|
2512 storePoint.XMLDOM.appendChild(root);
|
nicholas@2224
|
2513 return root;
|
nicholas@2224
|
2514 };
|
nicholas@2224
|
2515 this.resize = function()
|
nicholas@2224
|
2516 {
|
nicholas@2224
|
2517 var boxwidth = (window.innerWidth-100)/2;
|
nicholas@2224
|
2518 if (boxwidth >= 600)
|
nicholas@2224
|
2519 {
|
nicholas@2224
|
2520 boxwidth = 600;
|
nicholas@2224
|
2521 }
|
nicholas@2224
|
2522 else if (boxwidth < 400)
|
nicholas@2224
|
2523 {
|
nicholas@2224
|
2524 boxwidth = 400;
|
nicholas@2224
|
2525 }
|
nicholas@2224
|
2526 this.holder.style.width = boxwidth+"px";
|
nicholas@2294
|
2527 var text = this.holder.getElementsByClassName("comment-radio-span-holder")[0];
|
nicholas@2294
|
2528 var options = this.holder.getElementsByClassName("comment-radio-inputs-holder")[0];
|
nicholas@2294
|
2529 var optCount = options.childElementCount;
|
nicholas@2224
|
2530 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
|
nicholas@2224
|
2531 var options = options.firstChild;
|
nicholas@2224
|
2532 var text = text.firstChild;
|
nicholas@2224
|
2533 options.style.marginRight = spanMargin;
|
nicholas@2224
|
2534 options.style.marginLeft = spanMargin;
|
nicholas@2224
|
2535 text.style.marginRight = spanMargin;
|
nicholas@2224
|
2536 text.style.marginLeft = spanMargin;
|
nicholas@2224
|
2537 while(options.nextSibling != undefined)
|
nicholas@2224
|
2538 {
|
nicholas@2224
|
2539 options = options.nextSibling;
|
nicholas@2224
|
2540 text = text.nextSibling;
|
nicholas@2224
|
2541 options.style.marginRight = spanMargin;
|
nicholas@2224
|
2542 options.style.marginLeft = spanMargin;
|
nicholas@2224
|
2543 text.style.marginRight = spanMargin;
|
nicholas@2224
|
2544 text.style.marginLeft = spanMargin;
|
nicholas@2224
|
2545 }
|
nicholas@2224
|
2546 };
|
nicholas@2224
|
2547 this.resize();
|
nicholas@2224
|
2548 };
|
nicholas@2224
|
2549
|
nicholas@2224
|
2550 this.checkboxBox = function(commentQuestion) {
|
nicholas@2224
|
2551 this.specification = commentQuestion;
|
nicholas@2224
|
2552 // Create document objects to hold the comment boxes
|
nicholas@2224
|
2553 this.holder = document.createElement('div');
|
nicholas@2224
|
2554 this.holder.className = 'comment-div';
|
nicholas@2224
|
2555 // Create a string next to each comment asking for a comment
|
nicholas@2224
|
2556 this.string = document.createElement('span');
|
nicholas@2224
|
2557 this.string.innerHTML = commentQuestion.statement;
|
nicholas@2224
|
2558 var br = document.createElement('br');
|
nicholas@2224
|
2559 // Add to the holder.
|
nicholas@2224
|
2560 this.holder.appendChild(this.string);
|
nicholas@2224
|
2561 this.holder.appendChild(br);
|
nicholas@2224
|
2562 this.options = [];
|
nicholas@2224
|
2563 this.inputs = document.createElement('div');
|
nicholas@2224
|
2564 this.span = document.createElement('div');
|
nicholas@2224
|
2565 this.inputs.align = 'center';
|
nicholas@2224
|
2566 this.inputs.style.marginLeft = '12px';
|
nicholas@2294
|
2567 this.inputs.className = "comment-checkbox-inputs-holder";
|
nicholas@2224
|
2568 this.span.style.marginLeft = '12px';
|
nicholas@2224
|
2569 this.span.align = 'center';
|
nicholas@2224
|
2570 this.span.style.marginTop = '15px';
|
nicholas@2294
|
2571 this.span.className = "comment-checkbox-span-holder";
|
nicholas@2224
|
2572
|
nicholas@2224
|
2573 var optCount = commentQuestion.options.length;
|
nicholas@2224
|
2574 for (var i=0; i<optCount; i++)
|
nicholas@2224
|
2575 {
|
nicholas@2224
|
2576 var div = document.createElement('div');
|
nicholas@2224
|
2577 div.style.width = '80px';
|
nicholas@2224
|
2578 div.style.float = 'left';
|
nicholas@2224
|
2579 var input = document.createElement('input');
|
nicholas@2224
|
2580 input.type = 'checkbox';
|
nicholas@2224
|
2581 input.name = commentQuestion.id;
|
nicholas@2224
|
2582 input.setAttribute('setvalue',commentQuestion.options[i].name);
|
nicholas@2224
|
2583 input.className = 'comment-radio';
|
nicholas@2224
|
2584 div.appendChild(input);
|
nicholas@2224
|
2585 this.inputs.appendChild(div);
|
nicholas@2224
|
2586
|
nicholas@2224
|
2587
|
nicholas@2224
|
2588 div = document.createElement('div');
|
nicholas@2224
|
2589 div.style.width = '80px';
|
nicholas@2224
|
2590 div.style.float = 'left';
|
nicholas@2224
|
2591 div.align = 'center';
|
nicholas@2224
|
2592 var span = document.createElement('span');
|
nicholas@2224
|
2593 span.textContent = commentQuestion.options[i].text;
|
nicholas@2224
|
2594 span.className = 'comment-radio-span';
|
nicholas@2224
|
2595 div.appendChild(span);
|
nicholas@2224
|
2596 this.span.appendChild(div);
|
nicholas@2224
|
2597 this.options.push(input);
|
nicholas@2224
|
2598 }
|
nicholas@2224
|
2599 this.holder.appendChild(this.span);
|
nicholas@2224
|
2600 this.holder.appendChild(this.inputs);
|
nicholas@2224
|
2601
|
nicholas@2224
|
2602 this.exportXMLDOM = function(storePoint) {
|
nicholas@2224
|
2603 var root = storePoint.parent.document.createElement('comment');
|
nicholas@2224
|
2604 root.id = this.specification.id;
|
nicholas@2224
|
2605 root.setAttribute('type',this.specification.type);
|
nicholas@2224
|
2606 var question = document.createElement('question');
|
nicholas@2224
|
2607 question.textContent = this.string.textContent;
|
nicholas@2224
|
2608 root.appendChild(question);
|
nicholas@2224
|
2609 console.log('Comment: '+question.textContent);
|
nicholas@2224
|
2610 for (var i=0; i<this.options.length; i++) {
|
nicholas@2224
|
2611 var response = document.createElement('response');
|
nicholas@2224
|
2612 response.textContent = this.options[i].checked;
|
nicholas@2224
|
2613 response.setAttribute('name',this.options[i].getAttribute('setvalue'));
|
nicholas@2224
|
2614 root.appendChild(response);
|
nicholas@2224
|
2615 console.log('Response '+response.getAttribute('name') +': '+response.textContent);
|
nicholas@2224
|
2616 }
|
nicholas@2224
|
2617 storePoint.XMLDOM.appendChild(root);
|
nicholas@2224
|
2618 return root;
|
nicholas@2224
|
2619 };
|
nicholas@2224
|
2620 this.resize = function()
|
nicholas@2224
|
2621 {
|
nicholas@2224
|
2622 var boxwidth = (window.innerWidth-100)/2;
|
nicholas@2224
|
2623 if (boxwidth >= 600)
|
nicholas@2224
|
2624 {
|
nicholas@2224
|
2625 boxwidth = 600;
|
nicholas@2224
|
2626 }
|
nicholas@2224
|
2627 else if (boxwidth < 400)
|
nicholas@2224
|
2628 {
|
nicholas@2224
|
2629 boxwidth = 400;
|
nicholas@2224
|
2630 }
|
nicholas@2224
|
2631 this.holder.style.width = boxwidth+"px";
|
nicholas@2294
|
2632 var text = this.holder.getElementsByClassName("comment-checkbox-span-holder")[0];
|
nicholas@2294
|
2633 var options = this.holder.getElementsByClassName("comment-checkbox-inputs-holder")[0];
|
nicholas@2294
|
2634 var optCount = options.childElementCount;
|
nicholas@2224
|
2635 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
|
nicholas@2224
|
2636 var options = options.firstChild;
|
nicholas@2224
|
2637 var text = text.firstChild;
|
nicholas@2224
|
2638 options.style.marginRight = spanMargin;
|
nicholas@2224
|
2639 options.style.marginLeft = spanMargin;
|
nicholas@2224
|
2640 text.style.marginRight = spanMargin;
|
nicholas@2224
|
2641 text.style.marginLeft = spanMargin;
|
nicholas@2224
|
2642 while(options.nextSibling != undefined)
|
nicholas@2224
|
2643 {
|
nicholas@2224
|
2644 options = options.nextSibling;
|
nicholas@2224
|
2645 text = text.nextSibling;
|
nicholas@2224
|
2646 options.style.marginRight = spanMargin;
|
nicholas@2224
|
2647 options.style.marginLeft = spanMargin;
|
nicholas@2224
|
2648 text.style.marginRight = spanMargin;
|
nicholas@2224
|
2649 text.style.marginLeft = spanMargin;
|
nicholas@2224
|
2650 }
|
nicholas@2224
|
2651 };
|
nicholas@2224
|
2652 this.resize();
|
nicholas@2224
|
2653 };
|
nicholas@2224
|
2654
|
nicholas@2224
|
2655 this.createCommentQuestion = function(element) {
|
nicholas@2224
|
2656 var node;
|
nicholas@2224
|
2657 if (element.type == 'question') {
|
nicholas@2224
|
2658 node = new this.commentBox(element);
|
nicholas@2224
|
2659 } else if (element.type == 'radio') {
|
nicholas@2224
|
2660 node = new this.radioBox(element);
|
nicholas@2224
|
2661 } else if (element.type == 'checkbox') {
|
nicholas@2224
|
2662 node = new this.checkboxBox(element);
|
nicholas@2224
|
2663 }
|
nicholas@2224
|
2664 this.commentQuestions.push(node);
|
nicholas@2224
|
2665 return node;
|
nicholas@2224
|
2666 };
|
nicholas@2224
|
2667
|
nicholas@2224
|
2668 this.deleteCommentQuestions = function()
|
nicholas@2224
|
2669 {
|
nicholas@2224
|
2670 this.commentQuestions = [];
|
nicholas@2224
|
2671 };
|
nicholas@2224
|
2672
|
nicholas@2224
|
2673 this.outsideReferenceDOM = function(audioObject,index,inject)
|
nicholas@2224
|
2674 {
|
nicholas@2224
|
2675 this.parent = audioObject;
|
nicholas@2224
|
2676 this.outsideReferenceHolder = document.createElement('button');
|
nicholas@2224
|
2677 this.outsideReferenceHolder.className = 'outside-reference';
|
nicholas@2224
|
2678 this.outsideReferenceHolder.setAttribute('track-id',index);
|
nicholas@2409
|
2679 this.outsideReferenceHolder.textContent = this.parent.specification.label || "Reference";
|
nicholas@2224
|
2680 this.outsideReferenceHolder.disabled = true;
|
nicholas@2224
|
2681
|
nicholas@2224
|
2682 this.outsideReferenceHolder.onclick = function(event)
|
nicholas@2224
|
2683 {
|
nicholas@2224
|
2684 audioEngineContext.play(event.currentTarget.getAttribute('track-id'));
|
nicholas@2224
|
2685 };
|
nicholas@2224
|
2686 inject.appendChild(this.outsideReferenceHolder);
|
nicholas@2224
|
2687 this.enable = function()
|
nicholas@2224
|
2688 {
|
nicholas@2224
|
2689 if (this.parent.state == 1)
|
nicholas@2224
|
2690 {
|
nicholas@2224
|
2691 this.outsideReferenceHolder.disabled = false;
|
nicholas@2224
|
2692 }
|
nicholas@2224
|
2693 };
|
nicholas@2224
|
2694 this.updateLoading = function(progress)
|
nicholas@2224
|
2695 {
|
nicholas@2224
|
2696 if (progress != 100)
|
nicholas@2224
|
2697 {
|
nicholas@2224
|
2698 progress = String(progress);
|
nicholas@2224
|
2699 progress = progress.split('.')[0];
|
nicholas@2224
|
2700 this.outsideReferenceHolder.textContent = progress+'%';
|
nicholas@2224
|
2701 } else {
|
nicholas@2409
|
2702 this.outsideReferenceHolder.textContent = this.parent.specification.label || "Reference";
|
nicholas@2224
|
2703 }
|
nicholas@2224
|
2704 };
|
nicholas@2224
|
2705 this.startPlayback = function()
|
nicholas@2224
|
2706 {
|
nicholas@2224
|
2707 // Called when playback has begun
|
nicholas@2224
|
2708 $('.track-slider').removeClass('track-slider-playing');
|
nicholas@2224
|
2709 $('.comment-div').removeClass('comment-box-playing');
|
nicholas@2224
|
2710 this.outsideReferenceHolder.style.backgroundColor = "#FDD";
|
nicholas@2224
|
2711 };
|
nicholas@2224
|
2712 this.stopPlayback = function()
|
nicholas@2224
|
2713 {
|
nicholas@2224
|
2714 // Called when playback has stopped. This gets called even if playback never started!
|
nicholas@2224
|
2715 this.outsideReferenceHolder.style.backgroundColor = "";
|
nicholas@2224
|
2716 };
|
nicholas@2224
|
2717 this.exportXMLDOM = function(audioObject)
|
nicholas@2224
|
2718 {
|
nicholas@2224
|
2719 return null;
|
nicholas@2224
|
2720 };
|
nicholas@2224
|
2721 this.getValue = function()
|
nicholas@2224
|
2722 {
|
nicholas@2224
|
2723 return 0;
|
nicholas@2224
|
2724 };
|
nicholas@2224
|
2725 this.getPresentedId = function()
|
nicholas@2224
|
2726 {
|
nicholas@2409
|
2727 return this.parent.specification.label || "Reference";
|
nicholas@2224
|
2728 };
|
nicholas@2224
|
2729 this.canMove = function()
|
nicholas@2224
|
2730 {
|
nicholas@2224
|
2731 return false;
|
nicholas@2224
|
2732 };
|
nicholas@2224
|
2733 this.error = function() {
|
nicholas@2224
|
2734 // audioObject has an error!!
|
nicholas@2224
|
2735 this.outsideReferenceHolder.textContent = "Error";
|
nicholas@2224
|
2736 this.outsideReferenceHolder.style.backgroundColor = "#F00";
|
nicholas@2224
|
2737 }
|
nicholas@2224
|
2738 }
|
nicholas@2224
|
2739
|
nicholas@2224
|
2740 this.playhead = new function()
|
nicholas@2224
|
2741 {
|
nicholas@2224
|
2742 this.object = document.createElement('div');
|
nicholas@2224
|
2743 this.object.className = 'playhead';
|
nicholas@2224
|
2744 this.object.align = 'left';
|
nicholas@2224
|
2745 var curTime = document.createElement('div');
|
nicholas@2224
|
2746 curTime.style.width = '50px';
|
nicholas@2224
|
2747 this.curTimeSpan = document.createElement('span');
|
nicholas@2224
|
2748 this.curTimeSpan.textContent = '00:00';
|
nicholas@2224
|
2749 curTime.appendChild(this.curTimeSpan);
|
nicholas@2224
|
2750 this.object.appendChild(curTime);
|
nicholas@2224
|
2751 this.scrubberTrack = document.createElement('div');
|
nicholas@2224
|
2752 this.scrubberTrack.className = 'playhead-scrub-track';
|
nicholas@2224
|
2753
|
nicholas@2224
|
2754 this.scrubberHead = document.createElement('div');
|
nicholas@2224
|
2755 this.scrubberHead.id = 'playhead-scrubber';
|
nicholas@2224
|
2756 this.scrubberTrack.appendChild(this.scrubberHead);
|
nicholas@2224
|
2757 this.object.appendChild(this.scrubberTrack);
|
nicholas@2224
|
2758
|
nicholas@2224
|
2759 this.timePerPixel = 0;
|
nicholas@2224
|
2760 this.maxTime = 0;
|
nicholas@2224
|
2761
|
nicholas@2224
|
2762 this.playbackObject;
|
nicholas@2224
|
2763
|
nicholas@2224
|
2764 this.setTimePerPixel = function(audioObject) {
|
nicholas@2224
|
2765 //maxTime must be in seconds
|
nicholas@2224
|
2766 this.playbackObject = audioObject;
|
nicholas@2224
|
2767 this.maxTime = audioObject.buffer.buffer.duration;
|
nicholas@2224
|
2768 var width = 490; //500 - 10, 5 each side of the tracker head
|
nicholas@2224
|
2769 this.timePerPixel = this.maxTime/490;
|
nicholas@2224
|
2770 if (this.maxTime < 60) {
|
nicholas@2224
|
2771 this.curTimeSpan.textContent = '0.00';
|
nicholas@2224
|
2772 } else {
|
nicholas@2224
|
2773 this.curTimeSpan.textContent = '00:00';
|
nicholas@2224
|
2774 }
|
nicholas@2224
|
2775 };
|
nicholas@2224
|
2776
|
nicholas@2224
|
2777 this.update = function() {
|
nicholas@2224
|
2778 // Update the playhead position, startPlay must be called
|
nicholas@2224
|
2779 if (this.timePerPixel > 0) {
|
nicholas@2224
|
2780 var time = this.playbackObject.getCurrentPosition();
|
nicholas@2224
|
2781 if (time > 0 && time < this.maxTime) {
|
nicholas@2224
|
2782 var width = 490;
|
nicholas@2224
|
2783 var pix = Math.floor(time/this.timePerPixel);
|
nicholas@2224
|
2784 this.scrubberHead.style.left = pix+'px';
|
nicholas@2224
|
2785 if (this.maxTime > 60.0) {
|
nicholas@2224
|
2786 var secs = time%60;
|
nicholas@2224
|
2787 var mins = Math.floor((time-secs)/60);
|
nicholas@2224
|
2788 secs = secs.toString();
|
nicholas@2224
|
2789 secs = secs.substr(0,2);
|
nicholas@2224
|
2790 mins = mins.toString();
|
nicholas@2224
|
2791 this.curTimeSpan.textContent = mins+':'+secs;
|
nicholas@2224
|
2792 } else {
|
nicholas@2224
|
2793 time = time.toString();
|
nicholas@2224
|
2794 this.curTimeSpan.textContent = time.substr(0,4);
|
nicholas@2224
|
2795 }
|
nicholas@2224
|
2796 } else {
|
nicholas@2224
|
2797 this.scrubberHead.style.left = '0px';
|
nicholas@2224
|
2798 if (this.maxTime < 60) {
|
nicholas@2224
|
2799 this.curTimeSpan.textContent = '0.00';
|
nicholas@2224
|
2800 } else {
|
nicholas@2224
|
2801 this.curTimeSpan.textContent = '00:00';
|
nicholas@2224
|
2802 }
|
nicholas@2224
|
2803 }
|
nicholas@2224
|
2804 }
|
nicholas@2224
|
2805 };
|
nicholas@2224
|
2806
|
nicholas@2224
|
2807 this.interval = undefined;
|
nicholas@2224
|
2808
|
nicholas@2224
|
2809 this.start = function() {
|
nicholas@2224
|
2810 if (this.playbackObject != undefined && this.interval == undefined) {
|
nicholas@2224
|
2811 if (this.maxTime < 60) {
|
nicholas@2224
|
2812 this.interval = setInterval(function(){interfaceContext.playhead.update();},10);
|
nicholas@2224
|
2813 } else {
|
nicholas@2224
|
2814 this.interval = setInterval(function(){interfaceContext.playhead.update();},100);
|
nicholas@2224
|
2815 }
|
nicholas@2224
|
2816 }
|
nicholas@2224
|
2817 };
|
nicholas@2224
|
2818 this.stop = function() {
|
nicholas@2224
|
2819 clearInterval(this.interval);
|
nicholas@2224
|
2820 this.interval = undefined;
|
nicholas@2224
|
2821 this.scrubberHead.style.left = '0px';
|
nicholas@2224
|
2822 if (this.maxTime < 60) {
|
nicholas@2224
|
2823 this.curTimeSpan.textContent = '0.00';
|
nicholas@2224
|
2824 } else {
|
nicholas@2224
|
2825 this.curTimeSpan.textContent = '00:00';
|
nicholas@2224
|
2826 }
|
nicholas@2224
|
2827 };
|
nicholas@2224
|
2828 };
|
nicholas@2224
|
2829
|
nicholas@2224
|
2830 this.volume = new function()
|
nicholas@2224
|
2831 {
|
nicholas@2224
|
2832 // An in-built volume module which can be viewed on page
|
nicholas@2224
|
2833 // Includes trackers on page-by-page data
|
nicholas@2224
|
2834 // Volume does NOT reset to 0dB on each page load
|
nicholas@2224
|
2835 this.valueLin = 1.0;
|
nicholas@2224
|
2836 this.valueDB = 0.0;
|
nicholas@2352
|
2837 this.root = document.createElement('div');
|
nicholas@2352
|
2838 this.root.id = 'master-volume-root';
|
nicholas@2224
|
2839 this.object = document.createElement('div');
|
nicholas@2352
|
2840 this.object.className = 'master-volume-holder-float';
|
nicholas@2352
|
2841 this.object.appendChild(this.root);
|
nicholas@2224
|
2842 this.slider = document.createElement('input');
|
nicholas@2224
|
2843 this.slider.id = 'master-volume-control';
|
nicholas@2224
|
2844 this.slider.type = 'range';
|
nicholas@2224
|
2845 this.valueText = document.createElement('span');
|
nicholas@2224
|
2846 this.valueText.id = 'master-volume-feedback';
|
nicholas@2224
|
2847 this.valueText.textContent = '0dB';
|
nicholas@2224
|
2848
|
nicholas@2224
|
2849 this.slider.min = -60;
|
nicholas@2224
|
2850 this.slider.max = 12;
|
nicholas@2224
|
2851 this.slider.value = 0;
|
nicholas@2224
|
2852 this.slider.step = 1;
|
nicholas@2224
|
2853 this.slider.onmousemove = function(event)
|
nicholas@2224
|
2854 {
|
nicholas@2224
|
2855 interfaceContext.volume.valueDB = event.currentTarget.value;
|
nicholas@2224
|
2856 interfaceContext.volume.valueLin = decibelToLinear(interfaceContext.volume.valueDB);
|
nicholas@2224
|
2857 interfaceContext.volume.valueText.textContent = interfaceContext.volume.valueDB+'dB';
|
nicholas@2224
|
2858 audioEngineContext.outputGain.gain.value = interfaceContext.volume.valueLin;
|
nicholas@2224
|
2859 }
|
nicholas@2224
|
2860 this.slider.onmouseup = function(event)
|
nicholas@2224
|
2861 {
|
nicholas@2224
|
2862 var storePoint = testState.currentStore.XMLDOM.getElementsByTagName('metric')[0].getAllElementsByName('volumeTracker');
|
nicholas@2224
|
2863 if (storePoint.length == 0)
|
nicholas@2224
|
2864 {
|
nicholas@2224
|
2865 storePoint = storage.document.createElement('metricresult');
|
nicholas@2224
|
2866 storePoint.setAttribute('name','volumeTracker');
|
nicholas@2224
|
2867 testState.currentStore.XMLDOM.getElementsByTagName('metric')[0].appendChild(storePoint);
|
nicholas@2224
|
2868 }
|
nicholas@2224
|
2869 else {
|
nicholas@2224
|
2870 storePoint = storePoint[0];
|
nicholas@2224
|
2871 }
|
nicholas@2224
|
2872 var node = storage.document.createElement('movement');
|
nicholas@2224
|
2873 node.setAttribute('test-time',audioEngineContext.timer.getTestTime());
|
nicholas@2224
|
2874 node.setAttribute('volume',interfaceContext.volume.valueDB);
|
nicholas@2224
|
2875 node.setAttribute('format','dBFS');
|
nicholas@2224
|
2876 storePoint.appendChild(node);
|
nicholas@2224
|
2877 }
|
nicholas@2224
|
2878
|
nicholas@2224
|
2879 var title = document.createElement('div');
|
nicholas@2224
|
2880 title.innerHTML = '<span>Master Volume Control</span>';
|
nicholas@2224
|
2881 title.style.fontSize = '0.75em';
|
nicholas@2224
|
2882 title.style.width = "100%";
|
nicholas@2224
|
2883 title.align = 'center';
|
nicholas@2352
|
2884 this.root.appendChild(title);
|
nicholas@2224
|
2885
|
nicholas@2352
|
2886 this.root.appendChild(this.slider);
|
nicholas@2352
|
2887 this.root.appendChild(this.valueText);
|
nicholas@2352
|
2888
|
nicholas@2352
|
2889 this.resize = function(event) {
|
nicholas@2352
|
2890 if (window.innerWidth < 1000) {
|
nicholas@2352
|
2891 this.object.className = "master-volume-holder-inline"
|
nicholas@2352
|
2892 } else {
|
nicholas@2352
|
2893 this.object.className = 'master-volume-holder-float';
|
nicholas@2352
|
2894 }
|
nicholas@2352
|
2895 }
|
nicholas@2224
|
2896 }
|
nicholas@2224
|
2897
|
nicholas@2224
|
2898 this.calibrationModuleObject = null;
|
nicholas@2224
|
2899 this.calibrationModule = function() {
|
nicholas@2224
|
2900 // This creates an on-page calibration module
|
nicholas@2224
|
2901 this.storeDOM = storage.document.createElement("calibration");
|
nicholas@2224
|
2902 storage.root.appendChild(this.storeDOM);
|
nicholas@2224
|
2903 // The calibration is a fixed state module
|
nicholas@2224
|
2904 this.calibrationNodes = [];
|
nicholas@2224
|
2905 this.holder = null;
|
nicholas@2224
|
2906 this.build = function(inject) {
|
nicholas@2224
|
2907 var f0 = 62.5;
|
nicholas@2224
|
2908 this.holder = document.createElement("div");
|
nicholas@2224
|
2909 this.holder.className = "calibration-holder";
|
nicholas@2224
|
2910 this.calibrationNodes = [];
|
nicholas@2224
|
2911 while(f0 < 20000) {
|
nicholas@2224
|
2912 var obj = {
|
nicholas@2224
|
2913 root: document.createElement("div"),
|
nicholas@2224
|
2914 input: document.createElement("input"),
|
nicholas@2224
|
2915 oscillator: audioContext.createOscillator(),
|
nicholas@2224
|
2916 gain: audioContext.createGain(),
|
nicholas@2224
|
2917 f: f0,
|
nicholas@2224
|
2918 parent: this,
|
nicholas@2224
|
2919 handleEvent: function(event) {
|
nicholas@2224
|
2920 switch(event.type) {
|
nicholas@2224
|
2921 case "mouseenter":
|
nicholas@2224
|
2922 this.oscillator.start(0);
|
nicholas@2224
|
2923 break;
|
nicholas@2224
|
2924 case "mouseleave":
|
nicholas@2224
|
2925 this.oscillator.stop(0);
|
nicholas@2224
|
2926 this.oscillator = audioContext.createOscillator();
|
nicholas@2224
|
2927 this.oscillator.connect(this.gain);
|
nicholas@2224
|
2928 this.oscillator.frequency.value = this.f;
|
nicholas@2224
|
2929 break;
|
nicholas@2224
|
2930 case "mousemove":
|
nicholas@2224
|
2931 var value = Math.pow(10,this.input.value/20);
|
nicholas@2224
|
2932 if (this.f == 1000) {
|
nicholas@2224
|
2933 audioEngineContext.outputGain.gain.value = value;
|
nicholas@2224
|
2934 interfaceContext.volume.slider.value = this.input.value;
|
nicholas@2224
|
2935 } else {
|
nicholas@2224
|
2936 this.gain.gain.value = value
|
nicholas@2224
|
2937 }
|
nicholas@2224
|
2938 break;
|
nicholas@2224
|
2939 }
|
nicholas@2224
|
2940 },
|
nicholas@2224
|
2941 disconnect: function() {
|
nicholas@2224
|
2942 this.gain.disconnect();
|
nicholas@2224
|
2943 }
|
nicholas@2224
|
2944 }
|
nicholas@2224
|
2945 obj.root.className = "calibration-slider";
|
nicholas@2224
|
2946 obj.root.appendChild(obj.input);
|
nicholas@2224
|
2947 obj.oscillator.connect(obj.gain);
|
nicholas@2224
|
2948 obj.gain.connect(audioEngineContext.outputGain);
|
nicholas@2224
|
2949 obj.gain.gain.value = Math.random()*2;
|
nicholas@2224
|
2950 obj.input.value = obj.gain.gain.value;
|
nicholas@2224
|
2951 obj.input.setAttribute('orient','vertical');
|
nicholas@2224
|
2952 obj.input.type = "range";
|
nicholas@2224
|
2953 obj.input.min = -6;
|
nicholas@2224
|
2954 obj.input.max = 6;
|
nicholas@2224
|
2955 obj.input.step = 0.25;
|
nicholas@2224
|
2956 if (f0 != 1000) {
|
nicholas@2224
|
2957 obj.input.value = (Math.random()*12)-6;
|
nicholas@2224
|
2958 } else {
|
nicholas@2224
|
2959 obj.input.value = 0;
|
nicholas@2224
|
2960 obj.root.style.backgroundColor="rgb(255,125,125)";
|
nicholas@2224
|
2961 }
|
nicholas@2224
|
2962 obj.input.addEventListener("mousemove",obj);
|
nicholas@2224
|
2963 obj.input.addEventListener("mouseenter",obj);
|
nicholas@2224
|
2964 obj.input.addEventListener("mouseleave",obj);
|
nicholas@2224
|
2965 obj.gain.gain.value = Math.pow(10,obj.input.value/20);
|
nicholas@2224
|
2966 obj.oscillator.frequency.value = f0;
|
nicholas@2224
|
2967 this.calibrationNodes.push(obj);
|
nicholas@2224
|
2968 this.holder.appendChild(obj.root);
|
nicholas@2224
|
2969 f0 *= 2;
|
nicholas@2224
|
2970 }
|
nicholas@2224
|
2971 inject.appendChild(this.holder);
|
nicholas@2224
|
2972 }
|
nicholas@2224
|
2973 this.collect = function() {
|
nicholas@2224
|
2974 for (var obj of this.calibrationNodes) {
|
nicholas@2224
|
2975 var node = storage.document.createElement("calibrationresult");
|
nicholas@2224
|
2976 node.setAttribute("frequency",obj.f);
|
nicholas@2224
|
2977 node.setAttribute("range-min",obj.input.min);
|
nicholas@2224
|
2978 node.setAttribute("range-max",obj.input.max);
|
nicholas@2224
|
2979 node.setAttribute("gain-lin",obj.gain.gain.value);
|
nicholas@2224
|
2980 this.storeDOM.appendChild(node);
|
nicholas@2224
|
2981 }
|
nicholas@2224
|
2982 }
|
nicholas@2224
|
2983 }
|
nicholas@2224
|
2984
|
nicholas@2224
|
2985
|
nicholas@2224
|
2986 // Global Checkers
|
nicholas@2224
|
2987 // These functions will help enforce the checkers
|
nicholas@2224
|
2988 this.checkHiddenAnchor = function()
|
nicholas@2224
|
2989 {
|
nicholas@2224
|
2990 for (var ao of audioEngineContext.audioObjects)
|
nicholas@2224
|
2991 {
|
nicholas@2224
|
2992 if (ao.specification.type == "anchor")
|
nicholas@2224
|
2993 {
|
nicholas@2224
|
2994 if (ao.interfaceDOM.getValue() > (ao.specification.marker/100) && ao.specification.marker > 0) {
|
nicholas@2224
|
2995 // Anchor is not set below
|
nicholas@2224
|
2996 console.log('Anchor node not below marker value');
|
nicholas@2361
|
2997 interfaceContext.lightbox.post("Message",'Please keep listening');
|
nicholas@2224
|
2998 this.storeErrorNode('Anchor node not below marker value');
|
nicholas@2224
|
2999 return false;
|
nicholas@2224
|
3000 }
|
nicholas@2224
|
3001 }
|
nicholas@2224
|
3002 }
|
nicholas@2224
|
3003 return true;
|
nicholas@2224
|
3004 };
|
nicholas@2224
|
3005
|
nicholas@2224
|
3006 this.checkHiddenReference = function()
|
nicholas@2224
|
3007 {
|
nicholas@2224
|
3008 for (var ao of audioEngineContext.audioObjects)
|
nicholas@2224
|
3009 {
|
nicholas@2224
|
3010 if (ao.specification.type == "reference")
|
nicholas@2224
|
3011 {
|
nicholas@2224
|
3012 if (ao.interfaceDOM.getValue() < (ao.specification.marker/100) && ao.specification.marker > 0) {
|
nicholas@2224
|
3013 // Anchor is not set below
|
nicholas@2224
|
3014 console.log('Reference node not above marker value');
|
nicholas@2224
|
3015 this.storeErrorNode('Reference node not above marker value');
|
nicholas@2361
|
3016 interfaceContext.lightbox.post("Message",'Please keep listening');
|
nicholas@2224
|
3017 return false;
|
nicholas@2224
|
3018 }
|
nicholas@2224
|
3019 }
|
nicholas@2224
|
3020 }
|
nicholas@2224
|
3021 return true;
|
nicholas@2224
|
3022 };
|
nicholas@2224
|
3023
|
nicholas@2224
|
3024 this.checkFragmentsFullyPlayed = function ()
|
nicholas@2224
|
3025 {
|
nicholas@2224
|
3026 // Checks the entire file has been played back
|
nicholas@2224
|
3027 // NOTE ! This will return true IF playback is Looped!!!
|
nicholas@2224
|
3028 if (audioEngineContext.loopPlayback)
|
nicholas@2224
|
3029 {
|
nicholas@2224
|
3030 console.log("WARNING - Looped source: Cannot check fragments are fully played");
|
nicholas@2224
|
3031 return true;
|
nicholas@2224
|
3032 }
|
nicholas@2224
|
3033 var check_pass = true;
|
nicholas@2224
|
3034 var error_obj = [];
|
nicholas@2224
|
3035 for (var i = 0; i<audioEngineContext.audioObjects.length; i++)
|
nicholas@2224
|
3036 {
|
nicholas@2224
|
3037 var object = audioEngineContext.audioObjects[i];
|
nicholas@2224
|
3038 var time = object.buffer.buffer.duration;
|
nicholas@2224
|
3039 var metric = object.metric;
|
nicholas@2224
|
3040 var passed = false;
|
nicholas@2224
|
3041 for (var j=0; j<metric.listenTracker.length; j++)
|
nicholas@2224
|
3042 {
|
nicholas@2444
|
3043 var bt = metric.listenTracker[j].getElementsByTagName('testtime');
|
nicholas@2224
|
3044 var start_time = Number(bt[0].getAttribute('start'));
|
nicholas@2224
|
3045 var stop_time = Number(bt[0].getAttribute('stop'));
|
nicholas@2224
|
3046 var delta = stop_time - start_time;
|
nicholas@2224
|
3047 if (delta >= time)
|
nicholas@2224
|
3048 {
|
nicholas@2224
|
3049 passed = true;
|
nicholas@2224
|
3050 break;
|
nicholas@2224
|
3051 }
|
nicholas@2224
|
3052 }
|
nicholas@2224
|
3053 if (passed == false)
|
nicholas@2224
|
3054 {
|
nicholas@2224
|
3055 check_pass = false;
|
b@2258
|
3056 console.log("Continue listening to track-"+object.interfaceDOM.getPresentedId());
|
nicholas@2224
|
3057 error_obj.push(object.interfaceDOM.getPresentedId());
|
nicholas@2224
|
3058 }
|
nicholas@2224
|
3059 }
|
nicholas@2224
|
3060 if (check_pass == false)
|
nicholas@2224
|
3061 {
|
nicholas@2224
|
3062 var str_start = "You have not completely listened to fragments ";
|
nicholas@2224
|
3063 for (var i=0; i<error_obj.length; i++)
|
nicholas@2224
|
3064 {
|
nicholas@2224
|
3065 str_start += error_obj[i];
|
nicholas@2224
|
3066 if (i != error_obj.length-1)
|
nicholas@2224
|
3067 {
|
nicholas@2224
|
3068 str_start += ', ';
|
nicholas@2224
|
3069 }
|
nicholas@2224
|
3070 }
|
nicholas@2224
|
3071 str_start += ". Please keep listening";
|
nicholas@2224
|
3072 console.log("[ALERT]: "+str_start);
|
nicholas@2224
|
3073 this.storeErrorNode("[ALERT]: "+str_start);
|
nicholas@2361
|
3074 interfaceContext.lightbox.post("Error",str_start);
|
nicholas@2444
|
3075 return false;
|
nicholas@2224
|
3076 }
|
nicholas@2444
|
3077 return true;
|
nicholas@2224
|
3078 };
|
nicholas@2224
|
3079 this.checkAllMoved = function()
|
nicholas@2224
|
3080 {
|
nicholas@2224
|
3081 var str = "You have not moved ";
|
nicholas@2224
|
3082 var failed = [];
|
nicholas@2224
|
3083 for (var ao of audioEngineContext.audioObjects)
|
nicholas@2224
|
3084 {
|
nicholas@2224
|
3085 if(ao.metric.wasMoved == false && ao.interfaceDOM.canMove() == true)
|
nicholas@2224
|
3086 {
|
b@2258
|
3087 failed.push(ao.interfaceDOM.getPresentedId());
|
nicholas@2224
|
3088 }
|
nicholas@2224
|
3089 }
|
nicholas@2224
|
3090 if (failed.length == 0)
|
nicholas@2224
|
3091 {
|
nicholas@2224
|
3092 return true;
|
nicholas@2224
|
3093 } else if (failed.length == 1)
|
nicholas@2224
|
3094 {
|
nicholas@2224
|
3095 str += 'track '+failed[0];
|
nicholas@2224
|
3096 } else {
|
nicholas@2224
|
3097 str += 'tracks ';
|
nicholas@2224
|
3098 for (var i=0; i<failed.length-1; i++)
|
nicholas@2224
|
3099 {
|
nicholas@2224
|
3100 str += failed[i]+', ';
|
nicholas@2224
|
3101 }
|
nicholas@2224
|
3102 str += 'and '+failed[i];
|
nicholas@2224
|
3103 }
|
nicholas@2224
|
3104 str +='.';
|
nicholas@2361
|
3105 interfaceContext.lightbox.post("Error",str);
|
nicholas@2224
|
3106 console.log(str);
|
nicholas@2224
|
3107 this.storeErrorNode(str);
|
nicholas@2224
|
3108 return false;
|
nicholas@2224
|
3109 };
|
nicholas@2224
|
3110 this.checkAllPlayed = function()
|
nicholas@2224
|
3111 {
|
nicholas@2224
|
3112 var str = "You have not played ";
|
nicholas@2224
|
3113 var failed = [];
|
nicholas@2224
|
3114 for (var ao of audioEngineContext.audioObjects)
|
nicholas@2224
|
3115 {
|
nicholas@2224
|
3116 if(ao.metric.wasListenedTo == false)
|
nicholas@2224
|
3117 {
|
b@2258
|
3118 failed.push(ao.interfaceDOM.getPresentedId());
|
nicholas@2224
|
3119 }
|
nicholas@2224
|
3120 }
|
nicholas@2224
|
3121 if (failed.length == 0)
|
nicholas@2224
|
3122 {
|
nicholas@2224
|
3123 return true;
|
nicholas@2224
|
3124 } else if (failed.length == 1)
|
nicholas@2224
|
3125 {
|
nicholas@2224
|
3126 str += 'track '+failed[0];
|
nicholas@2224
|
3127 } else {
|
nicholas@2224
|
3128 str += 'tracks ';
|
nicholas@2224
|
3129 for (var i=0; i<failed.length-1; i++)
|
nicholas@2224
|
3130 {
|
nicholas@2224
|
3131 str += failed[i]+', ';
|
nicholas@2224
|
3132 }
|
nicholas@2224
|
3133 str += 'and '+failed[i];
|
nicholas@2224
|
3134 }
|
nicholas@2224
|
3135 str +='.';
|
nicholas@2361
|
3136 interfaceContext.lightbox.post("Error",str);
|
nicholas@2224
|
3137 console.log(str);
|
nicholas@2224
|
3138 this.storeErrorNode(str);
|
nicholas@2224
|
3139 return false;
|
nicholas@2224
|
3140 };
|
nicholas@2310
|
3141 this.checkScaleRange = function(min, max) {
|
nicholas@2310
|
3142 var page = testState.getCurrentTestPage();
|
nicholas@2310
|
3143 var audioObjects = audioEngineContext.audioObjects;
|
nicholas@2310
|
3144 var state = true;
|
nicholas@2310
|
3145 var str = "Please keep listening. ";
|
nicholas@2310
|
3146 var minRanking = Infinity;
|
nicholas@2310
|
3147 var maxRanking = -Infinity;
|
nicholas@2310
|
3148 for (var ao of audioObjects) {
|
nicholas@2310
|
3149 var rank = ao.interfaceDOM.getValue();
|
nicholas@2310
|
3150 if (rank < minRanking) {minRanking = rank;}
|
nicholas@2310
|
3151 if (rank > maxRanking) {maxRanking = rank;}
|
nicholas@2310
|
3152 }
|
nicholas@2310
|
3153 if (minRanking*100 > min) {
|
nicholas@2310
|
3154 str += "At least one fragment must be below the "+min+" mark.";
|
nicholas@2310
|
3155 state = false;
|
nicholas@2310
|
3156 }
|
nicholas@2358
|
3157 if (maxRanking*100 < max) {
|
nicholas@2358
|
3158 str += "At least one fragment must be above the "+max+" mark."
|
nicholas@2310
|
3159 state = false;
|
nicholas@2310
|
3160 }
|
nicholas@2310
|
3161 if (!state) {
|
nicholas@2310
|
3162 console.log(str);
|
nicholas@2310
|
3163 this.storeErrorNode(str);
|
nicholas@2361
|
3164 interfaceContext.lightbox.post("Error",str);
|
nicholas@2310
|
3165 }
|
nicholas@2310
|
3166 return state;
|
nicholas@2310
|
3167 }
|
nicholas@2358
|
3168
|
nicholas@2224
|
3169 this.storeErrorNode = function(errorMessage)
|
nicholas@2224
|
3170 {
|
nicholas@2224
|
3171 var time = audioEngineContext.timer.getTestTime();
|
nicholas@2224
|
3172 var node = storage.document.createElement('error');
|
nicholas@2224
|
3173 node.setAttribute('time',time);
|
nicholas@2224
|
3174 node.textContent = errorMessage;
|
nicholas@2224
|
3175 testState.currentStore.XMLDOM.appendChild(node);
|
nicholas@2224
|
3176 };
|
nicholas@2224
|
3177 }
|
nicholas@2224
|
3178
|
nicholas@2224
|
3179 function Storage()
|
nicholas@2224
|
3180 {
|
nicholas@2224
|
3181 // Holds results in XML format until ready for collection
|
nicholas@2224
|
3182 this.globalPreTest = null;
|
nicholas@2224
|
3183 this.globalPostTest = null;
|
nicholas@2224
|
3184 this.testPages = [];
|
nicholas@2224
|
3185 this.document = null;
|
nicholas@2224
|
3186 this.root = null;
|
nicholas@2224
|
3187 this.state = 0;
|
nicholas@2224
|
3188
|
nicholas@2224
|
3189 this.initialise = function(existingStore)
|
nicholas@2224
|
3190 {
|
nicholas@2224
|
3191 if (existingStore == undefined) {
|
nicholas@2224
|
3192 // We need to get the sessionKey
|
nicholas@2224
|
3193 this.SessionKey.generateKey();
|
nicholas@2376
|
3194 this.document = document.implementation.createDocument(null,"waetresult",null);
|
nicholas@2224
|
3195 this.root = this.document.childNodes[0];
|
nicholas@2224
|
3196 var projectDocument = specification.projectXML;
|
nicholas@2224
|
3197 projectDocument.setAttribute('file-name',url);
|
nicholas@2224
|
3198 projectDocument.setAttribute('url',qualifyURL(url));
|
nicholas@2224
|
3199 this.root.appendChild(projectDocument);
|
nicholas@2224
|
3200 this.root.appendChild(interfaceContext.returnDateNode());
|
nicholas@2224
|
3201 this.root.appendChild(interfaceContext.returnNavigator());
|
nicholas@2224
|
3202 } else {
|
nicholas@2224
|
3203 this.document = existingStore;
|
nicholas@2294
|
3204 this.root = existingStore.firstChild;
|
nicholas@2224
|
3205 this.SessionKey.key = this.root.getAttribute("key");
|
nicholas@2224
|
3206 }
|
nicholas@2224
|
3207 if (specification.preTest != undefined){this.globalPreTest = new this.surveyNode(this,this.root,specification.preTest);}
|
nicholas@2224
|
3208 if (specification.postTest != undefined){this.globalPostTest = new this.surveyNode(this,this.root,specification.postTest);}
|
nicholas@2224
|
3209 };
|
nicholas@2224
|
3210
|
nicholas@2224
|
3211 this.SessionKey = {
|
nicholas@2224
|
3212 key: null,
|
nicholas@2224
|
3213 request: new XMLHttpRequest(),
|
nicholas@2224
|
3214 parent: this,
|
nicholas@2224
|
3215 handleEvent: function() {
|
nicholas@2224
|
3216 var parse = new DOMParser();
|
nicholas@2224
|
3217 var xml = parse.parseFromString(this.request.response,"text/xml");
|
giuliomoro@2301
|
3218 var shouldGenerateKey = true;
|
nicholas@2376
|
3219 if (this.request.response.length == 0) {
|
nicholas@2376
|
3220 console.log("Error: Server did not respond");
|
nicholas@2376
|
3221 return;
|
nicholas@2376
|
3222 }
|
nicholas@2376
|
3223 if(xml.getElementsByTagName("state").length > 0){
|
nicholas@2376
|
3224 if (xml.getElementsByTagName("state")[0].textContent == "OK") {
|
giuliomoro@2301
|
3225 this.key = xml.getAllElementsByTagName("key")[0].textContent;
|
giuliomoro@2301
|
3226 this.parent.root.setAttribute("key",this.key);
|
giuliomoro@2301
|
3227 this.parent.root.setAttribute("state","empty");
|
giuliomoro@2301
|
3228 shouldGenerateKey = false;
|
giuliomoro@2301
|
3229 }
|
giuliomoro@2301
|
3230 }
|
giuliomoro@2301
|
3231 if(shouldGenerateKey === true){
|
giuliomoro@2301
|
3232 this.generateKey();
|
giuliomoro@2301
|
3233 }
|
nicholas@2224
|
3234 },
|
nicholas@2224
|
3235 generateKey: function() {
|
nicholas@2224
|
3236 var temp_key = randomString(32);
|
nicholas@2302
|
3237 var returnURL = "";
|
nicholas@2302
|
3238 if (typeof specification.projectReturn == "string") {
|
nicholas@2302
|
3239 if (specification.projectReturn.substr(0,4) == "http") {
|
nicholas@2302
|
3240 returnURL = specification.projectReturn;
|
nicholas@2302
|
3241 }
|
nicholas@2302
|
3242 }
|
nicholas@2302
|
3243 this.request.open("GET",returnURL+"php/keygen.php?key="+temp_key,true);
|
nicholas@2224
|
3244 this.request.addEventListener("load",this);
|
nicholas@2224
|
3245 this.request.send();
|
nicholas@2224
|
3246 },
|
nicholas@2224
|
3247 update: function() {
|
nicholas@2357
|
3248 if (this.key == null) {
|
nicholas@2357
|
3249 console.log("Cannot save as key == null");
|
nicholas@2357
|
3250 return;
|
nicholas@2357
|
3251 }
|
nicholas@2224
|
3252 this.parent.root.setAttribute("state","update");
|
nicholas@2224
|
3253 var xmlhttp = new XMLHttpRequest();
|
nicholas@2302
|
3254 var returnURL = "";
|
nicholas@2302
|
3255 if (typeof specification.projectReturn == "string") {
|
nicholas@2302
|
3256 if (specification.projectReturn.substr(0,4) == "http") {
|
nicholas@2302
|
3257 returnURL = specification.projectReturn;
|
nicholas@2302
|
3258 }
|
nicholas@2302
|
3259 }
|
nicholas@2302
|
3260 xmlhttp.open("POST",returnURL+"php/save.php?key="+this.key);
|
nicholas@2224
|
3261 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
|
nicholas@2224
|
3262 xmlhttp.onerror = function(){
|
nicholas@2224
|
3263 console.log('Error updating file to server!');
|
nicholas@2224
|
3264 };
|
nicholas@2224
|
3265 var hold = document.createElement("div");
|
nicholas@2224
|
3266 var clone = this.parent.root.cloneNode(true);
|
nicholas@2224
|
3267 hold.appendChild(clone);
|
nicholas@2224
|
3268 xmlhttp.onload = function() {
|
nicholas@2224
|
3269 if (this.status >= 300) {
|
nicholas@2224
|
3270 console.log("WARNING - Could not update at this time");
|
nicholas@2224
|
3271 } else {
|
nicholas@2224
|
3272 var parser = new DOMParser();
|
nicholas@2224
|
3273 var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml");
|
nicholas@2224
|
3274 var response = xmlDoc.getElementsByTagName('response')[0];
|
nicholas@2224
|
3275 if (response.getAttribute("state") == "OK") {
|
nicholas@2224
|
3276 var file = response.getElementsByTagName("file")[0];
|
nicholas@2224
|
3277 console.log("Intermediate save: OK, written "+file.getAttribute("bytes")+"B");
|
nicholas@2224
|
3278 } else {
|
nicholas@2224
|
3279 var message = response.getElementsByTagName("message");
|
nicholas@2224
|
3280 console.log("Intermediate save: Error! "+message.textContent);
|
nicholas@2224
|
3281 }
|
nicholas@2224
|
3282 }
|
nicholas@2224
|
3283 }
|
nicholas@2224
|
3284 xmlhttp.send([hold.innerHTML]);
|
nicholas@2224
|
3285 }
|
nicholas@2224
|
3286 }
|
nicholas@2224
|
3287
|
nicholas@2224
|
3288 this.createTestPageStore = function(specification)
|
nicholas@2224
|
3289 {
|
nicholas@2224
|
3290 var store = new this.pageNode(this,specification);
|
nicholas@2224
|
3291 this.testPages.push(store);
|
nicholas@2224
|
3292 return this.testPages[this.testPages.length-1];
|
nicholas@2224
|
3293 };
|
nicholas@2224
|
3294
|
nicholas@2224
|
3295 this.surveyNode = function(parent,root,specification)
|
nicholas@2224
|
3296 {
|
nicholas@2224
|
3297 this.specification = specification;
|
nicholas@2224
|
3298 this.parent = parent;
|
nicholas@2224
|
3299 this.state = "empty";
|
nicholas@2224
|
3300 this.XMLDOM = this.parent.document.createElement('survey');
|
nicholas@2224
|
3301 this.XMLDOM.setAttribute('location',this.specification.location);
|
nicholas@2224
|
3302 this.XMLDOM.setAttribute("state",this.state);
|
nicholas@2224
|
3303 for (var optNode of this.specification.options)
|
nicholas@2224
|
3304 {
|
nicholas@2224
|
3305 if (optNode.type != 'statement')
|
nicholas@2224
|
3306 {
|
nicholas@2224
|
3307 var node = this.parent.document.createElement('surveyresult');
|
nicholas@2224
|
3308 node.setAttribute("ref",optNode.id);
|
nicholas@2224
|
3309 node.setAttribute('type',optNode.type);
|
nicholas@2224
|
3310 this.XMLDOM.appendChild(node);
|
nicholas@2224
|
3311 }
|
nicholas@2224
|
3312 }
|
nicholas@2224
|
3313 root.appendChild(this.XMLDOM);
|
nicholas@2224
|
3314
|
nicholas@2224
|
3315 this.postResult = function(node)
|
nicholas@2224
|
3316 {
|
nicholas@2224
|
3317 // From popup: node is the popupOption node containing both spec. and results
|
nicholas@2224
|
3318 // ID is the position
|
nicholas@2224
|
3319 if (node.specification.type == 'statement'){return;}
|
nicholas@2294
|
3320 var surveyresult = this.XMLDOM.firstChild;
|
nicholas@2224
|
3321 while(surveyresult != null) {
|
nicholas@2224
|
3322 if (surveyresult.getAttribute("ref") == node.specification.id)
|
nicholas@2224
|
3323 {
|
nicholas@2224
|
3324 break;
|
nicholas@2224
|
3325 }
|
nicholas@2224
|
3326 surveyresult = surveyresult.nextElementSibling;
|
nicholas@2224
|
3327 }
|
nicholas@2224
|
3328 switch(node.specification.type)
|
nicholas@2224
|
3329 {
|
nicholas@2224
|
3330 case "number":
|
nicholas@2224
|
3331 case "question":
|
nicholas@2224
|
3332 var child = this.parent.document.createElement('response');
|
nicholas@2224
|
3333 child.textContent = node.response;
|
nicholas@2224
|
3334 surveyresult.appendChild(child);
|
nicholas@2224
|
3335 break;
|
nicholas@2224
|
3336 case "radio":
|
nicholas@2224
|
3337 var child = this.parent.document.createElement('response');
|
nicholas@2224
|
3338 child.setAttribute('name',node.response.name);
|
nicholas@2224
|
3339 child.textContent = node.response.text;
|
nicholas@2224
|
3340 surveyresult.appendChild(child);
|
nicholas@2224
|
3341 break;
|
nicholas@2224
|
3342 case "checkbox":
|
nicholas@2464
|
3343 if (node.response == undefined) {
|
nicholas@2464
|
3344 surveyresult.appendChild(this.parent.document.createElement('response'));
|
nicholas@2464
|
3345 break;
|
nicholas@2464
|
3346 }
|
nicholas@2224
|
3347 for (var i=0; i<node.response.length; i++)
|
nicholas@2224
|
3348 {
|
nicholas@2224
|
3349 var checkNode = this.parent.document.createElement('response');
|
nicholas@2224
|
3350 checkNode.setAttribute('name',node.response[i].name);
|
nicholas@2224
|
3351 checkNode.setAttribute('checked',node.response[i].checked);
|
nicholas@2224
|
3352 surveyresult.appendChild(checkNode);
|
nicholas@2224
|
3353 }
|
nicholas@2224
|
3354 break;
|
nicholas@2224
|
3355 }
|
nicholas@2224
|
3356 };
|
nicholas@2224
|
3357 this.complete = function() {
|
nicholas@2224
|
3358 this.state = "complete";
|
nicholas@2224
|
3359 this.XMLDOM.setAttribute("state",this.state);
|
nicholas@2224
|
3360 }
|
nicholas@2224
|
3361 };
|
nicholas@2224
|
3362
|
nicholas@2224
|
3363 this.pageNode = function(parent,specification)
|
nicholas@2224
|
3364 {
|
nicholas@2224
|
3365 // Create one store per test page
|
nicholas@2224
|
3366 this.specification = specification;
|
nicholas@2224
|
3367 this.parent = parent;
|
nicholas@2224
|
3368 this.state = "empty";
|
nicholas@2224
|
3369 this.XMLDOM = this.parent.document.createElement('page');
|
nicholas@2224
|
3370 this.XMLDOM.setAttribute('ref',specification.id);
|
nicholas@2224
|
3371 this.XMLDOM.setAttribute('presentedId',specification.presentedId);
|
nicholas@2224
|
3372 this.XMLDOM.setAttribute("state",this.state);
|
nicholas@2224
|
3373 if (specification.preTest != undefined){this.preTest = new this.parent.surveyNode(this.parent,this.XMLDOM,this.specification.preTest);}
|
nicholas@2224
|
3374 if (specification.postTest != undefined){this.postTest = new this.parent.surveyNode(this.parent,this.XMLDOM,this.specification.postTest);}
|
nicholas@2224
|
3375
|
nicholas@2224
|
3376 // Add any page metrics
|
nicholas@2224
|
3377 var page_metric = this.parent.document.createElement('metric');
|
nicholas@2224
|
3378 this.XMLDOM.appendChild(page_metric);
|
nicholas@2224
|
3379
|
nicholas@2224
|
3380 // Add the audioelement
|
nicholas@2224
|
3381 for (var element of this.specification.audioElements)
|
nicholas@2224
|
3382 {
|
nicholas@2224
|
3383 var aeNode = this.parent.document.createElement('audioelement');
|
nicholas@2224
|
3384 aeNode.setAttribute('ref',element.id);
|
nicholas@2224
|
3385 if (element.name != undefined){aeNode.setAttribute('name',element.name)};
|
nicholas@2224
|
3386 aeNode.setAttribute('type',element.type);
|
nicholas@2224
|
3387 aeNode.setAttribute('url', element.url);
|
nicholas@2224
|
3388 aeNode.setAttribute('fqurl',qualifyURL(element.url));
|
nicholas@2224
|
3389 aeNode.setAttribute('gain', element.gain);
|
nicholas@2224
|
3390 if (element.type == 'anchor' || element.type == 'reference')
|
nicholas@2224
|
3391 {
|
nicholas@2224
|
3392 if (element.marker > 0)
|
nicholas@2224
|
3393 {
|
nicholas@2224
|
3394 aeNode.setAttribute('marker',element.marker);
|
nicholas@2224
|
3395 }
|
nicholas@2224
|
3396 }
|
nicholas@2224
|
3397 var ae_metric = this.parent.document.createElement('metric');
|
nicholas@2224
|
3398 aeNode.appendChild(ae_metric);
|
nicholas@2224
|
3399 this.XMLDOM.appendChild(aeNode);
|
nicholas@2224
|
3400 }
|
nicholas@2224
|
3401
|
nicholas@2224
|
3402 this.parent.root.appendChild(this.XMLDOM);
|
nicholas@2224
|
3403
|
nicholas@2224
|
3404 this.complete = function() {
|
nicholas@2224
|
3405 this.state = "complete";
|
nicholas@2224
|
3406 this.XMLDOM.setAttribute("state","complete");
|
nicholas@2224
|
3407 }
|
nicholas@2224
|
3408 };
|
nicholas@2224
|
3409 this.update = function() {
|
nicholas@2224
|
3410 this.SessionKey.update();
|
nicholas@2224
|
3411 }
|
nicholas@2224
|
3412 this.finish = function()
|
nicholas@2224
|
3413 {
|
nicholas@2224
|
3414 if (this.state == 0)
|
nicholas@2224
|
3415 {
|
nicholas@2224
|
3416 this.update();
|
nicholas@2224
|
3417 }
|
nicholas@2224
|
3418 this.state = 1;
|
nicholas@2446
|
3419 this.root.setAttribute("state","complete");
|
nicholas@2224
|
3420 return this.root;
|
nicholas@2224
|
3421 };
|
nicholas@2224
|
3422 }
|
nicholas@2384
|
3423
|
nicholas@2401
|
3424 var window_depedancy_callback;
|
nicholas@2401
|
3425 window_depedancy_callback = window.setInterval(function(){
|
nicholas@2401
|
3426 if (check_dependancies()) {
|
nicholas@2401
|
3427 window.clearInterval(window_depedancy_callback);
|
nicholas@2401
|
3428 onload();
|
nicholas@2401
|
3429 } else {
|
nicholas@2401
|
3430 document.getElementById("topLevelBody").innerHTML = "<h1>Loading Resources</h1>";
|
nicholas@2401
|
3431 }
|
nicholas@2401
|
3432 },100); |