annotate core.js @ 1347:e3bc0083d515

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