annotate core.js @ 2009:1e2e43794291

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