annotate core.js @ 136:c849dcbe71aa

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