This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
spectrogram.js
executable file
·280 lines (223 loc) · 8.58 KB
/
spectrogram.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
(function(root) {
'use strict';
function _isFunction(v) {
return typeof v === 'function';
}
function _result(v) {
return _isFunction(v) ? v() : v;
}
var toString = Object.prototype.toString;
function Spectrogram(canvas, options) {
if (!(this instanceof Spectrogram)) {
return new Spectrogram(canvas, options);
}
var baseCanvasOptions = options.canvas || {};
this._audioEnded = null;
this._paused = null;
this._pausedAt = null;
this._startedAt = null;
this._sources = {
audioBufferStream: null,
userMediaStream: null
};
this._baseCanvas = canvas;
this._baseCanvasContext = this._baseCanvas.getContext('2d');
this._baseCanvas.width = _result(baseCanvasOptions.width) || this._baseCanvas.width;
this._baseCanvas.height = _result(baseCanvasOptions.height) || this._baseCanvas.height;
window.onresize = function() {
this._baseCanvas.width = _result(baseCanvasOptions.width) || this._baseCanvas.width;
this._baseCanvas.height = _result(baseCanvasOptions.height) || this._baseCanvas.height;
}.bind(this);
var audioOptions = options.audio || {};
this.audio = audioOptions;
var colors = [];
if (typeof options.colors === 'function') {
colors = options.colors(275);
} else {
colors = this._generateDefaultColors(275);
}
this._colors = colors;
this._baseCanvasContext.fillStyle = this._getColor(0);
this._baseCanvasContext.fillRect(0, 0, this._baseCanvas.width, this._baseCanvas.height);
}
Spectrogram.prototype._init = function() {
var source = this._sources.audioBufferStream;
source.scriptNode = source.audioContext.createScriptProcessor(2048, 1, 1);
source.scriptNode.connect(source.audioContext.destination);
source.scriptNode.onaudioprocess = function(event) {
var array = new Uint8Array(source.analyser.frequencyBinCount);
source.analyser.getByteFrequencyData(array);
this._draw(array, source.canvasContext);
}.bind(this);
source.sourceNode.onended = function() {
this.stop();
}.bind(this);
source.analyser = source.audioContext.createAnalyser();
source.analyser.smoothingTimeConstant = 0;
source.analyser.fftSize = 1024;
source.analyser.connect(source.scriptNode);
source.sourceNode.connect(source.analyser);
if (this.audio.enable) {
source.sourceNode.connect(source.audioContext.destination);
}
};
Spectrogram.prototype._draw = function(array, canvasContext) {
if (this._paused) {
return false;
}
var canvas = canvasContext.canvas;
var width = canvas.width;
var height = canvas.height;
var tempCanvasContext = canvasContext._tempContext;
var tempCanvas = tempCanvasContext.canvas;
tempCanvasContext.drawImage(canvas, 0, 0, width, height);
for (var i = 0; i < array.length; i++) {
var value = array[i];
canvasContext.fillStyle = this._getColor(value);
if (this._audioEnded) {
canvasContext.fillStyle = this._getColor(0);
}
canvasContext.fillRect(width - 1, height - i, 1, 1);
}
canvasContext.translate(-1, 0);
// draw prev canvas before translation
canvasContext.drawImage(tempCanvas, 0, 0, width, height, 0, 0, width, height);
canvasContext.drawImage(tempCanvas, 0, 0, width, height, 0, 0, width, height);
// reset transformation matrix
canvasContext.setTransform(1, 0, 0, 1, 0, 0);
this._baseCanvasContext.drawImage(canvas, 0, 0, width, height);
};
Spectrogram.prototype._startMediaStreamDraw = function(analyser, canvasContext) {
window.requestAnimationFrame(this._startMediaStreamDraw.bind(this, analyser, canvasContext));
var audioData = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(audioData);
this._draw(audioData, canvasContext);
};
Spectrogram.prototype.connectSource = function(audioBuffer, audioContext) {
var source = this._sources.audioBufferStream || {};
// clear current audio process
if (toString.call(source.scriptNode) === '[object ScriptProcessorNode]') {
source.scriptNode.onaudioprocess = null;
}
if (toString.call(audioBuffer) === '[object AudioBuffer]') {
audioContext = (!audioContext && source.audioBuffer.context) || (!audioContext && source.audioContext) || audioContext;
var sourceNode = audioContext.createBufferSource();
sourceNode.buffer = audioBuffer;
var canvasContext = source.canvasContext;
if (!source.canvasContext) {
var canvas = document.createElement('canvas');
canvas.width = this._baseCanvas.width;
canvas.height = this._baseCanvas.height;
canvasContext = canvas.getContext('2d');
var tempCanvas = document.createElement('canvas');
tempCanvas.width = canvas.width;
tempCanvas.height = canvas.height;
canvasContext._tempContext = tempCanvas.getContext('2d');
}
source = {
audioBuffer: audioBuffer,
audioContext: audioContext,
sourceNode: sourceNode,
analyser: null,
scriptNode: null,
canvasContext: canvasContext
};
this._sources.audioBufferStream = source;
this._init();
}
if (toString.call(audioBuffer) === '[object AnalyserNode]') {
source = this._sources.userMediaStream || {};
source.analyser = audioBuffer;
this._sources.userMediaStream = source;
}
};
Spectrogram.prototype.start = function(offset) {
var source = this._sources.audioBufferStream;
var sourceMedia = this._sources.userMediaStream;
if (source && source.sourceNode) {
source.sourceNode.start(0, offset||0);
this._audioEnded = false;
this._paused = false;
this._startedAt = Date.now();
}
// media stream uses an analyser for audio data
if (sourceMedia && sourceMedia.analyser) {
source = sourceMedia;
var canvas = document.createElement('canvas');
canvas.width = this._baseCanvas.width;
canvas.height = this._baseCanvas.height;
var canvasContext = canvas.getContext('2d');
var tempCanvas = document.createElement('canvas');
tempCanvas.width = canvas.width;
tempCanvas.height = canvas.height;
canvasContext._tempContext = tempCanvas.getContext('2d');
this._startMediaStreamDraw(source.analyser, canvasContext);
}
};
Spectrogram.prototype.stop = function() {
var source = this._sources[Object.keys(this._sources)[0]];
if (source && source.sourceNode) {
source.sourceNode.stop();
}
this._audioEnded = true;
};
Spectrogram.prototype.pause = function() {
this.stop();
this._paused = true;
this._pausedAt += Date.now() - this._startedAt;
};
Spectrogram.prototype.resume = function(offset) {
var source = this._sources[Object.keys(this._sources)[0]];
this._paused = false;
if (this._pausedAt) {
this.connectSource(source.audioBuffer, source.audioContext);
this.start(offset || (this._pausedAt / 1000));
}
};
Spectrogram.prototype.clear = function(canvasContext) {
var source = this._sources[Object.keys(this._sources)[0]];
this.stop();
if (toString.call(source.scriptNode) === '[object ScriptProcessorNode]') {
source.scriptNode.onaudioprocess = null;
}
canvasContext = canvasContext || source.canvasContext;
var canvas = canvasContext.canvas;
canvasContext.clearRect(0, 0, canvas.width, canvas.height);
canvasContext._tempContext.clearRect(0, 0, canvas.width, canvas.height);
this._baseCanvasContext.clearRect(0, 0, canvas.width, canvas.height);
};
Spectrogram.prototype._generateDefaultColors = function(steps) {
var frequency = Math.PI / steps;
var amplitude = 127;
var center = 128;
var slice = (Math.PI / 2) * 3.1;
var colors = [];
function toRGBString(v) {
return 'rgba(' + [v,v,v,1].toString() + ')';
}
for (var i = 0; i < steps; i++) {
var v = (Math.sin((frequency * i) + slice) * amplitude + center) >> 0;
colors.push(toRGBString(v));
}
return colors;
};
Spectrogram.prototype._getColor = function(index) {
var color = this._colors[index>>0];
if (typeof color === 'undefined') {
color = this._colors[0];
}
return color;
};
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = Spectrogram;
}
exports.Spectrogram = Spectrogram;
} else if (typeof define === 'function' && define.amd) {
define([], function() {
return Spectrogram;
});
} else {
root.Spectrogram = Spectrogram;
}
})(this);