annotate core.js @ 1583:5e20f0db13b0

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