forked from miguelmota/spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFFT.js
More file actions
163 lines (135 loc) · 4.41 KB
/
FFT.js
File metadata and controls
163 lines (135 loc) · 4.41 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
onmessage = function analyzeFrequenciesOverTime({data: {channelDataBuffer, fftSize}}) {
const channelData = new Float32Array(channelDataBuffer);
const fft = new FFT(fftSize);
const freqData = []
let currentOffset = 0;
while (currentOffset + fftSize < channelData.length) {
const segment = channelData.slice(
currentOffset,
currentOffset + fftSize
);
const spectrum = fft.calculateSpectrum(segment);
const array = new Uint8Array(fftSize / 2);
for (let j = 0; j < fftSize / 2; j++) {
array[j] = Math.max(-255, (Math.log(spectrum[j]) * Math.LOG10E) * 45);
}
const segmentNumber = ((currentOffset + fftSize) / fftSize) - 1;
freqData[segmentNumber] = array;
currentOffset += fftSize;
}
postMessage(freqData);
}
/**
* Calculate FFT - Based on https://github.com/katspaugh/wavesurfer.js/blob/5.2.0/src/plugin/spectrogram/fft.js
* but whittled down to just use Hann window function, which is the default for wavesurfer:
* https://github.com/katspaugh/wavesurfer.js/blob/d6d4638eba7a2c08c3415d24f22259893519d604/src/plugin/spectrogram.js#L228
*/
class FFT {
constructor(fftSize) {
let i;
this.bufferSize = fftSize;
this.sinTable = new Float32Array(this.bufferSize);
this.cosTable = new Float32Array(this.bufferSize);
this.windowValues = new Float32Array(this.bufferSize);
this.reverseTable = new Uint32Array(this.bufferSize);
this.peak = 0;
// Hann window function
for (i = 0; i < this.bufferSize; i++) {
this.windowValues[i] =
0.5 * (1 - Math.cos((Math.PI * 2 * i) / (this.bufferSize - 1)));
}
let limit = 1;
let bit = this.bufferSize >> 1;
while (limit < this.bufferSize) {
for (i = 0; i < limit; i++) {
this.reverseTable[i + limit] = this.reverseTable[i] + bit;
}
limit = limit << 1;
bit = bit >> 1;
}
for (i = 0; i < this.bufferSize; i++) {
this.sinTable[i] = Math.sin(-Math.PI / i);
this.cosTable[i] = Math.cos(-Math.PI / i);
}
}
calculateSpectrum(segment) {
let i;
// Locally scope variables for speed up
let bufferSize = this.bufferSize,
cosTable = this.cosTable,
sinTable = this.sinTable,
reverseTable = this.reverseTable,
real = new Float32Array(bufferSize),
imag = new Float32Array(bufferSize),
bSi = 2 / this.bufferSize,
sqrt = Math.sqrt,
rval,
ival,
mag,
spectrum = new Float32Array(bufferSize / 2);
const k = Math.floor(Math.log(bufferSize) / Math.LN2);
if (Math.pow(2, k) !== bufferSize) {
throw 'Invalid buffer size, must be a power of 2.';
}
if (bufferSize !== segment.length) {
throw 'Supplied segment is not the same size as defined FFT. FFT Size: ' + bufferSize;
}
let halfSize = 1,
phaseShiftStepReal,
phaseShiftStepImag,
currentPhaseShiftReal,
currentPhaseShiftImag,
off,
tr,
ti,
tmpReal;
for (i = 0; i < bufferSize; i++) {
real[i] =
segment[reverseTable[i]] * this.windowValues[reverseTable[i]];
imag[i] = 0;
}
while (halfSize < bufferSize) {
phaseShiftStepReal = cosTable[halfSize];
phaseShiftStepImag = sinTable[halfSize];
currentPhaseShiftReal = 1;
currentPhaseShiftImag = 0;
for (let fftStep = 0; fftStep < halfSize; fftStep++) {
i = fftStep;
while (i < bufferSize) {
off = i + halfSize;
tr =
currentPhaseShiftReal * real[off] -
currentPhaseShiftImag * imag[off];
ti =
currentPhaseShiftReal * imag[off] +
currentPhaseShiftImag * real[off];
real[off] = real[i] - tr;
imag[off] = imag[i] - ti;
real[i] += tr;
imag[i] += ti;
i += halfSize << 1;
}
tmpReal = currentPhaseShiftReal;
currentPhaseShiftReal =
tmpReal * phaseShiftStepReal -
currentPhaseShiftImag * phaseShiftStepImag;
currentPhaseShiftImag =
tmpReal * phaseShiftStepImag +
currentPhaseShiftImag * phaseShiftStepReal;
}
halfSize = halfSize << 1;
}
i = 0;
const N = bufferSize / 2;
for (; i < N; i++) {
rval = real[i];
ival = imag[i];
mag = bSi * sqrt(rval * rval + ival * ival);
if (mag > this.peak) {
this.peak = mag;
}
spectrum[i] = mag;
}
return spectrum;
}
}