annotate core.js @ 1535:d31600062d1c

Feature #1287: Padding added when looping is enabled to give synchronised looping.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Thu, 23 Jul 2015 10:10:38 +0100
parents f9e50328a26a
children 97ae294cdfb8
rev   line source
nickjillings@1528 1 /**
nickjillings@1528 2 * core.js
nickjillings@1528 3 *
nickjillings@1528 4 * Main script to run, calls all other core functions and manages loading/store to backend.
nickjillings@1528 5 * Also contains all global variables.
nickjillings@1528 6 */
nickjillings@1528 7
nickjillings@1528 8 /* create the web audio API context and store in audioContext*/
nickjillings@1528 9 var audioContext; // Hold the browser web audio API
nickjillings@1528 10 var projectXML; // Hold the parsed setup XML
nickjillings@1528 11 var specification;
nickjillings@1528 12 var interfaceContext;
nickjillings@1528 13 var popup; // Hold the interfacePopup object
nickjillings@1528 14 var testState;
nickjillings@1528 15 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
nickjillings@1528 16 var audioEngineContext; // The custome AudioEngine object
nickjillings@1528 17 var projectReturn; // Hold the URL for the return
nickjillings@1528 18
nickjillings@1528 19
nickjillings@1528 20 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
nickjillings@1528 21 AudioBufferSourceNode.prototype.owner = undefined;
nickjillings@1528 22
nickjillings@1528 23 window.onload = function() {
nickjillings@1528 24 // Function called once the browser has loaded all files.
nickjillings@1528 25 // This should perform any initial commands such as structure / loading documents
nickjillings@1528 26
nickjillings@1528 27 // Create a web audio API context
nickjillings@1528 28 // Fixed for cross-browser support
nickjillings@1528 29 var AudioContext = window.AudioContext || window.webkitAudioContext;
nickjillings@1528 30 audioContext = new AudioContext;
nickjillings@1528 31
nickjillings@1528 32 // Create test state
nickjillings@1528 33 testState = new stateMachine();
nickjillings@1528 34
nickjillings@1528 35 // Create the audio engine object
nickjillings@1528 36 audioEngineContext = new AudioEngine();
nickjillings@1528 37
nickjillings@1528 38 // Create the popup interface object
nickjillings@1528 39 popup = new interfacePopup();
nickjillings@1528 40
nickjillings@1528 41 // Create the specification object
nickjillings@1528 42 specification = new Specification();
nickjillings@1528 43
nickjillings@1528 44 // Create the interface object
nickjillings@1528 45 interfaceContext = new Interface(specification);
nickjillings@1528 46 };
nickjillings@1528 47
nickjillings@1528 48 function interfacePopup() {
nickjillings@1528 49 // Creates an object to manage the popup
nickjillings@1528 50 this.popup = null;
nickjillings@1528 51 this.popupContent = null;
nickjillings@1528 52 this.buttonProceed = null;
nickjillings@1528 53 this.buttonPrevious = null;
nickjillings@1528 54 this.popupOptions = null;
nickjillings@1528 55 this.currentIndex = null;
nickjillings@1528 56 this.responses = null;
nickjillings@1528 57
nickjillings@1528 58 this.createPopup = function(){
nickjillings@1528 59 // Create popup window interface
nickjillings@1528 60 var insertPoint = document.getElementById("topLevelBody");
nickjillings@1528 61 var blank = document.createElement('div');
nickjillings@1528 62 blank.className = 'testHalt';
nickjillings@1528 63
nickjillings@1528 64 this.popup = document.createElement('div');
nickjillings@1528 65 this.popup.id = 'popupHolder';
nickjillings@1528 66 this.popup.className = 'popupHolder';
nickjillings@1528 67 this.popup.style.position = 'absolute';
nickjillings@1528 68 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
nickjillings@1528 69 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
nickjillings@1528 70
nickjillings@1528 71 this.popupContent = document.createElement('div');
nickjillings@1528 72 this.popupContent.id = 'popupContent';
nickjillings@1528 73 this.popupContent.style.marginTop = '25px';
nickjillings@1528 74 this.popupContent.align = 'center';
nickjillings@1528 75 this.popup.appendChild(this.popupContent);
nickjillings@1528 76
nickjillings@1528 77 this.buttonProceed = document.createElement('button');
nickjillings@1528 78 this.buttonProceed.className = 'popupButton';
nickjillings@1528 79 this.buttonProceed.style.left = '440px';
nickjillings@1528 80 this.buttonProceed.style.top = '215px';
nickjillings@1528 81 this.buttonProceed.innerHTML = 'Next';
nickjillings@1528 82 this.buttonProceed.onclick = function(){popup.proceedClicked();};
nickjillings@1528 83
nickjillings@1528 84 this.buttonPrevious = document.createElement('button');
nickjillings@1528 85 this.buttonPrevious.className = 'popupButton';
nickjillings@1528 86 this.buttonPrevious.style.left = '10px';
nickjillings@1528 87 this.buttonPrevious.style.top = '215px';
nickjillings@1528 88 this.buttonPrevious.innerHTML = 'Back';
nickjillings@1528 89 this.buttonPrevious.onclick = function(){popup.previousClick();};
nickjillings@1528 90
nickjillings@1528 91 this.popup.style.zIndex = -1;
nickjillings@1528 92 this.popup.style.visibility = 'hidden';
nickjillings@1528 93 blank.style.zIndex = -2;
nickjillings@1528 94 blank.style.visibility = 'hidden';
nickjillings@1528 95 insertPoint.appendChild(this.popup);
nickjillings@1528 96 insertPoint.appendChild(blank);
nickjillings@1528 97 };
nickjillings@1528 98
nickjillings@1528 99 this.showPopup = function(){
nickjillings@1528 100 if (this.popup == null) {
nickjillings@1528 101 this.createPopup();
nickjillings@1528 102 }
nickjillings@1528 103 this.popup.style.zIndex = 3;
nickjillings@1528 104 this.popup.style.visibility = 'visible';
nickjillings@1528 105 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1528 106 blank.style.zIndex = 2;
nickjillings@1528 107 blank.style.visibility = 'visible';
nickjillings@1528 108 };
nickjillings@1528 109
nickjillings@1528 110 this.hidePopup = function(){
nickjillings@1528 111 this.popup.style.zIndex = -1;
nickjillings@1528 112 this.popup.style.visibility = 'hidden';
nickjillings@1528 113 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1528 114 blank.style.zIndex = -2;
nickjillings@1528 115 blank.style.visibility = 'hidden';
nickjillings@1528 116 };
nickjillings@1528 117
nickjillings@1528 118 this.postNode = function() {
nickjillings@1528 119 // This will take the node from the popupOptions and display it
nickjillings@1528 120 var node = this.popupOptions[this.currentIndex];
nickjillings@1528 121 this.popupContent.innerHTML = null;
nickjillings@1528 122 if (node.type == 'statement') {
nickjillings@1528 123 var span = document.createElement('span');
nickjillings@1528 124 span.textContent = node.statement;
nickjillings@1528 125 this.popupContent.appendChild(span);
nickjillings@1528 126 } else if (node.type == 'question') {
nickjillings@1528 127 var span = document.createElement('span');
nickjillings@1528 128 span.textContent = node.question;
nickjillings@1528 129 var textArea = document.createElement('textarea');
nickjillings@1528 130 switch (node.boxsize) {
nickjillings@1528 131 case 'small':
nickjillings@1528 132 textArea.cols = "20";
nickjillings@1528 133 textArea.rows = "1";
nickjillings@1528 134 break;
nickjillings@1528 135 case 'normal':
nickjillings@1528 136 textArea.cols = "30";
nickjillings@1528 137 textArea.rows = "2";
nickjillings@1528 138 break;
nickjillings@1528 139 case 'large':
nickjillings@1528 140 textArea.cols = "40";
nickjillings@1528 141 textArea.rows = "5";
nickjillings@1528 142 break;
nickjillings@1528 143 case 'huge':
nickjillings@1528 144 textArea.cols = "50";
nickjillings@1528 145 textArea.rows = "10";
nickjillings@1528 146 break;
nickjillings@1528 147 }
nickjillings@1528 148 var br = document.createElement('br');
nickjillings@1528 149 this.popupContent.appendChild(span);
nickjillings@1528 150 this.popupContent.appendChild(br);
nickjillings@1528 151 this.popupContent.appendChild(textArea);
nickjillings@1528 152 this.popupContent.childNodes[2].focus();
nickjillings@1528 153 } else if (node.type == 'checkbox') {
nickjillings@1528 154 var span = document.createElement('span');
nickjillings@1528 155 span.textContent = node.statement;
nickjillings@1528 156 this.popupContent.appendChild(span);
nickjillings@1528 157 var optHold = document.createElement('div');
nickjillings@1528 158 optHold.id = 'option-holder';
nickjillings@1528 159 optHold.align = 'left';
nickjillings@1528 160 for (var i=0; i<node.options.length; i++) {
nickjillings@1528 161 var option = node.options[i];
nickjillings@1528 162 var input = document.createElement('input');
nickjillings@1528 163 input.id = option.id;
nickjillings@1528 164 input.type = 'checkbox';
nickjillings@1528 165 var span = document.createElement('span');
nickjillings@1528 166 span.textContent = option.text;
nickjillings@1528 167 var hold = document.createElement('div');
nickjillings@1528 168 hold.setAttribute('name','option');
nickjillings@1528 169 hold.style.float = 'left';
nickjillings@1528 170 hold.style.padding = '4px';
nickjillings@1528 171 hold.appendChild(input);
nickjillings@1528 172 hold.appendChild(span);
nickjillings@1528 173 optHold.appendChild(hold);
nickjillings@1528 174 }
nickjillings@1528 175 this.popupContent.appendChild(optHold);
nickjillings@1528 176 } else if (node.type == 'radio') {
nickjillings@1528 177 var span = document.createElement('span');
nickjillings@1528 178 span.textContent = node.statement;
nickjillings@1528 179 this.popupContent.appendChild(span);
nickjillings@1528 180 var optHold = document.createElement('div');
nickjillings@1528 181 optHold.id = 'option-holder';
nickjillings@1528 182 optHold.align = 'none';
nickjillings@1528 183 optHold.style.float = 'left';
nickjillings@1528 184 optHold.style.width = "100%";
nickjillings@1528 185 for (var i=0; i<node.options.length; i++) {
nickjillings@1528 186 var option = node.options[i];
nickjillings@1528 187 var input = document.createElement('input');
nickjillings@1528 188 input.id = option.name;
nickjillings@1528 189 input.type = 'radio';
nickjillings@1528 190 input.name = node.id;
nickjillings@1528 191 var span = document.createElement('span');
nickjillings@1528 192 span.textContent = option.text;
nickjillings@1528 193 var hold = document.createElement('div');
nickjillings@1528 194 hold.setAttribute('name','option');
nickjillings@1528 195 hold.style.padding = '4px';
nickjillings@1528 196 hold.appendChild(input);
nickjillings@1528 197 hold.appendChild(span);
nickjillings@1528 198 optHold.appendChild(hold);
nickjillings@1528 199 }
nickjillings@1528 200 this.popupContent.appendChild(optHold);
nickjillings@1528 201 } else if (node.type == 'number') {
nickjillings@1528 202 var span = document.createElement('span');
nickjillings@1528 203 span.textContent = node.statement;
nickjillings@1528 204 this.popupContent.appendChild(span);
nickjillings@1528 205 this.popupContent.appendChild(document.createElement('br'));
nickjillings@1528 206 var input = document.createElement('input');
nickjillings@1528 207 input.type = 'textarea';
nickjillings@1528 208 if (node.min != null) {input.min = node.min;}
nickjillings@1528 209 if (node.max != null) {input.max = node.max;}
nickjillings@1528 210 if (node.step != null) {input.step = node.step;}
nickjillings@1528 211 this.popupContent.appendChild(input);
nickjillings@1528 212 }
nickjillings@1528 213 this.popupContent.appendChild(this.buttonProceed);
nickjillings@1528 214 if(this.currentIndex+1 == this.popupOptions.length) {
nickjillings@1531 215 if (this.responses.nodeName == "PRETEST") {
nickjillings@1531 216 this.buttonProceed.textContent = 'Start';
nickjillings@1531 217 } else {
nickjillings@1531 218 this.buttonProceed.textContent = 'Submit';
nickjillings@1531 219 }
nickjillings@1528 220 } else {
nickjillings@1528 221 this.buttonProceed.textContent = 'Next';
nickjillings@1528 222 }
nickjillings@1528 223 if(this.currentIndex > 0)
nickjillings@1528 224 this.popupContent.appendChild(this.buttonPrevious);
nickjillings@1528 225 };
nickjillings@1528 226
nickjillings@1528 227 this.initState = function(node) {
nickjillings@1528 228 //Call this with your preTest and postTest nodes when needed to
nickjillings@1528 229 // initialise the popup procedure.
nickjillings@1528 230 this.popupOptions = node.options;
nickjillings@1528 231 if (this.popupOptions.length > 0) {
nickjillings@1528 232 if (node.type == 'pretest') {
nickjillings@1528 233 this.responses = document.createElement('PreTest');
nickjillings@1528 234 } else if (node.type == 'posttest') {
nickjillings@1528 235 this.responses = document.createElement('PostTest');
nickjillings@1528 236 } else {
nickjillings@1528 237 console.log ('WARNING - popup node neither pre or post!');
nickjillings@1528 238 this.responses = document.createElement('responses');
nickjillings@1528 239 }
nickjillings@1528 240 this.currentIndex = 0;
nickjillings@1528 241 this.showPopup();
nickjillings@1528 242 this.postNode();
nickjillings@1528 243 } else {
nickjillings@1528 244 advanceState();
nickjillings@1528 245 }
nickjillings@1528 246 };
nickjillings@1528 247
nickjillings@1528 248 this.proceedClicked = function() {
nickjillings@1528 249 // Each time the popup button is clicked!
nickjillings@1528 250 var node = this.popupOptions[this.currentIndex];
nickjillings@1528 251 if (node.type == 'question') {
nickjillings@1528 252 // Must extract the question data
nickjillings@1528 253 var textArea = $(popup.popupContent).find('textarea')[0];
nickjillings@1528 254 if (node.mandatory == true && textArea.value.length == 0) {
nickjillings@1528 255 alert('This question is mandatory');
nickjillings@1528 256 return;
nickjillings@1528 257 } else {
nickjillings@1528 258 // Save the text content
nickjillings@1528 259 var hold = document.createElement('comment');
nickjillings@1528 260 hold.id = node.id;
nickjillings@1528 261 hold.innerHTML = textArea.value;
nickjillings@1528 262 console.log("Question: "+ node.question);
nickjillings@1528 263 console.log("Question Response: "+ textArea.value);
nickjillings@1528 264 this.responses.appendChild(hold);
nickjillings@1528 265 }
nickjillings@1528 266 } else if (node.type == 'checkbox') {
nickjillings@1528 267 // Must extract checkbox data
nickjillings@1528 268 var optHold = document.getElementById('option-holder');
nickjillings@1528 269 var hold = document.createElement('checkbox');
nickjillings@1528 270 console.log("Checkbox: "+ node.statement);
nickjillings@1528 271 hold.id = node.id;
nickjillings@1528 272 for (var i=0; i<optHold.childElementCount; i++) {
nickjillings@1528 273 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
nickjillings@1528 274 var statement = optHold.childNodes[i].getElementsByTagName('span')[0];
nickjillings@1528 275 var response = document.createElement('option');
nickjillings@1528 276 response.setAttribute('name',input.id);
nickjillings@1528 277 response.textContent = input.checked;
nickjillings@1528 278 hold.appendChild(response);
nickjillings@1528 279 console.log(input.id +': '+ input.checked);
nickjillings@1528 280 }
nickjillings@1528 281 this.responses.appendChild(hold);
nickjillings@1528 282 } else if (node.type == "radio") {
nickjillings@1528 283 var optHold = document.getElementById('option-holder');
nickjillings@1528 284 var hold = document.createElement('radio');
nickjillings@1528 285 var responseID = null;
nickjillings@1528 286 var i=0;
nickjillings@1528 287 while(responseID == null) {
nickjillings@1528 288 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
nickjillings@1528 289 if (input.checked == true) {
nickjillings@1528 290 responseID = i;
nickjillings@1528 291 }
nickjillings@1528 292 i++;
nickjillings@1528 293 }
nickjillings@1528 294 hold.id = node.id;
nickjillings@1528 295 hold.setAttribute('name',node.options[responseID].name);
nickjillings@1528 296 hold.textContent = node.options[responseID].text;
nickjillings@1528 297 this.responses.appendChild(hold);
nickjillings@1528 298 } else if (node.type == "number") {
nickjillings@1528 299 var input = this.popupContent.getElementsByTagName('input')[0];
nickjillings@1528 300 if (node.mandatory == true && input.value.length == 0) {
nickjillings@1528 301 alert('This question is mandatory. Please enter a number');
nickjillings@1528 302 return;
nickjillings@1528 303 }
nickjillings@1528 304 var enteredNumber = Number(input.value);
nickjillings@1528 305 if (isNaN(enteredNumber)) {
nickjillings@1528 306 alert('Please enter a valid number');
nickjillings@1528 307 return;
nickjillings@1528 308 }
nickjillings@1528 309 if (enteredNumber < node.min && node.min != null) {
nickjillings@1528 310 alert('Number is below the minimum value of '+node.min);
nickjillings@1528 311 return;
nickjillings@1528 312 }
nickjillings@1528 313 if (enteredNumber > node.max && node.max != null) {
nickjillings@1528 314 alert('Number is above the maximum value of '+node.max);
nickjillings@1528 315 return;
nickjillings@1528 316 }
nickjillings@1528 317 var hold = document.createElement('number');
nickjillings@1528 318 hold.id = node.id;
nickjillings@1528 319 hold.textContent = input.value;
nickjillings@1528 320 this.responses.appendChild(hold);
nickjillings@1528 321 }
nickjillings@1528 322 this.currentIndex++;
nickjillings@1528 323 if (this.currentIndex < this.popupOptions.length) {
nickjillings@1528 324 this.postNode();
nickjillings@1528 325 } else {
nickjillings@1528 326 // Reached the end of the popupOptions
nickjillings@1528 327 this.hidePopup();
nickjillings@1528 328 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) {
nickjillings@1528 329 testState.stateResults[testState.stateIndex] = this.responses;
nickjillings@1528 330 } else {
nickjillings@1528 331 testState.stateResults[testState.stateIndex].appendChild(this.responses);
nickjillings@1528 332 }
nickjillings@1528 333 advanceState();
nickjillings@1528 334 }
nickjillings@1528 335 };
nickjillings@1528 336
nickjillings@1528 337 this.previousClick = function() {
nickjillings@1528 338 // Triggered when the 'Back' button is clicked in the survey
nickjillings@1528 339 if (this.currentIndex > 0) {
nickjillings@1528 340 this.currentIndex--;
nickjillings@1528 341 var node = this.popupOptions[this.currentIndex];
nickjillings@1528 342 if (node.type != 'statement') {
nickjillings@1528 343 var prevResp = this.responses.childNodes[this.responses.childElementCount-1];
nickjillings@1528 344 this.responses.removeChild(prevResp);
nickjillings@1528 345 }
nickjillings@1528 346 this.postNode();
nickjillings@1528 347 if (node.type == 'question') {
nickjillings@1528 348 this.popupContent.getElementsByTagName('textarea')[0].value = prevResp.textContent;
nickjillings@1528 349 } else if (node.type == 'checkbox') {
nickjillings@1528 350 var options = this.popupContent.getElementsByTagName('input');
nickjillings@1528 351 var savedOptions = prevResp.getElementsByTagName('option');
nickjillings@1528 352 for (var i=0; i<options.length; i++) {
nickjillings@1528 353 var id = options[i].id;
nickjillings@1528 354 for (var j=0; j<savedOptions.length; j++) {
nickjillings@1528 355 if (savedOptions[j].getAttribute('name') == id) {
nickjillings@1528 356 if (savedOptions[j].textContent == 'true') {options[i].checked = true;}
nickjillings@1528 357 else {options[i].checked = false;}
nickjillings@1528 358 break;
nickjillings@1528 359 }
nickjillings@1528 360 }
nickjillings@1528 361 }
nickjillings@1528 362 } else if (node.type == 'number') {
nickjillings@1528 363 this.popupContent.getElementsByTagName('input')[0].value = prevResp.textContent;
nickjillings@1528 364 } else if (node.type == 'radio') {
nickjillings@1528 365 var options = this.popupContent.getElementsByTagName('input');
nickjillings@1528 366 var name = prevResp.getAttribute('name');
nickjillings@1528 367 for (var i=0; i<options.length; i++) {
nickjillings@1528 368 if (options[i].id == name) {
nickjillings@1528 369 options[i].checked = true;
nickjillings@1528 370 break;
nickjillings@1528 371 }
nickjillings@1528 372 }
nickjillings@1528 373 }
nickjillings@1528 374 }
nickjillings@1528 375 };
nickjillings@1528 376 }
nickjillings@1528 377
nickjillings@1528 378 function advanceState()
nickjillings@1528 379 {
nickjillings@1528 380 // Just for complete clarity
nickjillings@1528 381 testState.advanceState();
nickjillings@1528 382 }
nickjillings@1528 383
nickjillings@1528 384 function stateMachine()
nickjillings@1528 385 {
nickjillings@1528 386 // Object prototype for tracking and managing the test state
nickjillings@1528 387 this.stateMap = [];
nickjillings@1528 388 this.stateIndex = null;
nickjillings@1528 389 this.currentStateMap = [];
nickjillings@1528 390 this.currentIndex = null;
nickjillings@1528 391 this.currentTestId = 0;
nickjillings@1528 392 this.stateResults = [];
nickjillings@1528 393 this.timerCallBackHolders = null;
nickjillings@1528 394 this.initialise = function(){
nickjillings@1528 395 if (this.stateMap.length > 0) {
nickjillings@1528 396 if(this.stateIndex != null) {
nickjillings@1528 397 console.log('NOTE - State already initialise');
nickjillings@1528 398 }
nickjillings@1528 399 this.stateIndex = -1;
nickjillings@1528 400 var that = this;
nickjillings@1528 401 var aH_pId = 0;
nickjillings@1528 402 for (var id=0; id<this.stateMap.length; id++){
nickjillings@1528 403 var name = this.stateMap[id].type;
nickjillings@1528 404 var obj = document.createElement(name);
nickjillings@1528 405 if (name == 'audioHolder') {
nickjillings@1528 406 obj.id = this.stateMap[id].id;
nickjillings@1528 407 obj.setAttribute('presentedid',aH_pId);
nickjillings@1528 408 aH_pId+=1;
nickjillings@1528 409 }
nickjillings@1528 410 this.stateResults.push(obj);
nickjillings@1528 411 }
nickjillings@1528 412 } else {
nickjillings@1528 413 console.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
nickjillings@1528 414 }
nickjillings@1528 415 };
nickjillings@1528 416 this.advanceState = function(){
nickjillings@1528 417 if (this.stateIndex == null) {
nickjillings@1528 418 this.initialise();
nickjillings@1528 419 }
nickjillings@1528 420 if (this.stateIndex == -1) {
nickjillings@1528 421 console.log('Starting test...');
nickjillings@1528 422 }
nickjillings@1528 423 if (this.currentIndex == null){
nickjillings@1528 424 if (this.currentStateMap.type == "audioHolder") {
nickjillings@1528 425 // Save current page
nickjillings@1528 426 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId);
nickjillings@1528 427 this.currentTestId++;
nickjillings@1528 428 }
nickjillings@1528 429 this.stateIndex++;
nickjillings@1528 430 if (this.stateIndex >= this.stateMap.length) {
nickjillings@1528 431 console.log('Test Completed');
nickjillings@1528 432 createProjectSave(specification.projectReturn);
nickjillings@1528 433 } else {
nickjillings@1528 434 this.currentStateMap = this.stateMap[this.stateIndex];
nickjillings@1528 435 if (this.currentStateMap.type == "audioHolder") {
nickjillings@1528 436 console.log('Loading test page');
nickjillings@1528 437 loadTest(this.currentStateMap);
nickjillings@1528 438 this.initialiseInnerState(this.currentStateMap);
nickjillings@1528 439 } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") {
nickjillings@1528 440 if (this.currentStateMap.options.length >= 1) {
nickjillings@1528 441 popup.initState(this.currentStateMap);
nickjillings@1528 442 } else {
nickjillings@1528 443 this.advanceState();
nickjillings@1528 444 }
nickjillings@1528 445 } else {
nickjillings@1528 446 this.advanceState();
nickjillings@1528 447 }
nickjillings@1528 448 }
nickjillings@1528 449 } else {
nickjillings@1528 450 this.advanceInnerState();
nickjillings@1528 451 }
nickjillings@1528 452 };
nickjillings@1528 453
nickjillings@1528 454 this.testPageCompleted = function(store, testXML, testId) {
nickjillings@1528 455 // Function called each time a test page has been completed
nickjillings@1528 456 // Can be used to over-rule default behaviour
nickjillings@1528 457
nickjillings@1528 458 pageXMLSave(store, testXML);
nickjillings@1528 459 };
nickjillings@1528 460
nickjillings@1528 461 this.initialiseInnerState = function(node) {
nickjillings@1528 462 // Parses the received testXML for pre and post test options
nickjillings@1528 463 this.currentStateMap = [];
nickjillings@1528 464 var preTest = node.preTest;
nickjillings@1528 465 var postTest = node.postTest;
nickjillings@1528 466 if (preTest == undefined) {preTest = document.createElement("preTest");}
nickjillings@1528 467 if (postTest == undefined){postTest= document.createElement("postTest");}
nickjillings@1528 468 this.currentStateMap.push(preTest);
nickjillings@1528 469 this.currentStateMap.push(node);
nickjillings@1528 470 this.currentStateMap.push(postTest);
nickjillings@1528 471 this.currentIndex = -1;
nickjillings@1528 472 this.advanceInnerState();
nickjillings@1528 473 };
nickjillings@1528 474
nickjillings@1528 475 this.advanceInnerState = function() {
nickjillings@1528 476 this.currentIndex++;
nickjillings@1528 477 if (this.currentIndex >= this.currentStateMap.length) {
nickjillings@1528 478 this.currentIndex = null;
nickjillings@1528 479 this.currentStateMap = this.stateMap[this.stateIndex];
nickjillings@1528 480 this.advanceState();
nickjillings@1528 481 } else {
nickjillings@1528 482 if (this.currentStateMap[this.currentIndex].type == "audioHolder") {
nickjillings@1528 483 console.log("Loading test page"+this.currentTestId);
nickjillings@1528 484 } else if (this.currentStateMap[this.currentIndex].type == "pretest") {
nickjillings@1528 485 popup.initState(this.currentStateMap[this.currentIndex]);
nickjillings@1528 486 } else if (this.currentStateMap[this.currentIndex].type == "posttest") {
nickjillings@1528 487 popup.initState(this.currentStateMap[this.currentIndex]);
nickjillings@1528 488 } else {
nickjillings@1528 489 this.advanceInnerState();
nickjillings@1528 490 }
nickjillings@1528 491 }
nickjillings@1528 492 };
nickjillings@1528 493
nickjillings@1528 494 this.previousState = function(){};
nickjillings@1528 495 }
nickjillings@1528 496
nickjillings@1528 497 function testEnded(testId)
nickjillings@1528 498 {
nickjillings@1528 499 pageXMLSave(testId);
nickjillings@1528 500 if (testXMLSetups.length-1 > testId)
nickjillings@1528 501 {
nickjillings@1528 502 // Yes we have another test to perform
nickjillings@1528 503 testId = (Number(testId)+1);
nickjillings@1528 504 currentState = 'testRun-'+testId;
nickjillings@1528 505 loadTest(testId);
nickjillings@1528 506 } else {
nickjillings@1528 507 console.log('Testing Completed!');
nickjillings@1528 508 currentState = 'postTest';
nickjillings@1528 509 // Check for any post tests
nickjillings@1528 510 var xmlSetup = projectXML.find('setup');
nickjillings@1528 511 var postTest = xmlSetup.find('PostTest')[0];
nickjillings@1528 512 popup.initState(postTest);
nickjillings@1528 513 }
nickjillings@1528 514 }
nickjillings@1528 515
nickjillings@1528 516 function loadProjectSpec(url) {
nickjillings@1528 517 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
nickjillings@1528 518 // If url is null, request client to upload project XML document
nickjillings@1528 519 var r = new XMLHttpRequest();
nickjillings@1528 520 r.open('GET',url,true);
nickjillings@1528 521 r.onload = function() {
nickjillings@1528 522 loadProjectSpecCallback(r.response);
nickjillings@1528 523 };
nickjillings@1528 524 r.send();
nickjillings@1528 525 };
nickjillings@1528 526
nickjillings@1528 527 function loadProjectSpecCallback(response) {
nickjillings@1528 528 // Function called after asynchronous download of XML project specification
nickjillings@1528 529 //var decode = $.parseXML(response);
nickjillings@1528 530 //projectXML = $(decode);
nickjillings@1528 531
nickjillings@1528 532 var parse = new DOMParser();
nickjillings@1528 533 projectXML = parse.parseFromString(response,'text/xml');
nickjillings@1528 534
nickjillings@1528 535 // Build the specification
nickjillings@1528 536 specification.decode();
nickjillings@1528 537
nickjillings@1528 538 testState.stateMap.push(specification.preTest);
nickjillings@1528 539
nickjillings@1528 540 // New check if we need to randomise the test order
nickjillings@1528 541 if (specification.randomiseOrder)
nickjillings@1528 542 {
nickjillings@1528 543 specification.audioHolders = randomiseOrder(specification.audioHolders);
nickjillings@1528 544 }
nickjillings@1528 545
nickjillings@1528 546 $(specification.audioHolders).each(function(index,elem){
nickjillings@1528 547 testState.stateMap.push(elem);
nickjillings@1528 548 });
nickjillings@1528 549
nickjillings@1528 550 testState.stateMap.push(specification.postTest);
nickjillings@1528 551
nickjillings@1528 552 // Obtain the metrics enabled
nickjillings@1528 553 $(specification.metrics).each(function(index,node){
nickjillings@1528 554 var enabled = node.textContent;
nickjillings@1528 555 switch(node.enabled)
nickjillings@1528 556 {
nickjillings@1528 557 case 'testTimer':
nickjillings@1528 558 sessionMetrics.prototype.enableTestTimer = true;
nickjillings@1528 559 break;
nickjillings@1528 560 case 'elementTimer':
nickjillings@1528 561 sessionMetrics.prototype.enableElementTimer = true;
nickjillings@1528 562 break;
nickjillings@1528 563 case 'elementTracker':
nickjillings@1528 564 sessionMetrics.prototype.enableElementTracker = true;
nickjillings@1528 565 break;
nickjillings@1528 566 case 'elementListenTracker':
nickjillings@1528 567 sessionMetrics.prototype.enableElementListenTracker = true;
nickjillings@1528 568 break;
nickjillings@1528 569 case 'elementInitialPosition':
nickjillings@1528 570 sessionMetrics.prototype.enableElementInitialPosition = true;
nickjillings@1528 571 break;
nickjillings@1528 572 case 'elementFlagListenedTo':
nickjillings@1528 573 sessionMetrics.prototype.enableFlagListenedTo = true;
nickjillings@1528 574 break;
nickjillings@1528 575 case 'elementFlagMoved':
nickjillings@1528 576 sessionMetrics.prototype.enableFlagMoved = true;
nickjillings@1528 577 break;
nickjillings@1528 578 case 'elementFlagComments':
nickjillings@1528 579 sessionMetrics.prototype.enableFlagComments = true;
nickjillings@1528 580 break;
nickjillings@1528 581 }
nickjillings@1528 582 });
nickjillings@1528 583
nickjillings@1528 584
nickjillings@1528 585
nickjillings@1528 586 // Detect the interface to use and load the relevant javascripts.
nickjillings@1528 587 var interfaceJS = document.createElement('script');
nickjillings@1528 588 interfaceJS.setAttribute("type","text/javascript");
nickjillings@1528 589 if (specification.interfaceType == 'APE') {
nickjillings@1528 590 interfaceJS.setAttribute("src","ape.js");
nickjillings@1528 591
nickjillings@1528 592 // APE comes with a css file
nickjillings@1528 593 var css = document.createElement('link');
nickjillings@1528 594 css.rel = 'stylesheet';
nickjillings@1528 595 css.type = 'text/css';
nickjillings@1528 596 css.href = 'ape.css';
nickjillings@1528 597
nickjillings@1528 598 document.getElementsByTagName("head")[0].appendChild(css);
nickjillings@1528 599 }
nickjillings@1528 600 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
nickjillings@1528 601
nickjillings@1528 602 // Define window callbacks for interface
nickjillings@1528 603 window.onresize = function(event){resizeWindow(event);};
nickjillings@1528 604 }
nickjillings@1528 605
nickjillings@1528 606 function createProjectSave(destURL) {
nickjillings@1528 607 // Save the data from interface into XML and send to destURL
nickjillings@1528 608 // If destURL is null then download XML in client
nickjillings@1528 609 // Now time to render file locally
nickjillings@1528 610 var xmlDoc = interfaceXMLSave();
nickjillings@1528 611 var parent = document.createElement("div");
nickjillings@1528 612 parent.appendChild(xmlDoc);
nickjillings@1528 613 var file = [parent.innerHTML];
nickjillings@1528 614 if (destURL == "null" || destURL == undefined) {
nickjillings@1528 615 var bb = new Blob(file,{type : 'application/xml'});
nickjillings@1528 616 var dnlk = window.URL.createObjectURL(bb);
nickjillings@1528 617 var a = document.createElement("a");
nickjillings@1528 618 a.hidden = '';
nickjillings@1528 619 a.href = dnlk;
nickjillings@1528 620 a.download = "save.xml";
nickjillings@1528 621 a.textContent = "Save File";
nickjillings@1528 622
nickjillings@1528 623 popup.showPopup();
nickjillings@1528 624 popup.popupContent.innerHTML = null;
nickjillings@1528 625 popup.popupContent.appendChild(a);
nickjillings@1528 626 } else {
nickjillings@1528 627 var xmlhttp = new XMLHttpRequest;
nickjillings@1528 628 xmlhttp.open("POST",destURL,true);
nickjillings@1528 629 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
nickjillings@1528 630 xmlhttp.onerror = function(){
nickjillings@1528 631 console.log('Error saving file to server! Presenting download locally');
nickjillings@1528 632 createProjectSave(null);
nickjillings@1528 633 };
nickjillings@1528 634 xmlhttp.onreadystatechange = function() {
nickjillings@1528 635 console.log(xmlhttp.status);
nickjillings@1528 636 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) {
nickjillings@1528 637 createProjectSave(null);
nickjillings@1528 638 }
nickjillings@1528 639 };
nickjillings@1528 640 xmlhttp.send(file);
nickjillings@1528 641 }
nickjillings@1528 642 }
nickjillings@1528 643
nickjillings@1528 644 function errorSessionDump(msg){
nickjillings@1528 645 // Create the partial interface XML save
nickjillings@1528 646 // Include error node with message on why the dump occured
nickjillings@1528 647 var xmlDoc = interfaceXMLSave();
nickjillings@1528 648 var err = document.createElement('error');
nickjillings@1528 649 err.textContent = msg;
nickjillings@1528 650 xmlDoc.appendChild(err);
nickjillings@1528 651 var parent = document.createElement("div");
nickjillings@1528 652 parent.appendChild(xmlDoc);
nickjillings@1528 653 var file = [parent.innerHTML];
nickjillings@1528 654 var bb = new Blob(file,{type : 'application/xml'});
nickjillings@1528 655 var dnlk = window.URL.createObjectURL(bb);
nickjillings@1528 656 var a = document.createElement("a");
nickjillings@1528 657 a.hidden = '';
nickjillings@1528 658 a.href = dnlk;
nickjillings@1528 659 a.download = "save.xml";
nickjillings@1528 660 a.textContent = "Save File";
nickjillings@1528 661
nickjillings@1528 662 popup.showPopup();
nickjillings@1528 663 popup.popupContent.innerHTML = "ERROR : "+msg;
nickjillings@1528 664 popup.popupContent.appendChild(a);
nickjillings@1528 665 }
nickjillings@1528 666
nickjillings@1528 667 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nickjillings@1528 668 function interfaceXMLSave(){
nickjillings@1528 669 // Create the XML string to be exported with results
nickjillings@1528 670 var xmlDoc = document.createElement("BrowserEvaluationResult");
nickjillings@1528 671 var projectDocument = specification.projectXML;
nickjillings@1528 672 projectDocument.setAttribute('file-name',url);
nickjillings@1528 673 xmlDoc.appendChild(projectDocument);
nickjillings@1528 674 xmlDoc.appendChild(returnDateNode());
nickjillings@1528 675 for (var i=0; i<testState.stateResults.length; i++)
nickjillings@1528 676 {
nickjillings@1528 677 xmlDoc.appendChild(testState.stateResults[i]);
nickjillings@1528 678 }
nickjillings@1528 679
nickjillings@1528 680 return xmlDoc;
nickjillings@1528 681 }
nickjillings@1528 682
nickjillings@1528 683 function AudioEngine() {
nickjillings@1528 684
nickjillings@1528 685 // Create two output paths, the main outputGain and fooGain.
nickjillings@1528 686 // Output gain is default to 1 and any items for playback route here
nickjillings@1528 687 // Foo gain is used for analysis to ensure paths get processed, but are not heard
nickjillings@1528 688 // because web audio will optimise and any route which does not go to the destination gets ignored.
nickjillings@1528 689 this.outputGain = audioContext.createGain();
nickjillings@1528 690 this.fooGain = audioContext.createGain();
nickjillings@1528 691 this.fooGain.gain = 0;
nickjillings@1528 692
nickjillings@1528 693 // Use this to detect playback state: 0 - stopped, 1 - playing
nickjillings@1528 694 this.status = 0;
nickjillings@1528 695 this.audioObjectsReady = false;
nickjillings@1528 696
nickjillings@1528 697 // Connect both gains to output
nickjillings@1528 698 this.outputGain.connect(audioContext.destination);
nickjillings@1528 699 this.fooGain.connect(audioContext.destination);
nickjillings@1528 700
nickjillings@1528 701 // Create the timer Object
nickjillings@1528 702 this.timer = new timer();
nickjillings@1528 703 // Create session metrics
nickjillings@1528 704 this.metric = new sessionMetrics(this);
nickjillings@1528 705
nickjillings@1528 706 this.loopPlayback = false;
nickjillings@1528 707
nickjillings@1528 708 // Create store for new audioObjects
nickjillings@1528 709 this.audioObjects = [];
nickjillings@1528 710
nickjillings@1528 711 this.play = function(id) {
nickjillings@1528 712 // Start the timer and set the audioEngine state to playing (1)
nickjillings@1528 713 if (this.status == 0) {
nickjillings@1528 714 // Check if all audioObjects are ready
nickjillings@1528 715 if (this.audioObjectsReady == false) {
nickjillings@1528 716 this.audioObjectsReady = this.checkAllReady();
nickjillings@1528 717 }
nickjillings@1528 718 if (this.audioObjectsReady == true) {
nickjillings@1528 719 this.timer.startTest();
nickjillings@1528 720 this.status = 1;
nickjillings@1528 721 }
nickjillings@1528 722 }
nickjillings@1528 723 if (this.status== 1) {
nickjillings@1528 724 if (id == undefined) {
nickjillings@1528 725 id = -1;
nickjillings@1528 726 } else {
nickjillings@1528 727 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]);
nickjillings@1528 728 }
nickjillings@1528 729 if (this.loopPlayback) {
nickjillings@1528 730 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1528 731 {
nickjillings@1528 732 this.audioObjects[i].play(this.timer.getTestTime()+1);
nickjillings@1528 733 if (id == i) {
nickjillings@1528 734 this.audioObjects[i].loopStart();
nickjillings@1528 735 } else {
nickjillings@1528 736 this.audioObjects[i].loopStop();
nickjillings@1528 737 }
nickjillings@1528 738 }
nickjillings@1528 739 } else {
nickjillings@1528 740 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1528 741 {
nickjillings@1528 742 if (i != id) {
nickjillings@1528 743 this.audioObjects[i].outputGain.gain.value = 0.0;
nickjillings@1528 744 this.audioObjects[i].stop();
nickjillings@1528 745 } else if (i == id) {
nickjillings@1528 746 this.audioObjects[id].outputGain.gain.value = 1.0;
nickjillings@1528 747 this.audioObjects[id].play(audioContext.currentTime+0.01);
nickjillings@1528 748 }
nickjillings@1528 749 }
nickjillings@1528 750 }
nickjillings@1528 751 interfaceContext.playhead.start();
nickjillings@1528 752 }
nickjillings@1528 753 };
nickjillings@1528 754
nickjillings@1528 755 this.stop = function() {
nickjillings@1528 756 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
nickjillings@1528 757 if (this.status == 1) {
nickjillings@1528 758 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1528 759 {
nickjillings@1528 760 this.audioObjects[i].stop();
nickjillings@1528 761 }
nickjillings@1528 762 interfaceContext.playhead.stop();
nickjillings@1528 763 this.status = 0;
nickjillings@1528 764 }
nickjillings@1528 765 };
nickjillings@1528 766
nickjillings@1528 767 this.newTrack = function(element) {
nickjillings@1528 768 // Pull data from given URL into new audio buffer
nickjillings@1528 769 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
nickjillings@1528 770
nickjillings@1528 771 // Create the audioObject with ID of the new track length;
nickjillings@1528 772 audioObjectId = this.audioObjects.length;
nickjillings@1528 773 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
nickjillings@1528 774
nickjillings@1528 775 // AudioObject will get track itself.
nickjillings@1528 776 this.audioObjects[audioObjectId].specification = element;
nickjillings@1528 777 this.audioObjects[audioObjectId].constructTrack(element.parent.hostURL + element.url);
nickjillings@1528 778 return this.audioObjects[audioObjectId];
nickjillings@1528 779 };
nickjillings@1528 780
nickjillings@1528 781 this.newTestPage = function() {
nickjillings@1528 782 this.state = 0;
nickjillings@1528 783 this.audioObjectsReady = false;
nickjillings@1528 784 this.metric.reset();
nickjillings@1528 785 this.audioObjects = [];
nickjillings@1528 786 };
nickjillings@1528 787
nickjillings@1528 788 this.checkAllPlayed = function() {
nickjillings@1528 789 arr = [];
nickjillings@1528 790 for (var id=0; id<this.audioObjects.length; id++) {
nickjillings@1528 791 if (this.audioObjects[id].metric.wasListenedTo == false) {
nickjillings@1528 792 arr.push(this.audioObjects[id].id);
nickjillings@1528 793 }
nickjillings@1528 794 }
nickjillings@1528 795 return arr;
nickjillings@1528 796 };
nickjillings@1528 797
nickjillings@1528 798 this.checkAllReady = function() {
nickjillings@1528 799 var ready = true;
nickjillings@1528 800 for (var i=0; i<this.audioObjects.length; i++) {
nickjillings@1528 801 if (this.audioObjects[i].state == 0) {
nickjillings@1528 802 // Track not ready
nickjillings@1528 803 console.log('WAIT -- audioObject '+i+' not ready yet!');
nickjillings@1528 804 ready = false;
nickjillings@1528 805 };
nickjillings@1528 806 }
nickjillings@1528 807 return ready;
nickjillings@1528 808 };
nickjillings@1528 809
nickjillings@1535 810 this.setSynchronousLoop = function() {
nickjillings@1535 811 // Pads the signals so they are all exactly the same length
nickjillings@1535 812 if (this.audioObjectsReady)
nickjillings@1535 813 {
nickjillings@1535 814 var length = 0;
nickjillings@1535 815 var lens = [];
nickjillings@1535 816 var maxId;
nickjillings@1535 817 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1535 818 {
nickjillings@1535 819 lens.push(this.audioObjects[i].buffer.length);
nickjillings@1535 820 if (length < this.audioObjects[i].buffer.length)
nickjillings@1535 821 {
nickjillings@1535 822 length = this.audioObjects[i].buffer.length;
nickjillings@1535 823 maxId = i;
nickjillings@1535 824 }
nickjillings@1535 825 }
nickjillings@1535 826 // Perform difference
nickjillings@1535 827 for (var i=0; i<lens.length; i++)
nickjillings@1535 828 {
nickjillings@1535 829 lens[i] = length - lens[i];
nickjillings@1535 830 }
nickjillings@1535 831 // Extract the audio and zero-pad
nickjillings@1535 832 for (var i=0; i<lens.length; i++)
nickjillings@1535 833 {
nickjillings@1535 834 var orig = this.audioObjects[i].buffer;
nickjillings@1535 835 var hold = audioContext.createBuffer(orig.numberOfChannels,length,orig.sampleRate);
nickjillings@1535 836 for (var c=0; c<orig.numberOfChannels; c++)
nickjillings@1535 837 {
nickjillings@1535 838 var inData = hold.getChannelData(c);
nickjillings@1535 839 var outData = orig.getChannelData(c);
nickjillings@1535 840 for (var n=0; n<orig.length; n++)
nickjillings@1535 841 {inData[n] = outData[n];}
nickjillings@1535 842 }
nickjillings@1535 843 this.audioObjects[i].buffer = hold;
nickjillings@1535 844 delete orig;
nickjillings@1535 845 }
nickjillings@1535 846 }
nickjillings@1535 847 };
nickjillings@1535 848
nickjillings@1528 849 }
nickjillings@1528 850
nickjillings@1528 851 function audioObject(id) {
nickjillings@1528 852 // The main buffer object with common control nodes to the AudioEngine
nickjillings@1528 853
nickjillings@1528 854 this.specification;
nickjillings@1528 855 this.id = id;
nickjillings@1528 856 this.state = 0; // 0 - no data, 1 - ready
nickjillings@1528 857 this.url = null; // Hold the URL given for the output back to the results.
nickjillings@1528 858 this.metric = new metricTracker(this);
nickjillings@1528 859
nickjillings@1528 860 // Bindings for GUI
nickjillings@1528 861 this.interfaceDOM = null;
nickjillings@1528 862 this.commentDOM = null;
nickjillings@1528 863
nickjillings@1528 864 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
nickjillings@1528 865 this.bufferNode = undefined;
nickjillings@1528 866 this.outputGain = audioContext.createGain();
nickjillings@1528 867
nickjillings@1528 868 // Default output gain to be zero
nickjillings@1528 869 this.outputGain.gain.value = 0.0;
nickjillings@1528 870
nickjillings@1528 871 // Connect buffer to the audio graph
nickjillings@1528 872 this.outputGain.connect(audioEngineContext.outputGain);
nickjillings@1528 873
nickjillings@1528 874 // the audiobuffer is not designed for multi-start playback
nickjillings@1528 875 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
nickjillings@1528 876 this.buffer;
nickjillings@1528 877
nickjillings@1528 878 this.loopStart = function() {
nickjillings@1528 879 this.outputGain.gain.value = 1.0;
nickjillings@1528 880 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1528 881 };
nickjillings@1528 882
nickjillings@1528 883 this.loopStop = function() {
nickjillings@1528 884 if (this.outputGain.gain.value != 0.0) {
nickjillings@1528 885 this.outputGain.gain.value = 0.0;
nickjillings@1528 886 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1528 887 }
nickjillings@1528 888 };
nickjillings@1528 889
nickjillings@1528 890 this.play = function(startTime) {
nickjillings@1528 891 if (this.bufferNode == undefined) {
nickjillings@1528 892 this.bufferNode = audioContext.createBufferSource();
nickjillings@1528 893 this.bufferNode.owner = this;
nickjillings@1528 894 this.bufferNode.connect(this.outputGain);
nickjillings@1528 895 this.bufferNode.buffer = this.buffer;
nickjillings@1528 896 this.bufferNode.loop = audioEngineContext.loopPlayback;
nickjillings@1528 897 this.bufferNode.onended = function() {
nickjillings@1528 898 // Safari does not like using 'this' to reference the calling object!
nickjillings@1528 899 event.srcElement.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.srcElement.owner.getCurrentPosition());
nickjillings@1528 900 };
nickjillings@1528 901 if (this.bufferNode.loop == false) {
nickjillings@1528 902 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1528 903 }
nickjillings@1528 904 this.bufferNode.start(startTime);
nickjillings@1528 905 }
nickjillings@1528 906 };
nickjillings@1528 907
nickjillings@1528 908 this.stop = function() {
nickjillings@1528 909 if (this.bufferNode != undefined)
nickjillings@1528 910 {
nickjillings@1528 911 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition());
nickjillings@1528 912 this.bufferNode.stop(0);
nickjillings@1528 913 this.bufferNode = undefined;
nickjillings@1528 914 }
nickjillings@1528 915 };
nickjillings@1528 916
nickjillings@1528 917 this.getCurrentPosition = function() {
nickjillings@1528 918 var time = audioEngineContext.timer.getTestTime();
nickjillings@1528 919 if (this.bufferNode != undefined) {
nickjillings@1528 920 if (this.bufferNode.loop == true) {
nickjillings@1528 921 if (audioEngineContext.status == 1) {
nickjillings@1528 922 return time%this.buffer.duration;
nickjillings@1528 923 } else {
nickjillings@1528 924 return 0;
nickjillings@1528 925 }
nickjillings@1528 926 } else {
nickjillings@1528 927 if (this.metric.listenHold) {
nickjillings@1528 928 return time - this.metric.listenStart;
nickjillings@1528 929 } else {
nickjillings@1528 930 return 0;
nickjillings@1528 931 }
nickjillings@1528 932 }
nickjillings@1528 933 } else {
nickjillings@1528 934 return 0;
nickjillings@1528 935 }
nickjillings@1528 936 };
nickjillings@1528 937
nickjillings@1528 938 this.constructTrack = function(url) {
nickjillings@1528 939 var request = new XMLHttpRequest();
nickjillings@1528 940 this.url = url;
nickjillings@1528 941 request.open('GET',url,true);
nickjillings@1528 942 request.responseType = 'arraybuffer';
nickjillings@1528 943
nickjillings@1528 944 var audioObj = this;
nickjillings@1528 945
nickjillings@1528 946 // Create callback to decode the data asynchronously
nickjillings@1528 947 request.onloadend = function() {
nickjillings@1528 948 audioContext.decodeAudioData(request.response, function(decodedData) {
nickjillings@1528 949 audioObj.buffer = decodedData;
nickjillings@1528 950 audioObj.state = 1;
nickjillings@1534 951 if (audioObj.specification.type != 'outsidereference')
nickjillings@1534 952 {audioObj.interfaceDOM.enable();}
nickjillings@1528 953 }, function(){
nickjillings@1528 954 // Should only be called if there was an error, but sometimes gets called continuously
nickjillings@1528 955 // Check here if the error is genuine
nickjillings@1528 956 if (audioObj.state == 0 || audioObj.buffer == undefined) {
nickjillings@1528 957 // Genuine error
nickjillings@1528 958 console.log('FATAL - Error loading buffer on '+audioObj.id);
nickjillings@1528 959 if (request.status == 404)
nickjillings@1528 960 {
nickjillings@1528 961 console.log('FATAL - Fragment '+audioObj.id+' 404 error');
nickjillings@1528 962 console.log('URL: '+audioObj.url);
nickjillings@1528 963 errorSessionDump('Fragment '+audioObj.id+' 404 error');
nickjillings@1528 964 }
nickjillings@1528 965 }
nickjillings@1528 966 });
nickjillings@1528 967 };
nickjillings@1528 968 request.send();
nickjillings@1528 969 };
nickjillings@1528 970
nickjillings@1528 971 this.exportXMLDOM = function() {
nickjillings@1528 972 var root = document.createElement('audioElement');
nickjillings@1528 973 root.id = this.specification.id;
nickjillings@1528 974 root.setAttribute('url',this.url);
nickjillings@1528 975 var file = document.createElement('file');
nickjillings@1528 976 file.setAttribute('sampleRate',this.buffer.sampleRate);
nickjillings@1528 977 file.setAttribute('channels',this.buffer.numberOfChannels);
nickjillings@1528 978 file.setAttribute('sampleCount',this.buffer.length);
nickjillings@1528 979 file.setAttribute('duration',this.buffer.duration);
nickjillings@1528 980 root.appendChild(file);
nickjillings@1528 981 if (this.specification.type != 'outsidereference') {
nickjillings@1528 982 root.appendChild(this.interfaceDOM.exportXMLDOM(this));
nickjillings@1528 983 root.appendChild(this.commentDOM.exportXMLDOM(this));
nickjillings@1529 984 if(this.specification.type == 'anchor') {
nickjillings@1529 985 root.setAttribute('anchor',true);
nickjillings@1529 986 } else if(this.specification.type == 'reference') {
nickjillings@1529 987 root.setAttribute('reference',true);
nickjillings@1529 988 }
nickjillings@1528 989 }
nickjillings@1528 990 root.appendChild(this.metric.exportXMLDOM());
nickjillings@1528 991 return root;
nickjillings@1528 992 };
nickjillings@1528 993 }
nickjillings@1528 994
nickjillings@1528 995 function timer()
nickjillings@1528 996 {
nickjillings@1528 997 /* Timer object used in audioEngine to keep track of session timings
nickjillings@1528 998 * Uses the timer of the web audio API, so sample resolution
nickjillings@1528 999 */
nickjillings@1528 1000 this.testStarted = false;
nickjillings@1528 1001 this.testStartTime = 0;
nickjillings@1528 1002 this.testDuration = 0;
nickjillings@1528 1003 this.minimumTestTime = 0; // No minimum test time
nickjillings@1528 1004 this.startTest = function()
nickjillings@1528 1005 {
nickjillings@1528 1006 if (this.testStarted == false)
nickjillings@1528 1007 {
nickjillings@1528 1008 this.testStartTime = audioContext.currentTime;
nickjillings@1528 1009 this.testStarted = true;
nickjillings@1528 1010 this.updateTestTime();
nickjillings@1528 1011 audioEngineContext.metric.initialiseTest();
nickjillings@1528 1012 }
nickjillings@1528 1013 };
nickjillings@1528 1014 this.stopTest = function()
nickjillings@1528 1015 {
nickjillings@1528 1016 if (this.testStarted)
nickjillings@1528 1017 {
nickjillings@1528 1018 this.testDuration = this.getTestTime();
nickjillings@1528 1019 this.testStarted = false;
nickjillings@1528 1020 } else {
nickjillings@1528 1021 console.log('ERR: Test tried to end before beginning');
nickjillings@1528 1022 }
nickjillings@1528 1023 };
nickjillings@1528 1024 this.updateTestTime = function()
nickjillings@1528 1025 {
nickjillings@1528 1026 if (this.testStarted)
nickjillings@1528 1027 {
nickjillings@1528 1028 this.testDuration = audioContext.currentTime - this.testStartTime;
nickjillings@1528 1029 }
nickjillings@1528 1030 };
nickjillings@1528 1031 this.getTestTime = function()
nickjillings@1528 1032 {
nickjillings@1528 1033 this.updateTestTime();
nickjillings@1528 1034 return this.testDuration;
nickjillings@1528 1035 };
nickjillings@1528 1036 }
nickjillings@1528 1037
nickjillings@1528 1038 function sessionMetrics(engine)
nickjillings@1528 1039 {
nickjillings@1528 1040 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
nickjillings@1528 1041 */
nickjillings@1528 1042 this.engine = engine;
nickjillings@1528 1043 this.lastClicked = -1;
nickjillings@1528 1044 this.data = -1;
nickjillings@1528 1045 this.reset = function() {
nickjillings@1528 1046 this.lastClicked = -1;
nickjillings@1528 1047 this.data = -1;
nickjillings@1528 1048 };
nickjillings@1528 1049 this.initialiseTest = function(){};
nickjillings@1528 1050 }
nickjillings@1528 1051
nickjillings@1528 1052 function metricTracker(caller)
nickjillings@1528 1053 {
nickjillings@1528 1054 /* Custom object to track and collect metric data
nickjillings@1528 1055 * Used only inside the audioObjects object.
nickjillings@1528 1056 */
nickjillings@1528 1057
nickjillings@1528 1058 this.listenedTimer = 0;
nickjillings@1528 1059 this.listenStart = 0;
nickjillings@1528 1060 this.listenHold = false;
nickjillings@1528 1061 this.initialPosition = -1;
nickjillings@1528 1062 this.movementTracker = [];
nickjillings@1528 1063 this.listenTracker =[];
nickjillings@1528 1064 this.wasListenedTo = false;
nickjillings@1528 1065 this.wasMoved = false;
nickjillings@1528 1066 this.hasComments = false;
nickjillings@1528 1067 this.parent = caller;
nickjillings@1528 1068
nickjillings@1528 1069 this.initialised = function(position)
nickjillings@1528 1070 {
nickjillings@1528 1071 if (this.initialPosition == -1) {
nickjillings@1528 1072 this.initialPosition = position;
nickjillings@1528 1073 }
nickjillings@1528 1074 };
nickjillings@1528 1075
nickjillings@1528 1076 this.moved = function(time,position)
nickjillings@1528 1077 {
nickjillings@1528 1078 this.wasMoved = true;
nickjillings@1528 1079 this.movementTracker[this.movementTracker.length] = [time, position];
nickjillings@1528 1080 };
nickjillings@1528 1081
nickjillings@1528 1082 this.startListening = function(time)
nickjillings@1528 1083 {
nickjillings@1528 1084 if (this.listenHold == false)
nickjillings@1528 1085 {
nickjillings@1528 1086 this.wasListenedTo = true;
nickjillings@1528 1087 this.listenStart = time;
nickjillings@1528 1088 this.listenHold = true;
nickjillings@1528 1089
nickjillings@1528 1090 var evnt = document.createElement('event');
nickjillings@1528 1091 var testTime = document.createElement('testTime');
nickjillings@1528 1092 testTime.setAttribute('start',time);
nickjillings@1528 1093 var bufferTime = document.createElement('bufferTime');
nickjillings@1528 1094 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
nickjillings@1528 1095 evnt.appendChild(testTime);
nickjillings@1528 1096 evnt.appendChild(bufferTime);
nickjillings@1528 1097 this.listenTracker.push(evnt);
nickjillings@1528 1098
nickjillings@1528 1099 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1528 1100 }
nickjillings@1528 1101 };
nickjillings@1528 1102
nickjillings@1528 1103 this.stopListening = function(time,bufferStopTime)
nickjillings@1528 1104 {
nickjillings@1528 1105 if (this.listenHold == true)
nickjillings@1528 1106 {
nickjillings@1528 1107 var diff = time - this.listenStart;
nickjillings@1528 1108 this.listenedTimer += (diff);
nickjillings@1528 1109 this.listenStart = 0;
nickjillings@1528 1110 this.listenHold = false;
nickjillings@1528 1111
nickjillings@1528 1112 var evnt = this.listenTracker[this.listenTracker.length-1];
nickjillings@1528 1113 var testTime = evnt.getElementsByTagName('testTime')[0];
nickjillings@1528 1114 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
nickjillings@1528 1115 testTime.setAttribute('stop',time);
nickjillings@1528 1116 if (bufferStopTime == undefined) {
nickjillings@1528 1117 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
nickjillings@1528 1118 } else {
nickjillings@1528 1119 bufferTime.setAttribute('stop',bufferStopTime);
nickjillings@1528 1120 }
nickjillings@1528 1121 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1528 1122 }
nickjillings@1528 1123 };
nickjillings@1528 1124
nickjillings@1528 1125 this.exportXMLDOM = function() {
nickjillings@1528 1126 var root = document.createElement('metric');
nickjillings@1528 1127 if (audioEngineContext.metric.enableElementTimer) {
nickjillings@1528 1128 var mElementTimer = document.createElement('metricresult');
nickjillings@1528 1129 mElementTimer.setAttribute('name','enableElementTimer');
nickjillings@1528 1130 mElementTimer.textContent = this.listenedTimer;
nickjillings@1528 1131 root.appendChild(mElementTimer);
nickjillings@1528 1132 }
nickjillings@1528 1133 if (audioEngineContext.metric.enableElementTracker) {
nickjillings@1528 1134 var elementTrackerFull = document.createElement('metricResult');
nickjillings@1528 1135 elementTrackerFull.setAttribute('name','elementTrackerFull');
nickjillings@1528 1136 for (var k=0; k<this.movementTracker.length; k++)
nickjillings@1528 1137 {
nickjillings@1528 1138 var timePos = document.createElement('timePos');
nickjillings@1528 1139 timePos.id = k;
nickjillings@1528 1140 var time = document.createElement('time');
nickjillings@1528 1141 time.textContent = this.movementTracker[k][0];
nickjillings@1528 1142 var position = document.createElement('position');
nickjillings@1528 1143 position.textContent = this.movementTracker[k][1];
nickjillings@1528 1144 timePos.appendChild(time);
nickjillings@1528 1145 timePos.appendChild(position);
nickjillings@1528 1146 elementTrackerFull.appendChild(timePos);
nickjillings@1528 1147 }
nickjillings@1528 1148 root.appendChild(elementTrackerFull);
nickjillings@1528 1149 }
nickjillings@1528 1150 if (audioEngineContext.metric.enableElementListenTracker) {
nickjillings@1528 1151 var elementListenTracker = document.createElement('metricResult');
nickjillings@1528 1152 elementListenTracker.setAttribute('name','elementListenTracker');
nickjillings@1528 1153 for (var k=0; k<this.listenTracker.length; k++) {
nickjillings@1528 1154 elementListenTracker.appendChild(this.listenTracker[k]);
nickjillings@1528 1155 }
nickjillings@1528 1156 root.appendChild(elementListenTracker);
nickjillings@1528 1157 }
nickjillings@1528 1158 if (audioEngineContext.metric.enableElementInitialPosition) {
nickjillings@1528 1159 var elementInitial = document.createElement('metricResult');
nickjillings@1528 1160 elementInitial.setAttribute('name','elementInitialPosition');
nickjillings@1528 1161 elementInitial.textContent = this.initialPosition;
nickjillings@1528 1162 root.appendChild(elementInitial);
nickjillings@1528 1163 }
nickjillings@1528 1164 if (audioEngineContext.metric.enableFlagListenedTo) {
nickjillings@1528 1165 var flagListenedTo = document.createElement('metricResult');
nickjillings@1528 1166 flagListenedTo.setAttribute('name','elementFlagListenedTo');
nickjillings@1528 1167 flagListenedTo.textContent = this.wasListenedTo;
nickjillings@1528 1168 root.appendChild(flagListenedTo);
nickjillings@1528 1169 }
nickjillings@1528 1170 if (audioEngineContext.metric.enableFlagMoved) {
nickjillings@1528 1171 var flagMoved = document.createElement('metricResult');
nickjillings@1528 1172 flagMoved.setAttribute('name','elementFlagMoved');
nickjillings@1528 1173 flagMoved.textContent = this.wasMoved;
nickjillings@1528 1174 root.appendChild(flagMoved);
nickjillings@1528 1175 }
nickjillings@1528 1176 if (audioEngineContext.metric.enableFlagComments) {
nickjillings@1528 1177 var flagComments = document.createElement('metricResult');
nickjillings@1528 1178 flagComments.setAttribute('name','elementFlagComments');
nickjillings@1528 1179 if (this.parent.commentDOM == null)
nickjillings@1528 1180 {flag.textContent = 'false';}
nickjillings@1528 1181 else if (this.parent.commentDOM.textContent.length == 0)
nickjillings@1528 1182 {flag.textContent = 'false';}
nickjillings@1528 1183 else
nickjillings@1528 1184 {flag.textContet = 'true';}
nickjillings@1528 1185 root.appendChild(flagComments);
nickjillings@1528 1186 }
nickjillings@1528 1187
nickjillings@1528 1188 return root;
nickjillings@1528 1189 };
nickjillings@1528 1190 }
nickjillings@1528 1191
nickjillings@1528 1192 function randomiseOrder(input)
nickjillings@1528 1193 {
nickjillings@1528 1194 // This takes an array of information and randomises the order
nickjillings@1528 1195 var N = input.length;
nickjillings@1528 1196
nickjillings@1528 1197 var inputSequence = []; // For safety purposes: keep track of randomisation
nickjillings@1528 1198 for (var counter = 0; counter < N; ++counter)
nickjillings@1528 1199 inputSequence.push(counter) // Fill array
nickjillings@1528 1200 var inputSequenceClone = inputSequence.slice(0);
nickjillings@1528 1201
nickjillings@1528 1202 var holdArr = [];
nickjillings@1528 1203 var outputSequence = [];
nickjillings@1528 1204 for (var n=0; n<N; n++)
nickjillings@1528 1205 {
nickjillings@1528 1206 // First pick a random number
nickjillings@1528 1207 var r = Math.random();
nickjillings@1528 1208 // Multiply and floor by the number of elements left
nickjillings@1528 1209 r = Math.floor(r*input.length);
nickjillings@1528 1210 // Pick out that element and delete from the array
nickjillings@1528 1211 holdArr.push(input.splice(r,1)[0]);
nickjillings@1528 1212 // Do the same with sequence
nickjillings@1528 1213 outputSequence.push(inputSequence.splice(r,1)[0]);
nickjillings@1528 1214 }
nickjillings@1528 1215 console.log(inputSequenceClone.toString()); // print original array to console
nickjillings@1528 1216 console.log(outputSequence.toString()); // print randomised array to console
nickjillings@1528 1217 return holdArr;
nickjillings@1528 1218 }
nickjillings@1528 1219
nickjillings@1528 1220 function returnDateNode()
nickjillings@1528 1221 {
nickjillings@1528 1222 // Create an XML Node for the Date and Time a test was conducted
nickjillings@1528 1223 // Structure is
nickjillings@1528 1224 // <datetime>
nickjillings@1528 1225 // <date year="##" month="##" day="##">DD/MM/YY</date>
nickjillings@1528 1226 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
nickjillings@1528 1227 // </datetime>
nickjillings@1528 1228 var dateTime = new Date();
nickjillings@1528 1229 var year = document.createAttribute('year');
nickjillings@1528 1230 var month = document.createAttribute('month');
nickjillings@1528 1231 var day = document.createAttribute('day');
nickjillings@1528 1232 var hour = document.createAttribute('hour');
nickjillings@1528 1233 var minute = document.createAttribute('minute');
nickjillings@1528 1234 var secs = document.createAttribute('secs');
nickjillings@1528 1235
nickjillings@1528 1236 year.nodeValue = dateTime.getFullYear();
nickjillings@1528 1237 month.nodeValue = dateTime.getMonth()+1;
nickjillings@1528 1238 day.nodeValue = dateTime.getDate();
nickjillings@1528 1239 hour.nodeValue = dateTime.getHours();
nickjillings@1528 1240 minute.nodeValue = dateTime.getMinutes();
nickjillings@1528 1241 secs.nodeValue = dateTime.getSeconds();
nickjillings@1528 1242
nickjillings@1528 1243 var hold = document.createElement("datetime");
nickjillings@1528 1244 var date = document.createElement("date");
nickjillings@1528 1245 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
nickjillings@1528 1246 var time = document.createElement("time");
nickjillings@1528 1247 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
nickjillings@1528 1248
nickjillings@1528 1249 date.setAttributeNode(year);
nickjillings@1528 1250 date.setAttributeNode(month);
nickjillings@1528 1251 date.setAttributeNode(day);
nickjillings@1528 1252 time.setAttributeNode(hour);
nickjillings@1528 1253 time.setAttributeNode(minute);
nickjillings@1528 1254 time.setAttributeNode(secs);
nickjillings@1528 1255
nickjillings@1528 1256 hold.appendChild(date);
nickjillings@1528 1257 hold.appendChild(time);
nickjillings@1528 1258 return hold
nickjillings@1528 1259
nickjillings@1528 1260 }
nickjillings@1528 1261
nickjillings@1528 1262 function testWaitIndicator() {
nickjillings@1528 1263 if (audioEngineContext.checkAllReady() == false) {
nickjillings@1528 1264 var hold = document.createElement("div");
nickjillings@1528 1265 hold.id = "testWaitIndicator";
nickjillings@1528 1266 hold.className = "indicator-box";
nickjillings@1528 1267 hold.style.zIndex = 3;
nickjillings@1528 1268 var span = document.createElement("span");
nickjillings@1528 1269 span.textContent = "Please wait! Elements still loading";
nickjillings@1528 1270 hold.appendChild(span);
nickjillings@1528 1271 var blank = document.createElement('div');
nickjillings@1528 1272 blank.className = 'testHalt';
nickjillings@1528 1273 blank.id = "testHaltBlank";
nickjillings@1528 1274 var body = document.getElementsByTagName('body')[0];
nickjillings@1528 1275 body.appendChild(hold);
nickjillings@1528 1276 body.appendChild(blank);
nickjillings@1528 1277 testWaitTimerIntervalHolder = setInterval(function(){
nickjillings@1528 1278 var ready = audioEngineContext.checkAllReady();
nickjillings@1528 1279 if (ready) {
nickjillings@1528 1280 var elem = document.getElementById('testWaitIndicator');
nickjillings@1528 1281 var blank = document.getElementById('testHaltBlank');
nickjillings@1528 1282 var body = document.getElementsByTagName('body')[0];
nickjillings@1528 1283 body.removeChild(elem);
nickjillings@1528 1284 body.removeChild(blank);
nickjillings@1528 1285 clearInterval(testWaitTimerIntervalHolder);
nickjillings@1528 1286 }
nickjillings@1528 1287 },500,false);
nickjillings@1528 1288 }
nickjillings@1528 1289 }
nickjillings@1528 1290
nickjillings@1528 1291 var testWaitTimerIntervalHolder = null;
nickjillings@1528 1292
nickjillings@1528 1293 function Specification() {
nickjillings@1528 1294 // Handles the decoding of the project specification XML into a simple JavaScript Object.
nickjillings@1528 1295
nickjillings@1528 1296 this.interfaceType;
nickjillings@1528 1297 this.commonInterface;
nickjillings@1528 1298 this.projectReturn;
nickjillings@1528 1299 this.randomiseOrder;
nickjillings@1528 1300 this.collectMetrics;
nickjillings@1528 1301 this.preTest;
nickjillings@1528 1302 this.postTest;
nickjillings@1528 1303 this.metrics =[];
nickjillings@1528 1304
nickjillings@1528 1305 this.audioHolders = [];
nickjillings@1528 1306
nickjillings@1528 1307 this.decode = function() {
nickjillings@1528 1308 // projectXML - DOM Parsed document
nickjillings@1528 1309 this.projectXML = projectXML.childNodes[0];
nickjillings@1528 1310 var setupNode = projectXML.getElementsByTagName('setup')[0];
nickjillings@1528 1311 this.interfaceType = setupNode.getAttribute('interface');
nickjillings@1528 1312 this.projectReturn = setupNode.getAttribute('projectReturn');
nickjillings@1528 1313 if (setupNode.getAttribute('randomiseOrder') == "true") {
nickjillings@1528 1314 this.randomiseOrder = true;
nickjillings@1528 1315 } else {this.randomiseOrder = false;}
nickjillings@1528 1316 if (setupNode.getAttribute('collectMetrics') == "true") {
nickjillings@1528 1317 this.collectMetrics = true;
nickjillings@1528 1318 } else {this.collectMetrics = false;}
nickjillings@1528 1319 var metricCollection = setupNode.getElementsByTagName('Metric');
nickjillings@1528 1320
nickjillings@1528 1321 this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest'));
nickjillings@1528 1322 this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest'));
nickjillings@1528 1323
nickjillings@1528 1324 if (metricCollection.length > 0) {
nickjillings@1528 1325 metricCollection = metricCollection[0].getElementsByTagName('metricEnable');
nickjillings@1528 1326 for (var i=0; i<metricCollection.length; i++) {
nickjillings@1528 1327 this.metrics.push(new this.metricNode(metricCollection[i].textContent));
nickjillings@1528 1328 }
nickjillings@1528 1329 }
nickjillings@1528 1330
nickjillings@1528 1331 var commonInterfaceNode = setupNode.getElementsByTagName('interface');
nickjillings@1528 1332 if (commonInterfaceNode.length > 0) {
nickjillings@1528 1333 commonInterfaceNode = commonInterfaceNode[0];
nickjillings@1528 1334 } else {
nickjillings@1528 1335 commonInterfaceNode = undefined;
nickjillings@1528 1336 }
nickjillings@1528 1337
nickjillings@1528 1338 this.commonInterface = new function() {
nickjillings@1528 1339 this.OptionNode = function(child) {
nickjillings@1528 1340 this.type = child.nodeName;
nickjillings@1528 1341 if (this.type == 'option')
nickjillings@1528 1342 {
nickjillings@1528 1343 this.name = child.getAttribute('name');
nickjillings@1528 1344 }
nickjillings@1528 1345 else if (this.type == 'check') {
nickjillings@1528 1346 this.check = child.getAttribute('name');
nickjillings@1528 1347 if (this.check == 'scalerange') {
nickjillings@1528 1348 this.min = child.getAttribute('min');
nickjillings@1528 1349 this.max = child.getAttribute('max');
nickjillings@1528 1350 if (this.min == null) {this.min = 1;}
nickjillings@1528 1351 else if (Number(this.min) > 1 && this.min != null) {
nickjillings@1528 1352 this.min = Number(this.min)/100;
nickjillings@1528 1353 } else {
nickjillings@1528 1354 this.min = Number(this.min);
nickjillings@1528 1355 }
nickjillings@1528 1356 if (this.max == null) {this.max = 0;}
nickjillings@1528 1357 else if (Number(this.max) > 1 && this.max != null) {
nickjillings@1528 1358 this.max = Number(this.max)/100;
nickjillings@1528 1359 } else {
nickjillings@1528 1360 this.max = Number(this.max);
nickjillings@1528 1361 }
nickjillings@1528 1362 }
nickjillings@1528 1363 } else if (this.type == 'anchor' || this.type == 'reference') {
nickjillings@1528 1364 this.value = Number(child.textContent);
nickjillings@1532 1365 this.enforce = child.getAttribute('enforce');
nickjillings@1532 1366 if (this.enforce == 'true') {this.enforce = true;}
nickjillings@1532 1367 else {this.enforce = false;}
nickjillings@1528 1368 }
nickjillings@1528 1369 };
nickjillings@1528 1370 this.options = [];
nickjillings@1528 1371 if (commonInterfaceNode != undefined) {
nickjillings@1528 1372 var child = commonInterfaceNode.firstElementChild;
nickjillings@1528 1373 while (child != undefined) {
nickjillings@1528 1374 this.options.push(new this.OptionNode(child));
nickjillings@1528 1375 child = child.nextElementSibling;
nickjillings@1528 1376 }
nickjillings@1528 1377 }
nickjillings@1528 1378 };
nickjillings@1528 1379
nickjillings@1528 1380 var audioHolders = projectXML.getElementsByTagName('audioHolder');
nickjillings@1528 1381 for (var i=0; i<audioHolders.length; i++) {
nickjillings@1528 1382 this.audioHolders.push(new this.audioHolderNode(this,audioHolders[i]));
nickjillings@1528 1383 }
nickjillings@1528 1384
nickjillings@1528 1385 };
nickjillings@1528 1386
nickjillings@1528 1387 this.prepostNode = function(type,Collection) {
nickjillings@1528 1388 this.type = type;
nickjillings@1528 1389 this.options = [];
nickjillings@1528 1390
nickjillings@1528 1391 this.OptionNode = function(child) {
nickjillings@1528 1392
nickjillings@1528 1393 this.childOption = function(element) {
nickjillings@1528 1394 this.type = 'option';
nickjillings@1528 1395 this.id = element.id;
nickjillings@1528 1396 this.name = element.getAttribute('name');
nickjillings@1528 1397 this.text = element.textContent;
nickjillings@1528 1398 };
nickjillings@1528 1399
nickjillings@1528 1400 this.type = child.nodeName;
nickjillings@1528 1401 if (child.nodeName == "question") {
nickjillings@1528 1402 this.id = child.id;
nickjillings@1528 1403 this.mandatory;
nickjillings@1528 1404 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;}
nickjillings@1528 1405 else {this.mandatory = false;}
nickjillings@1528 1406 this.question = child.textContent;
nickjillings@1528 1407 if (child.getAttribute('boxsize') == null) {
nickjillings@1528 1408 this.boxsize = 'normal';
nickjillings@1528 1409 } else {
nickjillings@1528 1410 this.boxsize = child.getAttribute('boxsize');
nickjillings@1528 1411 }
nickjillings@1528 1412 } else if (child.nodeName == "statement") {
nickjillings@1528 1413 this.statement = child.textContent;
nickjillings@1528 1414 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") {
nickjillings@1528 1415 var element = child.firstElementChild;
nickjillings@1528 1416 this.id = child.id;
nickjillings@1528 1417 if (element == null) {
nickjillings@1528 1418 console.log('Malformed' +child.nodeName+ 'entry');
nickjillings@1528 1419 this.statement = 'Malformed' +child.nodeName+ 'entry';
nickjillings@1528 1420 this.type = 'statement';
nickjillings@1528 1421 } else {
nickjillings@1528 1422 this.options = [];
nickjillings@1528 1423 while (element != null) {
nickjillings@1528 1424 if (element.nodeName == 'statement' && this.statement == undefined){
nickjillings@1528 1425 this.statement = element.textContent;
nickjillings@1528 1426 } else if (element.nodeName == 'option') {
nickjillings@1528 1427 this.options.push(new this.childOption(element));
nickjillings@1528 1428 }
nickjillings@1528 1429 element = element.nextElementSibling;
nickjillings@1528 1430 }
nickjillings@1528 1431 }
nickjillings@1528 1432 } else if (child.nodeName == "number") {
nickjillings@1528 1433 this.statement = child.textContent;
nickjillings@1528 1434 this.id = child.id;
nickjillings@1528 1435 this.min = child.getAttribute('min');
nickjillings@1528 1436 this.max = child.getAttribute('max');
nickjillings@1528 1437 this.step = child.getAttribute('step');
nickjillings@1528 1438 }
nickjillings@1528 1439 };
nickjillings@1528 1440
nickjillings@1528 1441 // On construction:
nickjillings@1528 1442 if (Collection.length != 0) {
nickjillings@1528 1443 Collection = Collection[0];
nickjillings@1528 1444 if (Collection.childElementCount != 0) {
nickjillings@1528 1445 var child = Collection.firstElementChild;
nickjillings@1528 1446 this.options.push(new this.OptionNode(child));
nickjillings@1528 1447 while (child.nextElementSibling != null) {
nickjillings@1528 1448 child = child.nextElementSibling;
nickjillings@1528 1449 this.options.push(new this.OptionNode(child));
nickjillings@1528 1450 }
nickjillings@1528 1451 }
nickjillings@1528 1452 }
nickjillings@1528 1453 };
nickjillings@1528 1454
nickjillings@1528 1455 this.metricNode = function(name) {
nickjillings@1528 1456 this.enabled = name;
nickjillings@1528 1457 };
nickjillings@1528 1458
nickjillings@1528 1459 this.audioHolderNode = function(parent,xml) {
nickjillings@1528 1460 this.type = 'audioHolder';
nickjillings@1528 1461 this.interfaceNode = function(DOM) {
nickjillings@1528 1462 var title = DOM.getElementsByTagName('title');
nickjillings@1528 1463 if (title.length == 0) {this.title = null;}
nickjillings@1528 1464 else {this.title = title[0].textContent;}
nickjillings@1528 1465 this.options = parent.commonInterface.options;
nickjillings@1528 1466 var scale = DOM.getElementsByTagName('scale');
nickjillings@1528 1467 this.scale = [];
nickjillings@1528 1468 for (var i=0; i<scale.length; i++) {
nickjillings@1528 1469 var arr = [null, null];
nickjillings@1528 1470 arr[0] = scale[i].getAttribute('position');
nickjillings@1528 1471 arr[1] = scale[i].textContent;
nickjillings@1528 1472 this.scale.push(arr);
nickjillings@1528 1473 }
nickjillings@1528 1474 };
nickjillings@1528 1475
nickjillings@1528 1476 this.audioElementNode = function(parent,xml) {
nickjillings@1528 1477 this.url = xml.getAttribute('url');
nickjillings@1528 1478 this.id = xml.id;
nickjillings@1528 1479 this.parent = parent;
nickjillings@1528 1480 this.type = xml.getAttribute('type');
nickjillings@1528 1481 if (this.type == null) {this.type = "normal";}
nickjillings@1528 1482 if (this.type == 'anchor') {this.anchor = true;}
nickjillings@1528 1483 else {this.anchor = false;}
nickjillings@1528 1484 if (this.type == 'reference') {this.reference = true;}
nickjillings@1528 1485 else {this.reference = false;}
nickjillings@1528 1486
nickjillings@1528 1487 this.marker = xml.getAttribute('marker');
nickjillings@1528 1488 if (this.marker == null) {this.marker = undefined;}
nickjillings@1528 1489
nickjillings@1532 1490 if (this.anchor == true) {
nickjillings@1532 1491 if (this.marker != undefined) {this.enforce = true;}
nickjillings@1532 1492 else {this.enforce = enforceAnchor;}
nickjillings@1528 1493 this.marker = anchor;
nickjillings@1528 1494 }
nickjillings@1532 1495 else if (this.reference == true) {
nickjillings@1532 1496 if (this.marker != undefined) {this.enforce = true;}
nickjillings@1532 1497 else {this.enforce = enforceReference;}
nickjillings@1528 1498 this.marker = reference;
nickjillings@1528 1499 }
nickjillings@1528 1500
nickjillings@1528 1501 if (this.marker != undefined) {
nickjillings@1528 1502 this.marker = Number(this.marker);
nickjillings@1528 1503 if (this.marker > 1) {this.marker /= 100;}
nickjillings@1528 1504 }
nickjillings@1528 1505 };
nickjillings@1528 1506
nickjillings@1528 1507 this.commentQuestionNode = function(xml) {
nickjillings@1528 1508 this.childOption = function(element) {
nickjillings@1528 1509 this.type = 'option';
nickjillings@1528 1510 this.name = element.getAttribute('name');
nickjillings@1528 1511 this.text = element.textContent;
nickjillings@1528 1512 };
nickjillings@1528 1513 this.id = xml.id;
nickjillings@1528 1514 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;}
nickjillings@1528 1515 else {this.mandatory = false;}
nickjillings@1528 1516 this.type = xml.getAttribute('type');
nickjillings@1528 1517 if (this.type == undefined) {this.type = 'text';}
nickjillings@1528 1518 switch (this.type) {
nickjillings@1528 1519 case 'text':
nickjillings@1528 1520 this.question = xml.textContent;
nickjillings@1528 1521 break;
nickjillings@1528 1522 case 'radio':
nickjillings@1528 1523 var child = xml.firstElementChild;
nickjillings@1528 1524 this.options = [];
nickjillings@1528 1525 while (child != undefined) {
nickjillings@1528 1526 if (child.nodeName == 'statement' && this.statement == undefined) {
nickjillings@1528 1527 this.statement = child.textContent;
nickjillings@1528 1528 } else if (child.nodeName == 'option') {
nickjillings@1528 1529 this.options.push(new this.childOption(child));
nickjillings@1528 1530 }
nickjillings@1528 1531 child = child.nextElementSibling;
nickjillings@1528 1532 }
nickjillings@1528 1533 break;
nickjillings@1528 1534 case 'checkbox':
nickjillings@1528 1535 var child = xml.firstElementChild;
nickjillings@1528 1536 this.options = [];
nickjillings@1528 1537 while (child != undefined) {
nickjillings@1528 1538 if (child.nodeName == 'statement' && this.statement == undefined) {
nickjillings@1528 1539 this.statement = child.textContent;
nickjillings@1528 1540 } else if (child.nodeName == 'option') {
nickjillings@1528 1541 this.options.push(new this.childOption(child));
nickjillings@1528 1542 }
nickjillings@1528 1543 child = child.nextElementSibling;
nickjillings@1528 1544 }
nickjillings@1528 1545 break;
nickjillings@1528 1546 }
nickjillings@1528 1547 };
nickjillings@1528 1548
nickjillings@1528 1549 this.id = xml.id;
nickjillings@1528 1550 this.hostURL = xml.getAttribute('hostURL');
nickjillings@1528 1551 this.sampleRate = xml.getAttribute('sampleRate');
nickjillings@1528 1552 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;}
nickjillings@1528 1553 else {this.randomiseOrder = false;}
nickjillings@1528 1554 this.repeatCount = xml.getAttribute('repeatCount');
nickjillings@1528 1555 if (xml.getAttribute('loop') == 'true') {this.loop = true;}
nickjillings@1528 1556 else {this.loop == false;}
nickjillings@1528 1557 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;}
nickjillings@1528 1558 else {this.elementComments = false;}
nickjillings@1528 1559
nickjillings@1528 1560 var anchor = xml.getElementsByTagName('anchor');
nickjillings@1532 1561 var enforceAnchor = false;
nickjillings@1528 1562 if (anchor.length == 0) {
nickjillings@1528 1563 // Find anchor in commonInterface;
nickjillings@1528 1564 for (var i=0; i<parent.commonInterface.options.length; i++) {
nickjillings@1528 1565 if(parent.commonInterface.options[i].type == 'anchor') {
nickjillings@1528 1566 anchor = parent.commonInterface.options[i].value;
nickjillings@1532 1567 enforceAnchor = parent.commonInterface.options[i].enforce;
nickjillings@1528 1568 break;
nickjillings@1528 1569 }
nickjillings@1528 1570 }
nickjillings@1528 1571 if (typeof(anchor) == "object") {
nickjillings@1528 1572 anchor = null;
nickjillings@1528 1573 }
nickjillings@1528 1574 } else {
nickjillings@1528 1575 anchor = anchor[0].textContent;
nickjillings@1528 1576 }
nickjillings@1528 1577
nickjillings@1528 1578 var reference = xml.getElementsByTagName('anchor');
nickjillings@1532 1579 var enforceReference = false;
nickjillings@1528 1580 if (reference.length == 0) {
nickjillings@1528 1581 // Find anchor in commonInterface;
nickjillings@1528 1582 for (var i=0; i<parent.commonInterface.options.length; i++) {
nickjillings@1528 1583 if(parent.commonInterface.options[i].type == 'reference') {
nickjillings@1528 1584 reference = parent.commonInterface.options[i].value;
nickjillings@1532 1585 enforceReference = parent.commonInterface.options[i].enforce;
nickjillings@1528 1586 break;
nickjillings@1528 1587 }
nickjillings@1528 1588 }
nickjillings@1528 1589 if (typeof(reference) == "object") {
nickjillings@1528 1590 reference = null;
nickjillings@1528 1591 }
nickjillings@1528 1592 } else {
nickjillings@1528 1593 reference = reference[0].textContent;
nickjillings@1528 1594 }
nickjillings@1528 1595
nickjillings@1528 1596 if (typeof(anchor) == 'number') {
nickjillings@1528 1597 if (anchor > 1 && anchor < 100) {anchor /= 100.0;}
nickjillings@1528 1598 }
nickjillings@1528 1599
nickjillings@1528 1600 if (typeof(reference) == 'number') {
nickjillings@1528 1601 if (reference > 1 && reference < 100) {reference /= 100.0;}
nickjillings@1528 1602 }
nickjillings@1528 1603
nickjillings@1528 1604 this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest'));
nickjillings@1528 1605 this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest'));
nickjillings@1528 1606
nickjillings@1528 1607 this.interfaces = [];
nickjillings@1528 1608 var interfaceDOM = xml.getElementsByTagName('interface');
nickjillings@1528 1609 for (var i=0; i<interfaceDOM.length; i++) {
nickjillings@1528 1610 this.interfaces.push(new this.interfaceNode(interfaceDOM[i]));
nickjillings@1528 1611 }
nickjillings@1528 1612
nickjillings@1528 1613 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix');
nickjillings@1528 1614 if (this.commentBoxPrefix.length != 0) {
nickjillings@1528 1615 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent;
nickjillings@1528 1616 } else {
nickjillings@1528 1617 this.commentBoxPrefix = "Comment on track";
nickjillings@1528 1618 }
nickjillings@1528 1619
nickjillings@1528 1620 this.audioElements =[];
nickjillings@1528 1621 var audioElementsDOM = xml.getElementsByTagName('audioElements');
nickjillings@1528 1622 this.outsideReference = null;
nickjillings@1528 1623 for (var i=0; i<audioElementsDOM.length; i++) {
nickjillings@1528 1624 if (audioElementsDOM[i].getAttribute('type') == 'outsidereference') {
nickjillings@1528 1625 if (this.outsideReference == null) {
nickjillings@1528 1626 this.outsideReference = new this.audioElementNode(this,audioElementsDOM[i]);
nickjillings@1528 1627 } else {
nickjillings@1528 1628 console.log('Error only one audioelement can be of type outsidereference per audioholder');
nickjillings@1528 1629 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
nickjillings@1528 1630 console.log('Element id '+audioElementsDOM[i].id+' made into normal node');
nickjillings@1528 1631 }
nickjillings@1528 1632 } else {
nickjillings@1528 1633 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
nickjillings@1528 1634 }
nickjillings@1528 1635 }
nickjillings@1528 1636
nickjillings@1528 1637 if (this.randomiseOrder) {
nickjillings@1528 1638 this.audioElements = randomiseOrder(this.audioElements);
nickjillings@1528 1639 }
nickjillings@1528 1640
nickjillings@1528 1641 // Check only one anchor and one reference per audioNode
nickjillings@1528 1642 var anchor = [];
nickjillings@1528 1643 var reference = [];
nickjillings@1528 1644 this.anchorId = null;
nickjillings@1528 1645 this.referenceId = null;
nickjillings@1528 1646 for (var i=0; i<this.audioElements.length; i++)
nickjillings@1528 1647 {
nickjillings@1528 1648 if (this.audioElements[i].anchor == true) {anchor.push(i);}
nickjillings@1528 1649 if (this.audioElements[i].reference == true) {reference.push(i);}
nickjillings@1528 1650 }
nickjillings@1528 1651
nickjillings@1528 1652 if (anchor.length > 1) {
nickjillings@1528 1653 console.log('Error - cannot have more than one anchor!');
nickjillings@1528 1654 console.log('Each anchor node will be a normal mode to continue the test');
nickjillings@1528 1655 for (var i=0; i<anchor.length; i++)
nickjillings@1528 1656 {
nickjillings@1528 1657 this.audioElements[anchor[i]].anchor = false;
nickjillings@1528 1658 this.audioElements[anchor[i]].value = undefined;
nickjillings@1528 1659 }
nickjillings@1528 1660 } else {this.anchorId = anchor[0];}
nickjillings@1528 1661 if (reference.length > 1) {
nickjillings@1528 1662 console.log('Error - cannot have more than one anchor!');
nickjillings@1528 1663 console.log('Each anchor node will be a normal mode to continue the test');
nickjillings@1528 1664 for (var i=0; i<reference.length; i++)
nickjillings@1528 1665 {
nickjillings@1528 1666 this.audioElements[reference[i]].reference = false;
nickjillings@1528 1667 this.audioElements[reference[i]].value = undefined;
nickjillings@1528 1668 }
nickjillings@1528 1669 } else {this.referenceId = reference[0];}
nickjillings@1528 1670
nickjillings@1528 1671 this.commentQuestions = [];
nickjillings@1528 1672 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion');
nickjillings@1528 1673 for (var i=0; i<commentQuestionsDOM.length; i++) {
nickjillings@1528 1674 this.commentQuestions.push(new this.commentQuestionNode(commentQuestionsDOM[i]));
nickjillings@1528 1675 }
nickjillings@1528 1676 };
nickjillings@1528 1677 }
nickjillings@1528 1678
nickjillings@1528 1679 function Interface(specificationObject) {
nickjillings@1528 1680 // This handles the bindings between the interface and the audioEngineContext;
nickjillings@1528 1681 this.specification = specificationObject;
nickjillings@1528 1682 this.insertPoint = document.getElementById("topLevelBody");
nickjillings@1528 1683
nickjillings@1528 1684 // Bounded by interface!!
nickjillings@1528 1685 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
nickjillings@1528 1686 // For example, APE returns the slider position normalised in a <value> tag.
nickjillings@1528 1687 this.interfaceObjects = [];
nickjillings@1528 1688 this.interfaceObject = function(){};
nickjillings@1528 1689
nickjillings@1528 1690 this.commentBoxes = [];
nickjillings@1528 1691 this.elementCommentBox = function(audioObject) {
nickjillings@1528 1692 var element = audioObject.specification;
nickjillings@1528 1693 this.audioObject = audioObject;
nickjillings@1528 1694 this.id = audioObject.id;
nickjillings@1528 1695 var audioHolderObject = audioObject.specification.parent;
nickjillings@1528 1696 // Create document objects to hold the comment boxes
nickjillings@1528 1697 this.trackComment = document.createElement('div');
nickjillings@1528 1698 this.trackComment.className = 'comment-div';
nickjillings@1528 1699 this.trackComment.id = 'comment-div-'+audioObject.id;
nickjillings@1528 1700 // Create a string next to each comment asking for a comment
nickjillings@1528 1701 this.trackString = document.createElement('span');
nickjillings@1528 1702 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id;
nickjillings@1528 1703 // Create the HTML5 comment box 'textarea'
nickjillings@1528 1704 this.trackCommentBox = document.createElement('textarea');
nickjillings@1528 1705 this.trackCommentBox.rows = '4';
nickjillings@1528 1706 this.trackCommentBox.cols = '100';
nickjillings@1528 1707 this.trackCommentBox.name = 'trackComment'+audioObject.id;
nickjillings@1528 1708 this.trackCommentBox.className = 'trackComment';
nickjillings@1528 1709 var br = document.createElement('br');
nickjillings@1528 1710 // Add to the holder.
nickjillings@1528 1711 this.trackComment.appendChild(this.trackString);
nickjillings@1528 1712 this.trackComment.appendChild(br);
nickjillings@1528 1713 this.trackComment.appendChild(this.trackCommentBox);
nickjillings@1528 1714
nickjillings@1528 1715 this.exportXMLDOM = function() {
nickjillings@1528 1716 var root = document.createElement('comment');
nickjillings@1528 1717 if (this.audioObject.specification.parent.elementComments) {
nickjillings@1528 1718 var question = document.createElement('question');
nickjillings@1528 1719 question.textContent = this.trackString.textContent;
nickjillings@1528 1720 var response = document.createElement('response');
nickjillings@1528 1721 response.textContent = this.trackCommentBox.value;
nickjillings@1528 1722 console.log("Comment frag-"+this.id+": "+response.textContent);
nickjillings@1528 1723 root.appendChild(question);
nickjillings@1528 1724 root.appendChild(response);
nickjillings@1528 1725 }
nickjillings@1528 1726 return root;
nickjillings@1528 1727 };
nickjillings@1528 1728 };
nickjillings@1528 1729
nickjillings@1528 1730 this.commentQuestions = [];
nickjillings@1528 1731
nickjillings@1528 1732 this.commentBox = function(commentQuestion) {
nickjillings@1528 1733 this.specification = commentQuestion;
nickjillings@1528 1734 // Create document objects to hold the comment boxes
nickjillings@1528 1735 this.holder = document.createElement('div');
nickjillings@1528 1736 this.holder.className = 'comment-div';
nickjillings@1528 1737 // Create a string next to each comment asking for a comment
nickjillings@1528 1738 this.string = document.createElement('span');
nickjillings@1528 1739 this.string.innerHTML = commentQuestion.question;
nickjillings@1528 1740 // Create the HTML5 comment box 'textarea'
nickjillings@1528 1741 this.textArea = document.createElement('textarea');
nickjillings@1528 1742 this.textArea.rows = '4';
nickjillings@1528 1743 this.textArea.cols = '100';
nickjillings@1528 1744 this.textArea.className = 'trackComment';
nickjillings@1528 1745 var br = document.createElement('br');
nickjillings@1528 1746 // Add to the holder.
nickjillings@1528 1747 this.holder.appendChild(this.string);
nickjillings@1528 1748 this.holder.appendChild(br);
nickjillings@1528 1749 this.holder.appendChild(this.textArea);
nickjillings@1528 1750
nickjillings@1528 1751 this.exportXMLDOM = function() {
nickjillings@1528 1752 var root = document.createElement('comment');
nickjillings@1528 1753 root.id = this.specification.id;
nickjillings@1528 1754 root.setAttribute('type',this.specification.type);
nickjillings@1528 1755 root.textContent = this.textArea.value;
nickjillings@1528 1756 console.log("Question: "+this.string.textContent);
nickjillings@1528 1757 console.log("Response: "+root.textContent);
nickjillings@1528 1758 return root;
nickjillings@1528 1759 };
nickjillings@1528 1760 };
nickjillings@1528 1761
nickjillings@1528 1762 this.radioBox = function(commentQuestion) {
nickjillings@1528 1763 this.specification = commentQuestion;
nickjillings@1528 1764 // Create document objects to hold the comment boxes
nickjillings@1528 1765 this.holder = document.createElement('div');
nickjillings@1528 1766 this.holder.className = 'comment-div';
nickjillings@1528 1767 // Create a string next to each comment asking for a comment
nickjillings@1528 1768 this.string = document.createElement('span');
nickjillings@1528 1769 this.string.innerHTML = commentQuestion.statement;
nickjillings@1528 1770 var br = document.createElement('br');
nickjillings@1528 1771 // Add to the holder.
nickjillings@1528 1772 this.holder.appendChild(this.string);
nickjillings@1528 1773 this.holder.appendChild(br);
nickjillings@1528 1774 this.options = [];
nickjillings@1528 1775 this.inputs = document.createElement('div');
nickjillings@1528 1776 this.span = document.createElement('div');
nickjillings@1528 1777 this.inputs.align = 'center';
nickjillings@1528 1778 this.inputs.style.marginLeft = '12px';
nickjillings@1528 1779 this.span.style.marginLeft = '12px';
nickjillings@1528 1780 this.span.align = 'center';
nickjillings@1528 1781 this.span.style.marginTop = '15px';
nickjillings@1528 1782
nickjillings@1528 1783 var optCount = commentQuestion.options.length;
nickjillings@1528 1784 var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px';
nickjillings@1528 1785 console.log(spanMargin);
nickjillings@1528 1786 for (var i=0; i<optCount; i++)
nickjillings@1528 1787 {
nickjillings@1528 1788 var div = document.createElement('div');
nickjillings@1528 1789 div.style.width = '100px';
nickjillings@1528 1790 div.style.float = 'left';
nickjillings@1528 1791 div.style.marginRight = spanMargin;
nickjillings@1528 1792 div.style.marginLeft = spanMargin;
nickjillings@1528 1793 var input = document.createElement('input');
nickjillings@1528 1794 input.type = 'radio';
nickjillings@1528 1795 input.name = commentQuestion.id;
nickjillings@1528 1796 input.setAttribute('setvalue',commentQuestion.options[i].name);
nickjillings@1528 1797 input.className = 'comment-radio';
nickjillings@1528 1798 div.appendChild(input);
nickjillings@1528 1799 this.inputs.appendChild(div);
nickjillings@1528 1800
nickjillings@1528 1801
nickjillings@1528 1802 div = document.createElement('div');
nickjillings@1528 1803 div.style.width = '100px';
nickjillings@1528 1804 div.style.float = 'left';
nickjillings@1528 1805 div.style.marginRight = spanMargin;
nickjillings@1528 1806 div.style.marginLeft = spanMargin;
nickjillings@1528 1807 div.align = 'center';
nickjillings@1528 1808 var span = document.createElement('span');
nickjillings@1528 1809 span.textContent = commentQuestion.options[i].text;
nickjillings@1528 1810 span.className = 'comment-radio-span';
nickjillings@1528 1811 div.appendChild(span);
nickjillings@1528 1812 this.span.appendChild(div);
nickjillings@1528 1813 this.options.push(input);
nickjillings@1528 1814 }
nickjillings@1528 1815 this.holder.appendChild(this.span);
nickjillings@1528 1816 this.holder.appendChild(this.inputs);
nickjillings@1528 1817
nickjillings@1528 1818 this.exportXMLDOM = function() {
nickjillings@1528 1819 var root = document.createElement('comment');
nickjillings@1528 1820 root.id = this.specification.id;
nickjillings@1528 1821 root.setAttribute('type',this.specification.type);
nickjillings@1528 1822 var question = document.createElement('question');
nickjillings@1528 1823 question.textContent = this.string.textContent;
nickjillings@1528 1824 var response = document.createElement('response');
nickjillings@1528 1825 var i=0;
nickjillings@1528 1826 while(this.options[i].checked == false) {
nickjillings@1528 1827 i++;
nickjillings@1528 1828 if (i >= this.options.length) {
nickjillings@1528 1829 break;
nickjillings@1528 1830 }
nickjillings@1528 1831 }
nickjillings@1528 1832 if (i >= this.options.length) {
nickjillings@1528 1833 response.textContent = 'null';
nickjillings@1528 1834 } else {
nickjillings@1528 1835 response.textContent = this.options[i].getAttribute('setvalue');
nickjillings@1528 1836 response.setAttribute('number',i);
nickjillings@1528 1837 }
nickjillings@1528 1838 console.log('Comment: '+question.textContent);
nickjillings@1528 1839 console.log('Response: '+response.textContent);
nickjillings@1528 1840 root.appendChild(question);
nickjillings@1528 1841 root.appendChild(response);
nickjillings@1528 1842 return root;
nickjillings@1528 1843 };
nickjillings@1528 1844 };
nickjillings@1528 1845
nickjillings@1528 1846 this.checkboxBox = function(commentQuestion) {
nickjillings@1528 1847 this.specification = commentQuestion;
nickjillings@1528 1848 // Create document objects to hold the comment boxes
nickjillings@1528 1849 this.holder = document.createElement('div');
nickjillings@1528 1850 this.holder.className = 'comment-div';
nickjillings@1528 1851 // Create a string next to each comment asking for a comment
nickjillings@1528 1852 this.string = document.createElement('span');
nickjillings@1528 1853 this.string.innerHTML = commentQuestion.statement;
nickjillings@1528 1854 var br = document.createElement('br');
nickjillings@1528 1855 // Add to the holder.
nickjillings@1528 1856 this.holder.appendChild(this.string);
nickjillings@1528 1857 this.holder.appendChild(br);
nickjillings@1528 1858 this.options = [];
nickjillings@1528 1859 this.inputs = document.createElement('div');
nickjillings@1528 1860 this.span = document.createElement('div');
nickjillings@1528 1861 this.inputs.align = 'center';
nickjillings@1528 1862 this.inputs.style.marginLeft = '12px';
nickjillings@1528 1863 this.span.style.marginLeft = '12px';
nickjillings@1528 1864 this.span.align = 'center';
nickjillings@1528 1865 this.span.style.marginTop = '15px';
nickjillings@1528 1866
nickjillings@1528 1867 var optCount = commentQuestion.options.length;
nickjillings@1528 1868 var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px';
nickjillings@1528 1869 console.log(spanMargin);
nickjillings@1528 1870 for (var i=0; i<optCount; i++)
nickjillings@1528 1871 {
nickjillings@1528 1872 var div = document.createElement('div');
nickjillings@1528 1873 div.style.width = '100px';
nickjillings@1528 1874 div.style.float = 'left';
nickjillings@1528 1875 div.style.marginRight = spanMargin;
nickjillings@1528 1876 div.style.marginLeft = spanMargin;
nickjillings@1528 1877 var input = document.createElement('input');
nickjillings@1528 1878 input.type = 'checkbox';
nickjillings@1528 1879 input.name = commentQuestion.id;
nickjillings@1528 1880 input.setAttribute('setvalue',commentQuestion.options[i].name);
nickjillings@1528 1881 input.className = 'comment-radio';
nickjillings@1528 1882 div.appendChild(input);
nickjillings@1528 1883 this.inputs.appendChild(div);
nickjillings@1528 1884
nickjillings@1528 1885
nickjillings@1528 1886 div = document.createElement('div');
nickjillings@1528 1887 div.style.width = '100px';
nickjillings@1528 1888 div.style.float = 'left';
nickjillings@1528 1889 div.style.marginRight = spanMargin;
nickjillings@1528 1890 div.style.marginLeft = spanMargin;
nickjillings@1528 1891 div.align = 'center';
nickjillings@1528 1892 var span = document.createElement('span');
nickjillings@1528 1893 span.textContent = commentQuestion.options[i].text;
nickjillings@1528 1894 span.className = 'comment-radio-span';
nickjillings@1528 1895 div.appendChild(span);
nickjillings@1528 1896 this.span.appendChild(div);
nickjillings@1528 1897 this.options.push(input);
nickjillings@1528 1898 }
nickjillings@1528 1899 this.holder.appendChild(this.span);
nickjillings@1528 1900 this.holder.appendChild(this.inputs);
nickjillings@1528 1901
nickjillings@1528 1902 this.exportXMLDOM = function() {
nickjillings@1528 1903 var root = document.createElement('comment');
nickjillings@1528 1904 root.id = this.specification.id;
nickjillings@1528 1905 root.setAttribute('type',this.specification.type);
nickjillings@1528 1906 var question = document.createElement('question');
nickjillings@1528 1907 question.textContent = this.string.textContent;
nickjillings@1528 1908 root.appendChild(question);
nickjillings@1528 1909 console.log('Comment: '+question.textContent);
nickjillings@1528 1910 for (var i=0; i<this.options.length; i++) {
nickjillings@1528 1911 var response = document.createElement('response');
nickjillings@1528 1912 response.textContent = this.options[i].checked;
nickjillings@1528 1913 response.setAttribute('name',this.options[i].getAttribute('setvalue'));
nickjillings@1528 1914 root.appendChild(response);
nickjillings@1528 1915 console.log('Response '+response.getAttribute('name') +': '+response.textContent);
nickjillings@1528 1916 }
nickjillings@1528 1917 return root;
nickjillings@1528 1918 };
nickjillings@1528 1919 };
nickjillings@1528 1920
nickjillings@1528 1921 this.createCommentBox = function(audioObject) {
nickjillings@1528 1922 var node = new this.elementCommentBox(audioObject);
nickjillings@1528 1923 this.commentBoxes.push(node);
nickjillings@1528 1924 audioObject.commentDOM = node;
nickjillings@1528 1925 return node;
nickjillings@1528 1926 };
nickjillings@1528 1927
nickjillings@1528 1928 this.sortCommentBoxes = function() {
nickjillings@1528 1929 var holder = [];
nickjillings@1528 1930 while (this.commentBoxes.length > 0) {
nickjillings@1528 1931 var node = this.commentBoxes.pop(0);
nickjillings@1528 1932 holder[node.id] = node;
nickjillings@1528 1933 }
nickjillings@1528 1934 this.commentBoxes = holder;
nickjillings@1528 1935 };
nickjillings@1528 1936
nickjillings@1528 1937 this.showCommentBoxes = function(inject, sort) {
nickjillings@1528 1938 if (sort) {interfaceContext.sortCommentBoxes();}
nickjillings@1528 1939 for (var i=0; i<interfaceContext.commentBoxes.length; i++) {
nickjillings@1528 1940 inject.appendChild(this.commentBoxes[i].trackComment);
nickjillings@1528 1941 }
nickjillings@1528 1942 };
nickjillings@1528 1943
nickjillings@1528 1944 this.deleteCommentBoxes = function() {
nickjillings@1528 1945 this.commentBoxes = [];
nickjillings@1528 1946 };
nickjillings@1528 1947
nickjillings@1528 1948 this.createCommentQuestion = function(element) {
nickjillings@1528 1949 var node;
nickjillings@1528 1950 if (element.type == 'text') {
nickjillings@1528 1951 node = new this.commentBox(element);
nickjillings@1528 1952 } else if (element.type == 'radio') {
nickjillings@1528 1953 node = new this.radioBox(element);
nickjillings@1528 1954 } else if (element.type == 'checkbox') {
nickjillings@1528 1955 node = new this.checkboxBox(element);
nickjillings@1528 1956 }
nickjillings@1528 1957 this.commentQuestions.push(node);
nickjillings@1528 1958 return node;
nickjillings@1528 1959 };
nickjillings@1528 1960
nickjillings@1528 1961 this.deleteCommentQuestions = function()
nickjillings@1528 1962 {
nickjillings@1528 1963 this.commentQuestions = [];
nickjillings@1528 1964 };
nickjillings@1528 1965
nickjillings@1528 1966 this.playhead = new function()
nickjillings@1528 1967 {
nickjillings@1528 1968 this.object = document.createElement('div');
nickjillings@1528 1969 this.object.className = 'playhead';
nickjillings@1528 1970 this.object.align = 'left';
nickjillings@1528 1971 var curTime = document.createElement('div');
nickjillings@1528 1972 curTime.style.width = '50px';
nickjillings@1528 1973 this.curTimeSpan = document.createElement('span');
nickjillings@1528 1974 this.curTimeSpan.textContent = '00:00';
nickjillings@1528 1975 curTime.appendChild(this.curTimeSpan);
nickjillings@1528 1976 this.object.appendChild(curTime);
nickjillings@1528 1977 this.scrubberTrack = document.createElement('div');
nickjillings@1528 1978 this.scrubberTrack.className = 'playhead-scrub-track';
nickjillings@1528 1979
nickjillings@1528 1980 this.scrubberHead = document.createElement('div');
nickjillings@1528 1981 this.scrubberHead.id = 'playhead-scrubber';
nickjillings@1528 1982 this.scrubberTrack.appendChild(this.scrubberHead);
nickjillings@1528 1983 this.object.appendChild(this.scrubberTrack);
nickjillings@1528 1984
nickjillings@1528 1985 this.timePerPixel = 0;
nickjillings@1528 1986 this.maxTime = 0;
nickjillings@1528 1987
nickjillings@1528 1988 this.playbackObject;
nickjillings@1528 1989
nickjillings@1528 1990 this.setTimePerPixel = function(audioObject) {
nickjillings@1528 1991 //maxTime must be in seconds
nickjillings@1528 1992 this.playbackObject = audioObject;
nickjillings@1528 1993 this.maxTime = audioObject.buffer.duration;
nickjillings@1528 1994 var width = 490; //500 - 10, 5 each side of the tracker head
nickjillings@1528 1995 this.timePerPixel = this.maxTime/490;
nickjillings@1528 1996 if (this.maxTime < 60) {
nickjillings@1528 1997 this.curTimeSpan.textContent = '0.00';
nickjillings@1528 1998 } else {
nickjillings@1528 1999 this.curTimeSpan.textContent = '00:00';
nickjillings@1528 2000 }
nickjillings@1528 2001 };
nickjillings@1528 2002
nickjillings@1528 2003 this.update = function() {
nickjillings@1528 2004 // Update the playhead position, startPlay must be called
nickjillings@1528 2005 if (this.timePerPixel > 0) {
nickjillings@1528 2006 var time = this.playbackObject.getCurrentPosition();
nickjillings@1530 2007 if (time > 0) {
nickjillings@1530 2008 var width = 490;
nickjillings@1530 2009 var pix = Math.floor(time/this.timePerPixel);
nickjillings@1530 2010 this.scrubberHead.style.left = pix+'px';
nickjillings@1530 2011 if (this.maxTime > 60.0) {
nickjillings@1530 2012 var secs = time%60;
nickjillings@1530 2013 var mins = Math.floor((time-secs)/60);
nickjillings@1530 2014 secs = secs.toString();
nickjillings@1530 2015 secs = secs.substr(0,2);
nickjillings@1530 2016 mins = mins.toString();
nickjillings@1530 2017 this.curTimeSpan.textContent = mins+':'+secs;
nickjillings@1530 2018 } else {
nickjillings@1530 2019 time = time.toString();
nickjillings@1530 2020 this.curTimeSpan.textContent = time.substr(0,4);
nickjillings@1530 2021 }
nickjillings@1528 2022 } else {
nickjillings@1530 2023 this.scrubberHead.style.left = '0px';
nickjillings@1530 2024 if (this.maxTime < 60) {
nickjillings@1530 2025 this.curTimeSpan.textContent = '0.00';
nickjillings@1530 2026 } else {
nickjillings@1530 2027 this.curTimeSpan.textContent = '00:00';
nickjillings@1530 2028 }
nickjillings@1528 2029 }
nickjillings@1528 2030 }
nickjillings@1528 2031 };
nickjillings@1528 2032
nickjillings@1528 2033 this.interval = undefined;
nickjillings@1528 2034
nickjillings@1528 2035 this.start = function() {
nickjillings@1528 2036 if (this.playbackObject != undefined && this.interval == undefined) {
nickjillings@1530 2037 if (this.maxTime < 60) {
nickjillings@1530 2038 this.interval = setInterval(function(){interfaceContext.playhead.update();},10);
nickjillings@1530 2039 } else {
nickjillings@1530 2040 this.interval = setInterval(function(){interfaceContext.playhead.update();},100);
nickjillings@1530 2041 }
nickjillings@1528 2042 }
nickjillings@1528 2043 };
nickjillings@1528 2044 this.stop = function() {
nickjillings@1528 2045 clearInterval(this.interval);
nickjillings@1528 2046 this.interval = undefined;
nickjillings@1530 2047 if (this.maxTime < 60) {
nickjillings@1530 2048 this.curTimeSpan.textContent = '0.00';
nickjillings@1530 2049 } else {
nickjillings@1530 2050 this.curTimeSpan.textContent = '00:00';
nickjillings@1530 2051 }
nickjillings@1528 2052 };
nickjillings@1528 2053 };
nickjillings@1528 2054
nickjillings@1528 2055 // Global Checkers
nickjillings@1528 2056 // These functions will help enforce the checkers
nickjillings@1528 2057 this.checkHiddenAnchor = function()
nickjillings@1528 2058 {
nickjillings@1528 2059 var audioHolder = testState.currentStateMap[testState.currentIndex];
nickjillings@1528 2060 if (audioHolder.anchorId != null)
nickjillings@1528 2061 {
nickjillings@1528 2062 var audioObject = audioEngineContext.audioObjects[audioHolder.anchorId];
nickjillings@1532 2063 if (audioObject.interfaceDOM.getValue() > audioObject.specification.marker && audioObject.interfaceDOM.enforce == true)
nickjillings@1528 2064 {
nickjillings@1528 2065 // Anchor is not set below
nickjillings@1528 2066 console.log('Anchor node not below marker value');
nickjillings@1528 2067 alert('Please keep listening');
nickjillings@1528 2068 return false;
nickjillings@1528 2069 }
nickjillings@1528 2070 }
nickjillings@1528 2071 return true;
nickjillings@1528 2072 };
nickjillings@1528 2073
nickjillings@1528 2074 this.checkHiddenReference = function()
nickjillings@1528 2075 {
nickjillings@1528 2076 var audioHolder = testState.currentStateMap[testState.currentIndex];
nickjillings@1528 2077 if (audioHolder.referenceId != null)
nickjillings@1528 2078 {
nickjillings@1528 2079 var audioObject = audioEngineContext.audioObjects[audioHolder.referenceId];
nickjillings@1532 2080 if (audioObject.interfaceDOM.getValue() < audioObject.specification.marker && audioObject.interfaceDOM.enforce == true)
nickjillings@1528 2081 {
nickjillings@1528 2082 // Anchor is not set below
nickjillings@1528 2083 console.log('Reference node not above marker value');
nickjillings@1528 2084 alert('Please keep listening');
nickjillings@1528 2085 return false;
nickjillings@1528 2086 }
nickjillings@1528 2087 }
nickjillings@1528 2088 return true;
nickjillings@1528 2089 };
nickjillings@1528 2090 }