annotate core.js @ 1445:dd4fed6a03f2

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