annotate core.js @ 1343:9af89ccadb59

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