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