annotate core.js @ 149:52a42d24a3eb

Fix Bug #1259: testState now includes the audioHolder ids.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Mon, 01 Jun 2015 08:38:31 +0100
parents 431f6bec460b
children 789c3cd06290
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);
n@149 206 if (name == "audioHolder") {
n@149 207 obj.id = this.stateMap[id].id;
n@149 208 }
nicholas@129 209 this.stateResults.push(obj);
nicholas@129 210 }
nicholas@129 211 } else {
nicholas@129 212 conolse.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
nicholas@116 213 }
nicholas@129 214 };
nicholas@129 215 this.advanceState = function(){
nicholas@129 216 if (this.stateIndex == null) {
nicholas@129 217 this.initialise();
nicholas@129 218 }
nicholas@129 219 if (this.stateIndex == -1) {
nicholas@129 220 console.log('Starting test...');
nicholas@129 221 }
nicholas@129 222 if (this.currentIndex == null){
nicholas@129 223 if (this.currentStateMap.nodeName == "audioHolder") {
nicholas@129 224 // Save current page
nicholas@129 225 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId);
nicholas@129 226 this.currentTestId++;
nicholas@129 227 }
nicholas@129 228 this.stateIndex++;
nicholas@129 229 if (this.stateIndex >= this.stateMap.length) {
nicholas@129 230 console.log('Test Completed');
nicholas@129 231 createProjectSave(projectReturn);
nicholas@129 232 } else {
nicholas@129 233 this.currentStateMap = this.stateMap[this.stateIndex];
nicholas@129 234 if (this.currentStateMap.nodeName == "audioHolder") {
nicholas@129 235 console.log('Loading test page');
nicholas@129 236 loadTest(this.currentStateMap);
nicholas@129 237 this.initialiseInnerState(this.currentStateMap);
nicholas@129 238 } else if (this.currentStateMap.nodeName == "PreTest" || this.currentStateMap.nodeName == "PostTest") {
nicholas@129 239 if (this.currentStateMap.childElementCount >= 1) {
nicholas@129 240 popup.initState(this.currentStateMap);
nicholas@129 241 } else {
nicholas@129 242 this.advanceState();
nicholas@129 243 }
nicholas@129 244 } else {
nicholas@129 245 this.advanceState();
nicholas@129 246 }
nicholas@129 247 }
nicholas@129 248 } else {
nicholas@129 249 this.advanceInnerState();
nicholas@129 250 }
nicholas@129 251 };
nicholas@129 252
nicholas@129 253 this.testPageCompleted = function(store, testXML, testId) {
nicholas@129 254 // Function called each time a test page has been completed
nicholas@129 255 // Can be used to over-rule default behaviour
nicholas@129 256
nicholas@129 257 pageXMLSave(store, testXML, testId);
nicholas@129 258 }
nicholas@129 259
nicholas@129 260 this.initialiseInnerState = function(testXML) {
nicholas@129 261 // Parses the received testXML for pre and post test options
nicholas@129 262 this.currentStateMap = [];
nicholas@129 263 var preTest = $(testXML).find('PreTest')[0];
nicholas@129 264 var postTest = $(testXML).find('PostTest')[0];
nicholas@129 265 if (preTest == undefined) {preTest = document.createElement("preTest");}
nicholas@129 266 if (postTest == undefined){postTest= document.createElement("postTest");}
nicholas@129 267 this.currentStateMap.push(preTest);
nicholas@129 268 this.currentStateMap.push(testXML);
nicholas@129 269 this.currentStateMap.push(postTest);
nicholas@129 270 this.currentIndex = -1;
nicholas@129 271 this.advanceInnerState();
nicholas@129 272 }
nicholas@129 273
nicholas@129 274 this.advanceInnerState = function() {
nicholas@129 275 this.currentIndex++;
nicholas@129 276 if (this.currentIndex >= this.currentStateMap.length) {
nicholas@129 277 this.currentIndex = null;
nicholas@129 278 this.currentStateMap = this.stateMap[this.stateIndex];
nicholas@129 279 this.advanceState();
nicholas@129 280 } else {
nicholas@129 281 if (this.currentStateMap[this.currentIndex].nodeName == "audioHolder") {
nicholas@129 282 console.log("Loading test page"+this.currentTestId);
nicholas@129 283 } else if (this.currentStateMap[this.currentIndex].nodeName == "PreTest") {
nicholas@129 284 popup.initState(this.currentStateMap[this.currentIndex]);
nicholas@129 285 } else if (this.currentStateMap[this.currentIndex].nodeName == "PostTest") {
nicholas@129 286 popup.initState(this.currentStateMap[this.currentIndex]);
nicholas@129 287 } else {
nicholas@129 288 this.advanceInnerState();
nicholas@129 289 }
nicholas@116 290 }
nicholas@114 291 }
nicholas@129 292
nicholas@129 293 this.previousState = function(){};
nicholas@114 294 }
nicholas@114 295
nicholas@116 296 function testEnded(testId)
nicholas@114 297 {
nicholas@116 298 pageXMLSave(testId);
nicholas@116 299 if (testXMLSetups.length-1 > testId)
nicholas@116 300 {
nicholas@116 301 // Yes we have another test to perform
nicholas@116 302 testId = (Number(testId)+1);
nicholas@116 303 currentState = 'testRun-'+testId;
nicholas@116 304 loadTest(testId);
nicholas@116 305 } else {
nicholas@116 306 console.log('Testing Completed!');
nicholas@116 307 currentState = 'postTest';
nicholas@116 308 // Check for any post tests
nicholas@116 309 var xmlSetup = projectXML.find('setup');
nicholas@116 310 var postTest = xmlSetup.find('PostTest')[0];
nicholas@116 311 popup.initState(postTest);
nicholas@116 312 }
nicholas@114 313 }
nicholas@114 314
nicholas@1 315 function loadProjectSpec(url) {
nicholas@1 316 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
nicholas@1 317 // If url is null, request client to upload project XML document
nicholas@2 318 var r = new XMLHttpRequest();
nicholas@2 319 r.open('GET',url,true);
nicholas@2 320 r.onload = function() {
nicholas@2 321 loadProjectSpecCallback(r.response);
n@16 322 };
nicholas@2 323 r.send();
n@16 324 };
nicholas@2 325
nicholas@2 326 function loadProjectSpecCallback(response) {
nicholas@2 327 // Function called after asynchronous download of XML project specification
nicholas@2 328 var decode = $.parseXML(response);
nicholas@2 329 projectXML = $(decode);
nicholas@2 330
nicholas@2 331 // Now extract the setup tag
nicholas@2 332 var xmlSetup = projectXML.find('setup');
n@16 333 // Detect the interface to use and load the relevant javascripts.
nicholas@2 334 var interfaceType = xmlSetup[0].attributes['interface'];
nicholas@2 335 var interfaceJS = document.createElement('script');
nicholas@2 336 interfaceJS.setAttribute("type","text/javascript");
nicholas@2 337 if (interfaceType.value == 'APE') {
nicholas@2 338 interfaceJS.setAttribute("src","ape.js");
n@33 339
n@33 340 // APE comes with a css file
n@33 341 var css = document.createElement('link');
n@33 342 css.rel = 'stylesheet';
n@33 343 css.type = 'text/css';
n@33 344 css.href = 'ape.css';
n@33 345
n@33 346 document.getElementsByTagName("head")[0].appendChild(css);
nicholas@2 347 }
nicholas@2 348 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
n@127 349
n@127 350 // Define window callbacks for interface
n@127 351 window.onresize = function(event){resizeWindow(event);};
nicholas@1 352 }
nicholas@1 353
nicholas@1 354 function createProjectSave(destURL) {
nicholas@1 355 // Save the data from interface into XML and send to destURL
nicholas@1 356 // If destURL is null then download XML in client
nicholas@7 357 // Now time to render file locally
nicholas@7 358 var xmlDoc = interfaceXMLSave();
nicholas@122 359 var parent = document.createElement("div");
nicholas@122 360 parent.appendChild(xmlDoc);
nicholas@122 361 var file = [parent.innerHTML];
nicholas@7 362 if (destURL == "null" || destURL == undefined) {
nicholas@7 363 var bb = new Blob(file,{type : 'application/xml'});
nicholas@7 364 var dnlk = window.URL.createObjectURL(bb);
nicholas@7 365 var a = document.createElement("a");
nicholas@7 366 a.hidden = '';
nicholas@7 367 a.href = dnlk;
nicholas@7 368 a.download = "save.xml";
nicholas@7 369 a.textContent = "Save File";
nicholas@7 370
nicholas@7 371 var submitDiv = document.getElementById('download-point');
nicholas@7 372 submitDiv.appendChild(a);
nicholas@118 373 popup.showPopup();
nicholas@118 374 popup.popupContent.innerHTML = null;
nicholas@118 375 popup.popupContent.appendChild(submitDiv)
nicholas@122 376 } else {
nicholas@122 377 var xmlhttp = new XMLHttpRequest;
nicholas@122 378 xmlhttp.open("POST",destURL,true);
nicholas@122 379 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
n@123 380 xmlhttp.onerror = function(){
n@123 381 console.log('Error saving file to server! Presenting download locally');
n@123 382 createProjectSave(null);
n@123 383 };
nicholas@122 384 xmlhttp.send(file);
nicholas@7 385 }
n@43 386 return submitDiv;
nicholas@1 387 }
nicholas@1 388
nicholas@129 389 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nicholas@129 390 function interfaceXMLSave(){
nicholas@129 391 // Create the XML string to be exported with results
nicholas@129 392 var xmlDoc = document.createElement("BrowserEvaluationResult");
nicholas@129 393 xmlDoc.appendChild(returnDateNode());
nicholas@129 394 for (var i=0; i<testState.stateResults.length; i++)
nicholas@129 395 {
nicholas@129 396 xmlDoc.appendChild(testState.stateResults[i]);
nicholas@129 397 }
nicholas@129 398
nicholas@129 399 return xmlDoc;
nicholas@129 400 }
nicholas@129 401
nicholas@1 402 function AudioEngine() {
nicholas@1 403
nicholas@1 404 // Create two output paths, the main outputGain and fooGain.
nicholas@1 405 // Output gain is default to 1 and any items for playback route here
nicholas@1 406 // Foo gain is used for analysis to ensure paths get processed, but are not heard
nicholas@1 407 // because web audio will optimise and any route which does not go to the destination gets ignored.
nicholas@1 408 this.outputGain = audioContext.createGain();
nicholas@1 409 this.fooGain = audioContext.createGain();
nicholas@1 410 this.fooGain.gain = 0;
nicholas@1 411
nicholas@7 412 // Use this to detect playback state: 0 - stopped, 1 - playing
nicholas@7 413 this.status = 0;
n@113 414 this.audioObjectsReady = false;
nicholas@7 415
nicholas@1 416 // Connect both gains to output
nicholas@1 417 this.outputGain.connect(audioContext.destination);
nicholas@1 418 this.fooGain.connect(audioContext.destination);
nicholas@1 419
n@49 420 // Create the timer Object
n@49 421 this.timer = new timer();
n@49 422 // Create session metrics
n@49 423 this.metric = new sessionMetrics(this);
n@49 424
n@57 425 this.loopPlayback = false;
n@57 426
nicholas@1 427 // Create store for new audioObjects
nicholas@1 428 this.audioObjects = [];
nicholas@1 429
n@113 430 this.play = function() {
n@113 431 // Start the timer and set the audioEngine state to playing (1)
n@113 432 if (this.status == 0) {
n@113 433 // Check if all audioObjects are ready
n@113 434 if (this.audioObjectsReady == false) {
n@113 435 this.audioObjectsReady = this.checkAllReady();
n@113 436 }
n@113 437 if (this.audioObjectsReady == true) {
n@113 438 this.timer.startTest();
nicholas@131 439 if (this.loopPlayback) {
nicholas@131 440 for(var i=0; i<this.audioObjects.length; i++) {
nicholas@131 441 this.audioObjects[i].play(this.timer.getTestTime()+1);
nicholas@131 442 }
nicholas@131 443 }
n@113 444 this.status = 1;
n@113 445 }
n@113 446 }
n@113 447 };
nicholas@1 448
n@113 449 this.stop = function() {
n@113 450 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
n@113 451 if (this.status == 1) {
n@113 452 for (var i=0; i<this.audioObjects.length; i++)
n@113 453 {
n@113 454 this.audioObjects[i].stop();
n@113 455 }
n@113 456 this.status = 0;
n@113 457 }
n@113 458 };
nicholas@8 459
nicholas@8 460
nicholas@1 461 this.newTrack = function(url) {
nicholas@1 462 // Pull data from given URL into new audio buffer
nicholas@1 463 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
nicholas@7 464
nicholas@1 465 // Create the audioObject with ID of the new track length;
n@49 466 audioObjectId = this.audioObjects.length;
nicholas@1 467 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
nicholas@7 468
nicholas@7 469 // AudioObject will get track itself.
nicholas@7 470 this.audioObjects[audioObjectId].constructTrack(url);
n@16 471 };
nicholas@1 472
n@113 473 this.newTestPage = function() {
n@113 474 this.state = 0;
n@113 475 this.audioObjectsReady = false;
n@113 476 this.metric.reset();
n@113 477 this.audioObjects = [];
n@113 478 };
n@113 479
nicholas@107 480 this.checkAllPlayed = function() {
nicholas@107 481 arr = [];
nicholas@107 482 for (var id=0; id<this.audioObjects.length; id++) {
nicholas@142 483 if (this.audioObjects[id].metric.wasListenedTo == false) {
nicholas@107 484 arr.push(this.audioObjects[id].id);
nicholas@107 485 }
nicholas@107 486 }
nicholas@107 487 return arr;
nicholas@107 488 };
nicholas@107 489
n@113 490 this.checkAllReady = function() {
n@113 491 var ready = true;
n@113 492 for (var i=0; i<this.audioObjects.length; i++) {
n@113 493 if (this.audioObjects[i].state == 0) {
n@113 494 // Track not ready
n@113 495 console.log('WAIT -- audioObject '+i+' not ready yet!');
n@113 496 ready = false;
n@113 497 };
n@113 498 }
nicholas@135 499 if (ready == false) {
nicholas@135 500 var holder = document.getElementById('testWaitIndicator');
nicholas@135 501 holder.style.visibility = "visible";
nicholas@135 502 setInterval(function() {
nicholas@135 503 document.getElementById('testWaitIndicator').style.visibility = "hidden";
nicholas@135 504 }, 10000);
nicholas@135 505 }
n@113 506 return ready;
n@113 507 };
n@113 508
nicholas@1 509 }
nicholas@1 510
nicholas@1 511 function audioObject(id) {
nicholas@1 512 // The main buffer object with common control nodes to the AudioEngine
nicholas@1 513
nicholas@1 514 this.id = id;
nicholas@1 515 this.state = 0; // 0 - no data, 1 - ready
n@24 516 this.url = null; // Hold the URL given for the output back to the results.
n@139 517 this.metric = new metricTracker(this);
nicholas@1 518
nicholas@1 519 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
n@57 520 this.bufferNode = undefined;
nicholas@1 521 this.outputGain = audioContext.createGain();
nicholas@1 522
nicholas@8 523 // Default output gain to be zero
nicholas@8 524 this.outputGain.gain.value = 0.0;
nicholas@8 525
nicholas@1 526 // Connect buffer to the audio graph
nicholas@1 527 this.outputGain.connect(audioEngineContext.outputGain);
nicholas@1 528
nicholas@1 529 // the audiobuffer is not designed for multi-start playback
nicholas@1 530 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
nicholas@1 531 this.buffer;
b@134 532
nicholas@132 533 this.loopStart = function() {
nicholas@132 534 this.outputGain.gain.value = 1.0;
nicholas@132 535 this.metric.startListening(audioEngineContext.timer.getTestTime());
nicholas@132 536 }
nicholas@132 537
nicholas@132 538 this.loopStop = function() {
nicholas@132 539 if (this.outputGain.gain.value != 0.0) {
nicholas@132 540 this.outputGain.gain.value = 0.0;
nicholas@132 541 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nicholas@132 542 }
nicholas@132 543 }
nicholas@132 544
nicholas@1 545 this.play = function(startTime) {
n@57 546 this.bufferNode = audioContext.createBufferSource();
nicholas@110 547 this.bufferNode.owner = this;
n@57 548 this.bufferNode.connect(this.outputGain);
n@57 549 this.bufferNode.buffer = this.buffer;
n@57 550 this.bufferNode.loop = audioEngineContext.loopPlayback;
nicholas@132 551 this.bufferNode.onended = function() {
nicholas@142 552 // Safari does not like using 'this' to reference the calling object!
nicholas@143 553 event.srcElement.owner.metric.stopListening(audioEngineContext.timer.getTestTime());
nicholas@142 554 };
nicholas@110 555 if (this.bufferNode.loop == false) {
nicholas@132 556 this.metric.startListening(audioEngineContext.timer.getTestTime());
nicholas@110 557 }
nicholas@1 558 this.bufferNode.start(startTime);
n@16 559 };
nicholas@1 560
nicholas@1 561 this.stop = function() {
n@97 562 if (this.bufferNode != undefined)
n@97 563 {
n@97 564 this.bufferNode.stop(0);
n@97 565 this.bufferNode = undefined;
nicholas@132 566 this.metric.stopListening(audioEngineContext.timer.getTestTime());
n@97 567 }
n@16 568 };
nicholas@8 569
nicholas@7 570 this.constructTrack = function(url) {
nicholas@7 571 var request = new XMLHttpRequest();
n@24 572 this.url = url;
nicholas@7 573 request.open('GET',url,true);
nicholas@7 574 request.responseType = 'arraybuffer';
nicholas@7 575
nicholas@7 576 var audioObj = this;
nicholas@7 577
nicholas@7 578 // Create callback to decode the data asynchronously
nicholas@7 579 request.onloadend = function() {
nicholas@7 580 audioContext.decodeAudioData(request.response, function(decodedData) {
nicholas@7 581 audioObj.buffer = decodedData;
nicholas@7 582 audioObj.state = 1;
nicholas@7 583 }, function(){
nicholas@7 584 // Should only be called if there was an error, but sometimes gets called continuously
nicholas@7 585 // Check here if the error is genuine
nicholas@7 586 if (audioObj.state == 0 || audioObj.buffer == undefined) {
nicholas@7 587 // Genuine error
nicholas@7 588 console.log('FATAL - Error loading buffer on '+audioObj.id);
nicholas@7 589 }
nicholas@7 590 });
n@16 591 };
nicholas@7 592 request.send();
n@16 593 };
nicholas@7 594
n@49 595 }
n@49 596
n@49 597 function timer()
n@49 598 {
n@49 599 /* Timer object used in audioEngine to keep track of session timings
n@49 600 * Uses the timer of the web audio API, so sample resolution
n@49 601 */
n@49 602 this.testStarted = false;
n@49 603 this.testStartTime = 0;
n@49 604 this.testDuration = 0;
n@49 605 this.minimumTestTime = 0; // No minimum test time
n@49 606 this.startTest = function()
n@49 607 {
n@49 608 if (this.testStarted == false)
n@49 609 {
n@49 610 this.testStartTime = audioContext.currentTime;
n@49 611 this.testStarted = true;
n@49 612 this.updateTestTime();
n@52 613 audioEngineContext.metric.initialiseTest();
n@49 614 }
n@49 615 };
n@49 616 this.stopTest = function()
n@49 617 {
n@49 618 if (this.testStarted)
n@49 619 {
n@49 620 this.testDuration = this.getTestTime();
n@49 621 this.testStarted = false;
n@49 622 } else {
n@49 623 console.log('ERR: Test tried to end before beginning');
n@49 624 }
n@49 625 };
n@49 626 this.updateTestTime = function()
n@49 627 {
n@49 628 if (this.testStarted)
n@49 629 {
n@49 630 this.testDuration = audioContext.currentTime - this.testStartTime;
n@49 631 }
n@49 632 };
n@49 633 this.getTestTime = function()
n@49 634 {
n@49 635 this.updateTestTime();
n@49 636 return this.testDuration;
n@49 637 };
n@49 638 }
n@49 639
n@49 640 function sessionMetrics(engine)
n@49 641 {
n@49 642 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
n@49 643 */
n@49 644 this.engine = engine;
n@49 645 this.lastClicked = -1;
n@49 646 this.data = -1;
n@113 647 this.reset = function() {
n@113 648 this.lastClicked = -1;
n@113 649 this.data = -1;
n@113 650 };
n@52 651 this.initialiseTest = function(){};
n@49 652 }
n@49 653
n@139 654 function metricTracker(caller)
n@49 655 {
n@49 656 /* Custom object to track and collect metric data
n@49 657 * Used only inside the audioObjects object.
n@49 658 */
n@49 659
n@49 660 this.listenedTimer = 0;
n@49 661 this.listenStart = 0;
nicholas@110 662 this.listenHold = false;
n@51 663 this.initialPosition = -1;
n@49 664 this.movementTracker = [];
n@49 665 this.wasListenedTo = false;
n@49 666 this.wasMoved = false;
n@49 667 this.hasComments = false;
n@139 668 this.parent = caller;
n@49 669
n@49 670 this.initialised = function(position)
n@49 671 {
n@51 672 if (this.initialPosition == -1) {
n@51 673 this.initialPosition = position;
n@51 674 }
n@49 675 };
n@49 676
n@49 677 this.moved = function(time,position)
n@49 678 {
n@49 679 this.wasMoved = true;
n@49 680 this.movementTracker[this.movementTracker.length] = [time, position];
n@49 681 };
n@49 682
nicholas@132 683 this.startListening = function(time)
n@49 684 {
nicholas@110 685 if (this.listenHold == false)
n@49 686 {
n@49 687 this.wasListenedTo = true;
n@49 688 this.listenStart = time;
nicholas@110 689 this.listenHold = true;
n@139 690 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
n@139 691 }
n@139 692 };
nicholas@132 693
nicholas@132 694 this.stopListening = function(time)
nicholas@132 695 {
nicholas@132 696 if (this.listenHold == true)
nicholas@132 697 {
n@49 698 this.listenedTimer += (time - this.listenStart);
n@49 699 this.listenStart = 0;
nicholas@110 700 this.listenHold = false;
n@49 701 }
n@49 702 };
n@54 703 }
n@54 704
n@54 705 function randomiseOrder(input)
n@54 706 {
n@54 707 // This takes an array of information and randomises the order
n@54 708 var N = input.length;
n@54 709 var K = N;
n@54 710 var holdArr = [];
n@54 711 for (var n=0; n<N; n++)
n@54 712 {
n@54 713 // First pick a random number
n@54 714 var r = Math.random();
n@54 715 // Multiply and floor by the number of elements left
n@54 716 r = Math.floor(r*input.length);
n@54 717 // Pick out that element and delete from the array
n@54 718 holdArr.push(input.splice(r,1)[0]);
n@54 719 }
n@54 720 return holdArr;
n@125 721 }
n@125 722
n@125 723 function returnDateNode()
n@125 724 {
n@125 725 // Create an XML Node for the Date and Time a test was conducted
n@125 726 // Structure is
n@125 727 // <datetime>
n@125 728 // <date year="##" month="##" day="##">DD/MM/YY</date>
n@125 729 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
n@125 730 // </datetime>
n@125 731 var dateTime = new Date();
n@125 732 var year = document.createAttribute('year');
n@125 733 var month = document.createAttribute('month');
n@125 734 var day = document.createAttribute('day');
n@125 735 var hour = document.createAttribute('hour');
n@125 736 var minute = document.createAttribute('minute');
n@125 737 var secs = document.createAttribute('secs');
n@125 738
n@125 739 year.nodeValue = dateTime.getFullYear();
n@125 740 month.nodeValue = dateTime.getMonth()+1;
n@125 741 day.nodeValue = dateTime.getDate();
n@125 742 hour.nodeValue = dateTime.getHours();
n@125 743 minute.nodeValue = dateTime.getMinutes();
n@125 744 secs.nodeValue = dateTime.getSeconds();
n@125 745
n@125 746 var hold = document.createElement("datetime");
n@125 747 var date = document.createElement("date");
n@125 748 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
n@125 749 var time = document.createElement("time");
n@125 750 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
n@125 751
n@125 752 date.setAttributeNode(year);
n@125 753 date.setAttributeNode(month);
n@125 754 date.setAttributeNode(day);
n@125 755 time.setAttributeNode(hour);
n@125 756 time.setAttributeNode(minute);
n@125 757 time.setAttributeNode(secs);
n@125 758
n@125 759 hold.appendChild(date);
n@125 760 hold.appendChild(time);
n@125 761 return hold
n@125 762
nicholas@135 763 }
nicholas@135 764
nicholas@135 765 function testWaitIndicator() {
nicholas@135 766 var hold = document.createElement("div");
nicholas@135 767 hold.id = "testWaitIndicator";
nicholas@135 768 hold.style.position = "absolute";
nicholas@135 769 hold.style.left = "100px";
nicholas@135 770 hold.style.top = "10px";
nicholas@135 771 hold.style.width = "500px";
nicholas@135 772 hold.style.height = "100px";
nicholas@135 773 hold.style.padding = "20px";
nicholas@135 774 hold.style.backgroundColor = "rgb(100,200,200)";
nicholas@135 775 hold.style.visibility = "hidden";
nicholas@135 776 var span = document.createElement("span");
nicholas@135 777 span.textContent = "Please wait! Elements still loading";
nicholas@135 778 hold.appendChild(span);
nicholas@135 779 var body = document.getElementsByTagName('body')[0];
nicholas@135 780 body.appendChild(hold);
nicholas@135 781 }
nicholas@135 782
nicholas@135 783 var hidetestwait = null;