annotate core.js @ 1575:9eae8c728460

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