annotate core.js @ 1596:4d71d16c1816

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