BrechtDeMan@938: /** BrechtDeMan@938: * core.js BrechtDeMan@938: * BrechtDeMan@938: * Main script to run, calls all other core functions and manages loading/store to backend. BrechtDeMan@938: * Also contains all global variables. BrechtDeMan@938: */ BrechtDeMan@938: BrechtDeMan@938: /* create the web audio API context and store in audioContext*/ BrechtDeMan@938: var audioContext; // Hold the browser web audio API BrechtDeMan@938: var projectXML; // Hold the parsed setup XML BrechtDeMan@938: BrechtDeMan@938: var testXMLSetups = []; // Hold the parsed test instances BrechtDeMan@938: var testResultsHolders =[]; // Hold the results from each test for publishing to XML BrechtDeMan@938: var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order BrechtDeMan@938: var currentTestHolder; // Hold any intermediate results during test - metrics BrechtDeMan@938: var audioEngineContext; // The custome AudioEngine object BrechtDeMan@938: var projectReturn; // Hold the URL for the return BrechtDeMan@938: var preTestQuestions = document.createElement('PreTest'); // Store any pre-test question response BrechtDeMan@938: var postTestQuestions = document.createElement('PostTest'); // Store any post-test question response BrechtDeMan@938: BrechtDeMan@938: // Add a prototype to the bufferSourceNode to reference to the audioObject holding it BrechtDeMan@938: AudioBufferSourceNode.prototype.owner = undefined; BrechtDeMan@938: BrechtDeMan@938: window.onload = function() { BrechtDeMan@938: // Function called once the browser has loaded all files. BrechtDeMan@938: // This should perform any initial commands such as structure / loading documents BrechtDeMan@938: BrechtDeMan@938: // Create a web audio API context BrechtDeMan@938: // Fixed for cross-browser support BrechtDeMan@938: var AudioContext = window.AudioContext || window.webkitAudioContext; BrechtDeMan@938: audioContext = new AudioContext; BrechtDeMan@938: BrechtDeMan@938: // Create the audio engine object BrechtDeMan@938: audioEngineContext = new AudioEngine(); BrechtDeMan@938: }; BrechtDeMan@938: BrechtDeMan@938: function loadProjectSpec(url) { BrechtDeMan@938: // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data BrechtDeMan@938: // If url is null, request client to upload project XML document BrechtDeMan@938: var r = new XMLHttpRequest(); BrechtDeMan@938: r.open('GET',url,true); BrechtDeMan@938: r.onload = function() { BrechtDeMan@938: loadProjectSpecCallback(r.response); BrechtDeMan@938: }; BrechtDeMan@938: r.send(); BrechtDeMan@938: }; BrechtDeMan@938: BrechtDeMan@938: function loadProjectSpecCallback(response) { BrechtDeMan@938: // Function called after asynchronous download of XML project specification BrechtDeMan@938: var decode = $.parseXML(response); BrechtDeMan@938: projectXML = $(decode); BrechtDeMan@938: BrechtDeMan@938: // Now extract the setup tag BrechtDeMan@938: var xmlSetup = projectXML.find('setup'); BrechtDeMan@938: // Detect the interface to use and load the relevant javascripts. BrechtDeMan@938: var interfaceType = xmlSetup[0].attributes['interface']; BrechtDeMan@938: var interfaceJS = document.createElement('script'); BrechtDeMan@938: interfaceJS.setAttribute("type","text/javascript"); BrechtDeMan@938: if (interfaceType.value == 'APE') { BrechtDeMan@938: interfaceJS.setAttribute("src","ape.js"); BrechtDeMan@938: BrechtDeMan@938: // APE comes with a css file BrechtDeMan@938: var css = document.createElement('link'); BrechtDeMan@938: css.rel = 'stylesheet'; BrechtDeMan@938: css.type = 'text/css'; BrechtDeMan@938: css.href = 'ape.css'; BrechtDeMan@938: BrechtDeMan@938: document.getElementsByTagName("head")[0].appendChild(css); BrechtDeMan@938: } BrechtDeMan@938: document.getElementsByTagName("head")[0].appendChild(interfaceJS); BrechtDeMan@938: } BrechtDeMan@938: BrechtDeMan@938: function createProjectSave(destURL) { BrechtDeMan@938: // Save the data from interface into XML and send to destURL BrechtDeMan@938: // If destURL is null then download XML in client BrechtDeMan@938: // Now time to render file locally BrechtDeMan@938: var xmlDoc = interfaceXMLSave(); BrechtDeMan@938: if (destURL == "null" || destURL == undefined) { BrechtDeMan@938: var parent = document.createElement("div"); BrechtDeMan@938: parent.appendChild(xmlDoc); BrechtDeMan@938: var file = [parent.innerHTML]; BrechtDeMan@938: var bb = new Blob(file,{type : 'application/xml'}); BrechtDeMan@938: var dnlk = window.URL.createObjectURL(bb); BrechtDeMan@938: var a = document.createElement("a"); BrechtDeMan@938: a.hidden = ''; BrechtDeMan@938: a.href = dnlk; BrechtDeMan@938: a.download = "save.xml"; BrechtDeMan@938: a.textContent = "Save File"; BrechtDeMan@938: BrechtDeMan@938: var submitDiv = document.getElementById('download-point'); BrechtDeMan@938: submitDiv.appendChild(a); BrechtDeMan@938: } BrechtDeMan@938: return submitDiv; BrechtDeMan@938: } BrechtDeMan@938: BrechtDeMan@938: function AudioEngine() { BrechtDeMan@938: BrechtDeMan@938: // Create two output paths, the main outputGain and fooGain. BrechtDeMan@938: // Output gain is default to 1 and any items for playback route here BrechtDeMan@938: // Foo gain is used for analysis to ensure paths get processed, but are not heard BrechtDeMan@938: // because web audio will optimise and any route which does not go to the destination gets ignored. BrechtDeMan@938: this.outputGain = audioContext.createGain(); BrechtDeMan@938: this.fooGain = audioContext.createGain(); BrechtDeMan@938: this.fooGain.gain = 0; BrechtDeMan@938: BrechtDeMan@938: // Use this to detect playback state: 0 - stopped, 1 - playing BrechtDeMan@938: this.status = 0; BrechtDeMan@938: BrechtDeMan@938: // Connect both gains to output BrechtDeMan@938: this.outputGain.connect(audioContext.destination); BrechtDeMan@938: this.fooGain.connect(audioContext.destination); BrechtDeMan@938: BrechtDeMan@938: // Create the timer Object BrechtDeMan@938: this.timer = new timer(); BrechtDeMan@938: // Create session metrics BrechtDeMan@938: this.metric = new sessionMetrics(this); BrechtDeMan@938: BrechtDeMan@938: this.loopPlayback = false; BrechtDeMan@938: BrechtDeMan@938: // Create store for new audioObjects BrechtDeMan@938: this.audioObjects = []; BrechtDeMan@938: BrechtDeMan@938: this.play = function(){}; BrechtDeMan@938: BrechtDeMan@938: this.stop = function(){}; BrechtDeMan@938: BrechtDeMan@938: BrechtDeMan@938: this.newTrack = function(url) { BrechtDeMan@938: // Pull data from given URL into new audio buffer BrechtDeMan@938: // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin' BrechtDeMan@938: BrechtDeMan@938: // Create the audioObject with ID of the new track length; BrechtDeMan@938: audioObjectId = this.audioObjects.length; BrechtDeMan@938: this.audioObjects[audioObjectId] = new audioObject(audioObjectId); BrechtDeMan@938: BrechtDeMan@938: // AudioObject will get track itself. BrechtDeMan@938: this.audioObjects[audioObjectId].constructTrack(url); BrechtDeMan@938: }; BrechtDeMan@938: nicholas@944: this.checkAllPlayed = function() { nicholas@944: arr = []; nicholas@944: for (var id=0; id