annotate core.js @ 1531:3d63ae9a389a

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