summaryrefslogtreecommitdiff
path: root/tools/mq_editor/fft.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/mq_editor/fft.js')
-rw-r--r--tools/mq_editor/fft.js24
1 files changed, 17 insertions, 7 deletions
diff --git a/tools/mq_editor/fft.js b/tools/mq_editor/fft.js
index 0668906..36b9936 100644
--- a/tools/mq_editor/fft.js
+++ b/tools/mq_editor/fft.js
@@ -134,7 +134,15 @@ class STFTCache {
// Compute FFT
const spectrum = realFFT(windowed);
- this.frames.push({time, offset, spectrum});
+ // Cache squared amplitudes (re*re + im*im, no sqrt)
+ const squaredAmplitude = new Float32Array(this.fftSize / 2);
+ for (let i = 0; i < this.fftSize / 2; ++i) {
+ const re = spectrum[i * 2];
+ const im = spectrum[i * 2 + 1];
+ squaredAmplitude[i] = re * re + im * im;
+ }
+
+ this.frames.push({time, offset, spectrum, squaredAmplitude});
}
}
@@ -172,18 +180,20 @@ class STFTCache {
return frame ? frame.spectrum : null;
}
+ getSquaredAmplitude(t) {
+ const frame = this.getFrameAtTime(t);
+ return frame ? frame.squaredAmplitude : null;
+ }
+
// Get magnitude in dB at specific time and frequency
getMagnitudeDB(t, freq) {
- const spectrum = this.getFFT(t);
- if (!spectrum) return -80;
+ const sq = this.getSquaredAmplitude(t);
+ if (!sq) return -80;
const bin = Math.round(freq * this.fftSize / this.sampleRate);
if (bin < 0 || bin >= this.fftSize / 2) return -80;
- const re = spectrum[bin * 2];
- const im = spectrum[bin * 2 + 1];
- const mag = Math.sqrt(re * re + im * im);
- return 20 * Math.log10(Math.max(mag, 1e-10));
+ return 10 * Math.log10(Math.max(sq[bin], 1e-20));
}
}