annotate core.js @ 2030:979474558a2a

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