annotate core.js @ 1750:1365362c12d5

Merge from 156:402bb0f56dc4
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Mon, 01 Jun 2015 12:56:15 +0100
parents b3c0d605f7c9
children 4df1b4e7596f
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@1655 14 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
nickjillings@1643 15 var audioEngineContext; // The custome AudioEngine object
nickjillings@1643 16 var projectReturn; // Hold the URL for the return
nickjillings@1746 17
nickjillings@1642 18
nickjillings@1667 19 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
nickjillings@1667 20 AudioBufferSourceNode.prototype.owner = undefined;
nickjillings@1682 21
nickjillings@1682 22 window.onload = function() {
nickjillings@1682 23 // Function called once the browser has loaded all files.
nickjillings@1682 24 // This should perform any initial commands such as structure / loading documents
nickjillings@1682 25
nickjillings@1682 26 // Create a web audio API context
nickjillings@1701 27 // Fixed for cross-browser support
nickjillings@1701 28 var AudioContext = window.AudioContext || window.webkitAudioContext;
nickjillings@1688 29 audioContext = new AudioContext;
nickjillings@1682 30
nickjillings@1634 31 // Create test state
nickjillings@1634 32 testState = new stateMachine();
nickjillings@1634 33
nickjillings@1682 34 // Create the audio engine object
nickjillings@1682 35 audioEngineContext = new AudioEngine();
nickjillings@1622 36
nickjillings@1622 37 // Create the popup interface object
nickjillings@1622 38 popup = new interfacePopup();
nickjillings@1697 39 };
nickjillings@1682 40
nickjillings@1622 41 function interfacePopup() {
nickjillings@1622 42 // Creates an object to manage the popup
nickjillings@1622 43 this.popup = null;
nickjillings@1622 44 this.popupContent = null;
nickjillings@1622 45 this.popupButton = null;
nickjillings@1622 46 this.popupOptions = null;
nickjillings@1622 47 this.currentIndex = null;
nickjillings@1622 48 this.responses = null;
nickjillings@1622 49 this.createPopup = function(){
nickjillings@1622 50 // Create popup window interface
nickjillings@1622 51 var insertPoint = document.getElementById("topLevelBody");
nickjillings@1622 52 var blank = document.createElement('div');
nickjillings@1622 53 blank.className = 'testHalt';
nickjillings@1622 54
nickjillings@1622 55 this.popup = document.createElement('div');
nickjillings@1622 56 this.popup.id = 'popupHolder';
nickjillings@1622 57 this.popup.className = 'popupHolder';
nickjillings@1622 58 this.popup.style.position = 'absolute';
nickjillings@1622 59 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
nickjillings@1622 60 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
nickjillings@1622 61
nickjillings@1622 62 this.popupContent = document.createElement('div');
nickjillings@1622 63 this.popupContent.id = 'popupContent';
nickjillings@1622 64 this.popupContent.style.marginTop = '25px';
nickjillings@1622 65 this.popupContent.align = 'center';
nickjillings@1622 66 this.popup.appendChild(this.popupContent);
nickjillings@1622 67
nickjillings@1622 68 this.popupButton = document.createElement('button');
nickjillings@1622 69 this.popupButton.className = 'popupButton';
nickjillings@1622 70 this.popupButton.innerHTML = 'Next';
nickjillings@1622 71 this.popupButton.onclick = function(){popup.buttonClicked();};
nickjillings@1622 72 insertPoint.appendChild(this.popup);
nickjillings@1622 73 insertPoint.appendChild(blank);
nickjillings@1622 74 };
nickjillings@1621 75
nickjillings@1622 76 this.showPopup = function(){
nickjillings@1622 77 if (this.popup == null || this.popup == undefined) {
nickjillings@1622 78 this.createPopup();
nickjillings@1622 79 }
nickjillings@1622 80 this.popup.style.zIndex = 3;
nickjillings@1622 81 this.popup.style.visibility = 'visible';
nickjillings@1622 82 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1622 83 blank.style.zIndex = 2;
nickjillings@1622 84 blank.style.visibility = 'visible';
nickjillings@1622 85 };
nickjillings@1622 86
nickjillings@1622 87 this.hidePopup = function(){
nickjillings@1622 88 this.popup.style.zIndex = -1;
nickjillings@1622 89 this.popup.style.visibility = 'hidden';
nickjillings@1622 90 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1622 91 blank.style.zIndex = -2;
nickjillings@1622 92 blank.style.visibility = 'hidden';
nickjillings@1622 93 };
nickjillings@1622 94
nickjillings@1622 95 this.postNode = function() {
nickjillings@1622 96 // This will take the node from the popupOptions and display it
nickjillings@1622 97 var node = this.popupOptions[this.currentIndex];
nickjillings@1622 98 this.popupContent.innerHTML = null;
nickjillings@1622 99 if (node.nodeName == 'statement') {
nickjillings@1622 100 var span = document.createElement('span');
nickjillings@1622 101 span.textContent = node.textContent;
nickjillings@1622 102 this.popupContent.appendChild(span);
nickjillings@1622 103 } else if (node.nodeName == 'question') {
nickjillings@1622 104 var span = document.createElement('span');
nickjillings@1622 105 span.textContent = node.textContent;
nickjillings@1622 106 var textArea = document.createElement('textarea');
nickjillings@1622 107 var br = document.createElement('br');
nickjillings@1622 108 this.popupContent.appendChild(span);
nickjillings@1622 109 this.popupContent.appendChild(br);
nickjillings@1622 110 this.popupContent.appendChild(textArea);
nickjillings@1748 111 this.popupContent.childNodes[2].focus();
nickjillings@1622 112 }
nickjillings@1622 113 this.popupContent.appendChild(this.popupButton);
nickjillings@1748 114 };
nickjillings@1622 115
nickjillings@1622 116 this.initState = function(node) {
nickjillings@1622 117 //Call this with your preTest and postTest nodes when needed to
nickjillings@1622 118 // initialise the popup procedure.
nickjillings@1622 119 this.popupOptions = $(node).children();
nickjillings@1622 120 if (this.popupOptions.length > 0) {
nickjillings@1622 121 if (node.nodeName == 'preTest' || node.nodeName == 'PreTest') {
nickjillings@1622 122 this.responses = document.createElement('PreTest');
nickjillings@1622 123 } else if (node.nodeName == 'postTest' || node.nodeName == 'PostTest') {
nickjillings@1622 124 this.responses = document.createElement('PostTest');
nickjillings@1622 125 } else {
nickjillings@1622 126 console.log ('WARNING - popup node neither pre or post!');
nickjillings@1622 127 this.responses = document.createElement('responses');
nickjillings@1622 128 }
nickjillings@1622 129 this.currentIndex = 0;
nickjillings@1622 130 this.showPopup();
nickjillings@1622 131 this.postNode();
nickjillings@1622 132 }
nickjillings@1748 133 };
nickjillings@1622 134
nickjillings@1622 135 this.buttonClicked = function() {
nickjillings@1622 136 // Each time the popup button is clicked!
nickjillings@1622 137 var node = this.popupOptions[this.currentIndex];
nickjillings@1622 138 if (node.nodeName == 'question') {
nickjillings@1622 139 // Must extract the question data
nickjillings@1622 140 var mandatory = node.attributes['mandatory'];
nickjillings@1622 141 if (mandatory == undefined) {
nickjillings@1622 142 mandatory = false;
nickjillings@1622 143 } else {
nickjillings@1622 144 if (mandatory.value == 'true'){mandatory = true;}
nickjillings@1622 145 else {mandatory = false;}
nickjillings@1622 146 }
nickjillings@1622 147 var textArea = $(popup.popupContent).find('textarea')[0];
nickjillings@1622 148 if (mandatory == true && textArea.value.length == 0) {
nickjillings@1622 149 alert('This question is mandatory');
nickjillings@1622 150 return;
nickjillings@1622 151 } else {
nickjillings@1622 152 // Save the text content
nickjillings@1622 153 var hold = document.createElement('comment');
nickjillings@1622 154 hold.id = node.attributes['id'].value;
nickjillings@1622 155 hold.innerHTML = textArea.value;
nickjillings@1623 156 console.log("Question: "+ node.textContent);
nickjillings@1623 157 console.log("Question Response: "+ textArea.value);
nickjillings@1622 158 this.responses.appendChild(hold);
nickjillings@1622 159 }
nickjillings@1622 160 }
nickjillings@1622 161 this.currentIndex++;
nickjillings@1622 162 if (this.currentIndex < this.popupOptions.length) {
nickjillings@1622 163 this.postNode();
nickjillings@1622 164 } else {
nickjillings@1622 165 // Reached the end of the popupOptions
nickjillings@1622 166 this.hidePopup();
nickjillings@1634 167 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) {
nickjillings@1634 168 testState.stateResults[testState.stateIndex] = this.responses;
nickjillings@1634 169 } else {
nickjillings@1634 170 testState.stateResults[testState.stateIndex].appendChild(this.responses);
nickjillings@1634 171 }
nickjillings@1622 172 advanceState();
nickjillings@1622 173 }
nickjillings@1748 174 };
nickjillings@1621 175 }
nickjillings@1621 176
nickjillings@1622 177 function advanceState()
nickjillings@1621 178 {
nickjillings@1634 179 // Just for complete clarity
nickjillings@1634 180 testState.advanceState();
nickjillings@1634 181 }
nickjillings@1634 182
nickjillings@1634 183 function stateMachine()
nickjillings@1634 184 {
nickjillings@1634 185 // Object prototype for tracking and managing the test state
nickjillings@1634 186 this.stateMap = [];
nickjillings@1634 187 this.stateIndex = null;
nickjillings@1634 188 this.currentStateMap = [];
nickjillings@1634 189 this.currentIndex = null;
nickjillings@1634 190 this.currentTestId = 0;
nickjillings@1634 191 this.stateResults = [];
nickjillings@1604 192 this.timerCallBackHolders = null;
nickjillings@1634 193 this.initialise = function(){
nickjillings@1634 194 if (this.stateMap.length > 0) {
nickjillings@1634 195 if(this.stateIndex != null) {
nickjillings@1634 196 console.log('NOTE - State already initialise');
nickjillings@1634 197 }
nickjillings@1634 198 this.stateIndex = -1;
nickjillings@1634 199 var that = this;
nickjillings@1634 200 for (var id=0; id<this.stateMap.length; id++){
nickjillings@1634 201 var name = this.stateMap[id].nodeName;
nickjillings@1634 202 var obj = document.createElement(name);
nickjillings@1600 203 if (name == "audioHolder") {
nickjillings@1600 204 obj.id = this.stateMap[id].id;
nickjillings@1600 205 }
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@1746 382 if (xmlhttp.status == 404) {
nickjillings@1746 383 createProjectSave(null);
nickjillings@1746 384 }
nickjillings@1688 385 }
nickjillings@1653 386 return submitDiv;
nickjillings@1682 387 }
nickjillings@1682 388
nickjillings@1634 389 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nickjillings@1634 390 function interfaceXMLSave(){
nickjillings@1634 391 // Create the XML string to be exported with results
nickjillings@1634 392 var xmlDoc = document.createElement("BrowserEvaluationResult");
nickjillings@1634 393 xmlDoc.appendChild(returnDateNode());
nickjillings@1634 394 for (var i=0; i<testState.stateResults.length; i++)
nickjillings@1634 395 {
nickjillings@1634 396 xmlDoc.appendChild(testState.stateResults[i]);
nickjillings@1634 397 }
nickjillings@1634 398
nickjillings@1634 399 return xmlDoc;
nickjillings@1634 400 }
nickjillings@1634 401
nickjillings@1682 402 function AudioEngine() {
nickjillings@1682 403
nickjillings@1682 404 // Create two output paths, the main outputGain and fooGain.
nickjillings@1682 405 // Output gain is default to 1 and any items for playback route here
nickjillings@1682 406 // Foo gain is used for analysis to ensure paths get processed, but are not heard
nickjillings@1682 407 // because web audio will optimise and any route which does not go to the destination gets ignored.
nickjillings@1682 408 this.outputGain = audioContext.createGain();
nickjillings@1682 409 this.fooGain = audioContext.createGain();
nickjillings@1682 410 this.fooGain.gain = 0;
nickjillings@1682 411
nickjillings@1688 412 // Use this to detect playback state: 0 - stopped, 1 - playing
nickjillings@1688 413 this.status = 0;
nickjillings@1620 414 this.audioObjectsReady = false;
nickjillings@1688 415
nickjillings@1682 416 // Connect both gains to output
nickjillings@1682 417 this.outputGain.connect(audioContext.destination);
nickjillings@1682 418 this.fooGain.connect(audioContext.destination);
nickjillings@1682 419
nickjillings@1659 420 // Create the timer Object
nickjillings@1659 421 this.timer = new timer();
nickjillings@1659 422 // Create session metrics
nickjillings@1659 423 this.metric = new sessionMetrics(this);
nickjillings@1659 424
nickjillings@1667 425 this.loopPlayback = false;
nickjillings@1667 426
nickjillings@1682 427 // Create store for new audioObjects
nickjillings@1682 428 this.audioObjects = [];
nickjillings@1682 429
nickjillings@1620 430 this.play = function() {
nickjillings@1620 431 // Start the timer and set the audioEngine state to playing (1)
nickjillings@1620 432 if (this.status == 0) {
nickjillings@1620 433 // Check if all audioObjects are ready
nickjillings@1620 434 if (this.audioObjectsReady == false) {
nickjillings@1620 435 this.audioObjectsReady = this.checkAllReady();
nickjillings@1620 436 }
nickjillings@1620 437 if (this.audioObjectsReady == true) {
nickjillings@1620 438 this.timer.startTest();
nickjillings@1636 439 if (this.loopPlayback) {
nickjillings@1636 440 for(var i=0; i<this.audioObjects.length; i++) {
nickjillings@1636 441 this.audioObjects[i].play(this.timer.getTestTime()+1);
nickjillings@1636 442 }
nickjillings@1636 443 }
nickjillings@1620 444 this.status = 1;
nickjillings@1620 445 }
nickjillings@1620 446 }
nickjillings@1620 447 };
nickjillings@1682 448
nickjillings@1620 449 this.stop = function() {
nickjillings@1620 450 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
nickjillings@1620 451 if (this.status == 1) {
nickjillings@1620 452 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1620 453 {
nickjillings@1620 454 this.audioObjects[i].stop();
nickjillings@1620 455 }
nickjillings@1620 456 this.status = 0;
nickjillings@1620 457 }
nickjillings@1620 458 };
nickjillings@1689 459
nickjillings@1689 460
nickjillings@1682 461 this.newTrack = function(url) {
nickjillings@1682 462 // Pull data from given URL into new audio buffer
nickjillings@1682 463 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
nickjillings@1688 464
nickjillings@1682 465 // Create the audioObject with ID of the new track length;
nickjillings@1659 466 audioObjectId = this.audioObjects.length;
nickjillings@1682 467 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
nickjillings@1688 468
nickjillings@1688 469 // AudioObject will get track itself.
nickjillings@1688 470 this.audioObjects[audioObjectId].constructTrack(url);
nickjillings@1697 471 };
nickjillings@1682 472
nickjillings@1620 473 this.newTestPage = function() {
nickjillings@1620 474 this.state = 0;
nickjillings@1620 475 this.audioObjectsReady = false;
nickjillings@1620 476 this.metric.reset();
nickjillings@1620 477 this.audioObjects = [];
nickjillings@1620 478 };
nickjillings@1620 479
nickjillings@1614 480 this.checkAllPlayed = function() {
nickjillings@1614 481 arr = [];
nickjillings@1614 482 for (var id=0; id<this.audioObjects.length; id++) {
nickjillings@1741 483 if (this.audioObjects[id].metric.wasListenedTo == false) {
nickjillings@1614 484 arr.push(this.audioObjects[id].id);
nickjillings@1614 485 }
nickjillings@1614 486 }
nickjillings@1614 487 return arr;
nickjillings@1614 488 };
nickjillings@1614 489
nickjillings@1620 490 this.checkAllReady = function() {
nickjillings@1620 491 var ready = true;
nickjillings@1620 492 for (var i=0; i<this.audioObjects.length; i++) {
nickjillings@1620 493 if (this.audioObjects[i].state == 0) {
nickjillings@1620 494 // Track not ready
nickjillings@1620 495 console.log('WAIT -- audioObject '+i+' not ready yet!');
nickjillings@1620 496 ready = false;
nickjillings@1620 497 };
nickjillings@1620 498 }
nickjillings@1620 499 return ready;
nickjillings@1620 500 };
nickjillings@1620 501
nickjillings@1682 502 }
nickjillings@1682 503
nickjillings@1682 504 function audioObject(id) {
nickjillings@1682 505 // The main buffer object with common control nodes to the AudioEngine
nickjillings@1682 506
nickjillings@1682 507 this.id = id;
nickjillings@1682 508 this.state = 0; // 0 - no data, 1 - ready
nickjillings@1704 509 this.url = null; // Hold the URL given for the output back to the results.
nickjillings@1602 510 this.metric = new metricTracker(this);
nickjillings@1682 511
nickjillings@1682 512 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
nickjillings@1667 513 this.bufferNode = undefined;
nickjillings@1682 514 this.outputGain = audioContext.createGain();
nickjillings@1682 515
nickjillings@1689 516 // Default output gain to be zero
nickjillings@1689 517 this.outputGain.gain.value = 0.0;
nickjillings@1689 518
nickjillings@1682 519 // Connect buffer to the audio graph
nickjillings@1682 520 this.outputGain.connect(audioEngineContext.outputGain);
nickjillings@1682 521
nickjillings@1682 522 // the audiobuffer is not designed for multi-start playback
nickjillings@1682 523 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
nickjillings@1682 524 this.buffer;
b@1639 525
nickjillings@1637 526 this.loopStart = function() {
nickjillings@1637 527 this.outputGain.gain.value = 1.0;
nickjillings@1637 528 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1637 529 }
nickjillings@1637 530
nickjillings@1637 531 this.loopStop = function() {
nickjillings@1637 532 if (this.outputGain.gain.value != 0.0) {
nickjillings@1637 533 this.outputGain.gain.value = 0.0;
nickjillings@1637 534 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1637 535 }
nickjillings@1637 536 }
nickjillings@1637 537
nickjillings@1682 538 this.play = function(startTime) {
nickjillings@1667 539 this.bufferNode = audioContext.createBufferSource();
nickjillings@1617 540 this.bufferNode.owner = this;
nickjillings@1667 541 this.bufferNode.connect(this.outputGain);
nickjillings@1667 542 this.bufferNode.buffer = this.buffer;
nickjillings@1667 543 this.bufferNode.loop = audioEngineContext.loopPlayback;
nickjillings@1637 544 this.bufferNode.onended = function() {
nickjillings@1741 545 // Safari does not like using 'this' to reference the calling object!
nickjillings@1742 546 event.srcElement.owner.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1741 547 };
nickjillings@1617 548 if (this.bufferNode.loop == false) {
nickjillings@1637 549 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1617 550 }
nickjillings@1682 551 this.bufferNode.start(startTime);
nickjillings@1697 552 };
nickjillings@1682 553
nickjillings@1682 554 this.stop = function() {
nickjillings@1735 555 if (this.bufferNode != undefined)
nickjillings@1735 556 {
nickjillings@1735 557 this.bufferNode.stop(0);
nickjillings@1735 558 this.bufferNode = undefined;
nickjillings@1637 559 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1735 560 }
nickjillings@1697 561 };
nickjillings@1689 562
nickjillings@1688 563 this.constructTrack = function(url) {
nickjillings@1688 564 var request = new XMLHttpRequest();
nickjillings@1704 565 this.url = url;
nickjillings@1688 566 request.open('GET',url,true);
nickjillings@1688 567 request.responseType = 'arraybuffer';
nickjillings@1688 568
nickjillings@1688 569 var audioObj = this;
nickjillings@1688 570
nickjillings@1688 571 // Create callback to decode the data asynchronously
nickjillings@1688 572 request.onloadend = function() {
nickjillings@1688 573 audioContext.decodeAudioData(request.response, function(decodedData) {
nickjillings@1688 574 audioObj.buffer = decodedData;
nickjillings@1688 575 audioObj.state = 1;
nickjillings@1688 576 }, function(){
nickjillings@1688 577 // Should only be called if there was an error, but sometimes gets called continuously
nickjillings@1688 578 // Check here if the error is genuine
nickjillings@1688 579 if (audioObj.state == 0 || audioObj.buffer == undefined) {
nickjillings@1688 580 // Genuine error
nickjillings@1688 581 console.log('FATAL - Error loading buffer on '+audioObj.id);
nickjillings@1688 582 }
nickjillings@1688 583 });
nickjillings@1697 584 };
nickjillings@1688 585 request.send();
nickjillings@1697 586 };
nickjillings@1688 587
nickjillings@1659 588 }
nickjillings@1659 589
nickjillings@1659 590 function timer()
nickjillings@1659 591 {
nickjillings@1659 592 /* Timer object used in audioEngine to keep track of session timings
nickjillings@1659 593 * Uses the timer of the web audio API, so sample resolution
nickjillings@1659 594 */
nickjillings@1659 595 this.testStarted = false;
nickjillings@1659 596 this.testStartTime = 0;
nickjillings@1659 597 this.testDuration = 0;
nickjillings@1659 598 this.minimumTestTime = 0; // No minimum test time
nickjillings@1659 599 this.startTest = function()
nickjillings@1659 600 {
nickjillings@1659 601 if (this.testStarted == false)
nickjillings@1659 602 {
nickjillings@1659 603 this.testStartTime = audioContext.currentTime;
nickjillings@1659 604 this.testStarted = true;
nickjillings@1659 605 this.updateTestTime();
nickjillings@1662 606 audioEngineContext.metric.initialiseTest();
nickjillings@1659 607 }
nickjillings@1659 608 };
nickjillings@1659 609 this.stopTest = function()
nickjillings@1659 610 {
nickjillings@1659 611 if (this.testStarted)
nickjillings@1659 612 {
nickjillings@1659 613 this.testDuration = this.getTestTime();
nickjillings@1659 614 this.testStarted = false;
nickjillings@1659 615 } else {
nickjillings@1659 616 console.log('ERR: Test tried to end before beginning');
nickjillings@1659 617 }
nickjillings@1659 618 };
nickjillings@1659 619 this.updateTestTime = function()
nickjillings@1659 620 {
nickjillings@1659 621 if (this.testStarted)
nickjillings@1659 622 {
nickjillings@1659 623 this.testDuration = audioContext.currentTime - this.testStartTime;
nickjillings@1659 624 }
nickjillings@1659 625 };
nickjillings@1659 626 this.getTestTime = function()
nickjillings@1659 627 {
nickjillings@1659 628 this.updateTestTime();
nickjillings@1659 629 return this.testDuration;
nickjillings@1659 630 };
nickjillings@1659 631 }
nickjillings@1659 632
nickjillings@1659 633 function sessionMetrics(engine)
nickjillings@1659 634 {
nickjillings@1659 635 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
nickjillings@1659 636 */
nickjillings@1659 637 this.engine = engine;
nickjillings@1659 638 this.lastClicked = -1;
nickjillings@1659 639 this.data = -1;
nickjillings@1620 640 this.reset = function() {
nickjillings@1620 641 this.lastClicked = -1;
nickjillings@1620 642 this.data = -1;
nickjillings@1620 643 };
nickjillings@1662 644 this.initialiseTest = function(){};
nickjillings@1659 645 }
nickjillings@1659 646
nickjillings@1602 647 function metricTracker(caller)
nickjillings@1659 648 {
nickjillings@1659 649 /* Custom object to track and collect metric data
nickjillings@1659 650 * Used only inside the audioObjects object.
nickjillings@1659 651 */
nickjillings@1659 652
nickjillings@1659 653 this.listenedTimer = 0;
nickjillings@1659 654 this.listenStart = 0;
nickjillings@1617 655 this.listenHold = false;
nickjillings@1661 656 this.initialPosition = -1;
nickjillings@1659 657 this.movementTracker = [];
nickjillings@1659 658 this.wasListenedTo = false;
nickjillings@1659 659 this.wasMoved = false;
nickjillings@1659 660 this.hasComments = false;
nickjillings@1602 661 this.parent = caller;
nickjillings@1659 662
nickjillings@1659 663 this.initialised = function(position)
nickjillings@1659 664 {
nickjillings@1661 665 if (this.initialPosition == -1) {
nickjillings@1661 666 this.initialPosition = position;
nickjillings@1661 667 }
nickjillings@1659 668 };
nickjillings@1659 669
nickjillings@1659 670 this.moved = function(time,position)
nickjillings@1659 671 {
nickjillings@1659 672 this.wasMoved = true;
nickjillings@1659 673 this.movementTracker[this.movementTracker.length] = [time, position];
nickjillings@1659 674 };
nickjillings@1659 675
nickjillings@1637 676 this.startListening = function(time)
nickjillings@1659 677 {
nickjillings@1617 678 if (this.listenHold == false)
nickjillings@1659 679 {
nickjillings@1659 680 this.wasListenedTo = true;
nickjillings@1659 681 this.listenStart = time;
nickjillings@1617 682 this.listenHold = true;
nickjillings@1602 683 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1602 684 }
nickjillings@1602 685 };
nickjillings@1637 686
nickjillings@1637 687 this.stopListening = function(time)
nickjillings@1637 688 {
nickjillings@1637 689 if (this.listenHold == true)
nickjillings@1637 690 {
nickjillings@1659 691 this.listenedTimer += (time - this.listenStart);
nickjillings@1659 692 this.listenStart = 0;
nickjillings@1617 693 this.listenHold = false;
nickjillings@1659 694 }
nickjillings@1659 695 };
nickjillings@1664 696 }
nickjillings@1664 697
nickjillings@1664 698 function randomiseOrder(input)
nickjillings@1664 699 {
nickjillings@1664 700 // This takes an array of information and randomises the order
nickjillings@1664 701 var N = input.length;
nickjillings@1664 702 var K = N;
nickjillings@1664 703 var holdArr = [];
nickjillings@1664 704 for (var n=0; n<N; n++)
nickjillings@1664 705 {
nickjillings@1664 706 // First pick a random number
nickjillings@1664 707 var r = Math.random();
nickjillings@1664 708 // Multiply and floor by the number of elements left
nickjillings@1664 709 r = Math.floor(r*input.length);
nickjillings@1664 710 // Pick out that element and delete from the array
nickjillings@1664 711 holdArr.push(input.splice(r,1)[0]);
nickjillings@1664 712 }
nickjillings@1664 713 return holdArr;
nickjillings@1631 714 }
nickjillings@1631 715
nickjillings@1631 716 function returnDateNode()
nickjillings@1631 717 {
nickjillings@1631 718 // Create an XML Node for the Date and Time a test was conducted
nickjillings@1631 719 // Structure is
nickjillings@1631 720 // <datetime>
nickjillings@1631 721 // <date year="##" month="##" day="##">DD/MM/YY</date>
nickjillings@1631 722 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
nickjillings@1631 723 // </datetime>
nickjillings@1631 724 var dateTime = new Date();
nickjillings@1631 725 var year = document.createAttribute('year');
nickjillings@1631 726 var month = document.createAttribute('month');
nickjillings@1631 727 var day = document.createAttribute('day');
nickjillings@1631 728 var hour = document.createAttribute('hour');
nickjillings@1631 729 var minute = document.createAttribute('minute');
nickjillings@1631 730 var secs = document.createAttribute('secs');
nickjillings@1631 731
nickjillings@1631 732 year.nodeValue = dateTime.getFullYear();
nickjillings@1631 733 month.nodeValue = dateTime.getMonth()+1;
nickjillings@1631 734 day.nodeValue = dateTime.getDate();
nickjillings@1631 735 hour.nodeValue = dateTime.getHours();
nickjillings@1631 736 minute.nodeValue = dateTime.getMinutes();
nickjillings@1631 737 secs.nodeValue = dateTime.getSeconds();
nickjillings@1631 738
nickjillings@1631 739 var hold = document.createElement("datetime");
nickjillings@1631 740 var date = document.createElement("date");
nickjillings@1631 741 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
nickjillings@1631 742 var time = document.createElement("time");
nickjillings@1631 743 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
nickjillings@1631 744
nickjillings@1631 745 date.setAttributeNode(year);
nickjillings@1631 746 date.setAttributeNode(month);
nickjillings@1631 747 date.setAttributeNode(day);
nickjillings@1631 748 time.setAttributeNode(hour);
nickjillings@1631 749 time.setAttributeNode(minute);
nickjillings@1631 750 time.setAttributeNode(secs);
nickjillings@1631 751
nickjillings@1631 752 hold.appendChild(date);
nickjillings@1631 753 hold.appendChild(time);
nickjillings@1631 754 return hold
nickjillings@1631 755
nickjillings@1604 756 }
nickjillings@1604 757
nickjillings@1604 758 function testWaitIndicator() {
nickjillings@1746 759 if (audioEngineContext.checkAllReady() == false) {
nickjillings@1746 760 var hold = document.createElement("div");
nickjillings@1746 761 hold.id = "testWaitIndicator";
nickjillings@1746 762 hold.className = "indicator-box";
nickjillings@1746 763 var span = document.createElement("span");
nickjillings@1746 764 span.textContent = "Please wait! Elements still loading";
nickjillings@1746 765 hold.appendChild(span);
nickjillings@1746 766 var body = document.getElementsByTagName('body')[0];
nickjillings@1746 767 body.appendChild(hold);
nickjillings@1746 768 testWaitTimerIntervalHolder = setInterval(function(){
nickjillings@1746 769 var ready = audioEngineContext.checkAllReady();
nickjillings@1746 770 if (ready) {
nickjillings@1746 771 var elem = document.getElementById('testWaitIndicator');
nickjillings@1746 772 var body = document.getElementsByTagName('body')[0];
nickjillings@1746 773 body.removeChild(elem);
nickjillings@1746 774 clearInterval(testWaitTimerIntervalHolder);
nickjillings@1746 775 }
nickjillings@1746 776 },500,false);
nickjillings@1746 777 }
nickjillings@1604 778 }
nickjillings@1604 779
nickjillings@1746 780 var testWaitTimerIntervalHolder = null;