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