forked from borismus/spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g-spectrogram.js
258 lines (222 loc) · 7.15 KB
/
g-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
// Assumes context is an AudioContext defined outside of this class.
Polymer('g-spectrogram', {
// Show the controls UI.
controls: false,
// Log mode.
log: false,
// Show axis labels, and how many ticks.
labels: false,
ticks: 5,
speed: 2,
// FFT bin size,
fftsize: 2048,
oscillator: false,
color: false,
attachedCallback: function() {
this.tempCanvas = document.createElement('canvas'),
console.log('Created spectrogram');
// Get input from the microphone.
if (navigator.mozGetUserMedia) {
navigator.mozGetUserMedia({audio: true},
this.onStream.bind(this),
this.onStreamError.bind(this));
} else if (navigator.webkitGetUserMedia) {
navigator.webkitGetUserMedia({audio: true},
this.onStream.bind(this),
this.onStreamError.bind(this));
}
this.ctx = this.$.canvas.getContext('2d');
},
render: function() {
//console.log('Render');
this.width = window.innerWidth;
this.height = window.innerHeight;
var didResize = false;
// Ensure dimensions are accurate.
if (this.$.canvas.width != this.width) {
this.$.canvas.width = this.width;
this.$.labels.width = this.width;
didResize = true;
}
if (this.$.canvas.height != this.height) {
this.$.canvas.height = this.height;
this.$.labels.height = this.height;
didResize = true;
}
//this.renderTimeDomain();
this.renderFreqDomain();
if (this.labels && didResize) {
this.renderAxesLabels();
}
requestAnimationFrame(this.render.bind(this));
var now = new Date();
if (this.lastRenderTime_) {
this.instantaneousFPS = now - this.lastRenderTime_;
}
this.lastRenderTime_ = now;
},
renderTimeDomain: function() {
var times = new Uint8Array(this.analyser.frequencyBinCount);
this.analyser.getByteTimeDomainData(times);
for (var i = 0; i < times.length; i++) {
var value = times[i];
var percent = value / 256;
var barHeight = this.height * percent;
var offset = this.height - barHeight - 1;
var barWidth = this.width/times.length;
this.ctx.fillStyle = 'black';
this.ctx.fillRect(i * barWidth, offset, 1, 1);
}
},
renderFreqDomain: function() {
var freq = new Uint8Array(this.analyser.frequencyBinCount);
this.analyser.getByteFrequencyData(freq);
var ctx = this.ctx;
// Copy the current canvas onto the temp canvas.
this.tempCanvas.width = this.width;
this.tempCanvas.height = this.height;
//console.log(this.$.canvas.height, this.tempCanvas.height);
var tempCtx = this.tempCanvas.getContext('2d');
tempCtx.drawImage(this.$.canvas, 0, 0, this.width, this.height);
// Iterate over the frequencies.
for (var i = 0; i < freq.length; i++) {
var value;
// Draw each pixel with the specific color.
if (this.log) {
logIndex = this.logScale(i, freq.length);
value = freq[logIndex];
} else {
value = freq[i];
}
ctx.fillStyle = (this.color ? this.getFullColor(value) : this.getGrayColor(value));
var percent = i / freq.length;
var y = Math.round(percent * this.height);
// draw the line at the right side of the canvas
ctx.fillRect(this.width - this.speed, this.height - y,
this.speed, this.speed);
}
// Translate the canvas.
ctx.translate(-this.speed, 0);
// Draw the copied image.
ctx.drawImage(this.tempCanvas, 0, 0, this.width, this.height,
0, 0, this.width, this.height);
// Reset the transformation matrix.
ctx.setTransform(1, 0, 0, 1, 0, 0);
},
/**
* Given an index and the total number of entries, return the
* log-scaled value.
*/
logScale: function(index, total, opt_base) {
var base = opt_base || 2;
var logmax = this.logBase(total + 1, base);
var exp = logmax * index / total;
return Math.round(Math.pow(base, exp) - 1);
},
logBase: function(val, base) {
return Math.log(val) / Math.log(base);
},
renderAxesLabels: function() {
var canvas = this.$.labels;
canvas.width = this.width;
canvas.height = this.height;
var ctx = canvas.getContext('2d');
var startFreq = 440;
var nyquist = context.sampleRate/2;
var endFreq = nyquist - startFreq;
var step = (endFreq - startFreq) / this.ticks;
var yLabelOffset = 5;
// Render the vertical frequency axis.
for (var i = 0; i <= this.ticks; i++) {
var freq = startFreq + (step * i);
// Get the y coordinate from the current label.
var index = this.freqToIndex(freq);
var percent = index / this.getFFTBinCount();
var y = (1-percent) * this.height;
var x = this.width - 60;
// Get the value for the current y coordinate.
var label;
if (this.log) {
// Handle a logarithmic scale.
var logIndex = this.logScale(index, this.getFFTBinCount());
// Never show 0 Hz.
freq = Math.max(1, this.indexToFreq(logIndex));
}
var label = this.formatFreq(freq);
var units = this.formatUnits(freq);
ctx.font = '16px Inconsolata';
// Draw the value.
ctx.textAlign = 'right';
ctx.fillText(label, x, y + yLabelOffset);
// Draw the units.
ctx.textAlign = 'left';
ctx.fillText(units, x + 10, y + yLabelOffset);
// Draw a tick mark.
ctx.fillRect(x + 40, y, 30, 2);
}
},
clearAxesLabels: function() {
var canvas = this.$.labels;
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, this.width, this.height);
},
formatFreq: function(freq) {
return (freq >= 1000 ? (freq/1000).toFixed(1) : Math.round(freq));
},
formatUnits: function(freq) {
return (freq >= 1000 ? 'KHz' : 'Hz');
},
indexToFreq: function(index) {
var nyquist = context.sampleRate/2;
return nyquist/this.getFFTBinCount() * index;
},
freqToIndex: function(frequency) {
var nyquist = context.sampleRate/2;
return Math.round(frequency/nyquist * this.getFFTBinCount());
},
getFFTBinCount: function() {
return this.fftsize / 2;
},
onStream: function(stream) {
var input = context.createMediaStreamSource(stream);
var analyser = context.createAnalyser();
analyser.smoothingTimeConstant = 0;
analyser.fftSize = this.fftsize;
// Connect graph.
input.connect(analyser);
this.analyser = analyser;
// Setup a timer to visualize some stuff.
this.render();
},
onStreamError: function(e) {
console.error(e);
},
getGrayColor: function(value) {
return 'rgb(V, V, V)'.replace(/V/g, 255 - value);
},
getFullColor: function(value) {
var fromH = 62;
var toH = 0;
var percent = value / 255;
var delta = percent * (toH - fromH);
var hue = fromH + delta;
return 'hsl(H, 100%, 50%)'.replace(/H/g, hue);
},
logChanged: function() {
if (this.labels) {
this.renderAxesLabels();
}
},
ticksChanged: function() {
if (this.labels) {
this.renderAxesLabels();
}
},
labelsChanged: function() {
if (this.labels) {
this.renderAxesLabels();
} else {
this.clearAxesLabels();
}
}
});