-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
156 lines (140 loc) · 3.79 KB
/
index.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
var interpolate = require('interpolate-color');
function Spectrograph (context, opts) {
// Defaults
opts = opts || {};
var p = this.meta.params;
var module = this;
this.input = this.output =context.createAnalyser();
this.processor = context.createScriptProcessor(256, 1, 1);
this.fft = new Uint8Array(this.input.frequencyBinCount);
'speed range minH minS minL maxH maxS maxL'.split(' ').forEach(function (prop) {
module[prop] = opts[prop] || p[prop].defaultValue;
});
try {
this.ctx = opts.canvas.getContext('2d');
} catch (e) {
throw new Error('Spectrograph must have a valid canvas element');
}
this.h = opts.canvas.height;
this.w = opts.canvas.width;
this.input.connect(this.processor);
this.processor.onaudioprocess = function loop () {
module.input.getByteFrequencyData(module.fft);
process(module);
};
}
function process (mod) {
var ctx = mod.ctx;
var fft = mod.fft;
var range = mod.range / ctx.sampleRate * fft.length;
var data = ctx.getImageData(0, 0, mod.w, mod.h);
ctx.putImageData(data, -mod.speed, 0);
for (var i = 0; i <= range; i++) {
ctx.fillStyle = interpolate(mod.minColor, mod.maxColor, fft[i] / 255);
ctx.fillRect(
mod.w - mod.speed,
~~(mod.h - (mod.h / range * i)),
mod.speed,
Math.ceil(mod.h / range) || 1
);
}
}
var spectrographProperties = {
connect: {
value: function (dest) {
this.output.connect(dest.input ? dest.input : dest);
this.processor.connect(dest.input ? dest.input : dest);
}
},
disconnect: {
value: function () {
this.output.disconnect();
this.processor.disconnect();
this.output.connect(this.processor);
}
},
/**
* Module parameter metadata.
*/
meta: {
value: {
name: "spectrograph",
type: "tool",
params: {
speed: {
min: 1,
max: 20,
defaultValue: 2,
type: "int",
description: "How many ms between each FFT update"
},
range: {
values: [4096, 8192, 11250, 22500],
defaultValue: 11250,
type: "enum",
description: "Max frequency of the analysis"
},
minH: {
min: -360,
max: 360,
defaultValue: -85,
type: "int",
description: "Hue of the minimum amplitude value"
},
minS: {
min: 0,
max: 100,
defaultValue: 50,
type: "int",
description: "Saturation of the minimum amplitude value"
},
minL: {
min: 0,
max: 100,
defaultValue: 10,
type: "int",
description: "Lightness of the minimum amplitude value"
},
maxH: {
min: -360,
max: 360,
defaultValue: 50,
type: "int",
description: "Hue of the maximum amplitude value"
},
maxS: {
min: 0,
max: 100,
defaultValue: 100,
type: "int",
description: "Saturation of the maximum amplitude value"
},
maxL: {
min: 0,
max: 100,
defaultValue: 100,
type: "int",
description: "Lightness of the maximum amplitude value"
}
}
}
}
};
'minH minS minL maxH maxS maxL'.split(' ').forEach(function (prop) {
spectrographProperties[prop] = {
enumerable: true,
get: function () { return this['_' + prop]; },
set: function (val) {
this['_' + prop] = val;
this.minColor = 'hsl(' +
this.minH + ', ' + this.minS + '%, ' + this.minL + '%)';
this.maxColor = 'hsl(' +
this.maxH + ', ' + this.maxS + '%, ' + this.maxL + '%)';
}
}
});
Spectrograph.prototype = Object.create(null, spectrographProperties);
/**
* Exports.
*/
module.exports = Spectrograph;