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