forked from 72lions/PlayingChunkedMP3-WebAudioAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp3chunksplayer.js
235 lines (208 loc) · 5.06 KB
/
mp3chunksplayer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* The MP3ChunksPlayer class
*
* @constructor
* @class MP3ChunksPlayer
*/
var MP3ChunksPlayer = function() {
EventTarget.call(this);
/**
* A reference to this specific instance.
*
*@private
* @type {MP3ChunksPlayer}
*/
var _self = this;
/**
* The ArrayBuffer that will have the new chunks appended
*
* @private
* @type {ArrayBuffer}
*/
var _activeBuffer;
/**
* The counter that holds the how many chunks we have loaded
*
* @private
* @type {Number}
*/
var _totalChunksLoaded = 0;
/**
* An array with all the individual files
* @private
* @type {Array}
*/
var _files = [
'xa',
'xb',
'xc',
'xd',
'xe',
'xf',
'xg',
'xh',
'xi',
'xj',
'xk',
'xl',
'xm',
'xn',
'xo',
'xp',
'xq',
'xr',
'xs',
'xt',
'xu',
'xv',
'xw',
'xx',
'xy'];
/**
* The AudioContext
*
* @private
* @type {AudioContext}
*/
var _context;
/**
* The audio buffer
*
* @private
* @type {AudioBuffer}
*/
var _audioBuffer;
/**
* The audio source is responsible for playing the music
*
* @private
* @type {AudioBufferSourceNode}
*/
var _audioSource;
/**
* The analyser
*
* @private
* @type {AnalyserNode}
*/
var _analyser;
/**
* It is responsible for loading all the different chunks
*
* @private
* @type {XMLHttpRequest}
*/
var _request = new XMLHttpRequest();
/**
* Creates a new Uint8Array based on two different ArrayBuffers
*
* @private
* @param {ArrayBuffers} buffer1 The first buffer.
* @param {ArrayBuffers} buffer2 The second buffer.
* @return {ArrayBuffers} The new ArrayBuffer created out of the two.
*/
var _appendBuffer = function(buffer1, buffer2) {
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
};
/**
* Instantiates all the WebAudio items
*
* @private
*/
var _initializeWebAudio = function() {
_context = new webkitAudioContext();
_analyser = _context.createAnalyser();
_analyser.fftSize = 2048;
};
/**
* Plays the music from the point that it currently is.
*
* @private
*/
var _play = function() {
// Adding a bit of scheduling so that we won't have single digit milisecond overlaps.
// Thanks to Chris Wilson for his suggestion.
var scheduledTime = 0.015;
try {
_audioSource.stop(scheduledTime);
} catch (e) {}
_audioSource = _context.createBufferSource();
_audioSource.buffer = _audioBuffer;
_audioSource.connect(_analyser);
_audioSource.connect(_context.destination);
var currentTime = _context.currentTime + 0.010 || 0;
_audioSource.start(scheduledTime - 0.005, currentTime, _audioBuffer.duration - currentTime);
_audioSource.playbackRate.value = 1;
_self.trigger('message', ['AudioBuffer is replaced!']);
};
/**
* Is triggered when a new chunk is loaded and makes sure to add the
* new chunk to a new ArrayBuffer.
*
* @private
*/
var _onChunkLoaded = function() {
console.log('Chunk loaded!');
_self.trigger('message', ['Chunk loaded!']);
if (_totalChunksLoaded === 0) {
_initializeWebAudio();
_activeBuffer = _request.response;
} else {
_self.trigger('message', ['Chunk is appended!']);
_activeBuffer = _appendBuffer(_activeBuffer, _request.response);
}
// Use decodeAudioData so that we don't block the main thread.
_context.decodeAudioData(_activeBuffer, function(buf) {
_self.trigger('message', ['AudioData decoded!']);
_audioBuffer = buf;
_play();
});
// If this is the first chunk then trigger play
if (_totalChunksLoaded === 0) {
_self.trigger('play');
}
_totalChunksLoaded++;
if (_totalChunksLoaded < _files.length) {
setTimeout(function() {
_loadChunk(_totalChunksLoaded);
}, 3000);
}
};
/**
* Loads a specific chunk based on an index in an array.
*
* @private
* @param {Number} index The index of the chunk in the files array.
*/
var _loadChunk = function(index) {
_self.trigger('message', ['Loading chunk', _files[index], '...']);
_request.open('GET', 'chunks/' + _files[index], true);
_request.send();
};
/**
* It gets the visualisation data
*
* @return {Uint8Array} The array that holds the visualisation data.
*/
this.getVisualisationData = function() {
// get the average for the first channel
var array = new Uint8Array(_analyser.frequencyBinCount);
_analyser.getByteFrequencyData(array);
return array;
};
/**
* Initializes the class by loading the first chunk
*
* @return {MP3ChunksPlayer} Returns a reference to this instance.
*/
this.init = function() {
console.log('MP3ChunksPlayer initialized!');
_request.responseType = 'arraybuffer';
_request.addEventListener('load', _onChunkLoaded, false);
_loadChunk(_totalChunksLoaded);
return this;
};
};