annotate core.js @ 1320:dd7f5b4d3b4e

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