annotate core.js @ 934:b93fb516ea6f

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