annotate core.js @ 2061:0fb464ac7313

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