-
Notifications
You must be signed in to change notification settings - Fork 8
/
core.js
243 lines (202 loc) · 5.96 KB
/
core.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
/**
* @module gl-spectrum/code
*/
'use strict'
const extend = require('object-assign')
const inherits = require('inherits')
const createGrid = require('plot-grid')
const clamp = require('clamp')
const Emitter = require('events')
const weighting = require('a-weighting')
const interpolate = require('color-interpolate')
const pretty = require('pretty-number')
const isPlainObj = require('is-plain-obj')
const createLoop = require('canvas-loop')
const db = require('decibels')
const getContext = require('gl-util/context')
const lg = Math.log10
module.exports = Spectrum;
inherits(Spectrum, Emitter);
/**
* @contructor
*/
function Spectrum (options) {
if (!(this instanceof Spectrum)) return new Spectrum(options);
Emitter.call();
extend(this, options);
//create canvas/container
//FIXME: this is not very good for 2d case though
if (!this.context) this.context = getContext(this);
if (!this.canvas) this.canvas = this.context.canvas;
if (!this.container) this.container = document.body || document.documentElement;
if (!this.canvas.parentNode) this.container.appendChild(this.canvas);
//create loop
this.loop = createLoop(this.canvas, {parent: this.container, scale: this.pixelRatio});
this.loop.on('tick', () => {
this.render();
});
this.loop.on('resize', () => {
this.update()
});
this.autostart && this.loop.start();
//create data holder
this.magnitudes = [];
}
Spectrum.prototype.pixelRatio = window.devicePixelRatio;
Spectrum.prototype.autostart = true;
Spectrum.prototype.className = 'gl-spectrum';
Spectrum.prototype.align = .5;
Spectrum.prototype.trail = true;
Spectrum.prototype.type = 'fill';
Spectrum.prototype.barWidth = 2;
Spectrum.prototype.grid = true;
Spectrum.prototype.maxDb = 0;
Spectrum.prototype.minDb = -100;
Spectrum.prototype.maxFrequency = 20000;
Spectrum.prototype.minFrequency = 20;
Spectrum.prototype.smoothing = 0.82;
Spectrum.prototype.log = true;
Spectrum.prototype.weighting = 'm';
Spectrum.prototype.sampleRate = 44100;
Spectrum.prototype.palette = 'black';
Spectrum.prototype.levels = 32;
Spectrum.prototype.balance = .5;
Spectrum.prototype.trailAlpha = .33;
Spectrum.prototype.interactions = false;
Spectrum.prototype.background = null;
/**
* Set data
*/
Spectrum.prototype.set = function (data) {
let halfRate = this.sampleRate * 0.5;
let nf = halfRate / data.length;
let weight = typeof this.weighting === 'string' ? weighting[this.weighting] : this.weighting;
let smoothing = this.smoothing;
let magnitudes = this.magnitudes;
magnitudes.length = data.length;
//apply weighting and clamping, bring db to 0..1 range
let peak = 0;
for (let i = 0; i < data.length; i++) {
let v = data[i]
if (weight) v += 20 * Math.log(weight(i * nf)) / Math.log(10);
v = clamp(v, -100, 0)
v = .01 * (v + 100);
if (v > peak) peak = v;
magnitudes[i] = v * (1 - smoothing) + (magnitudes[i] || 0) * smoothing;
}
this.peak = peak;
if (this.trail) {
if (!Array.isArray(this.trail)) {
this.trail = magnitudes;
this.trailPeak = this.peak;
}
else {
this.trail.length = magnitudes.length;
let trailPeak = 0;
this.trail = magnitudes.map((v, i) => {
v = Math.max(v, this.trail[i]);
if (v > trailPeak) trailPeak = v;
return v;
});
this.trailPeak = trailPeak;
}
}
this.emit('data', magnitudes, this.trail);
!this.autostart && this.render();
return this;
};
/**
* Update options
*/
Spectrum.prototype.update = function (options) {
if (!options) options = {};
extend(this, options);
//create colormap from palette
if (!Array.isArray(this.palette)) this.palette = [this.palette];
this.getColor = interpolate(this.palette);
this.infoColor = this.getColor(.5);
//limit base
this.minFrequency = Math.max(20, this.minFrequency);
this.maxFrequency = Math.min(this.sampleRate/2, this.maxFrequency);
//set bg in case of slave draw mode
if (this.background && this.alpha) {
this.canvas.style.background = this.background;
}
//create grid if true/options passed
if (this.grid === true || isPlainObj(this.grid)) {
if (!this._grid) {
this._grid = createGrid({
autostart: false,
context: this.context,
interactions: this.interactions,
x: extend({
type: this.log ? 'logarithmic' : 'linear',
minScale: 1e-10,
origin: 0,
axisOrigin: -Infinity,
format: (v) => {
// v = this.log ? Math.pow(10, v) : v;
return pretty(v);
}
}, this.grid),
// y: 'linear'
});
this._grid.on('interact', (grid) => {
if (!this.interactions) return;
let x = grid.x;
let leftF = x.offset;
let rightF = x.offset + x.scale*this.canvas.width;
if (this.log) {
leftF = Math.pow(10, leftF);
rightF = Math.pow(10, rightF);
}
this.update({minFrequency: leftF, maxFrequency: rightF});
});
}
this.grid = this._grid;
this._grid.update({x: {disabled: false}});
}
else if (!this.grid && this._grid) {
this._grid.update({x: {disabled: true}});
this._grid.draw();
}
//update grid
if (this.grid) {
let xOpts = {
color: this.getColor(.75),
// lineColor: .05
};
if (options.log != null) {
xOpts.type = this.log ? 'logarithmic' : 'linear';
xOpts.min = this.log ? lg(20) : 0;
xOpts.max = this.log ? lg(this.sampleRate/2) : this.sampleRate/2;
}
xOpts.offset = this.log ? lg(this.minFrequency) : this.minFrequency;
xOpts.scale = (this.log ? lg(this.maxFrequency/this.minFrequency) : (this.maxFrequency - this.minFrequency)) / this.canvas.width;
if (options.interactions) {
xOpts.pan = xOpts.zoom = options.interactions;
}
//FIXME: add better decibels rendering
// let yOpts = {
// axisOrigin: Infinity,
// origin: 0,
// offset: -this.align*200,
// scale: 200/height,
// lineColor: false,
// distance: 10,
// color: this.getColor(.75),
// format: v => {
// return pretty(-100 + Math.abs(v));
// }
// }
this.grid.update({
x: xOpts,
//y: yOpts
});
}
//reset trail if weight changed
if (options.weighting != null) this.trail = !!this.trail;
//emit update
this.emit('update');
return this;
};