annotate core.js @ 1560:950f14a6506e

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