diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-17 21:28:58 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-17 21:28:58 +0100 |
| commit | 99e94d18ce54d1ad73c7c0349119d4cd8fb4f965 (patch) | |
| tree | e39536f70a9ff62cc42315ecb1fb5397ccf762b8 /tools/mq_editor/fft.js | |
| parent | fbdfa2a7b54894a565fd57d5e27f19d752fe39fa (diff) | |
feat(mq_editor): peaks display, STFT cache refactor, threshold reactivity
- STFTCache: cache squaredAmplitude (re²+im²) per frame, add getSquaredAmplitude(t)
- getMagnitudeDB uses cached sq amp (10*log10, no sqrt)
- detectPeaks uses squaredAmp from cache instead of recomputing FFT
- extractPartials: use cache frames, return {partials, frames}
- Press 'p' to toggle raw peak overlay in viewer
- threshold input: step=any, change event triggers re-extraction
- runExtraction() shared by button and threshold change
handoff(Claude): mq_partial peaks/cache refactor complete
Diffstat (limited to 'tools/mq_editor/fft.js')
| -rw-r--r-- | tools/mq_editor/fft.js | 24 |
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)); } } |
