annotate core.js @ 1339:259a0cb6e805

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