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*/ n@33: var audioContext; // Hold the browser web audio API n@33: var projectXML; // Hold the parsed setup XML n@38: n@44: var testXMLSetups = []; // Hold the parsed test instances n@44: var testResultsHolders =[]; // Hold the results from each test for publishing to XML n@45: var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order n@46: var currentTestHolder; // Hold any intermediate results during test - metrics n@33: var audioEngineContext; // The custome AudioEngine object n@33: var projectReturn; // Hold the URL for the return n@33: var preTestQuestions = document.createElement('PreTest'); // Store any pre-test question response n@33: var postTestQuestions = document.createElement('PostTest'); // Store any post-test question response nicholas@1: n@57: // Add a prototype to the bufferSourceNode to reference to the audioObject holding it n@57: AudioBufferSourceNode.prototype.owner = undefined; n@57: 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@114: function createPopup() { nicholas@114: // Create popup window interface nicholas@114: var insertPoint = document.getElementById("topLevelBody"); nicholas@114: var blank = document.createElement('div'); nicholas@114: blank.className = 'testHalt'; nicholas@114: nicholas@114: var popupHolder = document.createElement('div'); nicholas@114: popupHolder.id = 'popupHolder'; nicholas@114: popupHolder.className = 'popupHolder'; nicholas@114: popupHolder.style.position = 'absolute'; nicholas@114: popupHolder.style.left = (window.innerWidth/2)-250 + 'px'; nicholas@114: popupHolder.style.top = (window.innerHeight/2)-125 + 'px'; nicholas@114: insertPoint.appendChild(popupHolder); nicholas@114: insertPoint.appendChild(blank); nicholas@114: } nicholas@114: nicholas@114: function showPopup() nicholas@114: { nicholas@114: var popupHolder = document.getElementById('popupHolder'); nicholas@114: if (popupHolder == null || popupHolder == undefined) { nicholas@114: createPopup(); nicholas@114: popupHolder = document.getElementById('popupHolder'); nicholas@114: } nicholas@114: popupHolder.style.zIndex = 3; nicholas@114: popupHolder.style.visibility = 'visible'; nicholas@114: var blank = document.getElementsByClassName('testHalt')[0]; nicholas@114: blank.style.zIndex = 2; nicholas@114: blank.style.visibility = 'visible'; nicholas@114: } nicholas@114: nicholas@114: function hidePopup() nicholas@114: { nicholas@114: var popupHolder = document.getElementById('popupHolder'); nicholas@114: popupHolder.style.zIndex = -1; nicholas@114: popupHolder.style.visibility = 'hidden'; nicholas@114: var blank = document.getElementsByClassName('testHalt')[0]; nicholas@114: blank.style.zIndex = -2; nicholas@114: blank.style.visibility = 'hidden'; nicholas@114: } nicholas@114: 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"); n@33: n@33: // APE comes with a css file n@33: var css = document.createElement('link'); n@33: css.rel = 'stylesheet'; n@33: css.type = 'text/css'; n@33: css.href = 'ape.css'; n@33: n@33: document.getElementsByTagName("head")[0].appendChild(css); 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: } n@43: return submitDiv; 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; n@113: this.audioObjectsReady = false; 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: n@49: // Create the timer Object n@49: this.timer = new timer(); n@49: // Create session metrics n@49: this.metric = new sessionMetrics(this); n@49: n@57: this.loopPlayback = false; n@57: nicholas@1: // Create store for new audioObjects nicholas@1: this.audioObjects = []; nicholas@1: n@113: this.play = function() { n@113: // Start the timer and set the audioEngine state to playing (1) n@113: if (this.status == 0) { n@113: // Check if all audioObjects are ready n@113: if (this.audioObjectsReady == false) { n@113: this.audioObjectsReady = this.checkAllReady(); n@113: } n@113: if (this.audioObjectsReady == true) { n@113: this.timer.startTest(); n@113: this.status = 1; n@113: } n@113: } n@113: }; nicholas@1: n@113: this.stop = function() { n@113: // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1) n@113: if (this.status == 1) { n@113: for (var i=0; i