annotate core.js @ 23:c4c7ddaec6fc

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