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