me@1972: /** me@1972: * core.js me@1972: * me@1972: * Main script to run, calls all other core functions and manages loading/store to backend. me@1972: * Also contains all global variables. me@1972: */ me@1972: me@1972: /* create the web audio API context and store in audioContext*/ me@1972: var audioContext; me@1972: var projectXML; me@1972: var audioEngineContext; me@1972: var projectReturn; me@1973: var preTestQuestions = document.createElement('PreTest'); me@1973: var postTestQuestions = document.createElement('PostTest'); me@1972: me@1972: window.onload = function() { me@1972: // Function called once the browser has loaded all files. me@1972: // This should perform any initial commands such as structure / loading documents me@1972: me@1972: // Create a web audio API context me@1973: // Fixed for cross-browser support me@1973: var AudioContext = window.AudioContext || window.webkitAudioContext; me@1972: audioContext = new AudioContext; me@1972: me@1972: // Create the audio engine object me@1972: audioEngineContext = new AudioEngine(); me@1972: }; me@1972: me@1972: function loadProjectSpec(url) { me@1972: // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data me@1972: // If url is null, request client to upload project XML document me@1972: var r = new XMLHttpRequest(); me@1972: r.open('GET',url,true); me@1972: r.onload = function() { me@1972: loadProjectSpecCallback(r.response); me@1972: }; me@1972: r.send(); me@1972: }; me@1972: me@1972: function loadProjectSpecCallback(response) { me@1972: // Function called after asynchronous download of XML project specification me@1972: var decode = $.parseXML(response); me@1972: projectXML = $(decode); me@1972: me@1972: // Now extract the setup tag me@1972: var xmlSetup = projectXML.find('setup'); me@1972: // Detect the interface to use and load the relevant javascripts. me@1972: var interfaceType = xmlSetup[0].attributes['interface']; me@1972: var interfaceJS = document.createElement('script'); me@1972: interfaceJS.setAttribute("type","text/javascript"); me@1972: if (interfaceType.value == 'APE') { me@1972: interfaceJS.setAttribute("src","ape.js"); me@1972: } me@1972: document.getElementsByTagName("head")[0].appendChild(interfaceJS); me@1972: } me@1972: me@1972: function createProjectSave(destURL) { me@1972: // Save the data from interface into XML and send to destURL me@1972: // If destURL is null then download XML in client me@1972: // Now time to render file locally me@1972: var xmlDoc = interfaceXMLSave(); me@1972: if (destURL == "null" || destURL == undefined) { me@1972: var parent = document.createElement("div"); me@1972: parent.appendChild(xmlDoc); me@1972: var file = [parent.innerHTML]; me@1972: var bb = new Blob(file,{type : 'application/xml'}); me@1972: var dnlk = window.URL.createObjectURL(bb); me@1972: var a = document.createElement("a"); me@1972: a.hidden = ''; me@1972: a.href = dnlk; me@1972: a.download = "save.xml"; me@1972: a.textContent = "Save File"; me@1972: me@1972: var submitDiv = document.getElementById('download-point'); me@1972: submitDiv.appendChild(a); me@1972: } me@1972: } me@1972: me@1972: function AudioEngine() { me@1972: me@1972: // Create two output paths, the main outputGain and fooGain. me@1972: // Output gain is default to 1 and any items for playback route here me@1972: // Foo gain is used for analysis to ensure paths get processed, but are not heard me@1972: // because web audio will optimise and any route which does not go to the destination gets ignored. me@1972: this.outputGain = audioContext.createGain(); me@1972: this.fooGain = audioContext.createGain(); me@1972: this.fooGain.gain = 0; me@1972: me@1972: // Use this to detect playback state: 0 - stopped, 1 - playing me@1972: this.status = 0; me@1972: me@1972: // Connect both gains to output me@1972: this.outputGain.connect(audioContext.destination); me@1972: this.fooGain.connect(audioContext.destination); me@1972: me@1972: // Create store for new audioObjects me@1972: this.audioObjects = []; me@1972: me@1972: this.play = function() { me@1972: // Send play command to all playback buffers for synchronised start me@1972: // Also start timer callbacks to detect if playback has finished me@1972: if (this.status == 0) { me@1972: // First get current clock me@1972: var timer = audioContext.currentTime; me@1972: // Add 3 seconds me@1972: timer += 3.0; me@1972: me@1972: // Send play to all tracks me@1972: for (var i=0; i