nickjillings@1679: /** nickjillings@1679: * core.js nickjillings@1679: * nickjillings@1679: * Main script to run, calls all other core functions and manages loading/store to backend. nickjillings@1679: * Also contains all global variables. nickjillings@1679: */ nickjillings@1679: nickjillings@1679: /* create the web audio API context and store in audioContext*/ nickjillings@1679: var audioContext; // Hold the browser web audio API nickjillings@1679: var projectXML; // Hold the parsed setup XML nickjillings@1679: nickjillings@1679: var testXMLSetups = []; // Hold the parsed test instances nickjillings@1679: var testResultsHolders =[]; // Hold the results from each test for publishing to XML nickjillings@1679: var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order nickjillings@1679: var currentTestHolder; // Hold any intermediate results during test - metrics nickjillings@1679: var audioEngineContext; // The custome AudioEngine object nickjillings@1679: var projectReturn; // Hold the URL for the return nickjillings@1679: var preTestQuestions = document.createElement('PreTest'); // Store any pre-test question response nickjillings@1679: var postTestQuestions = document.createElement('PostTest'); // Store any post-test question response nickjillings@1679: nickjillings@1679: // Add a prototype to the bufferSourceNode to reference to the audioObject holding it nickjillings@1679: AudioBufferSourceNode.prototype.owner = undefined; nickjillings@1679: nickjillings@1679: window.onload = function() { nickjillings@1679: // Function called once the browser has loaded all files. nickjillings@1679: // This should perform any initial commands such as structure / loading documents nickjillings@1679: nickjillings@1679: // Create a web audio API context nickjillings@1679: // Fixed for cross-browser support nickjillings@1679: var AudioContext = window.AudioContext || window.webkitAudioContext; nickjillings@1679: audioContext = new AudioContext; nickjillings@1679: nickjillings@1679: // Create the audio engine object nickjillings@1679: audioEngineContext = new AudioEngine(); nickjillings@1679: }; nickjillings@1679: nickjillings@1679: function loadProjectSpec(url) { nickjillings@1679: // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data nickjillings@1679: // If url is null, request client to upload project XML document nickjillings@1679: var r = new XMLHttpRequest(); nickjillings@1679: r.open('GET',url,true); nickjillings@1679: r.onload = function() { nickjillings@1679: loadProjectSpecCallback(r.response); nickjillings@1679: }; nickjillings@1679: r.send(); nickjillings@1679: }; nickjillings@1679: nickjillings@1679: function loadProjectSpecCallback(response) { nickjillings@1679: // Function called after asynchronous download of XML project specification nickjillings@1679: var decode = $.parseXML(response); nickjillings@1679: projectXML = $(decode); nickjillings@1679: nickjillings@1679: // Now extract the setup tag nickjillings@1679: var xmlSetup = projectXML.find('setup'); nickjillings@1679: // Detect the interface to use and load the relevant javascripts. nickjillings@1679: var interfaceType = xmlSetup[0].attributes['interface']; nickjillings@1679: var interfaceJS = document.createElement('script'); nickjillings@1679: interfaceJS.setAttribute("type","text/javascript"); nickjillings@1679: if (interfaceType.value == 'APE') { nickjillings@1679: interfaceJS.setAttribute("src","ape.js"); nickjillings@1679: nickjillings@1679: // APE comes with a css file nickjillings@1679: var css = document.createElement('link'); nickjillings@1679: css.rel = 'stylesheet'; nickjillings@1679: css.type = 'text/css'; nickjillings@1679: css.href = 'ape.css'; nickjillings@1679: nickjillings@1679: document.getElementsByTagName("head")[0].appendChild(css); nickjillings@1679: } nickjillings@1679: document.getElementsByTagName("head")[0].appendChild(interfaceJS); nickjillings@1679: } nickjillings@1679: nickjillings@1679: function createProjectSave(destURL) { nickjillings@1679: // Save the data from interface into XML and send to destURL nickjillings@1679: // If destURL is null then download XML in client nickjillings@1679: // Now time to render file locally nickjillings@1679: var xmlDoc = interfaceXMLSave(); nickjillings@1679: if (destURL == "null" || destURL == undefined) { nickjillings@1679: var parent = document.createElement("div"); nickjillings@1679: parent.appendChild(xmlDoc); nickjillings@1679: var file = [parent.innerHTML]; nickjillings@1679: var bb = new Blob(file,{type : 'application/xml'}); nickjillings@1679: var dnlk = window.URL.createObjectURL(bb); nickjillings@1679: var a = document.createElement("a"); nickjillings@1679: a.hidden = ''; nickjillings@1679: a.href = dnlk; nickjillings@1679: a.download = "save.xml"; nickjillings@1679: a.textContent = "Save File"; nickjillings@1679: nickjillings@1679: var submitDiv = document.getElementById('download-point'); nickjillings@1679: submitDiv.appendChild(a); nickjillings@1679: } nickjillings@1679: return submitDiv; nickjillings@1679: } nickjillings@1679: nickjillings@1679: function AudioEngine() { nickjillings@1679: nickjillings@1679: // Create two output paths, the main outputGain and fooGain. nickjillings@1679: // Output gain is default to 1 and any items for playback route here nickjillings@1679: // Foo gain is used for analysis to ensure paths get processed, but are not heard nickjillings@1679: // because web audio will optimise and any route which does not go to the destination gets ignored. nickjillings@1679: this.outputGain = audioContext.createGain(); nickjillings@1679: this.fooGain = audioContext.createGain(); nickjillings@1679: this.fooGain.gain = 0; nickjillings@1679: nickjillings@1679: // Use this to detect playback state: 0 - stopped, 1 - playing nickjillings@1679: this.status = 0; nickjillings@1679: nickjillings@1679: // Connect both gains to output nickjillings@1679: this.outputGain.connect(audioContext.destination); nickjillings@1679: this.fooGain.connect(audioContext.destination); nickjillings@1679: nickjillings@1679: // Create the timer Object nickjillings@1679: this.timer = new timer(); nickjillings@1679: // Create session metrics nickjillings@1679: this.metric = new sessionMetrics(this); nickjillings@1679: nickjillings@1679: this.loopPlayback = false; nickjillings@1679: nickjillings@1679: // Create store for new audioObjects nickjillings@1679: this.audioObjects = []; nickjillings@1679: nickjillings@1679: this.play = function(){}; nickjillings@1679: nickjillings@1679: this.stop = function(){}; nickjillings@1679: nickjillings@1679: nickjillings@1679: this.newTrack = function(url) { nickjillings@1679: // Pull data from given URL into new audio buffer nickjillings@1679: // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin' nickjillings@1679: nickjillings@1679: // Create the audioObject with ID of the new track length; nickjillings@1679: audioObjectId = this.audioObjects.length; nickjillings@1679: this.audioObjects[audioObjectId] = new audioObject(audioObjectId); nickjillings@1679: nickjillings@1679: // AudioObject will get track itself. nickjillings@1679: this.audioObjects[audioObjectId].constructTrack(url); nickjillings@1679: }; nickjillings@1679: nickjillings@1679: } nickjillings@1679: nickjillings@1679: function audioObject(id) { nickjillings@1679: // The main buffer object with common control nodes to the AudioEngine nickjillings@1679: nickjillings@1679: this.id = id; nickjillings@1679: this.state = 0; // 0 - no data, 1 - ready nickjillings@1679: this.url = null; // Hold the URL given for the output back to the results. nickjillings@1679: this.metric = new metricTracker(); nickjillings@1679: nickjillings@1679: // Create a buffer and external gain control to allow internal patching of effects and volume leveling. nickjillings@1679: this.bufferNode = undefined; nickjillings@1679: this.outputGain = audioContext.createGain(); nickjillings@1679: nickjillings@1679: // Default output gain to be zero nickjillings@1679: this.outputGain.gain.value = 0.0; nickjillings@1679: nickjillings@1679: // Connect buffer to the audio graph nickjillings@1679: this.outputGain.connect(audioEngineContext.outputGain); nickjillings@1679: nickjillings@1679: // the audiobuffer is not designed for multi-start playback nickjillings@1679: // When stopeed, the buffer node is deleted and recreated with the stored buffer. nickjillings@1679: this.buffer; nickjillings@1679: nickjillings@1679: this.play = function(startTime) { nickjillings@1679: this.bufferNode = audioContext.createBufferSource(); nickjillings@1679: this.bufferNode.connect(this.outputGain); nickjillings@1679: this.bufferNode.buffer = this.buffer; nickjillings@1679: this.bufferNode.loop = audioEngineContext.loopPlayback; nickjillings@1679: this.bufferNode.start(startTime); nickjillings@1679: }; nickjillings@1679: nickjillings@1679: this.stop = function() { nickjillings@1679: this.bufferNode.stop(0); nickjillings@1679: this.bufferNode = undefined; nickjillings@1679: }; nickjillings@1679: nickjillings@1679: this.constructTrack = function(url) { nickjillings@1679: var request = new XMLHttpRequest(); nickjillings@1679: this.url = url; nickjillings@1679: request.open('GET',url,true); nickjillings@1679: request.responseType = 'arraybuffer'; nickjillings@1679: nickjillings@1679: var audioObj = this; nickjillings@1679: nickjillings@1679: // Create callback to decode the data asynchronously nickjillings@1679: request.onloadend = function() { nickjillings@1679: audioContext.decodeAudioData(request.response, function(decodedData) { nickjillings@1679: audioObj.buffer = decodedData; nickjillings@1679: audioObj.state = 1; nickjillings@1679: }, function(){ nickjillings@1679: // Should only be called if there was an error, but sometimes gets called continuously nickjillings@1679: // Check here if the error is genuine nickjillings@1679: if (audioObj.state == 0 || audioObj.buffer == undefined) { nickjillings@1679: // Genuine error nickjillings@1679: console.log('FATAL - Error loading buffer on '+audioObj.id); nickjillings@1679: } nickjillings@1679: }); nickjillings@1679: }; nickjillings@1679: request.send(); nickjillings@1679: }; nickjillings@1679: nickjillings@1679: } nickjillings@1679: nickjillings@1679: function timer() nickjillings@1679: { nickjillings@1679: /* Timer object used in audioEngine to keep track of session timings nickjillings@1679: * Uses the timer of the web audio API, so sample resolution nickjillings@1679: */ nickjillings@1679: this.testStarted = false; nickjillings@1679: this.testStartTime = 0; nickjillings@1679: this.testDuration = 0; nickjillings@1679: this.minimumTestTime = 0; // No minimum test time nickjillings@1679: this.startTest = function() nickjillings@1679: { nickjillings@1679: if (this.testStarted == false) nickjillings@1679: { nickjillings@1679: this.testStartTime = audioContext.currentTime; nickjillings@1679: this.testStarted = true; nickjillings@1679: this.updateTestTime(); nickjillings@1679: audioEngineContext.metric.initialiseTest(); nickjillings@1679: } nickjillings@1679: }; nickjillings@1679: this.stopTest = function() nickjillings@1679: { nickjillings@1679: if (this.testStarted) nickjillings@1679: { nickjillings@1679: this.testDuration = this.getTestTime(); nickjillings@1679: this.testStarted = false; nickjillings@1679: } else { nickjillings@1679: console.log('ERR: Test tried to end before beginning'); nickjillings@1679: } nickjillings@1679: }; nickjillings@1679: this.updateTestTime = function() nickjillings@1679: { nickjillings@1679: if (this.testStarted) nickjillings@1679: { nickjillings@1679: this.testDuration = audioContext.currentTime - this.testStartTime; nickjillings@1679: } nickjillings@1679: }; nickjillings@1679: this.getTestTime = function() nickjillings@1679: { nickjillings@1679: this.updateTestTime(); nickjillings@1679: return this.testDuration; nickjillings@1679: }; nickjillings@1679: } nickjillings@1679: nickjillings@1679: function sessionMetrics(engine) nickjillings@1679: { nickjillings@1679: /* Used by audioEngine to link to audioObjects to minimise the timer call timers; nickjillings@1679: */ nickjillings@1679: this.engine = engine; nickjillings@1679: this.lastClicked = -1; nickjillings@1679: this.data = -1; nickjillings@1679: this.initialiseTest = function(){}; nickjillings@1679: } nickjillings@1679: nickjillings@1679: function metricTracker() nickjillings@1679: { nickjillings@1679: /* Custom object to track and collect metric data nickjillings@1679: * Used only inside the audioObjects object. nickjillings@1679: */ nickjillings@1679: nickjillings@1679: this.listenedTimer = 0; nickjillings@1679: this.listenStart = 0; nickjillings@1679: this.initialPosition = -1; nickjillings@1679: this.movementTracker = []; nickjillings@1679: this.wasListenedTo = false; nickjillings@1679: this.wasMoved = false; nickjillings@1679: this.hasComments = false; nickjillings@1679: nickjillings@1679: this.initialised = function(position) nickjillings@1679: { nickjillings@1679: if (this.initialPosition == -1) { nickjillings@1679: this.initialPosition = position; nickjillings@1679: } nickjillings@1679: }; nickjillings@1679: nickjillings@1679: this.moved = function(time,position) nickjillings@1679: { nickjillings@1679: this.wasMoved = true; nickjillings@1679: this.movementTracker[this.movementTracker.length] = [time, position]; nickjillings@1679: }; nickjillings@1679: nickjillings@1679: this.listening = function(time) nickjillings@1679: { nickjillings@1679: if (this.listenStart == 0) nickjillings@1679: { nickjillings@1679: this.wasListenedTo = true; nickjillings@1679: this.listenStart = time; nickjillings@1679: } else { nickjillings@1679: this.listenedTimer += (time - this.listenStart); nickjillings@1679: this.listenStart = 0; nickjillings@1679: } nickjillings@1679: }; nickjillings@1679: } nickjillings@1679: nickjillings@1679: function randomiseOrder(input) nickjillings@1679: { nickjillings@1679: // This takes an array of information and randomises the order nickjillings@1679: var N = input.length; nickjillings@1679: var K = N; nickjillings@1679: var holdArr = []; nickjillings@1679: for (var n=0; n