annotate core.js @ 1604:d5d7dfdbf335

Hacky test wait indicator!!
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Thu, 28 May 2015 18:58:45 +0100
parents
children dea30ed2b549
rev   line source
nickjillings@1604 1 /**
nickjillings@1604 2 * core.js
nickjillings@1604 3 *
nickjillings@1604 4 * Main script to run, calls all other core functions and manages loading/store to backend.
nickjillings@1604 5 * Also contains all global variables.
nickjillings@1604 6 */
nickjillings@1604 7
nickjillings@1604 8 /* create the web audio API context and store in audioContext*/
nickjillings@1604 9 var audioContext; // Hold the browser web audio API
nickjillings@1604 10 var projectXML; // Hold the parsed setup XML
nickjillings@1604 11 var popup; // Hold the interfacePopup object
nickjillings@1604 12 var testState;
nickjillings@1604 13 var currentState; // Keep track of the current state (pre/post test, which test, final test? first test?)
nickjillings@1604 14 //var testXMLSetups = []; // Hold the parsed test instances
nickjillings@1604 15 //var testResultsHolders =[]; // Hold the results from each test for publishing to XML
nickjillings@1604 16 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
nickjillings@1604 17 //var currentTestHolder; // Hold any intermediate results during test - metrics
nickjillings@1604 18 var audioEngineContext; // The custome AudioEngine object
nickjillings@1604 19 var projectReturn; // Hold the URL for the return
nickjillings@1604 20 //var preTestQuestions = document.createElement('PreTest'); // Store any pre-test question response
nickjillings@1604 21 //var postTestQuestions = document.createElement('PostTest'); // Store any post-test question response
nickjillings@1604 22
nickjillings@1604 23 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
nickjillings@1604 24 AudioBufferSourceNode.prototype.owner = undefined;
nickjillings@1604 25
nickjillings@1604 26 window.onload = function() {
nickjillings@1604 27 // Function called once the browser has loaded all files.
nickjillings@1604 28 // This should perform any initial commands such as structure / loading documents
nickjillings@1604 29
nickjillings@1604 30 // Create a web audio API context
nickjillings@1604 31 // Fixed for cross-browser support
nickjillings@1604 32 var AudioContext = window.AudioContext || window.webkitAudioContext;
nickjillings@1604 33 audioContext = new AudioContext;
nickjillings@1604 34
nickjillings@1604 35 // Create test state
nickjillings@1604 36 testState = new stateMachine();
nickjillings@1604 37
nickjillings@1604 38 // Create the audio engine object
nickjillings@1604 39 audioEngineContext = new AudioEngine();
nickjillings@1604 40
nickjillings@1604 41 // Create the popup interface object
nickjillings@1604 42 popup = new interfacePopup();
nickjillings@1604 43 };
nickjillings@1604 44
nickjillings@1604 45 function interfacePopup() {
nickjillings@1604 46 // Creates an object to manage the popup
nickjillings@1604 47 this.popup = null;
nickjillings@1604 48 this.popupContent = null;
nickjillings@1604 49 this.popupButton = null;
nickjillings@1604 50 this.popupOptions = null;
nickjillings@1604 51 this.currentIndex = null;
nickjillings@1604 52 this.responses = null;
nickjillings@1604 53 this.createPopup = function(){
nickjillings@1604 54 // Create popup window interface
nickjillings@1604 55 var insertPoint = document.getElementById("topLevelBody");
nickjillings@1604 56 var blank = document.createElement('div');
nickjillings@1604 57 blank.className = 'testHalt';
nickjillings@1604 58
nickjillings@1604 59 this.popup = document.createElement('div');
nickjillings@1604 60 this.popup.id = 'popupHolder';
nickjillings@1604 61 this.popup.className = 'popupHolder';
nickjillings@1604 62 this.popup.style.position = 'absolute';
nickjillings@1604 63 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
nickjillings@1604 64 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
nickjillings@1604 65
nickjillings@1604 66 this.popupContent = document.createElement('div');
nickjillings@1604 67 this.popupContent.id = 'popupContent';
nickjillings@1604 68 this.popupContent.style.marginTop = '25px';
nickjillings@1604 69 this.popupContent.align = 'center';
nickjillings@1604 70 this.popup.appendChild(this.popupContent);
nickjillings@1604 71
nickjillings@1604 72 this.popupButton = document.createElement('button');
nickjillings@1604 73 this.popupButton.className = 'popupButton';
nickjillings@1604 74 this.popupButton.innerHTML = 'Next';
nickjillings@1604 75 this.popupButton.onclick = function(){popup.buttonClicked();};
nickjillings@1604 76 insertPoint.appendChild(this.popup);
nickjillings@1604 77 insertPoint.appendChild(blank);
nickjillings@1604 78 };
nickjillings@1604 79
nickjillings@1604 80 this.showPopup = function(){
nickjillings@1604 81 if (this.popup == null || this.popup == undefined) {
nickjillings@1604 82 this.createPopup();
nickjillings@1604 83 }
nickjillings@1604 84 this.popup.style.zIndex = 3;
nickjillings@1604 85 this.popup.style.visibility = 'visible';
nickjillings@1604 86 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1604 87 blank.style.zIndex = 2;
nickjillings@1604 88 blank.style.visibility = 'visible';
nickjillings@1604 89 };
nickjillings@1604 90
nickjillings@1604 91 this.hidePopup = function(){
nickjillings@1604 92 this.popup.style.zIndex = -1;
nickjillings@1604 93 this.popup.style.visibility = 'hidden';
nickjillings@1604 94 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1604 95 blank.style.zIndex = -2;
nickjillings@1604 96 blank.style.visibility = 'hidden';
nickjillings@1604 97 };
nickjillings@1604 98
nickjillings@1604 99 this.postNode = function() {
nickjillings@1604 100 // This will take the node from the popupOptions and display it
nickjillings@1604 101 var node = this.popupOptions[this.currentIndex];
nickjillings@1604 102 this.popupContent.innerHTML = null;
nickjillings@1604 103 if (node.nodeName == 'statement') {
nickjillings@1604 104 var span = document.createElement('span');
nickjillings@1604 105 span.textContent = node.textContent;
nickjillings@1604 106 this.popupContent.appendChild(span);
nickjillings@1604 107 } else if (node.nodeName == 'question') {
nickjillings@1604 108 var span = document.createElement('span');
nickjillings@1604 109 span.textContent = node.textContent;
nickjillings@1604 110 var textArea = document.createElement('textarea');
nickjillings@1604 111 var br = document.createElement('br');
nickjillings@1604 112 this.popupContent.appendChild(span);
nickjillings@1604 113 this.popupContent.appendChild(br);
nickjillings@1604 114 this.popupContent.appendChild(textArea);
nickjillings@1604 115 }
nickjillings@1604 116 this.popupContent.appendChild(this.popupButton);
nickjillings@1604 117 }
nickjillings@1604 118
nickjillings@1604 119 this.initState = function(node) {
nickjillings@1604 120 //Call this with your preTest and postTest nodes when needed to
nickjillings@1604 121 // initialise the popup procedure.
nickjillings@1604 122 this.popupOptions = $(node).children();
nickjillings@1604 123 if (this.popupOptions.length > 0) {
nickjillings@1604 124 if (node.nodeName == 'preTest' || node.nodeName == 'PreTest') {
nickjillings@1604 125 this.responses = document.createElement('PreTest');
nickjillings@1604 126 } else if (node.nodeName == 'postTest' || node.nodeName == 'PostTest') {
nickjillings@1604 127 this.responses = document.createElement('PostTest');
nickjillings@1604 128 } else {
nickjillings@1604 129 console.log ('WARNING - popup node neither pre or post!');
nickjillings@1604 130 this.responses = document.createElement('responses');
nickjillings@1604 131 }
nickjillings@1604 132 this.currentIndex = 0;
nickjillings@1604 133 this.showPopup();
nickjillings@1604 134 this.postNode();
nickjillings@1604 135 }
nickjillings@1604 136 }
nickjillings@1604 137
nickjillings@1604 138 this.buttonClicked = function() {
nickjillings@1604 139 // Each time the popup button is clicked!
nickjillings@1604 140 var node = this.popupOptions[this.currentIndex];
nickjillings@1604 141 if (node.nodeName == 'question') {
nickjillings@1604 142 // Must extract the question data
nickjillings@1604 143 var mandatory = node.attributes['mandatory'];
nickjillings@1604 144 if (mandatory == undefined) {
nickjillings@1604 145 mandatory = false;
nickjillings@1604 146 } else {
nickjillings@1604 147 if (mandatory.value == 'true'){mandatory = true;}
nickjillings@1604 148 else {mandatory = false;}
nickjillings@1604 149 }
nickjillings@1604 150 var textArea = $(popup.popupContent).find('textarea')[0];
nickjillings@1604 151 if (mandatory == true && textArea.value.length == 0) {
nickjillings@1604 152 alert('This question is mandatory');
nickjillings@1604 153 return;
nickjillings@1604 154 } else {
nickjillings@1604 155 // Save the text content
nickjillings@1604 156 var hold = document.createElement('comment');
nickjillings@1604 157 hold.id = node.attributes['id'].value;
nickjillings@1604 158 hold.innerHTML = textArea.value;
nickjillings@1604 159 console.log("Question: "+ node.textContent);
nickjillings@1604 160 console.log("Question Response: "+ textArea.value);
nickjillings@1604 161 this.responses.appendChild(hold);
nickjillings@1604 162 }
nickjillings@1604 163 }
nickjillings@1604 164 this.currentIndex++;
nickjillings@1604 165 if (this.currentIndex < this.popupOptions.length) {
nickjillings@1604 166 this.postNode();
nickjillings@1604 167 } else {
nickjillings@1604 168 // Reached the end of the popupOptions
nickjillings@1604 169 this.hidePopup();
nickjillings@1604 170 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) {
nickjillings@1604 171 testState.stateResults[testState.stateIndex] = this.responses;
nickjillings@1604 172 } else {
nickjillings@1604 173 testState.stateResults[testState.stateIndex].appendChild(this.responses);
nickjillings@1604 174 }
nickjillings@1604 175 advanceState();
nickjillings@1604 176 }
nickjillings@1604 177 }
nickjillings@1604 178 }
nickjillings@1604 179
nickjillings@1604 180 function advanceState()
nickjillings@1604 181 {
nickjillings@1604 182 // Just for complete clarity
nickjillings@1604 183 testState.advanceState();
nickjillings@1604 184 }
nickjillings@1604 185
nickjillings@1604 186 function stateMachine()
nickjillings@1604 187 {
nickjillings@1604 188 // Object prototype for tracking and managing the test state
nickjillings@1604 189 this.stateMap = [];
nickjillings@1604 190 this.stateIndex = null;
nickjillings@1604 191 this.currentStateMap = [];
nickjillings@1604 192 this.currentIndex = null;
nickjillings@1604 193 this.currentTestId = 0;
nickjillings@1604 194 this.stateResults = [];
nickjillings@1604 195 this.timerCallBackHolders = null;
nickjillings@1604 196 this.initialise = function(){
nickjillings@1604 197 if (this.stateMap.length > 0) {
nickjillings@1604 198 if(this.stateIndex != null) {
nickjillings@1604 199 console.log('NOTE - State already initialise');
nickjillings@1604 200 }
nickjillings@1604 201 this.stateIndex = -1;
nickjillings@1604 202 var that = this;
nickjillings@1604 203 for (var id=0; id<this.stateMap.length; id++){
nickjillings@1604 204 var name = this.stateMap[id].nodeName;
nickjillings@1604 205 var obj = document.createElement(name);
nickjillings@1604 206 this.stateResults.push(obj);
nickjillings@1604 207 }
nickjillings@1604 208 } else {
nickjillings@1604 209 conolse.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
nickjillings@1604 210 }
nickjillings@1604 211 };
nickjillings@1604 212 this.advanceState = function(){
nickjillings@1604 213 if (this.stateIndex == null) {
nickjillings@1604 214 this.initialise();
nickjillings@1604 215 }
nickjillings@1604 216 if (this.stateIndex == -1) {
nickjillings@1604 217 console.log('Starting test...');
nickjillings@1604 218 }
nickjillings@1604 219 if (this.currentIndex == null){
nickjillings@1604 220 if (this.currentStateMap.nodeName == "audioHolder") {
nickjillings@1604 221 // Save current page
nickjillings@1604 222 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId);
nickjillings@1604 223 this.currentTestId++;
nickjillings@1604 224 }
nickjillings@1604 225 this.stateIndex++;
nickjillings@1604 226 if (this.stateIndex >= this.stateMap.length) {
nickjillings@1604 227 console.log('Test Completed');
nickjillings@1604 228 createProjectSave(projectReturn);
nickjillings@1604 229 } else {
nickjillings@1604 230 this.currentStateMap = this.stateMap[this.stateIndex];
nickjillings@1604 231 if (this.currentStateMap.nodeName == "audioHolder") {
nickjillings@1604 232 console.log('Loading test page');
nickjillings@1604 233 loadTest(this.currentStateMap);
nickjillings@1604 234 this.initialiseInnerState(this.currentStateMap);
nickjillings@1604 235 } else if (this.currentStateMap.nodeName == "PreTest" || this.currentStateMap.nodeName == "PostTest") {
nickjillings@1604 236 if (this.currentStateMap.childElementCount >= 1) {
nickjillings@1604 237 popup.initState(this.currentStateMap);
nickjillings@1604 238 } else {
nickjillings@1604 239 this.advanceState();
nickjillings@1604 240 }
nickjillings@1604 241 } else {
nickjillings@1604 242 this.advanceState();
nickjillings@1604 243 }
nickjillings@1604 244 }
nickjillings@1604 245 } else {
nickjillings@1604 246 this.advanceInnerState();
nickjillings@1604 247 }
nickjillings@1604 248 };
nickjillings@1604 249
nickjillings@1604 250 this.testPageCompleted = function(store, testXML, testId) {
nickjillings@1604 251 // Function called each time a test page has been completed
nickjillings@1604 252 // Can be used to over-rule default behaviour
nickjillings@1604 253
nickjillings@1604 254 pageXMLSave(store, testXML, testId);
nickjillings@1604 255 }
nickjillings@1604 256
nickjillings@1604 257 this.initialiseInnerState = function(testXML) {
nickjillings@1604 258 // Parses the received testXML for pre and post test options
nickjillings@1604 259 this.currentStateMap = [];
nickjillings@1604 260 var preTest = $(testXML).find('PreTest')[0];
nickjillings@1604 261 var postTest = $(testXML).find('PostTest')[0];
nickjillings@1604 262 if (preTest == undefined) {preTest = document.createElement("preTest");}
nickjillings@1604 263 if (postTest == undefined){postTest= document.createElement("postTest");}
nickjillings@1604 264 this.currentStateMap.push(preTest);
nickjillings@1604 265 this.currentStateMap.push(testXML);
nickjillings@1604 266 this.currentStateMap.push(postTest);
nickjillings@1604 267 this.currentIndex = -1;
nickjillings@1604 268 this.advanceInnerState();
nickjillings@1604 269 }
nickjillings@1604 270
nickjillings@1604 271 this.advanceInnerState = function() {
nickjillings@1604 272 this.currentIndex++;
nickjillings@1604 273 if (this.currentIndex >= this.currentStateMap.length) {
nickjillings@1604 274 this.currentIndex = null;
nickjillings@1604 275 this.currentStateMap = this.stateMap[this.stateIndex];
nickjillings@1604 276 this.advanceState();
nickjillings@1604 277 } else {
nickjillings@1604 278 if (this.currentStateMap[this.currentIndex].nodeName == "audioHolder") {
nickjillings@1604 279 console.log("Loading test page"+this.currentTestId);
nickjillings@1604 280 } else if (this.currentStateMap[this.currentIndex].nodeName == "PreTest") {
nickjillings@1604 281 popup.initState(this.currentStateMap[this.currentIndex]);
nickjillings@1604 282 } else if (this.currentStateMap[this.currentIndex].nodeName == "PostTest") {
nickjillings@1604 283 popup.initState(this.currentStateMap[this.currentIndex]);
nickjillings@1604 284 } else {
nickjillings@1604 285 this.advanceInnerState();
nickjillings@1604 286 }
nickjillings@1604 287 }
nickjillings@1604 288 }
nickjillings@1604 289
nickjillings@1604 290 this.previousState = function(){};
nickjillings@1604 291 }
nickjillings@1604 292
nickjillings@1604 293 function testEnded(testId)
nickjillings@1604 294 {
nickjillings@1604 295 pageXMLSave(testId);
nickjillings@1604 296 if (testXMLSetups.length-1 > testId)
nickjillings@1604 297 {
nickjillings@1604 298 // Yes we have another test to perform
nickjillings@1604 299 testId = (Number(testId)+1);
nickjillings@1604 300 currentState = 'testRun-'+testId;
nickjillings@1604 301 loadTest(testId);
nickjillings@1604 302 } else {
nickjillings@1604 303 console.log('Testing Completed!');
nickjillings@1604 304 currentState = 'postTest';
nickjillings@1604 305 // Check for any post tests
nickjillings@1604 306 var xmlSetup = projectXML.find('setup');
nickjillings@1604 307 var postTest = xmlSetup.find('PostTest')[0];
nickjillings@1604 308 popup.initState(postTest);
nickjillings@1604 309 }
nickjillings@1604 310 }
nickjillings@1604 311
nickjillings@1604 312 function loadProjectSpec(url) {
nickjillings@1604 313 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
nickjillings@1604 314 // If url is null, request client to upload project XML document
nickjillings@1604 315 var r = new XMLHttpRequest();
nickjillings@1604 316 r.open('GET',url,true);
nickjillings@1604 317 r.onload = function() {
nickjillings@1604 318 loadProjectSpecCallback(r.response);
nickjillings@1604 319 };
nickjillings@1604 320 r.send();
nickjillings@1604 321 };
nickjillings@1604 322
nickjillings@1604 323 function loadProjectSpecCallback(response) {
nickjillings@1604 324 // Function called after asynchronous download of XML project specification
nickjillings@1604 325 var decode = $.parseXML(response);
nickjillings@1604 326 projectXML = $(decode);
nickjillings@1604 327
nickjillings@1604 328 // Now extract the setup tag
nickjillings@1604 329 var xmlSetup = projectXML.find('setup');
nickjillings@1604 330 // Detect the interface to use and load the relevant javascripts.
nickjillings@1604 331 var interfaceType = xmlSetup[0].attributes['interface'];
nickjillings@1604 332 var interfaceJS = document.createElement('script');
nickjillings@1604 333 interfaceJS.setAttribute("type","text/javascript");
nickjillings@1604 334 if (interfaceType.value == 'APE') {
nickjillings@1604 335 interfaceJS.setAttribute("src","ape.js");
nickjillings@1604 336
nickjillings@1604 337 // APE comes with a css file
nickjillings@1604 338 var css = document.createElement('link');
nickjillings@1604 339 css.rel = 'stylesheet';
nickjillings@1604 340 css.type = 'text/css';
nickjillings@1604 341 css.href = 'ape.css';
nickjillings@1604 342
nickjillings@1604 343 document.getElementsByTagName("head")[0].appendChild(css);
nickjillings@1604 344 }
nickjillings@1604 345 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
nickjillings@1604 346
nickjillings@1604 347 // Define window callbacks for interface
nickjillings@1604 348 window.onresize = function(event){resizeWindow(event);};
nickjillings@1604 349 }
nickjillings@1604 350
nickjillings@1604 351 function createProjectSave(destURL) {
nickjillings@1604 352 // Save the data from interface into XML and send to destURL
nickjillings@1604 353 // If destURL is null then download XML in client
nickjillings@1604 354 // Now time to render file locally
nickjillings@1604 355 var xmlDoc = interfaceXMLSave();
nickjillings@1604 356 var parent = document.createElement("div");
nickjillings@1604 357 parent.appendChild(xmlDoc);
nickjillings@1604 358 var file = [parent.innerHTML];
nickjillings@1604 359 if (destURL == "null" || destURL == undefined) {
nickjillings@1604 360 var bb = new Blob(file,{type : 'application/xml'});
nickjillings@1604 361 var dnlk = window.URL.createObjectURL(bb);
nickjillings@1604 362 var a = document.createElement("a");
nickjillings@1604 363 a.hidden = '';
nickjillings@1604 364 a.href = dnlk;
nickjillings@1604 365 a.download = "save.xml";
nickjillings@1604 366 a.textContent = "Save File";
nickjillings@1604 367
nickjillings@1604 368 var submitDiv = document.getElementById('download-point');
nickjillings@1604 369 submitDiv.appendChild(a);
nickjillings@1604 370 popup.showPopup();
nickjillings@1604 371 popup.popupContent.innerHTML = null;
nickjillings@1604 372 popup.popupContent.appendChild(submitDiv)
nickjillings@1604 373 } else {
nickjillings@1604 374 var xmlhttp = new XMLHttpRequest;
nickjillings@1604 375 xmlhttp.open("POST",destURL,true);
nickjillings@1604 376 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
nickjillings@1604 377 xmlhttp.onerror = function(){
nickjillings@1604 378 console.log('Error saving file to server! Presenting download locally');
nickjillings@1604 379 createProjectSave(null);
nickjillings@1604 380 };
nickjillings@1604 381 xmlhttp.send(file);
nickjillings@1604 382 }
nickjillings@1604 383 return submitDiv;
nickjillings@1604 384 }
nickjillings@1604 385
nickjillings@1604 386 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nickjillings@1604 387 function interfaceXMLSave(){
nickjillings@1604 388 // Create the XML string to be exported with results
nickjillings@1604 389 var xmlDoc = document.createElement("BrowserEvaluationResult");
nickjillings@1604 390 xmlDoc.appendChild(returnDateNode());
nickjillings@1604 391 for (var i=0; i<testState.stateResults.length; i++)
nickjillings@1604 392 {
nickjillings@1604 393 xmlDoc.appendChild(testState.stateResults[i]);
nickjillings@1604 394 }
nickjillings@1604 395
nickjillings@1604 396 return xmlDoc;
nickjillings@1604 397 }
nickjillings@1604 398
nickjillings@1604 399 function AudioEngine() {
nickjillings@1604 400
nickjillings@1604 401 // Create two output paths, the main outputGain and fooGain.
nickjillings@1604 402 // Output gain is default to 1 and any items for playback route here
nickjillings@1604 403 // Foo gain is used for analysis to ensure paths get processed, but are not heard
nickjillings@1604 404 // because web audio will optimise and any route which does not go to the destination gets ignored.
nickjillings@1604 405 this.outputGain = audioContext.createGain();
nickjillings@1604 406 this.fooGain = audioContext.createGain();
nickjillings@1604 407 this.fooGain.gain = 0;
nickjillings@1604 408
nickjillings@1604 409 // Use this to detect playback state: 0 - stopped, 1 - playing
nickjillings@1604 410 this.status = 0;
nickjillings@1604 411 this.audioObjectsReady = false;
nickjillings@1604 412
nickjillings@1604 413 // Connect both gains to output
nickjillings@1604 414 this.outputGain.connect(audioContext.destination);
nickjillings@1604 415 this.fooGain.connect(audioContext.destination);
nickjillings@1604 416
nickjillings@1604 417 // Create the timer Object
nickjillings@1604 418 this.timer = new timer();
nickjillings@1604 419 // Create session metrics
nickjillings@1604 420 this.metric = new sessionMetrics(this);
nickjillings@1604 421
nickjillings@1604 422 this.loopPlayback = false;
nickjillings@1604 423
nickjillings@1604 424 // Create store for new audioObjects
nickjillings@1604 425 this.audioObjects = [];
nickjillings@1604 426
nickjillings@1604 427 this.play = function() {
nickjillings@1604 428 // Start the timer and set the audioEngine state to playing (1)
nickjillings@1604 429 if (this.status == 0) {
nickjillings@1604 430 // Check if all audioObjects are ready
nickjillings@1604 431 if (this.audioObjectsReady == false) {
nickjillings@1604 432 this.audioObjectsReady = this.checkAllReady();
nickjillings@1604 433 }
nickjillings@1604 434 if (this.audioObjectsReady == true) {
nickjillings@1604 435 this.timer.startTest();
nickjillings@1604 436 if (this.loopPlayback) {
nickjillings@1604 437 for(var i=0; i<this.audioObjects.length; i++) {
nickjillings@1604 438 this.audioObjects[i].play(this.timer.getTestTime()+1);
nickjillings@1604 439 }
nickjillings@1604 440 }
nickjillings@1604 441 this.status = 1;
nickjillings@1604 442 }
nickjillings@1604 443 }
nickjillings@1604 444 };
nickjillings@1604 445
nickjillings@1604 446 this.stop = function() {
nickjillings@1604 447 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
nickjillings@1604 448 if (this.status == 1) {
nickjillings@1604 449 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1604 450 {
nickjillings@1604 451 this.audioObjects[i].stop();
nickjillings@1604 452 }
nickjillings@1604 453 this.status = 0;
nickjillings@1604 454 }
nickjillings@1604 455 };
nickjillings@1604 456
nickjillings@1604 457
nickjillings@1604 458 this.newTrack = function(url) {
nickjillings@1604 459 // Pull data from given URL into new audio buffer
nickjillings@1604 460 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
nickjillings@1604 461
nickjillings@1604 462 // Create the audioObject with ID of the new track length;
nickjillings@1604 463 audioObjectId = this.audioObjects.length;
nickjillings@1604 464 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
nickjillings@1604 465
nickjillings@1604 466 // AudioObject will get track itself.
nickjillings@1604 467 this.audioObjects[audioObjectId].constructTrack(url);
nickjillings@1604 468 };
nickjillings@1604 469
nickjillings@1604 470 this.newTestPage = function() {
nickjillings@1604 471 this.state = 0;
nickjillings@1604 472 this.audioObjectsReady = false;
nickjillings@1604 473 this.metric.reset();
nickjillings@1604 474 this.audioObjects = [];
nickjillings@1604 475 };
nickjillings@1604 476
nickjillings@1604 477 this.checkAllPlayed = function() {
nickjillings@1604 478 arr = [];
nickjillings@1604 479 for (var id=0; id<this.audioObjects.length; id++) {
nickjillings@1604 480 if (this.audioObjects[id].played == false) {
nickjillings@1604 481 arr.push(this.audioObjects[id].id);
nickjillings@1604 482 }
nickjillings@1604 483 }
nickjillings@1604 484 return arr;
nickjillings@1604 485 };
nickjillings@1604 486
nickjillings@1604 487 this.checkAllReady = function() {
nickjillings@1604 488 var ready = true;
nickjillings@1604 489 for (var i=0; i<this.audioObjects.length; i++) {
nickjillings@1604 490 if (this.audioObjects[i].state == 0) {
nickjillings@1604 491 // Track not ready
nickjillings@1604 492 console.log('WAIT -- audioObject '+i+' not ready yet!');
nickjillings@1604 493 ready = false;
nickjillings@1604 494 };
nickjillings@1604 495 }
nickjillings@1604 496 if (ready == false) {
nickjillings@1604 497 var holder = document.getElementById('testWaitIndicator');
nickjillings@1604 498 holder.style.visibility = "visible";
nickjillings@1604 499 setInterval(function() {
nickjillings@1604 500 document.getElementById('testWaitIndicator').style.visibility = "hidden";
nickjillings@1604 501 }, 10000);
nickjillings@1604 502 }
nickjillings@1604 503 return ready;
nickjillings@1604 504 };
nickjillings@1604 505
nickjillings@1604 506 }
nickjillings@1604 507
nickjillings@1604 508 function audioObject(id) {
nickjillings@1604 509 // The main buffer object with common control nodes to the AudioEngine
nickjillings@1604 510
nickjillings@1604 511 this.id = id;
nickjillings@1604 512 this.state = 0; // 0 - no data, 1 - ready
nickjillings@1604 513 this.url = null; // Hold the URL given for the output back to the results.
nickjillings@1604 514 this.metric = new metricTracker();
nickjillings@1604 515
nickjillings@1604 516 this.played = false;
nickjillings@1604 517
nickjillings@1604 518 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
nickjillings@1604 519 this.bufferNode = undefined;
nickjillings@1604 520 this.outputGain = audioContext.createGain();
nickjillings@1604 521
nickjillings@1604 522 // Default output gain to be zero
nickjillings@1604 523 this.outputGain.gain.value = 0.0;
nickjillings@1604 524
nickjillings@1604 525 // Connect buffer to the audio graph
nickjillings@1604 526 this.outputGain.connect(audioEngineContext.outputGain);
nickjillings@1604 527
nickjillings@1604 528 // the audiobuffer is not designed for multi-start playback
nickjillings@1604 529 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
nickjillings@1604 530 this.buffer;
nickjillings@1604 531
nickjillings@1604 532 this.loopStart = function() {
nickjillings@1604 533 this.outputGain.gain.value = 1.0;
nickjillings@1604 534 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1604 535 }
nickjillings@1604 536
nickjillings@1604 537 this.loopStop = function() {
nickjillings@1604 538 if (this.outputGain.gain.value != 0.0) {
nickjillings@1604 539 this.outputGain.gain.value = 0.0;
nickjillings@1604 540 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1604 541 }
nickjillings@1604 542 }
nickjillings@1604 543
nickjillings@1604 544 this.play = function(startTime) {
nickjillings@1604 545 this.bufferNode = audioContext.createBufferSource();
nickjillings@1604 546 this.bufferNode.owner = this;
nickjillings@1604 547 this.bufferNode.connect(this.outputGain);
nickjillings@1604 548 this.bufferNode.buffer = this.buffer;
nickjillings@1604 549 this.bufferNode.loop = audioEngineContext.loopPlayback;
nickjillings@1604 550 this.bufferNode.onended = function() {
nickjillings@1604 551 this.owner.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1604 552 };
nickjillings@1604 553 if (this.bufferNode.loop == false) {
nickjillings@1604 554 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1604 555 }
nickjillings@1604 556 this.bufferNode.start(startTime);
nickjillings@1604 557 this.played = true;
nickjillings@1604 558 };
nickjillings@1604 559
nickjillings@1604 560 this.stop = function() {
nickjillings@1604 561 if (this.bufferNode != undefined)
nickjillings@1604 562 {
nickjillings@1604 563 this.bufferNode.stop(0);
nickjillings@1604 564 this.bufferNode = undefined;
nickjillings@1604 565 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1604 566 }
nickjillings@1604 567 };
nickjillings@1604 568
nickjillings@1604 569 this.constructTrack = function(url) {
nickjillings@1604 570 var request = new XMLHttpRequest();
nickjillings@1604 571 this.url = url;
nickjillings@1604 572 request.open('GET',url,true);
nickjillings@1604 573 request.responseType = 'arraybuffer';
nickjillings@1604 574
nickjillings@1604 575 var audioObj = this;
nickjillings@1604 576
nickjillings@1604 577 // Create callback to decode the data asynchronously
nickjillings@1604 578 request.onloadend = function() {
nickjillings@1604 579 audioContext.decodeAudioData(request.response, function(decodedData) {
nickjillings@1604 580 audioObj.buffer = decodedData;
nickjillings@1604 581 audioObj.state = 1;
nickjillings@1604 582 }, function(){
nickjillings@1604 583 // Should only be called if there was an error, but sometimes gets called continuously
nickjillings@1604 584 // Check here if the error is genuine
nickjillings@1604 585 if (audioObj.state == 0 || audioObj.buffer == undefined) {
nickjillings@1604 586 // Genuine error
nickjillings@1604 587 console.log('FATAL - Error loading buffer on '+audioObj.id);
nickjillings@1604 588 }
nickjillings@1604 589 });
nickjillings@1604 590 };
nickjillings@1604 591 request.send();
nickjillings@1604 592 };
nickjillings@1604 593
nickjillings@1604 594 }
nickjillings@1604 595
nickjillings@1604 596 function timer()
nickjillings@1604 597 {
nickjillings@1604 598 /* Timer object used in audioEngine to keep track of session timings
nickjillings@1604 599 * Uses the timer of the web audio API, so sample resolution
nickjillings@1604 600 */
nickjillings@1604 601 this.testStarted = false;
nickjillings@1604 602 this.testStartTime = 0;
nickjillings@1604 603 this.testDuration = 0;
nickjillings@1604 604 this.minimumTestTime = 0; // No minimum test time
nickjillings@1604 605 this.startTest = function()
nickjillings@1604 606 {
nickjillings@1604 607 if (this.testStarted == false)
nickjillings@1604 608 {
nickjillings@1604 609 this.testStartTime = audioContext.currentTime;
nickjillings@1604 610 this.testStarted = true;
nickjillings@1604 611 this.updateTestTime();
nickjillings@1604 612 audioEngineContext.metric.initialiseTest();
nickjillings@1604 613 }
nickjillings@1604 614 };
nickjillings@1604 615 this.stopTest = function()
nickjillings@1604 616 {
nickjillings@1604 617 if (this.testStarted)
nickjillings@1604 618 {
nickjillings@1604 619 this.testDuration = this.getTestTime();
nickjillings@1604 620 this.testStarted = false;
nickjillings@1604 621 } else {
nickjillings@1604 622 console.log('ERR: Test tried to end before beginning');
nickjillings@1604 623 }
nickjillings@1604 624 };
nickjillings@1604 625 this.updateTestTime = function()
nickjillings@1604 626 {
nickjillings@1604 627 if (this.testStarted)
nickjillings@1604 628 {
nickjillings@1604 629 this.testDuration = audioContext.currentTime - this.testStartTime;
nickjillings@1604 630 }
nickjillings@1604 631 };
nickjillings@1604 632 this.getTestTime = function()
nickjillings@1604 633 {
nickjillings@1604 634 this.updateTestTime();
nickjillings@1604 635 return this.testDuration;
nickjillings@1604 636 };
nickjillings@1604 637 }
nickjillings@1604 638
nickjillings@1604 639 function sessionMetrics(engine)
nickjillings@1604 640 {
nickjillings@1604 641 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
nickjillings@1604 642 */
nickjillings@1604 643 this.engine = engine;
nickjillings@1604 644 this.lastClicked = -1;
nickjillings@1604 645 this.data = -1;
nickjillings@1604 646 this.reset = function() {
nickjillings@1604 647 this.lastClicked = -1;
nickjillings@1604 648 this.data = -1;
nickjillings@1604 649 };
nickjillings@1604 650 this.initialiseTest = function(){};
nickjillings@1604 651 }
nickjillings@1604 652
nickjillings@1604 653 function metricTracker()
nickjillings@1604 654 {
nickjillings@1604 655 /* Custom object to track and collect metric data
nickjillings@1604 656 * Used only inside the audioObjects object.
nickjillings@1604 657 */
nickjillings@1604 658
nickjillings@1604 659 this.listenedTimer = 0;
nickjillings@1604 660 this.listenStart = 0;
nickjillings@1604 661 this.listenHold = false;
nickjillings@1604 662 this.initialPosition = -1;
nickjillings@1604 663 this.movementTracker = [];
nickjillings@1604 664 this.wasListenedTo = false;
nickjillings@1604 665 this.wasMoved = false;
nickjillings@1604 666 this.hasComments = false;
nickjillings@1604 667
nickjillings@1604 668 this.initialised = function(position)
nickjillings@1604 669 {
nickjillings@1604 670 if (this.initialPosition == -1) {
nickjillings@1604 671 this.initialPosition = position;
nickjillings@1604 672 }
nickjillings@1604 673 };
nickjillings@1604 674
nickjillings@1604 675 this.moved = function(time,position)
nickjillings@1604 676 {
nickjillings@1604 677 this.wasMoved = true;
nickjillings@1604 678 this.movementTracker[this.movementTracker.length] = [time, position];
nickjillings@1604 679 };
nickjillings@1604 680
nickjillings@1604 681 this.startListening = function(time)
nickjillings@1604 682 {
nickjillings@1604 683 if (this.listenHold == false)
nickjillings@1604 684 {
nickjillings@1604 685 this.wasListenedTo = true;
nickjillings@1604 686 this.listenStart = time;
nickjillings@1604 687 this.listenHold = true;
nickjillings@1604 688 }
nickjillings@1604 689 }
nickjillings@1604 690
nickjillings@1604 691 this.stopListening = function(time)
nickjillings@1604 692 {
nickjillings@1604 693 if (this.listenHold == true)
nickjillings@1604 694 {
nickjillings@1604 695 this.listenedTimer += (time - this.listenStart);
nickjillings@1604 696 this.listenStart = 0;
nickjillings@1604 697 this.listenHold = false;
nickjillings@1604 698 }
nickjillings@1604 699 };
nickjillings@1604 700 }
nickjillings@1604 701
nickjillings@1604 702 function randomiseOrder(input)
nickjillings@1604 703 {
nickjillings@1604 704 // This takes an array of information and randomises the order
nickjillings@1604 705 var N = input.length;
nickjillings@1604 706 var K = N;
nickjillings@1604 707 var holdArr = [];
nickjillings@1604 708 for (var n=0; n<N; n++)
nickjillings@1604 709 {
nickjillings@1604 710 // First pick a random number
nickjillings@1604 711 var r = Math.random();
nickjillings@1604 712 // Multiply and floor by the number of elements left
nickjillings@1604 713 r = Math.floor(r*input.length);
nickjillings@1604 714 // Pick out that element and delete from the array
nickjillings@1604 715 holdArr.push(input.splice(r,1)[0]);
nickjillings@1604 716 }
nickjillings@1604 717 return holdArr;
nickjillings@1604 718 }
nickjillings@1604 719
nickjillings@1604 720 function returnDateNode()
nickjillings@1604 721 {
nickjillings@1604 722 // Create an XML Node for the Date and Time a test was conducted
nickjillings@1604 723 // Structure is
nickjillings@1604 724 // <datetime>
nickjillings@1604 725 // <date year="##" month="##" day="##">DD/MM/YY</date>
nickjillings@1604 726 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
nickjillings@1604 727 // </datetime>
nickjillings@1604 728 var dateTime = new Date();
nickjillings@1604 729 var year = document.createAttribute('year');
nickjillings@1604 730 var month = document.createAttribute('month');
nickjillings@1604 731 var day = document.createAttribute('day');
nickjillings@1604 732 var hour = document.createAttribute('hour');
nickjillings@1604 733 var minute = document.createAttribute('minute');
nickjillings@1604 734 var secs = document.createAttribute('secs');
nickjillings@1604 735
nickjillings@1604 736 year.nodeValue = dateTime.getFullYear();
nickjillings@1604 737 month.nodeValue = dateTime.getMonth()+1;
nickjillings@1604 738 day.nodeValue = dateTime.getDate();
nickjillings@1604 739 hour.nodeValue = dateTime.getHours();
nickjillings@1604 740 minute.nodeValue = dateTime.getMinutes();
nickjillings@1604 741 secs.nodeValue = dateTime.getSeconds();
nickjillings@1604 742
nickjillings@1604 743 var hold = document.createElement("datetime");
nickjillings@1604 744 var date = document.createElement("date");
nickjillings@1604 745 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
nickjillings@1604 746 var time = document.createElement("time");
nickjillings@1604 747 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
nickjillings@1604 748
nickjillings@1604 749 date.setAttributeNode(year);
nickjillings@1604 750 date.setAttributeNode(month);
nickjillings@1604 751 date.setAttributeNode(day);
nickjillings@1604 752 time.setAttributeNode(hour);
nickjillings@1604 753 time.setAttributeNode(minute);
nickjillings@1604 754 time.setAttributeNode(secs);
nickjillings@1604 755
nickjillings@1604 756 hold.appendChild(date);
nickjillings@1604 757 hold.appendChild(time);
nickjillings@1604 758 return hold
nickjillings@1604 759
nickjillings@1604 760 }
nickjillings@1604 761
nickjillings@1604 762 function testWaitIndicator() {
nickjillings@1604 763 var hold = document.createElement("div");
nickjillings@1604 764 hold.id = "testWaitIndicator";
nickjillings@1604 765 hold.style.position = "absolute";
nickjillings@1604 766 hold.style.left = "100px";
nickjillings@1604 767 hold.style.top = "10px";
nickjillings@1604 768 hold.style.width = "500px";
nickjillings@1604 769 hold.style.height = "100px";
nickjillings@1604 770 hold.style.padding = "20px";
nickjillings@1604 771 hold.style.backgroundColor = "rgb(100,200,200)";
nickjillings@1604 772 hold.style.visibility = "hidden";
nickjillings@1604 773 var span = document.createElement("span");
nickjillings@1604 774 span.textContent = "Please wait! Elements still loading";
nickjillings@1604 775 hold.appendChild(span);
nickjillings@1604 776 var body = document.getElementsByTagName('body')[0];
nickjillings@1604 777 body.appendChild(hold);
nickjillings@1604 778 }
nickjillings@1604 779
nickjillings@1604 780 var hidetestwait = null;