@audio/stft 1.0.7 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,7 +4,7 @@ Try it in the browser: [Spectrogram](https://audiojs.dev/util/spectrogram/). Run
4
4
 
5
5
  > Canonical STFT for the `@audio` ecosystem — batch / stream / analyse, stream ≡ batch under any chunking.
6
6
 
7
- Hann analysis + synthesis windows with correctly normalized overlap-add. Extracted verbatim from `@audio/denoise-core` (differential-tested there since wave 1), including the shared stream ring-compaction fix.
7
+ Hann analysis + synthesis windows with correctly normalized overlap-add. Frames run on the hop grid from the one that starts N − hop before the input to the last sample, so every sample, the first and the last included, has the full overlap: an identity process returns the input exactly. Before the input, frames see its mirror image (numpy's `reflect`), so a process that learns from the first frames meets the signal's level. `ctx.pos` gives each frame's first input sample (negative for those early frames). Extracted verbatim from `@audio/denoise-core` (differential-tested there since wave 1), including the shared stream ring-compaction fix.
8
8
 
9
9
  ```js
10
10
  import { stftBatch, stftStream, stftAnalyse } from '@audio/stft'
@@ -24,7 +24,7 @@ let tail = s.flush()
24
24
  stftAnalyse(input, (mag, phase, ctx) => { /* inspect */ }, { fs: 44100 })
25
25
  ```
26
26
 
27
- Also exports `wrapPhase`, `hannWindow`, `normFloor`, `PI2` (window math shared with [`@audio/window`](https://github.com/audiojs/window)).
27
+ Also exports `wrapPhase`, `hannWindow` (periodic Hann, from [`window-function`](https://github.com/scijs/window-function)), `normFloor`, `PI2`.
28
28
 
29
29
  ## See also
30
30
 
package/index.d.ts CHANGED
@@ -3,6 +3,8 @@ export interface StftContext {
3
3
  hop: number; half: number; N: number; fs: number
4
4
  /** Radians per bin, 2π/N. */
5
5
  freqPerBin: number
6
+ /** The frame's first sample in the input: hop − N for the first frame (before the input it reads the input's mirror image), then every hop. */
7
+ pos: number
6
8
  }
7
9
  export interface Spectrum { mag: ArrayLike<number>; phase: ArrayLike<number> }
8
10
  /** State starts empty and persists for the duration of one batch/stream. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/stft",
3
- "version": "1.0.7",
3
+ "version": "2.0.0",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "Canonical STFT — batch / stream / analyse with normalized Hann OLA; stream ≡ batch under any chunking",
@@ -20,7 +20,7 @@
20
20
  ],
21
21
  "dependencies": {
22
22
  "fourier-transform": "^2.3.0",
23
- "@audio/window": "^1.0.2"
23
+ "window-function": "^3.0.0"
24
24
  },
25
25
  "keywords": [
26
26
  "audio",
package/stft.js CHANGED
@@ -1,6 +1,8 @@
1
1
  // One canonical STFT for the @audio ecosystem — batch / stream / analyse.
2
2
  // process(mag, phase, state, ctx) → { mag, phase } per frame; Hann analysis+synthesis
3
3
  // windows, correctly normalized overlap-add, stream ≡ batch under any chunking.
4
+ // ctx.pos: the frame's first sample in the input, hop − N for the first frame (which reads the input's
5
+ // mirror image before its start), then every hop.
4
6
  // Extracted verbatim from @audio/denoise-core (differential-tested there since wave 1).
5
7
  //
6
8
  // Two flavours:
@@ -10,11 +12,14 @@
10
12
  import { fft, ifft } from 'fourier-transform'
11
13
  import { hannWindow, makeStreamBufs, appendIn, growOut, compactIn, take, normFloor, PI2 } from './util.js'
12
14
 
13
- export function wrapPhase(p) { return p - Math.round(p / PI2) * PI2 }
15
+ export function wrapPhase(p) { return p - Math.floor(p / PI2 + 0.5) * PI2 } // floor(x + 0.5) rounds like Math.round here, several times faster in V8 (hot: per bin per frame)
14
16
 
15
17
  function frame(data, pos, win, half, process, state, ctx, sc) {
16
18
  let N = win.length, f = sc.f
17
- for (let i = 0; i < N; i++) f[i] = (data[pos + i] || 0) * win[i]
19
+ // before the input, its mirror image (x[-k] = x[k], numpy's 'reflect'): frames that start early carry the
20
+ // signal's own level and spectrum, so processes that learn from the first frames (a noise profile) learn the
21
+ // signal, not silence
22
+ for (let i = 0; i < N; i++) { let j = pos + i; f[i] = (data[j < 0 ? -j : j] || 0) * win[i] }
18
23
 
19
24
  let [re, im] = fft(f)
20
25
  let mag = sc.mag, phase = sc.phase
@@ -54,14 +59,17 @@ export function stftBatch(data, process, opts) {
54
59
  let norm = new Float32Array(outLen)
55
60
  let state = {}
56
61
  let sc = scratch(N, half)
57
- let pos = 0
62
+ // Frames on the hop grid from the first one that reaches sample 0 (it starts N − hop before it, over the
63
+ // input's mirror image) to the last sample: every sample keeps the steady-state overlap count at both ends. Starting at
64
+ // 0, the first N − hop samples had fewer frames under the normalization floor: a fade-in, -52 dB over the
65
+ // first 5 ms at N = 2048, -8 dB still at 10–20 ms. The tail likewise: bounding by `pos + N <= outLen`
66
+ // stopped N − hop samples early and dropped inputs shorter than N.
67
+ let pos = hop - N
58
68
 
59
- // Start a frame at every hop up to the last sample so the tail keeps the
60
- // steady-state overlap count — bounding by `pos + N <= outLen` would stop
61
- // N−hop samples early (tail under-normalized) and drop inputs shorter than N.
62
69
  while (pos < outLen) {
70
+ ctx.pos = pos
63
71
  let sf = frame(data, pos, win, half, process, state, ctx, sc)
64
- for (let i = 0; i < N && pos + i < outLen; i++) {
72
+ for (let i = Math.max(0, -pos); i < N && pos + i < outLen; i++) {
65
73
  out[pos + i] += sf[i] * win[i]
66
74
  norm[pos + i] += win[i] * win[i]
67
75
  }
@@ -85,12 +93,24 @@ export function stftStream(process, opts) {
85
93
  let state = {}, sc = scratch(N, half)
86
94
  let nf = normFloor(win, hop)
87
95
 
88
- let st = makeStreamBufs(N, nf)
89
- let aPos = 0, flushed = false, fed = 0, sent = 0
96
+ // the first frame starts N − hop before the input, as in stftBatch: the input buffer starts with those
97
+ // N − hop samples, the input's mirror image (written once N − hop + 1 samples have come, frames wait for it),
98
+ // and the output skips them; the grid stays on the hop, so the hold-back stays under a frame
99
+ let P = N - hop, st = makeStreamBufs(N, nf)
100
+ st.il = st.oread = P
101
+ let aPos = 0, flushed = false, fed = 0, sent = 0, mirrored = false
102
+ ctx.pos = hop - N
103
+
104
+ function mirror() {
105
+ for (let k = 1; k <= P; k++) st.ib[P - k] = P + k < st.il ? st.ib[P + k] : 0
106
+ mirrored = true
107
+ }
90
108
 
91
109
  function run() {
110
+ if (!mirrored) { if (fed <= P && !flushed) return; mirror() }
92
111
  while (aPos + N <= st.il) {
93
112
  let sf = frame(st.ib, aPos, win, half, process, state, ctx, sc)
113
+ ctx.pos += hop
94
114
  growOut(st, st.pos + N)
95
115
  let ob = st.ob, nb = st.nb, base = st.pos
96
116
  for (let i = 0; i < N; i++) {
@@ -106,7 +126,7 @@ export function stftStream(process, opts) {
106
126
 
107
127
  return {
108
128
  write(chunk) {
109
- appendIn(st, chunk); run(); fed += chunk.length
129
+ appendIn(st, chunk); fed += chunk.length; run()
110
130
  // samples below pos are final: every later frame starts at pos or beyond, so
111
131
  // emitting them now bounds the hold-back at N − 1 (a frame must fill first)
112
132
  let out = take(st, st.pos)
@@ -114,7 +134,7 @@ export function stftStream(process, opts) {
114
134
  return out
115
135
  },
116
136
  flush() {
117
- if (!flushed) { appendIn(st, new Float32Array(N)); flushed = true }
137
+ if (!flushed) { if (!mirrored) mirror(); appendIn(st, new Float32Array(N)); flushed = true }
118
138
  run()
119
139
  // the padding frames run past the input by up to a hop: stop where the input
120
140
  // stopped, so the stream's total length equals the batch's
package/util.js CHANGED
@@ -1,15 +1,17 @@
1
- // Internal — Hann window (via @audio/window) + stream ring buffers + OLA normalization.
1
+ // Internal — Hann window + stream ring buffers + OLA normalization.
2
2
 
3
- import window from '@audio/window'
3
+ import hann from 'window-function/hann'
4
4
 
5
5
  export const PI2 = Math.PI * 2
6
6
 
7
7
 
8
+ // Periodic (DFT-even) Hann: the N+1-point window without its last point. The one window, not the whole kit.
8
9
  let _hannCache = new Map()
9
10
  export function hannWindow(N) {
10
11
  let w = _hannCache.get(N)
11
12
  if (w) return w
12
- w = window('hann', N, { periodic: true })
13
+ w = new Float64Array(N)
14
+ for (let i = 0; i < N; i++) w[i] = hann(i, N + 1)
13
15
  _hannCache.set(N, w)
14
16
  return w
15
17
  }