annotate core.js @ 1565:15b981237bc9

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