n@656: /** n@656: * core.js n@656: * n@656: * Main script to run, calls all other core functions and manages loading/store to backend. n@656: * Also contains all global variables. n@656: */ n@656: n@656: n@656: /* n@656: * n@656: * WARNING!!! n@656: * n@656: * YOU ARE VIEWING THE DEV VERSION. THERE IS NO GUARANTEE THIS WILL BE FULLY FUNCTIONAL n@656: * n@656: * WARNING!!! n@656: * n@656: */ n@656: n@656: n@656: n@656: n@656: /* create the web audio API context and store in audioContext*/ n@657: var audioContext; // Hold the browser web audio API n@657: var projectXML; // Hold the parsed setup XML n@662: n@668: var testXMLSetups = []; // Hold the parsed test instances n@668: var testResultsHolders =[]; // Hold the results from each test for publishing to XML n@669: var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order n@670: var currentTestHolder; // Hold any intermediate results during test - metrics n@657: var audioEngineContext; // The custome AudioEngine object n@657: var projectReturn; // Hold the URL for the return n@657: var preTestQuestions = document.createElement('PreTest'); // Store any pre-test question response n@657: var postTestQuestions = document.createElement('PostTest'); // Store any post-test question response n@656: n@656: window.onload = function() { n@656: // Function called once the browser has loaded all files. n@656: // This should perform any initial commands such as structure / loading documents n@656: n@656: // Create a web audio API context n@656: // Fixed for cross-browser support n@656: var AudioContext = window.AudioContext || window.webkitAudioContext; n@656: audioContext = new AudioContext; n@656: n@656: // Create the audio engine object n@656: audioEngineContext = new AudioEngine(); n@656: }; n@656: n@656: function loadProjectSpec(url) { n@656: // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data n@656: // If url is null, request client to upload project XML document n@656: var r = new XMLHttpRequest(); n@656: r.open('GET',url,true); n@656: r.onload = function() { n@656: loadProjectSpecCallback(r.response); n@656: }; n@656: r.send(); n@656: }; n@656: n@656: function loadProjectSpecCallback(response) { n@656: // Function called after asynchronous download of XML project specification n@656: var decode = $.parseXML(response); n@656: projectXML = $(decode); n@656: n@656: // Now extract the setup tag n@656: var xmlSetup = projectXML.find('setup'); n@656: // Detect the interface to use and load the relevant javascripts. n@656: var interfaceType = xmlSetup[0].attributes['interface']; n@656: var interfaceJS = document.createElement('script'); n@656: interfaceJS.setAttribute("type","text/javascript"); n@656: if (interfaceType.value == 'APE') { n@656: interfaceJS.setAttribute("src","ape.js"); n@657: n@657: // APE comes with a css file n@657: var css = document.createElement('link'); n@657: css.rel = 'stylesheet'; n@657: css.type = 'text/css'; n@657: css.href = 'ape.css'; n@657: n@657: document.getElementsByTagName("head")[0].appendChild(css); n@656: } n@656: document.getElementsByTagName("head")[0].appendChild(interfaceJS); n@656: } n@656: n@656: function createProjectSave(destURL) { n@656: // Save the data from interface into XML and send to destURL n@656: // If destURL is null then download XML in client n@656: // Now time to render file locally n@656: var xmlDoc = interfaceXMLSave(); n@656: if (destURL == "null" || destURL == undefined) { n@656: var parent = document.createElement("div"); n@656: parent.appendChild(xmlDoc); n@656: var file = [parent.innerHTML]; n@656: var bb = new Blob(file,{type : 'application/xml'}); n@656: var dnlk = window.URL.createObjectURL(bb); n@656: var a = document.createElement("a"); n@656: a.hidden = ''; n@656: a.href = dnlk; n@656: a.download = "save.xml"; n@656: a.textContent = "Save File"; n@656: n@656: var submitDiv = document.getElementById('download-point'); n@656: submitDiv.appendChild(a); n@656: } n@667: return submitDiv; n@656: } n@656: n@656: function AudioEngine() { n@656: n@656: // Create two output paths, the main outputGain and fooGain. n@656: // Output gain is default to 1 and any items for playback route here n@656: // Foo gain is used for analysis to ensure paths get processed, but are not heard n@656: // because web audio will optimise and any route which does not go to the destination gets ignored. n@656: this.outputGain = audioContext.createGain(); n@656: this.fooGain = audioContext.createGain(); n@656: this.fooGain.gain = 0; n@656: n@656: // Use this to detect playback state: 0 - stopped, 1 - playing n@656: this.status = 0; n@656: n@656: // Connect both gains to output n@656: this.outputGain.connect(audioContext.destination); n@656: this.fooGain.connect(audioContext.destination); n@656: n@673: // Create the timer Object n@673: this.timer = new timer(); n@673: // Create session metrics n@673: this.metric = new sessionMetrics(this); n@673: n@656: // Create store for new audioObjects n@656: this.audioObjects = []; n@656: n@656: this.play = function() { n@656: // Send play command to all playback buffers for synchronised start n@656: // Also start timer callbacks to detect if playback has finished n@656: if (this.status == 0) { n@673: this.timer.startTest(); n@656: // First get current clock n@656: var timer = audioContext.currentTime; n@656: // Add 3 seconds n@656: timer += 3.0; n@656: n@656: // Send play to all tracks n@656: for (var i=0; i= 0) n@673: { n@673: engine.audioObjects[this.lastClicked].metric.listening(time); n@673: } n@673: this.lastClicked = id; n@673: engine.audioObjects[id].metric.listening(time); n@673: } n@673: }; n@673: } n@673: n@673: function metricTracker() n@673: { n@673: /* Custom object to track and collect metric data n@673: * Used only inside the audioObjects object. n@673: */ n@673: n@673: this.listenedTimer = 0; n@673: this.listenStart = 0; n@673: this.initialPosition = 0; n@673: this.movementTracker = []; n@673: this.wasListenedTo = false; n@673: this.wasMoved = false; n@673: this.hasComments = false; n@673: n@673: this.initialised = function(position) n@673: { n@673: this.initialPosition = position; n@673: }; n@673: n@673: this.moved = function(time,position) n@673: { n@673: this.wasMoved = true; n@673: this.movementTracker[this.movementTracker.length] = [time, position]; n@673: }; n@673: n@673: this.listening = function(time) n@673: { n@673: if (this.listenStart == 0) n@673: { n@673: this.wasListenedTo = true; n@673: this.listenStart = time; n@673: } else { n@673: this.listenedTimer += (time - this.listenStart); n@673: this.listenStart = 0; n@673: } n@673: }; n@674: }