annotate core.js @ 1324:95b40955f79a

--UNSTABLE-- Major revision. Updated Specification including verification. Added storage collector for XML results. Popup more stable.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Wed, 06 Jan 2016 10:36:37 +0000
parents dd7f5b4d3b4e
children 3f65e594154d
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@1318 1130
nickjillings@1318 1131 this.loopStart = function() {
nickjillings@1324 1132 this.outputGain.gain.value = this.onplayGain;
nickjillings@1318 1133 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1318 1134 };
nickjillings@1318 1135
nickjillings@1318 1136 this.loopStop = function() {
nickjillings@1318 1137 if (this.outputGain.gain.value != 0.0) {
nickjillings@1318 1138 this.outputGain.gain.value = 0.0;
nickjillings@1318 1139 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1318 1140 }
nickjillings@1318 1141 };
nickjillings@1318 1142
nickjillings@1318 1143 this.play = function(startTime) {
nickjillings@1318 1144 if (this.bufferNode == undefined && this.buffer.buffer != undefined) {
nickjillings@1318 1145 this.bufferNode = audioContext.createBufferSource();
nickjillings@1318 1146 this.bufferNode.owner = this;
nickjillings@1318 1147 this.bufferNode.connect(this.outputGain);
nickjillings@1318 1148 this.bufferNode.buffer = this.buffer.buffer;
nickjillings@1318 1149 this.bufferNode.loop = audioEngineContext.loopPlayback;
nickjillings@1318 1150 this.bufferNode.onended = function(event) {
nickjillings@1318 1151 // Safari does not like using 'this' to reference the calling object!
nickjillings@1318 1152 //event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition());
nickjillings@1318 1153 event.currentTarget.owner.stop();
nickjillings@1318 1154 };
nickjillings@1318 1155 if (this.bufferNode.loop == false) {
nickjillings@1318 1156 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1318 1157 }
nickjillings@1318 1158 this.bufferNode.start(startTime);
nickjillings@1318 1159 }
nickjillings@1318 1160 };
nickjillings@1318 1161
nickjillings@1318 1162 this.stop = function() {
nickjillings@1318 1163 if (this.bufferNode != undefined)
nickjillings@1318 1164 {
nickjillings@1318 1165 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition());
nickjillings@1318 1166 this.bufferNode.stop(0);
nickjillings@1318 1167 this.bufferNode = undefined;
nickjillings@1318 1168 }
nickjillings@1318 1169 };
nickjillings@1318 1170
nickjillings@1318 1171 this.getCurrentPosition = function() {
nickjillings@1318 1172 var time = audioEngineContext.timer.getTestTime();
nickjillings@1318 1173 if (this.bufferNode != undefined) {
nickjillings@1318 1174 if (this.bufferNode.loop == true) {
nickjillings@1318 1175 if (audioEngineContext.status == 1) {
nickjillings@1318 1176 return (time-this.metric.listenStart)%this.buffer.buffer.duration;
nickjillings@1318 1177 } else {
nickjillings@1318 1178 return 0;
nickjillings@1318 1179 }
nickjillings@1318 1180 } else {
nickjillings@1318 1181 if (this.metric.listenHold) {
nickjillings@1318 1182 return time - this.metric.listenStart;
nickjillings@1318 1183 } else {
nickjillings@1318 1184 return 0;
nickjillings@1318 1185 }
nickjillings@1318 1186 }
nickjillings@1318 1187 } else {
nickjillings@1318 1188 return 0;
nickjillings@1318 1189 }
nickjillings@1318 1190 };
nickjillings@1318 1191
nickjillings@1318 1192 this.exportXMLDOM = function() {
nickjillings@1324 1193 var file = storage.document.createElement('file');
nickjillings@1318 1194 file.setAttribute('sampleRate',this.buffer.buffer.sampleRate);
nickjillings@1318 1195 file.setAttribute('channels',this.buffer.buffer.numberOfChannels);
nickjillings@1318 1196 file.setAttribute('sampleCount',this.buffer.buffer.length);
nickjillings@1318 1197 file.setAttribute('duration',this.buffer.buffer.duration);
nickjillings@1324 1198 this.storeDOM.appendChild(file);
nickjillings@1324 1199 if (this.specification.type != 'outside-reference') {
nickjillings@1318 1200 var interfaceXML = this.interfaceDOM.exportXMLDOM(this);
nickjillings@1318 1201 if (interfaceXML.length == undefined) {
nickjillings@1324 1202 this.storeDOM.appendChild(interfaceXML);
nickjillings@1318 1203 } else {
nickjillings@1318 1204 for (var i=0; i<interfaceXML.length; i++)
nickjillings@1318 1205 {
nickjillings@1324 1206 this.storeDOM.appendChild(interfaceXML[i]);
nickjillings@1318 1207 }
nickjillings@1318 1208 }
nickjillings@1324 1209 this.storeDOM.appendChild(this.commentDOM.exportXMLDOM(this));
nickjillings@1318 1210 }
nickjillings@1324 1211 var nodes = this.metric.exportXMLDOM();
nickjillings@1324 1212 var mroot = this.storeDOM.getElementsByTagName('metric')[0];
nickjillings@1324 1213 for (var i=0; i<nodes.length; i++)
nickjillings@1324 1214 {
nickjillings@1324 1215 mroot.appendChild(nodes[i]);
nickjillings@1324 1216 }
nickjillings@1318 1217 };
nickjillings@1318 1218 }
nickjillings@1318 1219
nickjillings@1318 1220 function timer()
nickjillings@1318 1221 {
nickjillings@1318 1222 /* Timer object used in audioEngine to keep track of session timings
nickjillings@1318 1223 * Uses the timer of the web audio API, so sample resolution
nickjillings@1318 1224 */
nickjillings@1318 1225 this.testStarted = false;
nickjillings@1318 1226 this.testStartTime = 0;
nickjillings@1318 1227 this.testDuration = 0;
nickjillings@1318 1228 this.minimumTestTime = 0; // No minimum test time
nickjillings@1318 1229 this.startTest = function()
nickjillings@1318 1230 {
nickjillings@1318 1231 if (this.testStarted == false)
nickjillings@1318 1232 {
nickjillings@1318 1233 this.testStartTime = audioContext.currentTime;
nickjillings@1318 1234 this.testStarted = true;
nickjillings@1318 1235 this.updateTestTime();
nickjillings@1318 1236 audioEngineContext.metric.initialiseTest();
nickjillings@1318 1237 }
nickjillings@1318 1238 };
nickjillings@1318 1239 this.stopTest = function()
nickjillings@1318 1240 {
nickjillings@1318 1241 if (this.testStarted)
nickjillings@1318 1242 {
nickjillings@1318 1243 this.testDuration = this.getTestTime();
nickjillings@1318 1244 this.testStarted = false;
nickjillings@1318 1245 } else {
nickjillings@1318 1246 console.log('ERR: Test tried to end before beginning');
nickjillings@1318 1247 }
nickjillings@1318 1248 };
nickjillings@1318 1249 this.updateTestTime = function()
nickjillings@1318 1250 {
nickjillings@1318 1251 if (this.testStarted)
nickjillings@1318 1252 {
nickjillings@1318 1253 this.testDuration = audioContext.currentTime - this.testStartTime;
nickjillings@1318 1254 }
nickjillings@1318 1255 };
nickjillings@1318 1256 this.getTestTime = function()
nickjillings@1318 1257 {
nickjillings@1318 1258 this.updateTestTime();
nickjillings@1318 1259 return this.testDuration;
nickjillings@1318 1260 };
nickjillings@1318 1261 }
nickjillings@1318 1262
nickjillings@1318 1263 function sessionMetrics(engine,specification)
nickjillings@1318 1264 {
nickjillings@1318 1265 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
nickjillings@1318 1266 */
nickjillings@1318 1267 this.engine = engine;
nickjillings@1318 1268 this.lastClicked = -1;
nickjillings@1318 1269 this.data = -1;
nickjillings@1318 1270 this.reset = function() {
nickjillings@1318 1271 this.lastClicked = -1;
nickjillings@1318 1272 this.data = -1;
nickjillings@1318 1273 };
nickjillings@1318 1274
nickjillings@1318 1275 this.enableElementInitialPosition = false;
nickjillings@1318 1276 this.enableElementListenTracker = false;
nickjillings@1318 1277 this.enableElementTimer = false;
nickjillings@1318 1278 this.enableElementTracker = false;
nickjillings@1318 1279 this.enableFlagListenedTo = false;
nickjillings@1318 1280 this.enableFlagMoved = false;
nickjillings@1318 1281 this.enableTestTimer = false;
nickjillings@1318 1282 // Obtain the metrics enabled
nickjillings@1324 1283 for (var i=0; i<specification.metrics.enabled.length; i++)
nickjillings@1318 1284 {
nickjillings@1324 1285 var node = specification.metrics.enabled[i];
nickjillings@1324 1286 switch(node)
nickjillings@1318 1287 {
nickjillings@1318 1288 case 'testTimer':
nickjillings@1318 1289 this.enableTestTimer = true;
nickjillings@1318 1290 break;
nickjillings@1318 1291 case 'elementTimer':
nickjillings@1318 1292 this.enableElementTimer = true;
nickjillings@1318 1293 break;
nickjillings@1318 1294 case 'elementTracker':
nickjillings@1318 1295 this.enableElementTracker = true;
nickjillings@1318 1296 break;
nickjillings@1318 1297 case 'elementListenTracker':
nickjillings@1318 1298 this.enableElementListenTracker = true;
nickjillings@1318 1299 break;
nickjillings@1318 1300 case 'elementInitialPosition':
nickjillings@1318 1301 this.enableElementInitialPosition = true;
nickjillings@1318 1302 break;
nickjillings@1318 1303 case 'elementFlagListenedTo':
nickjillings@1318 1304 this.enableFlagListenedTo = true;
nickjillings@1318 1305 break;
nickjillings@1318 1306 case 'elementFlagMoved':
nickjillings@1318 1307 this.enableFlagMoved = true;
nickjillings@1318 1308 break;
nickjillings@1318 1309 case 'elementFlagComments':
nickjillings@1318 1310 this.enableFlagComments = true;
nickjillings@1318 1311 break;
nickjillings@1318 1312 }
nickjillings@1318 1313 }
nickjillings@1318 1314 this.initialiseTest = function(){};
nickjillings@1318 1315 }
nickjillings@1318 1316
nickjillings@1318 1317 function metricTracker(caller)
nickjillings@1318 1318 {
nickjillings@1318 1319 /* Custom object to track and collect metric data
nickjillings@1318 1320 * Used only inside the audioObjects object.
nickjillings@1318 1321 */
nickjillings@1318 1322
nickjillings@1318 1323 this.listenedTimer = 0;
nickjillings@1318 1324 this.listenStart = 0;
nickjillings@1318 1325 this.listenHold = false;
nickjillings@1318 1326 this.initialPosition = -1;
nickjillings@1318 1327 this.movementTracker = [];
nickjillings@1318 1328 this.listenTracker =[];
nickjillings@1318 1329 this.wasListenedTo = false;
nickjillings@1318 1330 this.wasMoved = false;
nickjillings@1318 1331 this.hasComments = false;
nickjillings@1318 1332 this.parent = caller;
nickjillings@1318 1333
nickjillings@1324 1334 this.initialise = function(position)
nickjillings@1318 1335 {
nickjillings@1318 1336 if (this.initialPosition == -1) {
nickjillings@1318 1337 this.initialPosition = position;
nickjillings@1318 1338 }
nickjillings@1318 1339 };
nickjillings@1318 1340
nickjillings@1318 1341 this.moved = function(time,position)
nickjillings@1318 1342 {
nickjillings@1318 1343 this.wasMoved = true;
nickjillings@1318 1344 this.movementTracker[this.movementTracker.length] = [time, position];
nickjillings@1318 1345 };
nickjillings@1318 1346
nickjillings@1318 1347 this.startListening = function(time)
nickjillings@1318 1348 {
nickjillings@1318 1349 if (this.listenHold == false)
nickjillings@1318 1350 {
nickjillings@1318 1351 this.wasListenedTo = true;
nickjillings@1318 1352 this.listenStart = time;
nickjillings@1318 1353 this.listenHold = true;
nickjillings@1318 1354
nickjillings@1318 1355 var evnt = document.createElement('event');
nickjillings@1318 1356 var testTime = document.createElement('testTime');
nickjillings@1318 1357 testTime.setAttribute('start',time);
nickjillings@1318 1358 var bufferTime = document.createElement('bufferTime');
nickjillings@1318 1359 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
nickjillings@1318 1360 evnt.appendChild(testTime);
nickjillings@1318 1361 evnt.appendChild(bufferTime);
nickjillings@1318 1362 this.listenTracker.push(evnt);
nickjillings@1318 1363
nickjillings@1318 1364 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1318 1365 }
nickjillings@1318 1366 };
nickjillings@1318 1367
nickjillings@1318 1368 this.stopListening = function(time,bufferStopTime)
nickjillings@1318 1369 {
nickjillings@1318 1370 if (this.listenHold == true)
nickjillings@1318 1371 {
nickjillings@1318 1372 var diff = time - this.listenStart;
nickjillings@1318 1373 this.listenedTimer += (diff);
nickjillings@1318 1374 this.listenStart = 0;
nickjillings@1318 1375 this.listenHold = false;
nickjillings@1318 1376
nickjillings@1318 1377 var evnt = this.listenTracker[this.listenTracker.length-1];
nickjillings@1318 1378 var testTime = evnt.getElementsByTagName('testTime')[0];
nickjillings@1318 1379 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
nickjillings@1318 1380 testTime.setAttribute('stop',time);
nickjillings@1318 1381 if (bufferStopTime == undefined) {
nickjillings@1318 1382 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
nickjillings@1318 1383 } else {
nickjillings@1318 1384 bufferTime.setAttribute('stop',bufferStopTime);
nickjillings@1318 1385 }
nickjillings@1318 1386 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1318 1387 }
nickjillings@1318 1388 };
nickjillings@1318 1389
nickjillings@1318 1390 this.exportXMLDOM = function() {
nickjillings@1324 1391 var storeDOM = [];
nickjillings@1318 1392 if (audioEngineContext.metric.enableElementTimer) {
nickjillings@1324 1393 var mElementTimer = storage.document.createElement('metricresult');
nickjillings@1318 1394 mElementTimer.setAttribute('name','enableElementTimer');
nickjillings@1318 1395 mElementTimer.textContent = this.listenedTimer;
nickjillings@1324 1396 storeDOM.push(mElementTimer);
nickjillings@1318 1397 }
nickjillings@1318 1398 if (audioEngineContext.metric.enableElementTracker) {
nickjillings@1324 1399 var elementTrackerFull = storage.document.createElement('metricResult');
nickjillings@1318 1400 elementTrackerFull.setAttribute('name','elementTrackerFull');
nickjillings@1318 1401 for (var k=0; k<this.movementTracker.length; k++)
nickjillings@1318 1402 {
nickjillings@1324 1403 var timePos = storage.document.createElement('timePos');
nickjillings@1318 1404 timePos.id = k;
nickjillings@1324 1405 var time = storage.document.createElement('time');
nickjillings@1318 1406 time.textContent = this.movementTracker[k][0];
nickjillings@1318 1407 var position = document.createElement('position');
nickjillings@1318 1408 position.textContent = this.movementTracker[k][1];
nickjillings@1318 1409 timePos.appendChild(time);
nickjillings@1318 1410 timePos.appendChild(position);
nickjillings@1318 1411 elementTrackerFull.appendChild(timePos);
nickjillings@1318 1412 }
nickjillings@1324 1413 storeDOM.push(elementTrackerFull);
nickjillings@1318 1414 }
nickjillings@1318 1415 if (audioEngineContext.metric.enableElementListenTracker) {
nickjillings@1324 1416 var elementListenTracker = storage.document.createElement('metricResult');
nickjillings@1318 1417 elementListenTracker.setAttribute('name','elementListenTracker');
nickjillings@1318 1418 for (var k=0; k<this.listenTracker.length; k++) {
nickjillings@1318 1419 elementListenTracker.appendChild(this.listenTracker[k]);
nickjillings@1318 1420 }
nickjillings@1324 1421 storeDOM.push(elementListenTracker);
nickjillings@1318 1422 }
nickjillings@1318 1423 if (audioEngineContext.metric.enableElementInitialPosition) {
nickjillings@1324 1424 var elementInitial = storage.document.createElement('metricResult');
nickjillings@1318 1425 elementInitial.setAttribute('name','elementInitialPosition');
nickjillings@1318 1426 elementInitial.textContent = this.initialPosition;
nickjillings@1324 1427 storeDOM.push(elementInitial);
nickjillings@1318 1428 }
nickjillings@1318 1429 if (audioEngineContext.metric.enableFlagListenedTo) {
nickjillings@1324 1430 var flagListenedTo = storage.document.createElement('metricResult');
nickjillings@1318 1431 flagListenedTo.setAttribute('name','elementFlagListenedTo');
nickjillings@1318 1432 flagListenedTo.textContent = this.wasListenedTo;
nickjillings@1324 1433 storeDOM.push(flagListenedTo);
nickjillings@1318 1434 }
nickjillings@1318 1435 if (audioEngineContext.metric.enableFlagMoved) {
nickjillings@1324 1436 var flagMoved = storage.document.createElement('metricResult');
nickjillings@1318 1437 flagMoved.setAttribute('name','elementFlagMoved');
nickjillings@1318 1438 flagMoved.textContent = this.wasMoved;
nickjillings@1324 1439 storeDOM.push(flagMoved);
nickjillings@1318 1440 }
nickjillings@1318 1441 if (audioEngineContext.metric.enableFlagComments) {
nickjillings@1324 1442 var flagComments = storage.document.createElement('metricResult');
nickjillings@1318 1443 flagComments.setAttribute('name','elementFlagComments');
nickjillings@1318 1444 if (this.parent.commentDOM == null)
nickjillings@1318 1445 {flag.textContent = 'false';}
nickjillings@1318 1446 else if (this.parent.commentDOM.textContent.length == 0)
nickjillings@1318 1447 {flag.textContent = 'false';}
nickjillings@1318 1448 else
nickjillings@1318 1449 {flag.textContet = 'true';}
nickjillings@1324 1450 storeDOM.push(flagComments);
nickjillings@1318 1451 }
nickjillings@1324 1452 return storeDOM;
nickjillings@1318 1453 };
nickjillings@1318 1454 }
nickjillings@1318 1455
nickjillings@1318 1456 function randomiseOrder(input)
nickjillings@1318 1457 {
nickjillings@1318 1458 // This takes an array of information and randomises the order
nickjillings@1318 1459 var N = input.length;
nickjillings@1318 1460
nickjillings@1318 1461 var inputSequence = []; // For safety purposes: keep track of randomisation
nickjillings@1318 1462 for (var counter = 0; counter < N; ++counter)
nickjillings@1318 1463 inputSequence.push(counter) // Fill array
nickjillings@1318 1464 var inputSequenceClone = inputSequence.slice(0);
nickjillings@1318 1465
nickjillings@1318 1466 var holdArr = [];
nickjillings@1318 1467 var outputSequence = [];
nickjillings@1318 1468 for (var n=0; n<N; n++)
nickjillings@1318 1469 {
nickjillings@1318 1470 // First pick a random number
nickjillings@1318 1471 var r = Math.random();
nickjillings@1318 1472 // Multiply and floor by the number of elements left
nickjillings@1318 1473 r = Math.floor(r*input.length);
nickjillings@1318 1474 // Pick out that element and delete from the array
nickjillings@1318 1475 holdArr.push(input.splice(r,1)[0]);
nickjillings@1318 1476 // Do the same with sequence
nickjillings@1318 1477 outputSequence.push(inputSequence.splice(r,1)[0]);
nickjillings@1318 1478 }
nickjillings@1318 1479 console.log(inputSequenceClone.toString()); // print original array to console
nickjillings@1318 1480 console.log(outputSequence.toString()); // print randomised array to console
nickjillings@1318 1481 return holdArr;
nickjillings@1318 1482 }
nickjillings@1318 1483
nickjillings@1318 1484 function returnDateNode()
nickjillings@1318 1485 {
nickjillings@1318 1486 // Create an XML Node for the Date and Time a test was conducted
nickjillings@1318 1487 // Structure is
nickjillings@1318 1488 // <datetime>
nickjillings@1318 1489 // <date year="##" month="##" day="##">DD/MM/YY</date>
nickjillings@1318 1490 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
nickjillings@1318 1491 // </datetime>
nickjillings@1318 1492 var dateTime = new Date();
nickjillings@1318 1493 var year = document.createAttribute('year');
nickjillings@1318 1494 var month = document.createAttribute('month');
nickjillings@1318 1495 var day = document.createAttribute('day');
nickjillings@1318 1496 var hour = document.createAttribute('hour');
nickjillings@1318 1497 var minute = document.createAttribute('minute');
nickjillings@1318 1498 var secs = document.createAttribute('secs');
nickjillings@1318 1499
nickjillings@1318 1500 year.nodeValue = dateTime.getFullYear();
nickjillings@1318 1501 month.nodeValue = dateTime.getMonth()+1;
nickjillings@1318 1502 day.nodeValue = dateTime.getDate();
nickjillings@1318 1503 hour.nodeValue = dateTime.getHours();
nickjillings@1318 1504 minute.nodeValue = dateTime.getMinutes();
nickjillings@1318 1505 secs.nodeValue = dateTime.getSeconds();
nickjillings@1318 1506
nickjillings@1318 1507 var hold = document.createElement("datetime");
nickjillings@1318 1508 var date = document.createElement("date");
nickjillings@1318 1509 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
nickjillings@1318 1510 var time = document.createElement("time");
nickjillings@1318 1511 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
nickjillings@1318 1512
nickjillings@1318 1513 date.setAttributeNode(year);
nickjillings@1318 1514 date.setAttributeNode(month);
nickjillings@1318 1515 date.setAttributeNode(day);
nickjillings@1318 1516 time.setAttributeNode(hour);
nickjillings@1318 1517 time.setAttributeNode(minute);
nickjillings@1318 1518 time.setAttributeNode(secs);
nickjillings@1318 1519
nickjillings@1318 1520 hold.appendChild(date);
nickjillings@1318 1521 hold.appendChild(time);
nickjillings@1318 1522 return hold;
nickjillings@1318 1523
nickjillings@1318 1524 }
nickjillings@1318 1525
nickjillings@1318 1526 function Specification() {
nickjillings@1318 1527 // Handles the decoding of the project specification XML into a simple JavaScript Object.
nickjillings@1318 1528
nickjillings@1324 1529 this.interface = null;
nickjillings@1324 1530 this.projectReturn = null;
nickjillings@1324 1531 this.randomiseOrder = null;
nickjillings@1324 1532 this.testPages = null;
nickjillings@1324 1533 this.pages = [];
nickjillings@1324 1534 this.metrics = null;
nickjillings@1324 1535 this.interfaces = null;
nickjillings@1324 1536 this.loudness = null;
nickjillings@1324 1537 this.errors = [];
nickjillings@1324 1538 this.schema = null;
nickjillings@1318 1539
nickjillings@1318 1540 this.randomiseOrder = function(input)
nickjillings@1318 1541 {
nickjillings@1318 1542 // This takes an array of information and randomises the order
nickjillings@1318 1543 var N = input.length;
nickjillings@1318 1544
nickjillings@1318 1545 var inputSequence = []; // For safety purposes: keep track of randomisation
nickjillings@1318 1546 for (var counter = 0; counter < N; ++counter)
nickjillings@1318 1547 inputSequence.push(counter) // Fill array
nickjillings@1318 1548 var inputSequenceClone = inputSequence.slice(0);
nickjillings@1318 1549
nickjillings@1318 1550 var holdArr = [];
nickjillings@1318 1551 var outputSequence = [];
nickjillings@1318 1552 for (var n=0; n<N; n++)
nickjillings@1318 1553 {
nickjillings@1318 1554 // First pick a random number
nickjillings@1318 1555 var r = Math.random();
nickjillings@1318 1556 // Multiply and floor by the number of elements left
nickjillings@1318 1557 r = Math.floor(r*input.length);
nickjillings@1318 1558 // Pick out that element and delete from the array
nickjillings@1318 1559 holdArr.push(input.splice(r,1)[0]);
nickjillings@1318 1560 // Do the same with sequence
nickjillings@1318 1561 outputSequence.push(inputSequence.splice(r,1)[0]);
nickjillings@1318 1562 }
nickjillings@1318 1563 console.log(inputSequenceClone.toString()); // print original array to console
nickjillings@1318 1564 console.log(outputSequence.toString()); // print randomised array to console
nickjillings@1318 1565 return holdArr;
nickjillings@1318 1566 };
nickjillings@1324 1567
nickjillings@1324 1568 this.processAttribute = function(attribute,schema)
nickjillings@1324 1569 {
nickjillings@1324 1570 // attribute is the string returned from getAttribute on the XML
nickjillings@1324 1571 // schema is the <xs:attribute> node
nickjillings@1324 1572 if (schema.getAttribute('name') == undefined && schema.getAttribute('ref') != undefined)
nickjillings@1324 1573 {
nickjillings@1324 1574 schema = this.schema.getElementsByName(schema.getAttribute('ref'))[0];
nickjillings@1324 1575 }
nickjillings@1324 1576 var defaultOpt = schema.getAttribute('default');
nickjillings@1324 1577 if (attribute == null) {
nickjillings@1324 1578 attribute = defaultOpt;
nickjillings@1324 1579 }
nickjillings@1324 1580 var dataType = schema.getAttribute('type');
nickjillings@1324 1581 if (typeof dataType == "string") { dataType = dataType.substr(3);}
nickjillings@1324 1582 else {dataType = "string";}
nickjillings@1324 1583 if (attribute == null)
nickjillings@1324 1584 {
nickjillings@1324 1585 return attribute;
nickjillings@1324 1586 }
nickjillings@1324 1587 switch(dataType)
nickjillings@1324 1588 {
nickjillings@1324 1589 case "boolean":
nickjillings@1324 1590 if (attribute == 'true'){attribute = true;}else{attribute=false;}
nickjillings@1324 1591 break;
nickjillings@1324 1592 case "negativeInteger":
nickjillings@1324 1593 case "positiveInteger":
nickjillings@1324 1594 case "nonNegativeInteger":
nickjillings@1324 1595 case "nonPositiveInteger":
nickjillings@1324 1596 case "integer":
nickjillings@1324 1597 case "decimal":
nickjillings@1324 1598 case "short":
nickjillings@1324 1599 attribute = Number(attribute);
nickjillings@1324 1600 break;
nickjillings@1324 1601 case "string":
nickjillings@1324 1602 default:
nickjillings@1324 1603 attribute = String(attribute);
nickjillings@1324 1604 break;
nickjillings@1324 1605 }
nickjillings@1324 1606 return attribute;
nickjillings@1324 1607 };
nickjillings@1318 1608
nickjillings@1318 1609 this.decode = function(projectXML) {
nickjillings@1324 1610 this.errors = [];
nickjillings@1318 1611 // projectXML - DOM Parsed document
nickjillings@1318 1612 this.projectXML = projectXML.childNodes[0];
nickjillings@1318 1613 var setupNode = projectXML.getElementsByTagName('setup')[0];
nickjillings@1324 1614 var schemaSetup = this.schema.getElementsByName('setup')[0];
nickjillings@1324 1615 // First decode the attributes
nickjillings@1324 1616 var attributes = schemaSetup.getElementsByTagName('attribute');
nickjillings@1324 1617 for (var i in attributes)
nickjillings@1318 1618 {
nickjillings@1324 1619 if (isNaN(Number(i)) == true){break;}
nickjillings@1324 1620 var attributeName = attributes[i].getAttribute('name');
nickjillings@1324 1621 var projectAttr = setupNode.getAttribute(attributeName);
nickjillings@1324 1622 projectAttr = this.processAttribute(projectAttr,attributes[i]);
nickjillings@1324 1623 switch(typeof projectAttr)
nickjillings@1318 1624 {
nickjillings@1324 1625 case "number":
nickjillings@1324 1626 case "boolean":
nickjillings@1324 1627 eval('this.'+attributeName+' = '+projectAttr);
nickjillings@1324 1628 break;
nickjillings@1324 1629 case "string":
nickjillings@1324 1630 eval('this.'+attributeName+' = "'+projectAttr+'"');
nickjillings@1324 1631 break;
nickjillings@1318 1632 }
nickjillings@1324 1633
nickjillings@1318 1634 }
nickjillings@1318 1635
nickjillings@1324 1636 this.metrics = {
nickjillings@1324 1637 enabled: [],
nickjillings@1324 1638 decode: function(parent, xml) {
nickjillings@1324 1639 var children = xml.getElementsByTagName('metricenable');
nickjillings@1324 1640 for (var i in children) {
nickjillings@1324 1641 if (isNaN(Number(i)) == true){break;}
nickjillings@1324 1642 this.enabled.push(children[i].textContent);
nickjillings@1324 1643 }
nickjillings@1324 1644 },
nickjillings@1324 1645 encode: function(root) {
nickjillings@1324 1646 var node = root.createElement('metric');
nickjillings@1324 1647 for (var i in this.enabled)
nickjillings@1324 1648 {
nickjillings@1324 1649 if (isNaN(Number(i)) == true){break;}
nickjillings@1324 1650 var child = root.createElement('metricenable');
nickjillings@1324 1651 child.textContent = this.enabled[i];
nickjillings@1324 1652 node.appendChild(child);
nickjillings@1324 1653 }
nickjillings@1324 1654 return node;
nickjillings@1324 1655 }
nickjillings@1324 1656 };
nickjillings@1318 1657
nickjillings@1324 1658 this.metrics.decode(this,setupNode.getElementsByTagName('metric')[0]);
nickjillings@1324 1659
nickjillings@1324 1660 // Now process the survey node options
nickjillings@1324 1661 var survey = setupNode.getElementsByTagName('survey');
nickjillings@1324 1662 var surveySchema = specification.schema.getElementsByName('survey')[0];
nickjillings@1324 1663 for (var i in survey) {
nickjillings@1324 1664 if (isNaN(Number(i)) == true){break;}
nickjillings@1324 1665 var location = survey[i].getAttribute('location');
nickjillings@1324 1666 if (location == 'pre' || location == 'before')
nickjillings@1324 1667 {
nickjillings@1324 1668 if (this.preTest != null){this.errors.push("Already a pre/before test survey defined! Ignoring second!!");}
nickjillings@1324 1669 else {
nickjillings@1324 1670 this.preTest = new this.surveyNode();
nickjillings@1324 1671 this.preTest.decode(this,survey[i],surveySchema);
nickjillings@1324 1672 }
nickjillings@1324 1673 } else if (location == 'post' || location == 'after') {
nickjillings@1324 1674 if (this.postTest != null){this.errors.push("Already a post/after test survey defined! Ignoring second!!");}
nickjillings@1324 1675 else {
nickjillings@1324 1676 this.postTest = new this.surveyNode();
nickjillings@1324 1677 this.postTest.decode(this,survey[i],surveySchema);
nickjillings@1324 1678 }
nickjillings@1318 1679 }
nickjillings@1318 1680 }
nickjillings@1318 1681
nickjillings@1324 1682 var interfaceNode = setupNode.getElementsByTagName('interface');
nickjillings@1324 1683 if (interfaceNode.length > 1)
nickjillings@1324 1684 {
nickjillings@1324 1685 this.errors.push("Only one <interface> node in the <setup> node allowed! Others except first ingnored!");
nickjillings@1324 1686 }
nickjillings@1324 1687 this.interfaces = new this.interfaceNode();
nickjillings@1324 1688 if (interfaceNode.length != 0)
nickjillings@1324 1689 {
nickjillings@1324 1690 interfaceNode = interfaceNode[0];
nickjillings@1324 1691 this.interfaces.decode(this,interfaceNode,this.schema.getElementsByName('interface')[1]);
nickjillings@1318 1692 }
nickjillings@1318 1693
nickjillings@1324 1694 // Page tags
nickjillings@1324 1695 var pageTags = projectXML.getElementsByTagName('page');
nickjillings@1324 1696 var pageSchema = this.schema.getElementsByName('page')[0];
nickjillings@1324 1697 for (var i=0; i<pageTags.length; i++)
nickjillings@1318 1698 {
nickjillings@1324 1699 var node = new this.page();
nickjillings@1324 1700 node.decode(this,pageTags[i],pageSchema);
nickjillings@1324 1701 this.pages.push(node);
nickjillings@1318 1702 }
nickjillings@1318 1703 };
nickjillings@1318 1704
nickjillings@1318 1705 this.encode = function()
nickjillings@1318 1706 {
nickjillings@1324 1707 var root = document.implementation.createDocument(null,"waet");
nickjillings@1318 1708
nickjillings@1324 1709 // Build setup node
nickjillings@1318 1710
nickjillings@1318 1711 return root;
nickjillings@1318 1712 };
nickjillings@1318 1713
nickjillings@1324 1714 this.surveyNode = function() {
nickjillings@1324 1715 this.location = null;
nickjillings@1318 1716 this.options = [];
nickjillings@1324 1717 this.schema = null;
nickjillings@1318 1718
nickjillings@1318 1719 this.OptionNode = function() {
nickjillings@1318 1720 this.type = undefined;
nickjillings@1324 1721 this.schema = undefined;
nickjillings@1318 1722 this.id = undefined;
nickjillings@1318 1723 this.mandatory = undefined;
nickjillings@1318 1724 this.statement = undefined;
nickjillings@1318 1725 this.boxsize = undefined;
nickjillings@1318 1726 this.options = [];
nickjillings@1318 1727 this.min = undefined;
nickjillings@1318 1728 this.max = undefined;
nickjillings@1318 1729 this.step = undefined;
nickjillings@1318 1730
nickjillings@1324 1731 this.decode = function(parent,child,schema)
nickjillings@1318 1732 {
nickjillings@1324 1733 this.schema = schema;
nickjillings@1324 1734 var attributeMap = schema.getElementsByTagName('attribute');
nickjillings@1324 1735 for (var i in attributeMap){
nickjillings@1324 1736 if(isNaN(Number(i)) == true){break;}
nickjillings@1324 1737 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nickjillings@1324 1738 var projectAttr = child.getAttribute(attributeName);
nickjillings@1324 1739 projectAttr = parent.processAttribute(projectAttr,attributeMap[i]);
nickjillings@1324 1740 switch(typeof projectAttr)
nickjillings@1324 1741 {
nickjillings@1324 1742 case "number":
nickjillings@1324 1743 case "boolean":
nickjillings@1324 1744 eval('this.'+attributeName+' = '+projectAttr);
nickjillings@1324 1745 break;
nickjillings@1324 1746 case "string":
nickjillings@1324 1747 eval('this.'+attributeName+' = "'+projectAttr+'"');
nickjillings@1324 1748 break;
nickjillings@1318 1749 }
nickjillings@1324 1750 }
nickjillings@1324 1751 this.statement = child.getElementsByTagName('statement')[0].textContent;
nickjillings@1324 1752 if (this.type == "checkbox" || this.type == "radio") {
nickjillings@1324 1753 var children = child.getElementsByTagName('option');
nickjillings@1324 1754 if (children.length == null) {
nickjillings@1318 1755 console.log('Malformed' +child.nodeName+ 'entry');
nickjillings@1318 1756 this.statement = 'Malformed' +child.nodeName+ 'entry';
nickjillings@1318 1757 this.type = 'statement';
nickjillings@1318 1758 } else {
nickjillings@1318 1759 this.options = [];
nickjillings@1324 1760 for (var i in children)
nickjillings@1324 1761 {
nickjillings@1324 1762 if (isNaN(Number(i))==true){break;}
nickjillings@1324 1763 this.options.push({
nickjillings@1324 1764 name: children[i].getAttribute('name'),
nickjillings@1324 1765 text: children[i].textContent
nickjillings@1324 1766 });
nickjillings@1318 1767 }
nickjillings@1318 1768 }
nickjillings@1318 1769 }
nickjillings@1318 1770 };
nickjillings@1318 1771
nickjillings@1318 1772 this.exportXML = function(root)
nickjillings@1318 1773 {
nickjillings@1324 1774 var node = root.createElement('surveyelement');
nickjillings@1324 1775 node.setAttribute('type',this.type);
nickjillings@1324 1776 var statement = root.createElement('statement');
nickjillings@1324 1777 statement.textContent = this.statement;
nickjillings@1324 1778 node.appendChild(statement);
nickjillings@1318 1779 switch(this.type)
nickjillings@1318 1780 {
nickjillings@1318 1781 case "statement":
nickjillings@1318 1782 break;
nickjillings@1318 1783 case "question":
nickjillings@1318 1784 node.id = this.id;
nickjillings@1318 1785 node.setAttribute("mandatory",this.mandatory);
nickjillings@1318 1786 node.setAttribute("boxsize",this.boxsize);
nickjillings@1318 1787 break;
nickjillings@1318 1788 case "number":
nickjillings@1318 1789 node.id = this.id;
nickjillings@1318 1790 node.setAttribute("mandatory",this.mandatory);
nickjillings@1318 1791 node.setAttribute("min", this.min);
nickjillings@1318 1792 node.setAttribute("max", this.max);
nickjillings@1318 1793 node.setAttribute("step", this.step);
nickjillings@1318 1794 break;
nickjillings@1318 1795 case "checkbox":
nickjillings@1318 1796 case "radio":
nickjillings@1318 1797 node.id = this.id;
nickjillings@1318 1798 for (var i=0; i<this.options.length; i++)
nickjillings@1318 1799 {
nickjillings@1318 1800 var option = this.options[i];
nickjillings@1318 1801 var optionNode = root.createElement("option");
nickjillings@1318 1802 optionNode.setAttribute("name",option.name);
nickjillings@1318 1803 optionNode.textContent = option.text;
nickjillings@1318 1804 node.appendChild(optionNode);
nickjillings@1318 1805 }
nickjillings@1318 1806 break;
nickjillings@1318 1807 }
nickjillings@1318 1808 return node;
nickjillings@1318 1809 };
nickjillings@1318 1810 };
nickjillings@1324 1811 this.decode = function(parent,xml,schema) {
nickjillings@1324 1812 this.schema = schema;
nickjillings@1324 1813 this.location = xml.getAttribute('location');
nickjillings@1324 1814 if (this.location == 'before'){this.location = 'pre';}
nickjillings@1324 1815 else if (this.location == 'after'){this.location = 'post';}
nickjillings@1324 1816 var surveyentrySchema = schema.getElementsByTagName('element')[0];
nickjillings@1324 1817 for (var i in xml.children)
nickjillings@1324 1818 {
nickjillings@1324 1819 if(isNaN(Number(i))==true){break;}
nickjillings@1318 1820 var node = new this.OptionNode();
nickjillings@1324 1821 node.decode(parent,xml.children[i],surveyentrySchema);
nickjillings@1318 1822 this.options.push(node);
nickjillings@1324 1823 }
nickjillings@1324 1824 };
nickjillings@1324 1825 this.encode = function(root) {
nickjillings@1324 1826 var node = root.createElement('survey');
nickjillings@1324 1827 node.setAttribute('location',this.location);
nickjillings@1324 1828 for (var i=0; i<this.options.length; i++)
nickjillings@1324 1829 {
nickjillings@1324 1830 node.appendChild(this.options[i].exportXML());
nickjillings@1324 1831 }
nickjillings@1324 1832 return node;
nickjillings@1324 1833 };
nickjillings@1324 1834 };
nickjillings@1324 1835
nickjillings@1324 1836 this.interfaceNode = function()
nickjillings@1324 1837 {
nickjillings@1324 1838 this.title = null;
nickjillings@1324 1839 this.name = null;
nickjillings@1324 1840 this.options = [];
nickjillings@1324 1841 this.scales = [];
nickjillings@1324 1842 this.schema = null;
nickjillings@1324 1843
nickjillings@1324 1844 this.decode = function(parent,xml,schema) {
nickjillings@1324 1845 this.schema = schema;
nickjillings@1324 1846 this.name = xml.getAttribute('name');
nickjillings@1324 1847 var titleNode = xml.getElementsByTagName('title');
nickjillings@1324 1848 if (titleNode.length == 1)
nickjillings@1324 1849 {
nickjillings@1324 1850 this.title = titleNode[0].textContent;
nickjillings@1324 1851 }
nickjillings@1324 1852 var interfaceOptionNodes = xml.getElementsByTagName('interfaceoption');
nickjillings@1324 1853 // Extract interfaceoption node schema
nickjillings@1324 1854 var interfaceOptionNodeSchema = schema.getElementsByTagName('element');
nickjillings@1324 1855 for (var i=0; i<interfaceOptionNodeSchema.length; i++) {
nickjillings@1324 1856 if (interfaceOptionNodeSchema[i].getAttribute('name') == 'interfaceoption') {
nickjillings@1324 1857 interfaceOptionNodeSchema = interfaceOptionNodeSchema[i];
nickjillings@1324 1858 break;
nickjillings@1324 1859 }
nickjillings@1324 1860 }
nickjillings@1324 1861 var attributeMap = interfaceOptionNodeSchema.getElementsByTagName('attribute');
nickjillings@1324 1862 for (var i=0; i<interfaceOptionNodes.length; i++)
nickjillings@1324 1863 {
nickjillings@1324 1864 var ioNode = interfaceOptionNodes[i];
nickjillings@1324 1865 var option = {};
nickjillings@1324 1866 for (var j=0; j<attributeMap.length; j++)
nickjillings@1324 1867 {
nickjillings@1324 1868 var attributeName = attributeMap[j].getAttribute('name') || attributeMap[j].getAttribute('ref');
nickjillings@1324 1869 var projectAttr = ioNode.getAttribute(attributeName);
nickjillings@1324 1870 projectAttr = parent.processAttribute(projectAttr,attributeMap[j]);
nickjillings@1324 1871 switch(typeof projectAttr)
nickjillings@1324 1872 {
nickjillings@1324 1873 case "number":
nickjillings@1324 1874 case "boolean":
nickjillings@1324 1875 eval('option.'+attributeName+' = '+projectAttr);
nickjillings@1324 1876 break;
nickjillings@1324 1877 case "string":
nickjillings@1324 1878 eval('option.'+attributeName+' = "'+projectAttr+'"');
nickjillings@1324 1879 break;
nickjillings@1324 1880 }
nickjillings@1324 1881 }
nickjillings@1324 1882 this.options.push(option);
nickjillings@1324 1883 }
nickjillings@1324 1884
nickjillings@1324 1885 // Now the scales nodes
nickjillings@1324 1886 var scaleParent = xml.getElementsByTagName('scales');
nickjillings@1324 1887 if (scaleParent.length == 1) {
nickjillings@1324 1888 scaleParent = scaleParent[0];
nickjillings@1324 1889 for (var i=0; i<scaleParent.children.length; i++) {
nickjillings@1324 1890 var child = scaleParent.children[i];
nickjillings@1324 1891 this.scales.push({
nickjillings@1324 1892 text: child.textContent,
nickjillings@1324 1893 position: Number(child.getAttribute('position'))
nickjillings@1324 1894 });
nickjillings@1318 1895 }
nickjillings@1318 1896 }
nickjillings@1318 1897 };
nickjillings@1324 1898
nickjillings@1324 1899 this.encode = function(root) {
nickjillings@1324 1900
nickjillings@1324 1901 };
nickjillings@1318 1902 };
nickjillings@1318 1903
nickjillings@1324 1904 this.page = function() {
nickjillings@1318 1905 this.presentedId = undefined;
nickjillings@1318 1906 this.id = undefined;
nickjillings@1318 1907 this.hostURL = undefined;
nickjillings@1318 1908 this.randomiseOrder = undefined;
nickjillings@1318 1909 this.loop = undefined;
nickjillings@1324 1910 this.showElementComments = undefined;
nickjillings@1318 1911 this.outsideReference = null;
nickjillings@1318 1912 this.loudness = null;
nickjillings@1324 1913 this.preTest = null;
nickjillings@1324 1914 this.postTest = null;
nickjillings@1318 1915 this.interfaces = [];
nickjillings@1318 1916 this.commentBoxPrefix = "Comment on track";
nickjillings@1318 1917 this.audioElements = [];
nickjillings@1318 1918 this.commentQuestions = [];
nickjillings@1324 1919 this.schema = null;
nickjillings@1318 1920
nickjillings@1324 1921 this.decode = function(parent,xml,schema)
nickjillings@1318 1922 {
nickjillings@1324 1923 this.schema = schema;
nickjillings@1324 1924 var attributeMap = this.schema.getElementsByTagName('attribute');
nickjillings@1324 1925 for (var i=0; i<attributeMap.length; i++)
nickjillings@1318 1926 {
nickjillings@1324 1927 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nickjillings@1324 1928 var projectAttr = xml.getAttribute(attributeName);
nickjillings@1324 1929 projectAttr = parent.processAttribute(projectAttr,attributeMap[i]);
nickjillings@1324 1930 switch(typeof projectAttr)
nickjillings@1318 1931 {
nickjillings@1324 1932 case "number":
nickjillings@1324 1933 case "boolean":
nickjillings@1324 1934 eval('this.'+attributeName+' = '+projectAttr);
nickjillings@1324 1935 break;
nickjillings@1324 1936 case "string":
nickjillings@1324 1937 eval('this.'+attributeName+' = "'+projectAttr+'"');
nickjillings@1324 1938 break;
nickjillings@1318 1939 }
nickjillings@1318 1940 }
nickjillings@1318 1941
nickjillings@1324 1942 // Get the Comment Box Prefix
nickjillings@1324 1943 var CBP = xml.getElementsByTagName('commentboxprefix');
nickjillings@1324 1944 if (CBP.length != 0) {
nickjillings@1324 1945 this.commentBoxPrefix = CBP[0].textContent;
nickjillings@1318 1946 }
nickjillings@1318 1947
nickjillings@1324 1948 // Now decode the interfaces
nickjillings@1324 1949 var interfaceNode = xml.getElementsByTagName('interface');
nickjillings@1324 1950 for (var i=0; i<interfaceNode.length; i++)
nickjillings@1324 1951 {
nickjillings@1324 1952 var node = new parent.interfaceNode();
nickjillings@1324 1953 node.decode(this,interfaceNode[i],parent.schema.getElementsByName('interface')[1]);
nickjillings@1324 1954 this.interfaces.push(node);
nickjillings@1324 1955 }
nickjillings@1318 1956
nickjillings@1324 1957 // Now process the survey node options
nickjillings@1324 1958 var survey = xml.getElementsByTagName('survey');
nickjillings@1324 1959 var surveySchema = parent.schema.getElementsByName('survey')[0];
nickjillings@1324 1960 for (var i in survey) {
nickjillings@1324 1961 if (isNaN(Number(i)) == true){break;}
nickjillings@1324 1962 var location = survey[i].getAttribute('location');
nickjillings@1324 1963 if (location == 'pre' || location == 'before')
nickjillings@1324 1964 {
nickjillings@1324 1965 if (this.preTest != null){this.errors.push("Already a pre/before test survey defined! Ignoring second!!");}
nickjillings@1324 1966 else {
nickjillings@1324 1967 this.preTest = new parent.surveyNode();
nickjillings@1324 1968 this.preTest.decode(parent,survey[i],surveySchema);
nickjillings@1324 1969 }
nickjillings@1324 1970 } else if (location == 'post' || location == 'after') {
nickjillings@1324 1971 if (this.postTest != null){this.errors.push("Already a post/after test survey defined! Ignoring second!!");}
nickjillings@1324 1972 else {
nickjillings@1324 1973 this.postTest = new parent.surveyNode();
nickjillings@1324 1974 this.postTest.decode(parent,survey[i],surveySchema);
nickjillings@1324 1975 }
nickjillings@1324 1976 }
nickjillings@1324 1977 }
nickjillings@1324 1978
nickjillings@1324 1979 // Now process the audioelement tags
nickjillings@1324 1980 var audioElements = xml.getElementsByTagName('audioelement');
nickjillings@1324 1981 var audioElementSchema = parent.schema.getElementsByName('audioelement')[0];
nickjillings@1324 1982 for (var i=0; i<audioElements.length; i++)
nickjillings@1324 1983 {
nickjillings@1324 1984 var node = new this.audioElementNode();
nickjillings@1324 1985 node.decode(this,audioElements[i],audioElementSchema);
nickjillings@1324 1986 this.audioElements.push(node);
nickjillings@1324 1987 }
nickjillings@1324 1988
nickjillings@1324 1989 // Now decode the commentquestions
nickjillings@1324 1990 var commentQuestions = xml.getElementsByTagName('commentquestion');
nickjillings@1324 1991 var commentQuestionSchema = parent.schema.getElementsByName('commentquestion')[0];
nickjillings@1324 1992 for (var i=0; i<commentQuestions.length; i++)
nickjillings@1324 1993 {
nickjillings@1318 1994 var node = new this.commentQuestionNode();
nickjillings@1324 1995 node.decode(parent,commentQuestions[i],commentQuestionSchema);
nickjillings@1318 1996 this.commentQuestions.push(node);
nickjillings@1318 1997 }
nickjillings@1318 1998 };
nickjillings@1318 1999
nickjillings@1318 2000 this.encode = function(root)
nickjillings@1318 2001 {
nickjillings@1318 2002 var AHNode = root.createElement("audioHolder");
nickjillings@1318 2003 AHNode.id = this.id;
nickjillings@1318 2004 AHNode.setAttribute("hostURL",this.hostURL);
nickjillings@1318 2005 AHNode.setAttribute("sampleRate",this.sampleRate);
nickjillings@1318 2006 AHNode.setAttribute("randomiseOrder",this.randomiseOrder);
nickjillings@1318 2007 AHNode.setAttribute("repeatCount",this.repeatCount);
nickjillings@1318 2008 AHNode.setAttribute("loop",this.loop);
nickjillings@1318 2009 AHNode.setAttribute("elementComments",this.elementComments);
nickjillings@1318 2010 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);}
nickjillings@1318 2011 if(this.initialPosition != null) {
nickjillings@1318 2012 AHNode.setAttribute("loudness",this.initialPosition*100);
nickjillings@1318 2013 }
nickjillings@1318 2014 for (var i=0; i<this.interfaces.length; i++)
nickjillings@1318 2015 {
nickjillings@1318 2016 AHNode.appendChild(this.interfaces[i].encode(root));
nickjillings@1318 2017 }
nickjillings@1318 2018
nickjillings@1318 2019 for (var i=0; i<this.audioElements.length; i++) {
nickjillings@1318 2020 AHNode.appendChild(this.audioElements[i].encode(root));
nickjillings@1318 2021 }
nickjillings@1318 2022 // Create <CommentQuestion>
nickjillings@1318 2023 for (var i=0; i<this.commentQuestions.length; i++)
nickjillings@1318 2024 {
nickjillings@1318 2025 AHNode.appendChild(this.commentQuestions[i].exportXML(root));
nickjillings@1318 2026 }
nickjillings@1318 2027
nickjillings@1318 2028 // Create <PreTest>
nickjillings@1318 2029 var AHPreTest = root.createElement("PreTest");
nickjillings@1318 2030 for (var i=0; i<this.preTest.options.length; i++)
nickjillings@1318 2031 {
nickjillings@1318 2032 AHPreTest.appendChild(this.preTest.options[i].exportXML(root));
nickjillings@1318 2033 }
nickjillings@1318 2034
nickjillings@1318 2035 var AHPostTest = root.createElement("PostTest");
nickjillings@1318 2036 for (var i=0; i<this.postTest.options.length; i++)
nickjillings@1318 2037 {
nickjillings@1318 2038 AHPostTest.appendChild(this.postTest.options[i].exportXML(root));
nickjillings@1318 2039 }
nickjillings@1318 2040 AHNode.appendChild(AHPreTest);
nickjillings@1318 2041 AHNode.appendChild(AHPostTest);
nickjillings@1318 2042 return AHNode;
nickjillings@1318 2043 };
nickjillings@1318 2044
nickjillings@1324 2045 this.commentQuestionNode = function() {
nickjillings@1324 2046 this.id = null;
nickjillings@1324 2047 this.type = undefined;
nickjillings@1318 2048 this.options = [];
nickjillings@1324 2049 this.statement = undefined;
nickjillings@1324 2050 this.schema = null;
nickjillings@1324 2051 this.decode = function(parent,xml,schema)
nickjillings@1318 2052 {
nickjillings@1324 2053 this.id = xml.id;
nickjillings@1324 2054 this.type = xml.getAttribute('type');
nickjillings@1324 2055 this.statement = xml.getElementsByTagName('statement')[0].textContent;
nickjillings@1324 2056 var optNodes = xml.getElementsByTagName('option');
nickjillings@1324 2057 for (var i=0; i<optNodes.length; i++)
nickjillings@1324 2058 {
nickjillings@1324 2059 var optNode = optNodes[i];
nickjillings@1324 2060 this.options.push({
nickjillings@1324 2061 name: optNode.getAttribute('name'),
nickjillings@1324 2062 text: optNode.textContent
nickjillings@1324 2063 });
nickjillings@1318 2064 }
nickjillings@1318 2065 };
nickjillings@1324 2066
nickjillings@1318 2067 this.encode = function(root)
nickjillings@1318 2068 {
nickjillings@1324 2069
nickjillings@1318 2070 };
nickjillings@1318 2071 };
nickjillings@1318 2072
nickjillings@1318 2073 this.audioElementNode = function() {
nickjillings@1318 2074 this.url = null;
nickjillings@1318 2075 this.id = null;
nickjillings@1318 2076 this.parent = null;
nickjillings@1324 2077 this.type = null;
nickjillings@1318 2078 this.marker = false;
nickjillings@1318 2079 this.enforce = false;
nickjillings@1318 2080 this.gain = 1.0;
nickjillings@1324 2081 this.schema = null;
nickjillings@1324 2082 this.parent = null;
nickjillings@1324 2083 this.decode = function(parent,xml,schema)
nickjillings@1318 2084 {
nickjillings@1324 2085 this.schema = schema;
nickjillings@1318 2086 this.parent = parent;
nickjillings@1324 2087 var attributeMap = this.schema.getElementsByTagName('attribute');
nickjillings@1324 2088 for (var i=0; i<attributeMap.length; i++)
nickjillings@1318 2089 {
nickjillings@1324 2090 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nickjillings@1324 2091 var projectAttr = xml.getAttribute(attributeName);
nickjillings@1324 2092 projectAttr = specification.processAttribute(projectAttr,attributeMap[i]);
nickjillings@1324 2093 switch(typeof projectAttr)
nickjillings@1318 2094 {
nickjillings@1324 2095 case "number":
nickjillings@1324 2096 case "boolean":
nickjillings@1324 2097 eval('this.'+attributeName+' = '+projectAttr);
nickjillings@1324 2098 break;
nickjillings@1324 2099 case "string":
nickjillings@1324 2100 eval('this.'+attributeName+' = "'+projectAttr+'"');
nickjillings@1324 2101 break;
nickjillings@1318 2102 }
nickjillings@1318 2103 }
nickjillings@1324 2104
nickjillings@1318 2105 };
nickjillings@1318 2106 this.encode = function(root)
nickjillings@1318 2107 {
nickjillings@1318 2108 var AENode = root.createElement("audioElements");
nickjillings@1318 2109 AENode.id = this.id;
nickjillings@1318 2110 AENode.setAttribute("url",this.url);
nickjillings@1318 2111 AENode.setAttribute("type",this.type);
nickjillings@1318 2112 AENode.setAttribute("gain",linearToDecibel(this.gain));
nickjillings@1318 2113 if (this.marker != false)
nickjillings@1318 2114 {
nickjillings@1318 2115 AENode.setAttribute("marker",this.marker*100);
nickjillings@1318 2116 }
nickjillings@1318 2117 return AENode;
nickjillings@1318 2118 };
nickjillings@1318 2119 };
nickjillings@1318 2120 };
nickjillings@1318 2121 }
nickjillings@1318 2122
nickjillings@1318 2123 function Interface(specificationObject) {
nickjillings@1318 2124 // This handles the bindings between the interface and the audioEngineContext;
nickjillings@1318 2125 this.specification = specificationObject;
nickjillings@1318 2126 this.insertPoint = document.getElementById("topLevelBody");
nickjillings@1318 2127
nickjillings@1324 2128 this.newPage = function(audioHolderObject,store)
nickjillings@1318 2129 {
nickjillings@1324 2130 audioEngineContext.newTestPage(store);
nickjillings@1318 2131 /// CHECK FOR SAMPLE RATE COMPATIBILITY
nickjillings@1318 2132 if (audioHolderObject.sampleRate != undefined) {
nickjillings@1318 2133 if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) {
nickjillings@1318 2134 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 2135 alert(errStr);
nickjillings@1318 2136 return;
nickjillings@1318 2137 }
nickjillings@1318 2138 }
nickjillings@1318 2139
nickjillings@1318 2140 audioEngineContext.loopPlayback = audioHolderObject.loop;
nickjillings@1318 2141 // Delete any previous audioObjects associated with the audioEngine
nickjillings@1318 2142 audioEngineContext.audioObjects = [];
nickjillings@1318 2143 interfaceContext.deleteCommentBoxes();
nickjillings@1318 2144 interfaceContext.deleteCommentQuestions();
nickjillings@1324 2145 loadTest(audioHolderObject,store);
nickjillings@1318 2146 };
nickjillings@1318 2147
nickjillings@1318 2148 // Bounded by interface!!
nickjillings@1318 2149 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
nickjillings@1318 2150 // For example, APE returns the slider position normalised in a <value> tag.
nickjillings@1318 2151 this.interfaceObjects = [];
nickjillings@1318 2152 this.interfaceObject = function(){};
nickjillings@1318 2153
nickjillings@1318 2154 this.resizeWindow = function(event)
nickjillings@1318 2155 {
nickjillings@1318 2156 popup.resize(event);
nickjillings@1318 2157 for(var i=0; i<this.commentBoxes.length; i++)
nickjillings@1318 2158 {this.commentBoxes[i].resize();}
nickjillings@1318 2159 for(var i=0; i<this.commentQuestions.length; i++)
nickjillings@1318 2160 {this.commentQuestions[i].resize();}
nickjillings@1318 2161 try
nickjillings@1318 2162 {
nickjillings@1318 2163 resizeWindow(event);
nickjillings@1318 2164 }
nickjillings@1318 2165 catch(err)
nickjillings@1318 2166 {
nickjillings@1318 2167 console.log("Warning - Interface does not have Resize option");
nickjillings@1318 2168 console.log(err);
nickjillings@1318 2169 }
nickjillings@1318 2170 };
nickjillings@1318 2171
nickjillings@1318 2172 this.returnNavigator = function()
nickjillings@1318 2173 {
nickjillings@1318 2174 var node = document.createElement("navigator");
nickjillings@1318 2175 var platform = document.createElement("platform");
nickjillings@1318 2176 platform.textContent = navigator.platform;
nickjillings@1318 2177 var vendor = document.createElement("vendor");
nickjillings@1318 2178 vendor.textContent = navigator.vendor;
nickjillings@1318 2179 var userAgent = document.createElement("uagent");
nickjillings@1318 2180 userAgent.textContent = navigator.userAgent;
nickjillings@1318 2181 node.appendChild(platform);
nickjillings@1318 2182 node.appendChild(vendor);
nickjillings@1318 2183 node.appendChild(userAgent);
nickjillings@1318 2184 return node;
nickjillings@1318 2185 };
nickjillings@1318 2186
nickjillings@1318 2187 this.commentBoxes = [];
nickjillings@1318 2188 this.elementCommentBox = function(audioObject) {
nickjillings@1318 2189 var element = audioObject.specification;
nickjillings@1318 2190 this.audioObject = audioObject;
nickjillings@1318 2191 this.id = audioObject.id;
nickjillings@1318 2192 var audioHolderObject = audioObject.specification.parent;
nickjillings@1318 2193 // Create document objects to hold the comment boxes
nickjillings@1318 2194 this.trackComment = document.createElement('div');
nickjillings@1318 2195 this.trackComment.className = 'comment-div';
nickjillings@1318 2196 this.trackComment.id = 'comment-div-'+audioObject.id;
nickjillings@1318 2197 // Create a string next to each comment asking for a comment
nickjillings@1318 2198 this.trackString = document.createElement('span');
nickjillings@1318 2199 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id;
nickjillings@1318 2200 // Create the HTML5 comment box 'textarea'
nickjillings@1318 2201 this.trackCommentBox = document.createElement('textarea');
nickjillings@1318 2202 this.trackCommentBox.rows = '4';
nickjillings@1318 2203 this.trackCommentBox.cols = '100';
nickjillings@1318 2204 this.trackCommentBox.name = 'trackComment'+audioObject.id;
nickjillings@1318 2205 this.trackCommentBox.className = 'trackComment';
nickjillings@1318 2206 var br = document.createElement('br');
nickjillings@1318 2207 // Add to the holder.
nickjillings@1318 2208 this.trackComment.appendChild(this.trackString);
nickjillings@1318 2209 this.trackComment.appendChild(br);
nickjillings@1318 2210 this.trackComment.appendChild(this.trackCommentBox);
nickjillings@1318 2211
nickjillings@1318 2212 this.exportXMLDOM = function() {
nickjillings@1318 2213 var root = document.createElement('comment');
nickjillings@1318 2214 if (this.audioObject.specification.parent.elementComments) {
nickjillings@1318 2215 var question = document.createElement('question');
nickjillings@1318 2216 question.textContent = this.trackString.textContent;
nickjillings@1318 2217 var response = document.createElement('response');
nickjillings@1318 2218 response.textContent = this.trackCommentBox.value;
nickjillings@1318 2219 console.log("Comment frag-"+this.id+": "+response.textContent);
nickjillings@1318 2220 root.appendChild(question);
nickjillings@1318 2221 root.appendChild(response);
nickjillings@1318 2222 }
nickjillings@1318 2223 return root;
nickjillings@1318 2224 };
nickjillings@1318 2225 this.resize = function()
nickjillings@1318 2226 {
nickjillings@1318 2227 var boxwidth = (window.innerWidth-100)/2;
nickjillings@1318 2228 if (boxwidth >= 600)
nickjillings@1318 2229 {
nickjillings@1318 2230 boxwidth = 600;
nickjillings@1318 2231 }
nickjillings@1318 2232 else if (boxwidth < 400)
nickjillings@1318 2233 {
nickjillings@1318 2234 boxwidth = 400;
nickjillings@1318 2235 }
nickjillings@1318 2236 this.trackComment.style.width = boxwidth+"px";
nickjillings@1318 2237 this.trackCommentBox.style.width = boxwidth-6+"px";
nickjillings@1318 2238 };
nickjillings@1318 2239 this.resize();
nickjillings@1318 2240 };
nickjillings@1318 2241
nickjillings@1318 2242 this.commentQuestions = [];
nickjillings@1318 2243
nickjillings@1318 2244 this.commentBox = function(commentQuestion) {
nickjillings@1318 2245 this.specification = commentQuestion;
nickjillings@1318 2246 // Create document objects to hold the comment boxes
nickjillings@1318 2247 this.holder = document.createElement('div');
nickjillings@1318 2248 this.holder.className = 'comment-div';
nickjillings@1318 2249 // Create a string next to each comment asking for a comment
nickjillings@1318 2250 this.string = document.createElement('span');
nickjillings@1324 2251 this.string.innerHTML = commentQuestion.statement;
nickjillings@1318 2252 // Create the HTML5 comment box 'textarea'
nickjillings@1318 2253 this.textArea = document.createElement('textarea');
nickjillings@1318 2254 this.textArea.rows = '4';
nickjillings@1318 2255 this.textArea.cols = '100';
nickjillings@1318 2256 this.textArea.className = 'trackComment';
nickjillings@1318 2257 var br = document.createElement('br');
nickjillings@1318 2258 // Add to the holder.
nickjillings@1318 2259 this.holder.appendChild(this.string);
nickjillings@1318 2260 this.holder.appendChild(br);
nickjillings@1318 2261 this.holder.appendChild(this.textArea);
nickjillings@1318 2262
nickjillings@1318 2263 this.exportXMLDOM = function() {
nickjillings@1318 2264 var root = document.createElement('comment');
nickjillings@1318 2265 root.id = this.specification.id;
nickjillings@1318 2266 root.setAttribute('type',this.specification.type);
nickjillings@1318 2267 root.textContent = this.textArea.value;
nickjillings@1318 2268 console.log("Question: "+this.string.textContent);
nickjillings@1318 2269 console.log("Response: "+root.textContent);
nickjillings@1318 2270 return root;
nickjillings@1318 2271 };
nickjillings@1318 2272 this.resize = function()
nickjillings@1318 2273 {
nickjillings@1318 2274 var boxwidth = (window.innerWidth-100)/2;
nickjillings@1318 2275 if (boxwidth >= 600)
nickjillings@1318 2276 {
nickjillings@1318 2277 boxwidth = 600;
nickjillings@1318 2278 }
nickjillings@1318 2279 else if (boxwidth < 400)
nickjillings@1318 2280 {
nickjillings@1318 2281 boxwidth = 400;
nickjillings@1318 2282 }
nickjillings@1318 2283 this.holder.style.width = boxwidth+"px";
nickjillings@1318 2284 this.textArea.style.width = boxwidth-6+"px";
nickjillings@1318 2285 };
nickjillings@1318 2286 this.resize();
nickjillings@1318 2287 };
nickjillings@1318 2288
nickjillings@1318 2289 this.radioBox = function(commentQuestion) {
nickjillings@1318 2290 this.specification = commentQuestion;
nickjillings@1318 2291 // Create document objects to hold the comment boxes
nickjillings@1318 2292 this.holder = document.createElement('div');
nickjillings@1318 2293 this.holder.className = 'comment-div';
nickjillings@1318 2294 // Create a string next to each comment asking for a comment
nickjillings@1318 2295 this.string = document.createElement('span');
nickjillings@1318 2296 this.string.innerHTML = commentQuestion.statement;
nickjillings@1318 2297 var br = document.createElement('br');
nickjillings@1318 2298 // Add to the holder.
nickjillings@1318 2299 this.holder.appendChild(this.string);
nickjillings@1318 2300 this.holder.appendChild(br);
nickjillings@1318 2301 this.options = [];
nickjillings@1318 2302 this.inputs = document.createElement('div');
nickjillings@1318 2303 this.span = document.createElement('div');
nickjillings@1318 2304 this.inputs.align = 'center';
nickjillings@1318 2305 this.inputs.style.marginLeft = '12px';
nickjillings@1318 2306 this.span.style.marginLeft = '12px';
nickjillings@1318 2307 this.span.align = 'center';
nickjillings@1318 2308 this.span.style.marginTop = '15px';
nickjillings@1318 2309
nickjillings@1318 2310 var optCount = commentQuestion.options.length;
nickjillings@1324 2311 for (var optNode of commentQuestion.options)
nickjillings@1318 2312 {
nickjillings@1318 2313 var div = document.createElement('div');
nickjillings@1318 2314 div.style.width = '80px';
nickjillings@1318 2315 div.style.float = 'left';
nickjillings@1318 2316 var input = document.createElement('input');
nickjillings@1318 2317 input.type = 'radio';
nickjillings@1318 2318 input.name = commentQuestion.id;
nickjillings@1324 2319 input.setAttribute('setvalue',optNode.name);
nickjillings@1318 2320 input.className = 'comment-radio';
nickjillings@1318 2321 div.appendChild(input);
nickjillings@1318 2322 this.inputs.appendChild(div);
nickjillings@1318 2323
nickjillings@1318 2324
nickjillings@1318 2325 div = document.createElement('div');
nickjillings@1318 2326 div.style.width = '80px';
nickjillings@1318 2327 div.style.float = 'left';
nickjillings@1318 2328 div.align = 'center';
nickjillings@1318 2329 var span = document.createElement('span');
nickjillings@1324 2330 span.textContent = optNode.text;
nickjillings@1318 2331 span.className = 'comment-radio-span';
nickjillings@1318 2332 div.appendChild(span);
nickjillings@1318 2333 this.span.appendChild(div);
nickjillings@1318 2334 this.options.push(input);
nickjillings@1318 2335 }
nickjillings@1318 2336 this.holder.appendChild(this.span);
nickjillings@1318 2337 this.holder.appendChild(this.inputs);
nickjillings@1318 2338
nickjillings@1318 2339 this.exportXMLDOM = function() {
nickjillings@1318 2340 var root = document.createElement('comment');
nickjillings@1318 2341 root.id = this.specification.id;
nickjillings@1318 2342 root.setAttribute('type',this.specification.type);
nickjillings@1318 2343 var question = document.createElement('question');
nickjillings@1318 2344 question.textContent = this.string.textContent;
nickjillings@1318 2345 var response = document.createElement('response');
nickjillings@1318 2346 var i=0;
nickjillings@1318 2347 while(this.options[i].checked == false) {
nickjillings@1318 2348 i++;
nickjillings@1318 2349 if (i >= this.options.length) {
nickjillings@1318 2350 break;
nickjillings@1318 2351 }
nickjillings@1318 2352 }
nickjillings@1318 2353 if (i >= this.options.length) {
nickjillings@1318 2354 response.textContent = 'null';
nickjillings@1318 2355 } else {
nickjillings@1318 2356 response.textContent = this.options[i].getAttribute('setvalue');
nickjillings@1318 2357 response.setAttribute('number',i);
nickjillings@1318 2358 }
nickjillings@1318 2359 console.log('Comment: '+question.textContent);
nickjillings@1318 2360 console.log('Response: '+response.textContent);
nickjillings@1318 2361 root.appendChild(question);
nickjillings@1318 2362 root.appendChild(response);
nickjillings@1318 2363 return root;
nickjillings@1318 2364 };
nickjillings@1318 2365 this.resize = function()
nickjillings@1318 2366 {
nickjillings@1318 2367 var boxwidth = (window.innerWidth-100)/2;
nickjillings@1318 2368 if (boxwidth >= 600)
nickjillings@1318 2369 {
nickjillings@1318 2370 boxwidth = 600;
nickjillings@1318 2371 }
nickjillings@1318 2372 else if (boxwidth < 400)
nickjillings@1318 2373 {
nickjillings@1318 2374 boxwidth = 400;
nickjillings@1318 2375 }
nickjillings@1318 2376 this.holder.style.width = boxwidth+"px";
nickjillings@1318 2377 var text = this.holder.children[2];
nickjillings@1318 2378 var options = this.holder.children[3];
nickjillings@1318 2379 var optCount = options.children.length;
nickjillings@1318 2380 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
nickjillings@1318 2381 var options = options.firstChild;
nickjillings@1318 2382 var text = text.firstChild;
nickjillings@1318 2383 options.style.marginRight = spanMargin;
nickjillings@1318 2384 options.style.marginLeft = spanMargin;
nickjillings@1318 2385 text.style.marginRight = spanMargin;
nickjillings@1318 2386 text.style.marginLeft = spanMargin;
nickjillings@1318 2387 while(options.nextSibling != undefined)
nickjillings@1318 2388 {
nickjillings@1318 2389 options = options.nextSibling;
nickjillings@1318 2390 text = text.nextSibling;
nickjillings@1318 2391 options.style.marginRight = spanMargin;
nickjillings@1318 2392 options.style.marginLeft = spanMargin;
nickjillings@1318 2393 text.style.marginRight = spanMargin;
nickjillings@1318 2394 text.style.marginLeft = spanMargin;
nickjillings@1318 2395 }
nickjillings@1318 2396 };
nickjillings@1318 2397 this.resize();
nickjillings@1318 2398 };
nickjillings@1318 2399
nickjillings@1318 2400 this.checkboxBox = function(commentQuestion) {
nickjillings@1318 2401 this.specification = commentQuestion;
nickjillings@1318 2402 // Create document objects to hold the comment boxes
nickjillings@1318 2403 this.holder = document.createElement('div');
nickjillings@1318 2404 this.holder.className = 'comment-div';
nickjillings@1318 2405 // Create a string next to each comment asking for a comment
nickjillings@1318 2406 this.string = document.createElement('span');
nickjillings@1318 2407 this.string.innerHTML = commentQuestion.statement;
nickjillings@1318 2408 var br = document.createElement('br');
nickjillings@1318 2409 // Add to the holder.
nickjillings@1318 2410 this.holder.appendChild(this.string);
nickjillings@1318 2411 this.holder.appendChild(br);
nickjillings@1318 2412 this.options = [];
nickjillings@1318 2413 this.inputs = document.createElement('div');
nickjillings@1318 2414 this.span = document.createElement('div');
nickjillings@1318 2415 this.inputs.align = 'center';
nickjillings@1318 2416 this.inputs.style.marginLeft = '12px';
nickjillings@1318 2417 this.span.style.marginLeft = '12px';
nickjillings@1318 2418 this.span.align = 'center';
nickjillings@1318 2419 this.span.style.marginTop = '15px';
nickjillings@1318 2420
nickjillings@1318 2421 var optCount = commentQuestion.options.length;
nickjillings@1318 2422 for (var i=0; i<optCount; i++)
nickjillings@1318 2423 {
nickjillings@1318 2424 var div = document.createElement('div');
nickjillings@1318 2425 div.style.width = '80px';
nickjillings@1318 2426 div.style.float = 'left';
nickjillings@1318 2427 var input = document.createElement('input');
nickjillings@1318 2428 input.type = 'checkbox';
nickjillings@1318 2429 input.name = commentQuestion.id;
nickjillings@1318 2430 input.setAttribute('setvalue',commentQuestion.options[i].name);
nickjillings@1318 2431 input.className = 'comment-radio';
nickjillings@1318 2432 div.appendChild(input);
nickjillings@1318 2433 this.inputs.appendChild(div);
nickjillings@1318 2434
nickjillings@1318 2435
nickjillings@1318 2436 div = document.createElement('div');
nickjillings@1318 2437 div.style.width = '80px';
nickjillings@1318 2438 div.style.float = 'left';
nickjillings@1318 2439 div.align = 'center';
nickjillings@1318 2440 var span = document.createElement('span');
nickjillings@1318 2441 span.textContent = commentQuestion.options[i].text;
nickjillings@1318 2442 span.className = 'comment-radio-span';
nickjillings@1318 2443 div.appendChild(span);
nickjillings@1318 2444 this.span.appendChild(div);
nickjillings@1318 2445 this.options.push(input);
nickjillings@1318 2446 }
nickjillings@1318 2447 this.holder.appendChild(this.span);
nickjillings@1318 2448 this.holder.appendChild(this.inputs);
nickjillings@1318 2449
nickjillings@1318 2450 this.exportXMLDOM = function() {
nickjillings@1318 2451 var root = document.createElement('comment');
nickjillings@1318 2452 root.id = this.specification.id;
nickjillings@1318 2453 root.setAttribute('type',this.specification.type);
nickjillings@1318 2454 var question = document.createElement('question');
nickjillings@1318 2455 question.textContent = this.string.textContent;
nickjillings@1318 2456 root.appendChild(question);
nickjillings@1318 2457 console.log('Comment: '+question.textContent);
nickjillings@1318 2458 for (var i=0; i<this.options.length; i++) {
nickjillings@1318 2459 var response = document.createElement('response');
nickjillings@1318 2460 response.textContent = this.options[i].checked;
nickjillings@1318 2461 response.setAttribute('name',this.options[i].getAttribute('setvalue'));
nickjillings@1318 2462 root.appendChild(response);
nickjillings@1318 2463 console.log('Response '+response.getAttribute('name') +': '+response.textContent);
nickjillings@1318 2464 }
nickjillings@1318 2465 return root;
nickjillings@1318 2466 };
nickjillings@1318 2467 this.resize = function()
nickjillings@1318 2468 {
nickjillings@1318 2469 var boxwidth = (window.innerWidth-100)/2;
nickjillings@1318 2470 if (boxwidth >= 600)
nickjillings@1318 2471 {
nickjillings@1318 2472 boxwidth = 600;
nickjillings@1318 2473 }
nickjillings@1318 2474 else if (boxwidth < 400)
nickjillings@1318 2475 {
nickjillings@1318 2476 boxwidth = 400;
nickjillings@1318 2477 }
nickjillings@1318 2478 this.holder.style.width = boxwidth+"px";
nickjillings@1318 2479 var text = this.holder.children[2];
nickjillings@1318 2480 var options = this.holder.children[3];
nickjillings@1318 2481 var optCount = options.children.length;
nickjillings@1318 2482 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
nickjillings@1318 2483 var options = options.firstChild;
nickjillings@1318 2484 var text = text.firstChild;
nickjillings@1318 2485 options.style.marginRight = spanMargin;
nickjillings@1318 2486 options.style.marginLeft = spanMargin;
nickjillings@1318 2487 text.style.marginRight = spanMargin;
nickjillings@1318 2488 text.style.marginLeft = spanMargin;
nickjillings@1318 2489 while(options.nextSibling != undefined)
nickjillings@1318 2490 {
nickjillings@1318 2491 options = options.nextSibling;
nickjillings@1318 2492 text = text.nextSibling;
nickjillings@1318 2493 options.style.marginRight = spanMargin;
nickjillings@1318 2494 options.style.marginLeft = spanMargin;
nickjillings@1318 2495 text.style.marginRight = spanMargin;
nickjillings@1318 2496 text.style.marginLeft = spanMargin;
nickjillings@1318 2497 }
nickjillings@1318 2498 };
nickjillings@1318 2499 this.resize();
nickjillings@1318 2500 };
nickjillings@1318 2501
nickjillings@1318 2502 this.createCommentBox = function(audioObject) {
nickjillings@1318 2503 var node = new this.elementCommentBox(audioObject);
nickjillings@1318 2504 this.commentBoxes.push(node);
nickjillings@1318 2505 audioObject.commentDOM = node;
nickjillings@1318 2506 return node;
nickjillings@1318 2507 };
nickjillings@1318 2508
nickjillings@1318 2509 this.sortCommentBoxes = function() {
nickjillings@1318 2510 var holder = [];
nickjillings@1318 2511 while (this.commentBoxes.length > 0) {
nickjillings@1318 2512 var node = this.commentBoxes.pop(0);
nickjillings@1318 2513 holder[node.id] = node;
nickjillings@1318 2514 }
nickjillings@1318 2515 this.commentBoxes = holder;
nickjillings@1318 2516 };
nickjillings@1318 2517
nickjillings@1318 2518 this.showCommentBoxes = function(inject, sort) {
nickjillings@1318 2519 if (sort) {interfaceContext.sortCommentBoxes();}
nickjillings@1318 2520 for (var i=0; i<interfaceContext.commentBoxes.length; i++) {
nickjillings@1318 2521 inject.appendChild(this.commentBoxes[i].trackComment);
nickjillings@1318 2522 }
nickjillings@1318 2523 };
nickjillings@1318 2524
nickjillings@1318 2525 this.deleteCommentBoxes = function() {
nickjillings@1318 2526 this.commentBoxes = [];
nickjillings@1318 2527 };
nickjillings@1318 2528
nickjillings@1318 2529 this.createCommentQuestion = function(element) {
nickjillings@1318 2530 var node;
nickjillings@1324 2531 if (element.type == 'question') {
nickjillings@1318 2532 node = new this.commentBox(element);
nickjillings@1318 2533 } else if (element.type == 'radio') {
nickjillings@1318 2534 node = new this.radioBox(element);
nickjillings@1318 2535 } else if (element.type == 'checkbox') {
nickjillings@1318 2536 node = new this.checkboxBox(element);
nickjillings@1318 2537 }
nickjillings@1318 2538 this.commentQuestions.push(node);
nickjillings@1318 2539 return node;
nickjillings@1318 2540 };
nickjillings@1318 2541
nickjillings@1318 2542 this.deleteCommentQuestions = function()
nickjillings@1318 2543 {
nickjillings@1318 2544 this.commentQuestions = [];
nickjillings@1318 2545 };
nickjillings@1318 2546
nickjillings@1318 2547 this.playhead = new function()
nickjillings@1318 2548 {
nickjillings@1318 2549 this.object = document.createElement('div');
nickjillings@1318 2550 this.object.className = 'playhead';
nickjillings@1318 2551 this.object.align = 'left';
nickjillings@1318 2552 var curTime = document.createElement('div');
nickjillings@1318 2553 curTime.style.width = '50px';
nickjillings@1318 2554 this.curTimeSpan = document.createElement('span');
nickjillings@1318 2555 this.curTimeSpan.textContent = '00:00';
nickjillings@1318 2556 curTime.appendChild(this.curTimeSpan);
nickjillings@1318 2557 this.object.appendChild(curTime);
nickjillings@1318 2558 this.scrubberTrack = document.createElement('div');
nickjillings@1318 2559 this.scrubberTrack.className = 'playhead-scrub-track';
nickjillings@1318 2560
nickjillings@1318 2561 this.scrubberHead = document.createElement('div');
nickjillings@1318 2562 this.scrubberHead.id = 'playhead-scrubber';
nickjillings@1318 2563 this.scrubberTrack.appendChild(this.scrubberHead);
nickjillings@1318 2564 this.object.appendChild(this.scrubberTrack);
nickjillings@1318 2565
nickjillings@1318 2566 this.timePerPixel = 0;
nickjillings@1318 2567 this.maxTime = 0;
nickjillings@1318 2568
nickjillings@1318 2569 this.playbackObject;
nickjillings@1318 2570
nickjillings@1318 2571 this.setTimePerPixel = function(audioObject) {
nickjillings@1318 2572 //maxTime must be in seconds
nickjillings@1318 2573 this.playbackObject = audioObject;
nickjillings@1318 2574 this.maxTime = audioObject.buffer.buffer.duration;
nickjillings@1318 2575 var width = 490; //500 - 10, 5 each side of the tracker head
nickjillings@1318 2576 this.timePerPixel = this.maxTime/490;
nickjillings@1318 2577 if (this.maxTime < 60) {
nickjillings@1318 2578 this.curTimeSpan.textContent = '0.00';
nickjillings@1318 2579 } else {
nickjillings@1318 2580 this.curTimeSpan.textContent = '00:00';
nickjillings@1318 2581 }
nickjillings@1318 2582 };
nickjillings@1318 2583
nickjillings@1318 2584 this.update = function() {
nickjillings@1318 2585 // Update the playhead position, startPlay must be called
nickjillings@1318 2586 if (this.timePerPixel > 0) {
nickjillings@1318 2587 var time = this.playbackObject.getCurrentPosition();
nickjillings@1318 2588 if (time > 0) {
nickjillings@1318 2589 var width = 490;
nickjillings@1318 2590 var pix = Math.floor(time/this.timePerPixel);
nickjillings@1318 2591 this.scrubberHead.style.left = pix+'px';
nickjillings@1318 2592 if (this.maxTime > 60.0) {
nickjillings@1318 2593 var secs = time%60;
nickjillings@1318 2594 var mins = Math.floor((time-secs)/60);
nickjillings@1318 2595 secs = secs.toString();
nickjillings@1318 2596 secs = secs.substr(0,2);
nickjillings@1318 2597 mins = mins.toString();
nickjillings@1318 2598 this.curTimeSpan.textContent = mins+':'+secs;
nickjillings@1318 2599 } else {
nickjillings@1318 2600 time = time.toString();
nickjillings@1318 2601 this.curTimeSpan.textContent = time.substr(0,4);
nickjillings@1318 2602 }
nickjillings@1318 2603 } else {
nickjillings@1318 2604 this.scrubberHead.style.left = '0px';
nickjillings@1318 2605 if (this.maxTime < 60) {
nickjillings@1318 2606 this.curTimeSpan.textContent = '0.00';
nickjillings@1318 2607 } else {
nickjillings@1318 2608 this.curTimeSpan.textContent = '00:00';
nickjillings@1318 2609 }
nickjillings@1318 2610 }
nickjillings@1318 2611 }
nickjillings@1318 2612 };
nickjillings@1318 2613
nickjillings@1318 2614 this.interval = undefined;
nickjillings@1318 2615
nickjillings@1318 2616 this.start = function() {
nickjillings@1318 2617 if (this.playbackObject != undefined && this.interval == undefined) {
nickjillings@1318 2618 if (this.maxTime < 60) {
nickjillings@1318 2619 this.interval = setInterval(function(){interfaceContext.playhead.update();},10);
nickjillings@1318 2620 } else {
nickjillings@1318 2621 this.interval = setInterval(function(){interfaceContext.playhead.update();},100);
nickjillings@1318 2622 }
nickjillings@1318 2623 }
nickjillings@1318 2624 };
nickjillings@1318 2625 this.stop = function() {
nickjillings@1318 2626 clearInterval(this.interval);
nickjillings@1318 2627 this.interval = undefined;
nickjillings@1318 2628 if (this.maxTime < 60) {
nickjillings@1318 2629 this.curTimeSpan.textContent = '0.00';
nickjillings@1318 2630 } else {
nickjillings@1318 2631 this.curTimeSpan.textContent = '00:00';
nickjillings@1318 2632 }
nickjillings@1318 2633 };
nickjillings@1318 2634 };
nickjillings@1318 2635
nickjillings@1318 2636 // Global Checkers
nickjillings@1318 2637 // These functions will help enforce the checkers
nickjillings@1318 2638 this.checkHiddenAnchor = function()
nickjillings@1318 2639 {
nickjillings@1324 2640 for (var ao of audioEngineContext.audioObjects)
nickjillings@1318 2641 {
nickjillings@1324 2642 if (ao.specification.type == "anchor")
nickjillings@1318 2643 {
nickjillings@1324 2644 if (ao.interfaceDOM.getValue() > ao.specification.marker && ao.interfaceDOM.enforce == true) {
nickjillings@1324 2645 // Anchor is not set below
nickjillings@1324 2646 console.log('Anchor node not below marker value');
nickjillings@1324 2647 alert('Please keep listening');
nickjillings@1324 2648 return false;
nickjillings@1324 2649 }
nickjillings@1318 2650 }
nickjillings@1318 2651 }
nickjillings@1318 2652 return true;
nickjillings@1318 2653 };
nickjillings@1318 2654
nickjillings@1318 2655 this.checkHiddenReference = function()
nickjillings@1318 2656 {
nickjillings@1324 2657 for (var ao of audioEngineContext.audioObjects)
nickjillings@1318 2658 {
nickjillings@1324 2659 if (ao.specification.type == "reference")
nickjillings@1318 2660 {
nickjillings@1324 2661 if (ao.interfaceDOM.getValue() < ao.specification.marker && ao.interfaceDOM.enforce == true) {
nickjillings@1324 2662 // Anchor is not set below
nickjillings@1324 2663 console.log('Reference node not below marker value');
nickjillings@1324 2664 alert('Please keep listening');
nickjillings@1324 2665 return false;
nickjillings@1324 2666 }
nickjillings@1318 2667 }
nickjillings@1318 2668 }
nickjillings@1318 2669 return true;
nickjillings@1318 2670 };
nickjillings@1318 2671
nickjillings@1318 2672 this.checkFragmentsFullyPlayed = function ()
nickjillings@1318 2673 {
nickjillings@1318 2674 // Checks the entire file has been played back
nickjillings@1318 2675 // NOTE ! This will return true IF playback is Looped!!!
nickjillings@1318 2676 if (audioEngineContext.loopPlayback)
nickjillings@1318 2677 {
nickjillings@1318 2678 console.log("WARNING - Looped source: Cannot check fragments are fully played");
nickjillings@1318 2679 return true;
nickjillings@1318 2680 }
nickjillings@1318 2681 var check_pass = true;
nickjillings@1318 2682 var error_obj = [];
nickjillings@1318 2683 for (var i = 0; i<audioEngineContext.audioObjects.length; i++)
nickjillings@1318 2684 {
nickjillings@1318 2685 var object = audioEngineContext.audioObjects[i];
nickjillings@1318 2686 var time = object.buffer.buffer.duration;
nickjillings@1318 2687 var metric = object.metric;
nickjillings@1318 2688 var passed = false;
nickjillings@1318 2689 for (var j=0; j<metric.listenTracker.length; j++)
nickjillings@1318 2690 {
nickjillings@1318 2691 var bt = metric.listenTracker[j].getElementsByTagName('buffertime');
nickjillings@1318 2692 var start_time = Number(bt[0].getAttribute('start'));
nickjillings@1318 2693 var stop_time = Number(bt[0].getAttribute('stop'));
nickjillings@1318 2694 var delta = stop_time - start_time;
nickjillings@1318 2695 if (delta >= time)
nickjillings@1318 2696 {
nickjillings@1318 2697 passed = true;
nickjillings@1318 2698 break;
nickjillings@1318 2699 }
nickjillings@1318 2700 }
nickjillings@1318 2701 if (passed == false)
nickjillings@1318 2702 {
nickjillings@1318 2703 check_pass = false;
nickjillings@1318 2704 console.log("Continue listening to track-"+i);
nickjillings@1318 2705 error_obj.push(i);
nickjillings@1318 2706 }
nickjillings@1318 2707 }
nickjillings@1318 2708 if (check_pass == false)
nickjillings@1318 2709 {
nickjillings@1318 2710 var str_start = "You have not completely listened to fragments ";
nickjillings@1318 2711 for (var i=0; i<error_obj.length; i++)
nickjillings@1318 2712 {
nickjillings@1318 2713 str_start += error_obj[i];
nickjillings@1318 2714 if (i != error_obj.length-1)
nickjillings@1318 2715 {
nickjillings@1318 2716 str_start += ', ';
nickjillings@1318 2717 }
nickjillings@1318 2718 }
nickjillings@1318 2719 str_start += ". Please keep listening";
nickjillings@1318 2720 console.log("[ALERT]: "+str_start);
nickjillings@1318 2721 alert(str_start);
nickjillings@1318 2722 }
nickjillings@1318 2723 };
nickjillings@1318 2724 this.checkAllMoved = function()
nickjillings@1318 2725 {
nickjillings@1318 2726 var str = "You have not moved ";
nickjillings@1318 2727 var failed = [];
nickjillings@1318 2728 for (var i in audioEngineContext.audioObjects)
nickjillings@1318 2729 {
nickjillings@1318 2730 if(audioEngineContext.audioObjects[i].metric.wasMoved == false && audioEngineContext.audioObjects[i].specification.type != 'outsidereference')
nickjillings@1318 2731 {
nickjillings@1318 2732 failed.push(audioEngineContext.audioObjects[i].id);
nickjillings@1318 2733 }
nickjillings@1318 2734 }
nickjillings@1318 2735 if (failed.length == 0)
nickjillings@1318 2736 {
nickjillings@1318 2737 return true;
nickjillings@1318 2738 } else if (failed.length == 1)
nickjillings@1318 2739 {
nickjillings@1318 2740 str += 'track '+failed[0];
nickjillings@1318 2741 } else {
nickjillings@1318 2742 str += 'tracks ';
nickjillings@1318 2743 for (var i=0; i<failed.length-1; i++)
nickjillings@1318 2744 {
nickjillings@1318 2745 str += failed[i]+', ';
nickjillings@1318 2746 }
nickjillings@1318 2747 str += 'and '+failed[i];
nickjillings@1318 2748 }
nickjillings@1318 2749 str +='.';
nickjillings@1318 2750 alert(str);
nickjillings@1318 2751 console.log(str);
nickjillings@1318 2752 return false;
nickjillings@1318 2753 };
nickjillings@1318 2754 this.checkAllPlayed = function()
nickjillings@1318 2755 {
nickjillings@1318 2756 var str = "You have not played ";
nickjillings@1318 2757 var failed = [];
nickjillings@1318 2758 for (var i in audioEngineContext.audioObjects)
nickjillings@1318 2759 {
nickjillings@1318 2760 if(audioEngineContext.audioObjects[i].metric.wasListenedTo == false)
nickjillings@1318 2761 {
nickjillings@1318 2762 failed.push(audioEngineContext.audioObjects[i].id);
nickjillings@1318 2763 }
nickjillings@1318 2764 }
nickjillings@1318 2765 if (failed.length == 0)
nickjillings@1318 2766 {
nickjillings@1318 2767 return true;
nickjillings@1318 2768 } else if (failed.length == 1)
nickjillings@1318 2769 {
nickjillings@1318 2770 str += 'track '+failed[0];
nickjillings@1318 2771 } else {
nickjillings@1318 2772 str += 'tracks ';
nickjillings@1318 2773 for (var i=0; i<failed.length-1; i++)
nickjillings@1318 2774 {
nickjillings@1318 2775 str += failed[i]+', ';
nickjillings@1318 2776 }
nickjillings@1318 2777 str += 'and '+failed[i];
nickjillings@1318 2778 }
nickjillings@1318 2779 str +='.';
nickjillings@1318 2780 alert(str);
nickjillings@1318 2781 console.log(str);
nickjillings@1318 2782 return false;
nickjillings@1318 2783 };
nickjillings@1324 2784 }
nickjillings@1324 2785
nickjillings@1324 2786 function Storage()
nickjillings@1324 2787 {
nickjillings@1324 2788 // Holds results in XML format until ready for collection
nickjillings@1324 2789 this.globalPreTest = null;
nickjillings@1324 2790 this.globalPostTest = null;
nickjillings@1324 2791 this.testPages = [];
nickjillings@1324 2792 this.document = document.implementation.createDocument(null,"waetresult");
nickjillings@1324 2793 this.root = this.document.children[0];
nickjillings@1324 2794 this.state = 0;
nickjillings@1324 2795
nickjillings@1324 2796 this.initialise = function()
nickjillings@1324 2797 {
nickjillings@1324 2798 this.globalPreTest = new this.surveyNode(this,this.root,specification.preTest);
nickjillings@1324 2799 this.globalPostTest = new this.surveyNode(this,this.root,specification.postTest);
nickjillings@1324 2800 };
nickjillings@1324 2801
nickjillings@1324 2802 this.createTestPageStore = function(specification)
nickjillings@1324 2803 {
nickjillings@1324 2804 var store = new this.pageNode(this,specification);
nickjillings@1324 2805 this.testPages.push(store);
nickjillings@1324 2806 return this.testPages[this.testPages.length-1];
nickjillings@1324 2807 };
nickjillings@1324 2808
nickjillings@1324 2809 this.surveyNode = function(parent,root,specification)
nickjillings@1324 2810 {
nickjillings@1324 2811 this.specification = specification;
nickjillings@1324 2812 this.parent = parent;
nickjillings@1324 2813 this.XMLDOM = this.parent.document.createElement('survey');
nickjillings@1324 2814 this.XMLDOM.setAttribute('location',this.specification.location);
nickjillings@1324 2815 for (var optNode of this.specification.options)
nickjillings@1324 2816 {
nickjillings@1324 2817 if (optNode.type != 'statement')
nickjillings@1324 2818 {
nickjillings@1324 2819 var node = this.parent.document.createElement('surveyresult');
nickjillings@1324 2820 node.id = optNode.id;
nickjillings@1324 2821 node.setAttribute('type',optNode.type);
nickjillings@1324 2822 this.XMLDOM.appendChild(node);
nickjillings@1324 2823 }
nickjillings@1324 2824 }
nickjillings@1324 2825 root.appendChild(this.XMLDOM);
nickjillings@1324 2826
nickjillings@1324 2827 this.postResult = function(node)
nickjillings@1324 2828 {
nickjillings@1324 2829 // From popup: node is the popupOption node containing both spec. and results
nickjillings@1324 2830 // ID is the position
nickjillings@1324 2831 if (node.specification.type == 'statement'){return;}
nickjillings@1324 2832 var surveyresult = this.parent.document.getElementById(node.specification.id);
nickjillings@1324 2833 switch(node.specification.type)
nickjillings@1324 2834 {
nickjillings@1324 2835 case "number":
nickjillings@1324 2836 case "question":
nickjillings@1324 2837 var child = this.parent.document.createElement('response');
nickjillings@1324 2838 child.textContent = node.response;
nickjillings@1324 2839 surveyresult.appendChild(child);
nickjillings@1324 2840 break;
nickjillings@1324 2841 case "radio":
nickjillings@1324 2842 var child = this.parent.document.createElement('response');
nickjillings@1324 2843 child.setAttribute('name',node.response.name);
nickjillings@1324 2844 child.textContent = node.response.text;
nickjillings@1324 2845 surveyresult.appendChild(child);
nickjillings@1324 2846 break;
nickjillings@1324 2847 case "checkbox":
nickjillings@1324 2848 for (var i=0; i<node.response.length; i++)
nickjillings@1324 2849 {
nickjillings@1324 2850 var checkNode = this.parent.document.createElement('response');
nickjillings@1324 2851 child.setAttribute('name',node.response.name);
nickjillings@1324 2852 child.setAttribute('checked',node.response.checked);
nickjillings@1324 2853 child.textContent = node.response.text;
nickjillings@1324 2854 surveyresult.appendChild(child);
nickjillings@1324 2855 }
nickjillings@1324 2856 break;
nickjillings@1324 2857 }
nickjillings@1324 2858 };
nickjillings@1324 2859 };
nickjillings@1324 2860
nickjillings@1324 2861 this.pageNode = function(parent,specification)
nickjillings@1324 2862 {
nickjillings@1324 2863 // Create one store per test page
nickjillings@1324 2864 this.specification = specification;
nickjillings@1324 2865 this.parent = parent;
nickjillings@1324 2866 this.XMLDOM = this.parent.document.createElement('page');
nickjillings@1324 2867 this.XMLDOM.setAttribute('id',specification.id);
nickjillings@1324 2868 this.XMLDOM.setAttribute('presentedId',specification.presentedId);
nickjillings@1324 2869 this.preTest = new this.parent.surveyNode(parent,this.XMLDOM,specification.preTest);
nickjillings@1324 2870 this.postTest = new this.parent.surveyNode(parent,this.XMLDOM,specification.postTest);
nickjillings@1324 2871
nickjillings@1324 2872 // Add any page metrics
nickjillings@1324 2873 var page_metric = this.parent.document.createElement('metric');
nickjillings@1324 2874 this.XMLDOM.appendChild(page_metric);
nickjillings@1324 2875
nickjillings@1324 2876 // Add the audioelement
nickjillings@1324 2877 for (var element of this.specification.audioElements)
nickjillings@1324 2878 {
nickjillings@1324 2879 var aeNode = this.parent.document.createElement('audioelement');
nickjillings@1324 2880 aeNode.id = element.id;
nickjillings@1324 2881 aeNode.setAttribute('type',element.type);
nickjillings@1324 2882 aeNode.setAttribute('url', element.url);
nickjillings@1324 2883 aeNode.setAttribute('gain', element.gain);
nickjillings@1324 2884 if (element.type == 'anchor' || element.type == 'reference')
nickjillings@1324 2885 {
nickjillings@1324 2886 if (element.marker > 0)
nickjillings@1324 2887 {
nickjillings@1324 2888 aeNode.setAttribute('marker',element.marker);
nickjillings@1324 2889 }
nickjillings@1324 2890 }
nickjillings@1324 2891 var ae_metric = this.parent.document.createElement('metric');
nickjillings@1324 2892 aeNode.appendChild(ae_metric);
nickjillings@1324 2893 this.XMLDOM.appendChild(aeNode);
nickjillings@1324 2894 }
nickjillings@1324 2895
nickjillings@1324 2896 // Add any commentQuestions
nickjillings@1324 2897 for (var element of this.specification.commentQuestions)
nickjillings@1324 2898 {
nickjillings@1324 2899 var cqNode = this.parent.document.createElement('commentquestion');
nickjillings@1324 2900 cqNode.id = element.id;
nickjillings@1324 2901 cqNode.setAttribute('type',element.type);
nickjillings@1324 2902 var statement = this.parent.document.createElement('statement');
nickjillings@1324 2903 statement.textContent = cqNode.statement;
nickjillings@1324 2904 cqNode.appendChild(statement);
nickjillings@1324 2905 var response = this.parent.document.createElement('response');
nickjillings@1324 2906 cqNode.appendChild(response);
nickjillings@1324 2907 this.XMLDOM.appendChild(cqNode);
nickjillings@1324 2908 }
nickjillings@1324 2909
nickjillings@1324 2910 this.parent.root.appendChild(this.XMLDOM);
nickjillings@1324 2911 };
nickjillings@1324 2912 this.finish = function()
nickjillings@1324 2913 {
nickjillings@1324 2914 if (this.state == 0)
nickjillings@1324 2915 {
nickjillings@1324 2916 var projectDocument = specification.projectXML;
nickjillings@1324 2917 projectDocument.setAttribute('file-name',url);
nickjillings@1324 2918 this.root.appendChild(projectDocument);
nickjillings@1324 2919 this.root.appendChild(returnDateNode());
nickjillings@1324 2920 this.root.appendChild(interfaceContext.returnNavigator());
nickjillings@1324 2921 }
nickjillings@1324 2922 this.state = 1;
nickjillings@1324 2923 return this.root;
nickjillings@1324 2924 };
nickjillings@1324 2925 }