annotate core.js @ 1388:57c7dd771212

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