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