annotate core.js @ 1653:8ba09a517300

Completed more pre-test/post-test popup functions. Presents save and locks test on completion
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Fri, 10 Apr 2015 15:30:44 +0100
parents dc2969dbe0a2
children 6455bee85090
rev   line source
nickjillings@1642 1 /**
nickjillings@1642 2 * core.js
nickjillings@1642 3 *
nickjillings@1642 4 * Main script to run, calls all other core functions and manages loading/store to backend.
nickjillings@1642 5 * Also contains all global variables.
nickjillings@1642 6 */
nickjillings@1642 7
nickjillings@1642 8
nickjillings@1642 9 /*
nickjillings@1642 10 *
nickjillings@1642 11 * WARNING!!!
nickjillings@1642 12 *
nickjillings@1642 13 * YOU ARE VIEWING THE DEV VERSION. THERE IS NO GUARANTEE THIS WILL BE FULLY FUNCTIONAL
nickjillings@1642 14 *
nickjillings@1642 15 * WARNING!!!
nickjillings@1642 16 *
nickjillings@1642 17 */
nickjillings@1642 18
nickjillings@1642 19
nickjillings@1642 20
nickjillings@1642 21
nickjillings@1642 22 /* create the web audio API context and store in audioContext*/
nickjillings@1643 23 var audioContext; // Hold the browser web audio API
nickjillings@1643 24 var projectXML; // Hold the parsed setup XML
nickjillings@1648 25
nickjillings@1643 26 var testXMLSetups; // Hold the parsed test instances
nickjillings@1643 27 var testResultsHolders; // Hold the results from each test for publishing to XML
nickjillings@1643 28 var currentTestHolder; // Hold an intermediate results during test - metrics
nickjillings@1643 29 var audioEngineContext; // The custome AudioEngine object
nickjillings@1643 30 var projectReturn; // Hold the URL for the return
nickjillings@1643 31 var preTestQuestions = document.createElement('PreTest'); // Store any pre-test question response
nickjillings@1643 32 var postTestQuestions = document.createElement('PostTest'); // Store any post-test question response
nickjillings@1642 33
nickjillings@1642 34 window.onload = function() {
nickjillings@1642 35 // Function called once the browser has loaded all files.
nickjillings@1642 36 // This should perform any initial commands such as structure / loading documents
nickjillings@1642 37
nickjillings@1642 38 // Create a web audio API context
nickjillings@1642 39 // Fixed for cross-browser support
nickjillings@1642 40 var AudioContext = window.AudioContext || window.webkitAudioContext;
nickjillings@1642 41 audioContext = new AudioContext;
nickjillings@1642 42
nickjillings@1642 43 // Create the audio engine object
nickjillings@1642 44 audioEngineContext = new AudioEngine();
nickjillings@1642 45 };
nickjillings@1642 46
nickjillings@1642 47 function loadProjectSpec(url) {
nickjillings@1642 48 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
nickjillings@1642 49 // If url is null, request client to upload project XML document
nickjillings@1642 50 var r = new XMLHttpRequest();
nickjillings@1642 51 r.open('GET',url,true);
nickjillings@1642 52 r.onload = function() {
nickjillings@1642 53 loadProjectSpecCallback(r.response);
nickjillings@1642 54 };
nickjillings@1642 55 r.send();
nickjillings@1642 56 };
nickjillings@1642 57
nickjillings@1642 58 function loadProjectSpecCallback(response) {
nickjillings@1642 59 // Function called after asynchronous download of XML project specification
nickjillings@1642 60 var decode = $.parseXML(response);
nickjillings@1642 61 projectXML = $(decode);
nickjillings@1642 62
nickjillings@1642 63 // Now extract the setup tag
nickjillings@1642 64 var xmlSetup = projectXML.find('setup');
nickjillings@1642 65 // Detect the interface to use and load the relevant javascripts.
nickjillings@1642 66 var interfaceType = xmlSetup[0].attributes['interface'];
nickjillings@1642 67 var interfaceJS = document.createElement('script');
nickjillings@1642 68 interfaceJS.setAttribute("type","text/javascript");
nickjillings@1642 69 if (interfaceType.value == 'APE') {
nickjillings@1642 70 interfaceJS.setAttribute("src","ape.js");
nickjillings@1643 71
nickjillings@1643 72 // APE comes with a css file
nickjillings@1643 73 var css = document.createElement('link');
nickjillings@1643 74 css.rel = 'stylesheet';
nickjillings@1643 75 css.type = 'text/css';
nickjillings@1643 76 css.href = 'ape.css';
nickjillings@1643 77
nickjillings@1643 78 document.getElementsByTagName("head")[0].appendChild(css);
nickjillings@1642 79 }
nickjillings@1642 80 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
nickjillings@1642 81 }
nickjillings@1642 82
nickjillings@1642 83 function createProjectSave(destURL) {
nickjillings@1642 84 // Save the data from interface into XML and send to destURL
nickjillings@1642 85 // If destURL is null then download XML in client
nickjillings@1642 86 // Now time to render file locally
nickjillings@1642 87 var xmlDoc = interfaceXMLSave();
nickjillings@1642 88 if (destURL == "null" || destURL == undefined) {
nickjillings@1642 89 var parent = document.createElement("div");
nickjillings@1642 90 parent.appendChild(xmlDoc);
nickjillings@1642 91 var file = [parent.innerHTML];
nickjillings@1642 92 var bb = new Blob(file,{type : 'application/xml'});
nickjillings@1642 93 var dnlk = window.URL.createObjectURL(bb);
nickjillings@1642 94 var a = document.createElement("a");
nickjillings@1642 95 a.hidden = '';
nickjillings@1642 96 a.href = dnlk;
nickjillings@1642 97 a.download = "save.xml";
nickjillings@1642 98 a.textContent = "Save File";
nickjillings@1642 99
nickjillings@1642 100 var submitDiv = document.getElementById('download-point');
nickjillings@1642 101 submitDiv.appendChild(a);
nickjillings@1642 102 }
nickjillings@1653 103 return submitDiv;
nickjillings@1642 104 }
nickjillings@1642 105
nickjillings@1642 106 function AudioEngine() {
nickjillings@1642 107
nickjillings@1642 108 // Create two output paths, the main outputGain and fooGain.
nickjillings@1642 109 // Output gain is default to 1 and any items for playback route here
nickjillings@1642 110 // Foo gain is used for analysis to ensure paths get processed, but are not heard
nickjillings@1642 111 // because web audio will optimise and any route which does not go to the destination gets ignored.
nickjillings@1642 112 this.outputGain = audioContext.createGain();
nickjillings@1642 113 this.fooGain = audioContext.createGain();
nickjillings@1642 114 this.fooGain.gain = 0;
nickjillings@1642 115
nickjillings@1642 116 // Use this to detect playback state: 0 - stopped, 1 - playing
nickjillings@1642 117 this.status = 0;
nickjillings@1642 118
nickjillings@1642 119 // Connect both gains to output
nickjillings@1642 120 this.outputGain.connect(audioContext.destination);
nickjillings@1642 121 this.fooGain.connect(audioContext.destination);
nickjillings@1642 122
nickjillings@1642 123 // Create store for new audioObjects
nickjillings@1642 124 this.audioObjects = [];
nickjillings@1642 125
nickjillings@1642 126 this.play = function() {
nickjillings@1642 127 // Send play command to all playback buffers for synchronised start
nickjillings@1642 128 // Also start timer callbacks to detect if playback has finished
nickjillings@1642 129 if (this.status == 0) {
nickjillings@1642 130 // First get current clock
nickjillings@1642 131 var timer = audioContext.currentTime;
nickjillings@1642 132 // Add 3 seconds
nickjillings@1642 133 timer += 3.0;
nickjillings@1642 134
nickjillings@1642 135 // Send play to all tracks
nickjillings@1642 136 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1642 137 {
nickjillings@1642 138 this.audioObjects[i].play(timer);
nickjillings@1642 139 }
nickjillings@1642 140 this.status = 1;
nickjillings@1642 141 }
nickjillings@1642 142 };
nickjillings@1642 143
nickjillings@1642 144 this.stop = function() {
nickjillings@1642 145 // Send stop and reset command to all playback buffers
nickjillings@1642 146 if (this.status == 1) {
nickjillings@1642 147 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1642 148 {
nickjillings@1642 149 this.audioObjects[i].stop();
nickjillings@1642 150 }
nickjillings@1642 151 this.status = 0;
nickjillings@1642 152 }
nickjillings@1642 153 };
nickjillings@1642 154
nickjillings@1642 155 this.selectedTrack = function(id) {
nickjillings@1642 156 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1642 157 {
nickjillings@1642 158 if (id == i) {
nickjillings@1642 159 this.audioObjects[i].outputGain.gain.value = 1.0;
nickjillings@1642 160 } else {
nickjillings@1642 161 this.audioObjects[i].outputGain.gain.value = 0.0;
nickjillings@1642 162 }
nickjillings@1642 163 }
nickjillings@1642 164 };
nickjillings@1642 165
nickjillings@1642 166
nickjillings@1642 167 this.newTrack = function(url) {
nickjillings@1642 168 // Pull data from given URL into new audio buffer
nickjillings@1642 169 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
nickjillings@1642 170
nickjillings@1642 171 // Create the audioObject with ID of the new track length;
nickjillings@1642 172 audioObjectId = this.audioObjects.length
nickjillings@1642 173 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
nickjillings@1642 174
nickjillings@1642 175 // AudioObject will get track itself.
nickjillings@1642 176 this.audioObjects[audioObjectId].constructTrack(url);
nickjillings@1642 177 };
nickjillings@1642 178
nickjillings@1642 179 }
nickjillings@1642 180
nickjillings@1642 181 function audioObject(id) {
nickjillings@1642 182 // The main buffer object with common control nodes to the AudioEngine
nickjillings@1642 183
nickjillings@1642 184 this.id = id;
nickjillings@1642 185 this.state = 0; // 0 - no data, 1 - ready
nickjillings@1642 186 this.url = null; // Hold the URL given for the output back to the results.
nickjillings@1642 187
nickjillings@1642 188 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
nickjillings@1642 189 this.bufferNode = audioContext.createBufferSource();
nickjillings@1642 190 this.outputGain = audioContext.createGain();
nickjillings@1642 191
nickjillings@1642 192 // Default output gain to be zero
nickjillings@1642 193 this.outputGain.gain.value = 0.0;
nickjillings@1642 194
nickjillings@1642 195 // Connect buffer to the audio graph
nickjillings@1642 196 this.bufferNode.connect(this.outputGain);
nickjillings@1642 197 this.outputGain.connect(audioEngineContext.outputGain);
nickjillings@1642 198
nickjillings@1642 199 // the audiobuffer is not designed for multi-start playback
nickjillings@1642 200 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
nickjillings@1642 201 this.buffer;
nickjillings@1642 202
nickjillings@1642 203 this.play = function(startTime) {
nickjillings@1642 204 this.bufferNode.start(startTime);
nickjillings@1642 205 };
nickjillings@1642 206
nickjillings@1642 207 this.stop = function() {
nickjillings@1642 208 this.bufferNode.stop(0);
nickjillings@1642 209 this.bufferNode = audioContext.createBufferSource();
nickjillings@1642 210 this.bufferNode.connect(this.outputGain);
nickjillings@1642 211 this.bufferNode.buffer = this.buffer;
nickjillings@1642 212 this.bufferNode.loop = true;
nickjillings@1642 213 };
nickjillings@1642 214
nickjillings@1642 215 this.constructTrack = function(url) {
nickjillings@1642 216 var request = new XMLHttpRequest();
nickjillings@1642 217 this.url = url;
nickjillings@1642 218 request.open('GET',url,true);
nickjillings@1642 219 request.responseType = 'arraybuffer';
nickjillings@1642 220
nickjillings@1642 221 var audioObj = this;
nickjillings@1642 222
nickjillings@1642 223 // Create callback to decode the data asynchronously
nickjillings@1642 224 request.onloadend = function() {
nickjillings@1642 225 audioContext.decodeAudioData(request.response, function(decodedData) {
nickjillings@1642 226 audioObj.buffer = decodedData;
nickjillings@1642 227 audioObj.bufferNode.buffer = audioObj.buffer;
nickjillings@1642 228 audioObj.bufferNode.loop = true;
nickjillings@1642 229 audioObj.state = 1;
nickjillings@1642 230 }, function(){
nickjillings@1642 231 // Should only be called if there was an error, but sometimes gets called continuously
nickjillings@1642 232 // Check here if the error is genuine
nickjillings@1642 233 if (audioObj.state == 0 || audioObj.buffer == undefined) {
nickjillings@1642 234 // Genuine error
nickjillings@1642 235 console.log('FATAL - Error loading buffer on '+audioObj.id);
nickjillings@1642 236 }
nickjillings@1642 237 });
nickjillings@1642 238 };
nickjillings@1642 239 request.send();
nickjillings@1642 240 };
nickjillings@1642 241
nickjillings@1642 242 }