annotate core.js @ 1602:503ba0ff72c2

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