annotate core.js @ 2099:0e4723c6f533

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