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