annotate core.js @ 1325:d30cc402e6fa

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