-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpectrogram.js
More file actions
175 lines (147 loc) · 4.79 KB
/
Spectrogram.js
File metadata and controls
175 lines (147 loc) · 4.79 KB
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
// Assumes context is an AudioContext defined outside of this class.
// https://github.com/borismus/spectrogram/blob/master/g-spectrogram.js
class Spectrogram {
formatFreq(freq) {
return (freq >= 1000 ? (freq/1000).toFixed(1) : Math.round(freq));
}
formatUnits(freq) {
return (freq >= 1000 ? 'KHz' : 'Hz');
}
indexToFreq(index) {
var nyquist = context.sampleRate/2;
return nyquist/this.getFFTBinCount() * index;
}
freqToIndex(frequency) {
var nyquist = context.sampleRate/2;
return Math.round(frequency/nyquist * this.getFFTBinCount());
}
getFFTBinCount() {
return this.fftsize / 2;
}
onStreamError(e) {
console.error(e);
}
getGrayColor(value) {
return 'rgb(V, V, V)'.replace(/V/g, 255 - value);
}
getFullColor(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);
}
// renderTimeDomain() {
// 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() {
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(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(val, base) {
return Math.log(val) / Math.log(base);
}
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;
}
constructor(context, container) {
this.controls = false;
// Log mode.
this.log = false;
// Show axis labels, and how many ticks.
// this.labels = false;
// this.ticks = 5;
this.speed = 2;
// FFT bin size,
this.fftsize = 2048;
// this.oscillator = false;
this.color = true;
this.canvas = document.createElement('canvas');
this.tempCanvas = document.createElement('canvas');
// console.log('Created spectrogram');
var analyser = context.createAnalyser();
analyser.smoothingTimeConstant = 0;
analyser.fftSize = this.fftsize;
this.analyser = analyser;
// Setup a timer to visualize some stuff.
this.ctx = this.canvas.getContext('2d');
this.render();
container.appendChild(this.canvas);
}
}