annotate core.js @ 2092:f9523811cfb3

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