@audio/stft 1.0.6 → 1.0.8

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
@@ -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 ADDED
@@ -0,0 +1,21 @@
1
+ export interface StftOptions { frameSize?: number; hopSize?: number; fs?: number }
2
+ export interface StftContext {
3
+ hop: number; half: number; N: number; fs: number
4
+ /** Radians per bin, 2π/N. */
5
+ freqPerBin: number
6
+ }
7
+ export interface Spectrum { mag: ArrayLike<number>; phase: ArrayLike<number> }
8
+ /** State starts empty and persists for the duration of one batch/stream. */
9
+ export type SpectralProcessor = (mag: Float64Array, phase: Float64Array, state: Record<string, unknown>, ctx: StftContext) => Spectrum
10
+ export function stftBatch(data: ArrayLike<number>, process: SpectralProcessor, options?: StftOptions): Float32Array
11
+ export function stftStream(process: SpectralProcessor, options?: StftOptions): {
12
+ write(chunk: ArrayLike<number>): Float32Array
13
+ flush(): Float32Array
14
+ }
15
+ /** Visits complete frames only; pos is the frame's sample offset. */
16
+ export function stftAnalyse(data: ArrayLike<number>, visit: (mag: Float64Array, phase: Float64Array, pos: number) => void, options?: StftOptions): void
17
+ export function wrapPhase(phase: number): number
18
+ /** Returns a cached periodic Hann window. */
19
+ export function hannWindow(length: number): Float64Array
20
+ export function normFloor(win: ArrayLike<number>, hop: number): number
21
+ export const PI2: number
package/package.json CHANGED
@@ -1,22 +1,26 @@
1
1
  {
2
2
  "name": "@audio/stft",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
- "description": "Canonical STFT \u2014 batch / stream / analyse with normalized Hann OLA; stream \u2261 batch under any chunking",
6
+ "description": "Canonical STFT — batch / stream / analyse with normalized Hann OLA; stream ≡ batch under any chunking",
7
7
  "main": "index.js",
8
8
  "exports": {
9
- ".": "./index.js",
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "default": "./index.js"
12
+ },
10
13
  "./package.json": "./package.json"
11
14
  },
12
15
  "files": [
13
16
  "index.js",
14
17
  "stft.js",
15
- "util.js"
18
+ "util.js",
19
+ "*.d.ts"
16
20
  ],
17
21
  "dependencies": {
18
22
  "fourier-transform": "^2.3.0",
19
- "@audio/window": "^1.0.2"
23
+ "window-function": "^3.0.0"
20
24
  },
21
25
  "keywords": [
22
26
  "audio",
@@ -48,5 +52,6 @@
48
52
  "devDependencies": {
49
53
  "tst": "^9.4.0"
50
54
  },
51
- "funding": "https://github.com/sponsors/audiojs"
55
+ "funding": "https://github.com/sponsors/audiojs",
56
+ "types": "./index.d.ts"
52
57
  }
package/stft.js CHANGED
@@ -10,7 +10,7 @@
10
10
  import { fft, ifft } from 'fourier-transform'
11
11
  import { hannWindow, makeStreamBufs, appendIn, growOut, compactIn, take, normFloor, PI2 } from './util.js'
12
12
 
13
- export function wrapPhase(p) { return p - Math.round(p / PI2) * PI2 }
13
+ 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
14
 
15
15
  function frame(data, pos, win, half, process, state, ctx, sc) {
16
16
  let N = win.length, f = sc.f
@@ -86,7 +86,7 @@ export function stftStream(process, opts) {
86
86
  let nf = normFloor(win, hop)
87
87
 
88
88
  let st = makeStreamBufs(N, nf)
89
- let aPos = 0, flushed = false
89
+ let aPos = 0, flushed = false, fed = 0, sent = 0
90
90
 
91
91
  function run() {
92
92
  while (aPos + N <= st.il) {
@@ -106,13 +106,21 @@ export function stftStream(process, opts) {
106
106
 
107
107
  return {
108
108
  write(chunk) {
109
- appendIn(st, chunk); run()
110
- return take(st, Math.max(0, st.pos - N + hop))
109
+ appendIn(st, chunk); run(); fed += chunk.length
110
+ // samples below pos are final: every later frame starts at pos or beyond, so
111
+ // emitting them now bounds the hold-back at N − 1 (a frame must fill first)
112
+ let out = take(st, st.pos)
113
+ sent += out.length
114
+ return out
111
115
  },
112
116
  flush() {
113
117
  if (!flushed) { appendIn(st, new Float32Array(N)); flushed = true }
114
118
  run()
115
- return take(st, st.pos)
119
+ // the padding frames run past the input by up to a hop: stop where the input
120
+ // stopped, so the stream's total length equals the batch's
121
+ let out = take(st, st.pos).subarray(0, Math.max(0, fed - sent))
122
+ sent += out.length
123
+ return out
116
124
  }
117
125
  }
118
126
  }
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
  }