nickjillings@1682
|
1 /**
|
nickjillings@1682
|
2 * core.js
|
nickjillings@1682
|
3 *
|
nickjillings@1682
|
4 * Main script to run, calls all other core functions and manages loading/store to backend.
|
nickjillings@1682
|
5 * Also contains all global variables.
|
nickjillings@1682
|
6 */
|
nickjillings@1682
|
7
|
nickjillings@1682
|
8 /* create the web audio API context and store in audioContext*/
|
nickjillings@1682
|
9 var audioContext;
|
nickjillings@1682
|
10
|
nickjillings@1682
|
11 var audioEngineContext;
|
nickjillings@1682
|
12
|
nickjillings@1682
|
13 window.onload = function() {
|
nickjillings@1682
|
14 // Function called once the browser has loaded all files.
|
nickjillings@1682
|
15 // This should perform any initial commands such as structure / loading documents
|
nickjillings@1682
|
16
|
nickjillings@1682
|
17 // Create a web audio API context
|
nickjillings@1682
|
18 // NORE: Currently this will only work with webkit browsers (Chrome/Safari)!
|
nickjillings@1682
|
19 audioContext = new webkitAudioContext;
|
nickjillings@1682
|
20
|
nickjillings@1682
|
21 // Create the audio engine object
|
nickjillings@1682
|
22 audioEngineContext = new AudioEngine();
|
nickjillings@1682
|
23 }
|
nickjillings@1682
|
24
|
nickjillings@1682
|
25 function loadProjectSpec(url) {
|
nickjillings@1682
|
26 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
|
nickjillings@1682
|
27 // If url is null, request client to upload project XML document
|
nickjillings@1682
|
28 }
|
nickjillings@1682
|
29
|
nickjillings@1682
|
30 function createProjectSave(destURL) {
|
nickjillings@1682
|
31 // Save the data from interface into XML and send to destURL
|
nickjillings@1682
|
32 // If destURL is null then download XML in client
|
nickjillings@1682
|
33 }
|
nickjillings@1682
|
34
|
nickjillings@1682
|
35 function AudioEngine() {
|
nickjillings@1682
|
36
|
nickjillings@1682
|
37 // Create two output paths, the main outputGain and fooGain.
|
nickjillings@1682
|
38 // Output gain is default to 1 and any items for playback route here
|
nickjillings@1682
|
39 // Foo gain is used for analysis to ensure paths get processed, but are not heard
|
nickjillings@1682
|
40 // because web audio will optimise and any route which does not go to the destination gets ignored.
|
nickjillings@1682
|
41 this.outputGain = audioContext.createGain();
|
nickjillings@1682
|
42 this.fooGain = audioContext.createGain();
|
nickjillings@1682
|
43 this.fooGain.gain = 0;
|
nickjillings@1682
|
44
|
nickjillings@1682
|
45 // Connect both gains to output
|
nickjillings@1682
|
46 this.outputGain.connect(audioContext.destination);
|
nickjillings@1682
|
47 this.fooGain.connect(audioContext.destination);
|
nickjillings@1682
|
48
|
nickjillings@1682
|
49 // Create store for new audioObjects
|
nickjillings@1682
|
50 this.audioObjects = [];
|
nickjillings@1682
|
51
|
nickjillings@1682
|
52 this.play = function() {
|
nickjillings@1682
|
53 // Send play command to all playback buffers for synchronised start
|
nickjillings@1682
|
54 // Also start timer callbacks to detect if playback has finished
|
nickjillings@1682
|
55 }
|
nickjillings@1682
|
56
|
nickjillings@1682
|
57 this.stop = function() {
|
nickjillings@1682
|
58 // Send stop and reset command to all playback buffers
|
nickjillings@1682
|
59 }
|
nickjillings@1682
|
60
|
nickjillings@1682
|
61 this.newTrack = function(url) {
|
nickjillings@1682
|
62 // Pull data from given URL into new audio buffer
|
nickjillings@1682
|
63 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
|
nickjillings@1682
|
64 var request = new XMLHttpRequest();
|
nickjillings@1682
|
65 request.open('GET',url,true);
|
nickjillings@1682
|
66 request.responseType = 'arraybuffer';
|
nickjillings@1682
|
67 // Create the audioObject with ID of the new track length;
|
nickjillings@1682
|
68 audioObjectId = this.audioObjects.length
|
nickjillings@1682
|
69 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
|
nickjillings@1682
|
70
|
nickjillings@1682
|
71 // Create callback to decode the data asynchronously
|
nickjillings@1682
|
72 request.onload = function() {
|
nickjillings@1682
|
73 audioContext.decodeAudioData(request.response, function(decodedData) {
|
nickjillings@1682
|
74 audioObj = audioEngineContext.audioObjects[audioObjectId];
|
nickjillings@1682
|
75 audioObj.buffer = decodedData;
|
nickjillings@1682
|
76 audioObj.bufferNode.buffer = audioObj.buffer;
|
nickjillings@1682
|
77 audioObj.state = 1;
|
nickjillings@1682
|
78 }, console.log("Err - Buffer not added to " + audioObjectId));
|
nickjillings@1682
|
79 }
|
nickjillings@1682
|
80 request.send();
|
nickjillings@1682
|
81 }
|
nickjillings@1682
|
82
|
nickjillings@1682
|
83 }
|
nickjillings@1682
|
84
|
nickjillings@1682
|
85 function audioObject(id) {
|
nickjillings@1682
|
86 // The main buffer object with common control nodes to the AudioEngine
|
nickjillings@1682
|
87
|
nickjillings@1682
|
88 this.id = id;
|
nickjillings@1682
|
89 this.state = 0; // 0 - no data, 1 - ready
|
nickjillings@1682
|
90
|
nickjillings@1682
|
91 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
|
nickjillings@1682
|
92 this.bufferNode = audioContext.createBufferSource();
|
nickjillings@1682
|
93 this.outputGain = audioContext.createGain();
|
nickjillings@1682
|
94
|
nickjillings@1682
|
95 // Connect buffer to the audio graph
|
nickjillings@1682
|
96 this.bufferNode.connect(this.outputGain);
|
nickjillings@1682
|
97 this.outputGain.connect(audioEngineContext.outputGain);
|
nickjillings@1682
|
98
|
nickjillings@1682
|
99 // the audiobuffer is not designed for multi-start playback
|
nickjillings@1682
|
100 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
|
nickjillings@1682
|
101 this.buffer;
|
nickjillings@1682
|
102
|
nickjillings@1682
|
103 this.play = function(startTime) {
|
nickjillings@1682
|
104 this.bufferNode.start(startTime);
|
nickjillings@1682
|
105 }
|
nickjillings@1682
|
106
|
nickjillings@1682
|
107 this.stop = function() {
|
nickjillings@1682
|
108 this.bufferNode.stop(0);
|
nickjillings@1682
|
109 this.bufferNode = audioContext.createBufferSource();
|
nickjillings@1682
|
110 this.bufferNode.connect(this.outputGain);
|
nickjillings@1682
|
111 this.bufferNode.buffer = this.buffer;
|
nickjillings@1682
|
112 }
|
nickjillings@1682
|
113
|
nickjillings@1682
|
114 }
|