@audio/stft 1.0.6 → 1.0.7
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/index.d.ts +21 -0
- package/package.json +10 -5
- package/stft.js +12 -4
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,18 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@audio/stft",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
|
-
"description": "Canonical STFT
|
|
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
|
-
".":
|
|
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",
|
|
@@ -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
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
}
|