annotate core.js @ 921:533d51508e93

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