@margoengine/audio 0.0.0-stage → 1.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.
Files changed (59) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +987 -2
  4. package/dist/ambisonics.d.ts +54 -0
  5. package/dist/ambisonics.js +118 -0
  6. package/dist/analyser.d.ts +45 -0
  7. package/dist/audio.d.ts +282 -0
  8. package/dist/binaural.d.ts +35 -0
  9. package/dist/binaural.js +123 -0
  10. package/dist/doppler.d.ts +44 -0
  11. package/dist/doppler.js +82 -0
  12. package/dist/errors-D8rc8kzg.js +7 -0
  13. package/dist/events.d.ts +54 -0
  14. package/dist/events.js +78 -0
  15. package/dist/index.d.ts +12 -0
  16. package/dist/index.js +303 -0
  17. package/dist/listener.d.ts +116 -0
  18. package/dist/loader.d.ts +22 -0
  19. package/dist/math-C82bLsED.js +44 -0
  20. package/dist/math.d.ts +19 -0
  21. package/dist/music.d.ts +76 -0
  22. package/dist/music.js +106 -0
  23. package/dist/occlusion.d.ts +50 -0
  24. package/dist/occlusion.js +75 -0
  25. package/dist/outdoor.d.ts +37 -0
  26. package/dist/outdoor.js +52 -0
  27. package/dist/pan.d.ts +21 -0
  28. package/dist/pan.js +28 -0
  29. package/dist/rate-control-BiZ5FnNk.js +4 -0
  30. package/dist/rate-control.d.ts +5 -0
  31. package/dist/resample.d.ts +53 -0
  32. package/dist/resample.js +245 -0
  33. package/dist/scan.d.ts +66 -0
  34. package/dist/scan.js +75 -0
  35. package/dist/sinc-CbjImCBh.js +36 -0
  36. package/dist/sinc.d.ts +5 -0
  37. package/dist/spectrogram-fft-CRLetuHA.js +59 -0
  38. package/dist/spectrogram-fft.d.ts +26 -0
  39. package/dist/spectrogram-windows.d.ts +24 -0
  40. package/dist/spectrogram.d.ts +143 -0
  41. package/dist/spectrogram.js +384 -0
  42. package/dist/sprites.d.ts +59 -0
  43. package/dist/sprites.js +99 -0
  44. package/dist/stretch.d.ts +25 -0
  45. package/dist/stretch.js +55 -0
  46. package/dist/vocoder-envelope.d.ts +5 -0
  47. package/dist/vocoder-phase.d.ts +7 -0
  48. package/dist/vocoder-split.d.ts +10 -0
  49. package/dist/vocoder.d.ts +59 -0
  50. package/dist/vocoder.js +291 -0
  51. package/dist/voice-manager.d.ts +100 -0
  52. package/dist/voice-manager.js +178 -0
  53. package/dist/voices-CdfPmzIo.js +425 -0
  54. package/dist/voices.d.ts +45 -0
  55. package/dist/wav.d.ts +47 -0
  56. package/dist/wav.js +156 -0
  57. package/dist/zones.d.ts +43 -0
  58. package/dist/zones.js +75 -0
  59. package/package.json +130 -4
@@ -0,0 +1,4 @@
1
+ //#region src/rate-control.ts
2
+ var e = /* @__PURE__ */ new WeakMap();
3
+ //#endregion
4
+ export { e as t };
@@ -0,0 +1,5 @@
1
+ /** Internal authored-rate validation and modulation shared by sounds and optional controllers. */
2
+ export declare const playbackRateModifiers: WeakMap<object, {
3
+ apply(value: number): number;
4
+ validate(value: number): void;
5
+ }>;
@@ -0,0 +1,53 @@
1
+ /** Audio as `decodeWav` returns it: a rate and one Float32Array per channel. */
2
+ export interface ResampleInput {
3
+ sampleRate: number;
4
+ channels: Float32Array[];
5
+ }
6
+ /** What {@link resample} returns. */
7
+ export interface ResampledAudio {
8
+ sampleRate: number;
9
+ channels: Float32Array[];
10
+ }
11
+ /** Options of {@link resample}. */
12
+ export interface ResampleOptions {
13
+ /**
14
+ * Output channel count, mixed by {@link mixChannels} (the Web Audio speaker rules for 1, 2, 4 and 6 channels,
15
+ * discrete otherwise). Default: the input's count.
16
+ */
17
+ channels?: number;
18
+ /** Zero crossings of the windowed sinc on each side of an output sample, 4 to 256. Default 32. */
19
+ zeroCrossings?: number;
20
+ /**
21
+ * The filter's cutoff as a share of the lower of the two Nyquist frequencies, 0.5 to 1. Default 0.92: with 32 zero
22
+ * crossings and a 90 dB Kaiser window a tone at or above the lower Nyquist frequency comes out at least 90 dB down,
23
+ * so nothing folds back, and the response is flat within 0.1 dB to 0.86 of it (19.0 kHz for 44.1 kHz to 48 kHz).
24
+ */
25
+ rolloff?: number;
26
+ /** Stopband attenuation of the Kaiser window in dB, 40 to 160. Default 90. */
27
+ attenuation?: number;
28
+ }
29
+ /**
30
+ * Channels mixed to `count` by the Web Audio speaker rules: mono, stereo, quad (L, R, SL, SR) and 5.1 (L, R, C, LFE,
31
+ * SL, SR) up and down; any other pair is discrete (channels copied in order, missing ones silent, extra ones dropped).
32
+ * Returns new arrays.
33
+ *
34
+ * @example
35
+ * import {mixChannels} from '@margoengine/audio/resample';
36
+ * const [mono] = mixChannels([new Float32Array([1, 0.5]), new Float32Array([0, 0.5])], 1);
37
+ * console.log([...mono]); // [0.5, 0.5]
38
+ */
39
+ export declare function mixChannels(channels: readonly Float32Array[], count: number): Float32Array[];
40
+ /**
41
+ * Converts audio to `sampleRate` by bandlimited interpolation (a Kaiser-windowed sinc, Smith and Gossett 1984), on
42
+ * plain Float32Array channels, in Node or a page. The output holds `ceil(length * sampleRate / input rate)` samples,
43
+ * so one second stays one second; samples before the start and after the end count as silence. Converting to the
44
+ * same rate copies the samples. A rate drop also filters out what the new rate cannot hold, so nothing aliases.
45
+ *
46
+ * @example
47
+ * import {resample} from '@margoengine/audio/resample';
48
+ * const take = {sampleRate: 44100, channels: [new Float32Array(44100)]};
49
+ * const voice = resample(take, 48000, {channels: 2});
50
+ * console.log(voice.sampleRate, voice.channels.length, voice.channels[0].length); // 48000 2 48000
51
+ * @throws RangeError for a rate that is not a positive integer up to 768000, or an option out of range.
52
+ */
53
+ export declare function resample(audio: ResampleInput, sampleRate: number, options?: ResampleOptions): ResampledAudio;
@@ -0,0 +1,245 @@
1
+ import { i as e, r as t } from "./errors-D8rc8kzg.js";
2
+ import { t as n } from "./sinc-CbjImCBh.js";
3
+ //#region src/resample.ts
4
+ var r = Math.SQRT1_2;
5
+ function i(n, i) {
6
+ if (!Array.isArray(n) || !n.length || !n.every((e) => e instanceof Float32Array)) throw e("Pass the channels as an array of Float32Array", "audio-resample");
7
+ let a = n[0].length;
8
+ if (!n.every((e) => e.length === a)) throw t("Every channel must hold the same number of samples", "audio-resample");
9
+ if (!Number.isInteger(i) || i < 1 || i > 32) throw t("Pass an output channel count from 1 to 32", "audio-resample");
10
+ let o = n.length, s = Array.from({ length: i }, () => new Float32Array(a)), c = (e, t, r) => {
11
+ let i = s[e], o = n[t];
12
+ for (let e = 0; e < a; e++) i[e] += r * o[e];
13
+ }, l = o === i ? void 0 : {
14
+ "1>2": [[
15
+ 0,
16
+ 0,
17
+ 1
18
+ ], [
19
+ 1,
20
+ 0,
21
+ 1
22
+ ]],
23
+ "1>4": [[
24
+ 0,
25
+ 0,
26
+ 1
27
+ ], [
28
+ 1,
29
+ 0,
30
+ 1
31
+ ]],
32
+ "1>6": [[
33
+ 2,
34
+ 0,
35
+ 1
36
+ ]],
37
+ "2>4": [[
38
+ 0,
39
+ 0,
40
+ 1
41
+ ], [
42
+ 1,
43
+ 1,
44
+ 1
45
+ ]],
46
+ "2>6": [[
47
+ 0,
48
+ 0,
49
+ 1
50
+ ], [
51
+ 1,
52
+ 1,
53
+ 1
54
+ ]],
55
+ "4>6": [
56
+ [
57
+ 0,
58
+ 0,
59
+ 1
60
+ ],
61
+ [
62
+ 1,
63
+ 1,
64
+ 1
65
+ ],
66
+ [
67
+ 4,
68
+ 2,
69
+ 1
70
+ ],
71
+ [
72
+ 5,
73
+ 3,
74
+ 1
75
+ ]
76
+ ],
77
+ "2>1": [[
78
+ 0,
79
+ 0,
80
+ .5
81
+ ], [
82
+ 0,
83
+ 1,
84
+ .5
85
+ ]],
86
+ "4>1": [
87
+ [
88
+ 0,
89
+ 0,
90
+ .25
91
+ ],
92
+ [
93
+ 0,
94
+ 1,
95
+ .25
96
+ ],
97
+ [
98
+ 0,
99
+ 2,
100
+ .25
101
+ ],
102
+ [
103
+ 0,
104
+ 3,
105
+ .25
106
+ ]
107
+ ],
108
+ "6>1": [
109
+ [
110
+ 0,
111
+ 0,
112
+ r
113
+ ],
114
+ [
115
+ 0,
116
+ 1,
117
+ r
118
+ ],
119
+ [
120
+ 0,
121
+ 2,
122
+ 1
123
+ ],
124
+ [
125
+ 0,
126
+ 4,
127
+ .5
128
+ ],
129
+ [
130
+ 0,
131
+ 5,
132
+ .5
133
+ ]
134
+ ],
135
+ "4>2": [
136
+ [
137
+ 0,
138
+ 0,
139
+ .5
140
+ ],
141
+ [
142
+ 0,
143
+ 2,
144
+ .5
145
+ ],
146
+ [
147
+ 1,
148
+ 1,
149
+ .5
150
+ ],
151
+ [
152
+ 1,
153
+ 3,
154
+ .5
155
+ ]
156
+ ],
157
+ "6>2": [
158
+ [
159
+ 0,
160
+ 0,
161
+ 1
162
+ ],
163
+ [
164
+ 0,
165
+ 2,
166
+ r
167
+ ],
168
+ [
169
+ 0,
170
+ 4,
171
+ r
172
+ ],
173
+ [
174
+ 1,
175
+ 1,
176
+ 1
177
+ ],
178
+ [
179
+ 1,
180
+ 2,
181
+ r
182
+ ],
183
+ [
184
+ 1,
185
+ 5,
186
+ r
187
+ ]
188
+ ],
189
+ "6>4": [
190
+ [
191
+ 0,
192
+ 0,
193
+ 1
194
+ ],
195
+ [
196
+ 0,
197
+ 2,
198
+ r
199
+ ],
200
+ [
201
+ 1,
202
+ 1,
203
+ 1
204
+ ],
205
+ [
206
+ 1,
207
+ 2,
208
+ r
209
+ ],
210
+ [
211
+ 2,
212
+ 4,
213
+ 1
214
+ ],
215
+ [
216
+ 3,
217
+ 5,
218
+ 1
219
+ ]
220
+ ]
221
+ }[`${o}>${i}`];
222
+ if (l) for (let [e, t, n] of l) c(e, t, n);
223
+ else for (let e = 0; e < Math.min(o, i); e++) s[e].set(n[e]);
224
+ return s;
225
+ }
226
+ function a(r, a, o = {}) {
227
+ if (!r || typeof r != "object") throw e("Pass {sampleRate, channels} audio", "audio-resample");
228
+ let { zeroCrossings: s = 32, rolloff: c = .92, attenuation: l = 90 } = o;
229
+ for (let e of [r.sampleRate, a]) if (!Number.isInteger(e) || e < 1 || e > 768e3) throw t("Sample rates must be whole numbers from 1 to 768000", "audio-resample");
230
+ if (!Number.isInteger(s) || s < 4 || s > 256) throw t("zeroCrossings must be a whole number from 4 to 256", "audio-resample");
231
+ if (!(c >= .5 && c <= 1)) throw t("rolloff must be from 0.5 to 1", "audio-resample");
232
+ if (!(l >= 40 && l <= 160)) throw t("attenuation must be from 40 to 160 dB", "audio-resample");
233
+ let u = i(r.channels, o.channels ?? r.channels.length), d = r.sampleRate, f = u[0].length;
234
+ if (d === a) return {
235
+ sampleRate: a,
236
+ channels: u
237
+ };
238
+ let p = Math.floor((f * a + d - 1) / d), m = u.map(() => new Float32Array(p));
239
+ return n(d, a, s, c, l)(u, m, 0, p), {
240
+ sampleRate: a,
241
+ channels: m
242
+ };
243
+ }
244
+ //#endregion
245
+ export { i as mixChannels, a as resample };
package/dist/scan.d.ts ADDED
@@ -0,0 +1,66 @@
1
+ /** One clipped run: at least `run` samples in a row on one channel at or over `level` of full scale (either sign). */
2
+ export interface ClipRun {
3
+ /** The channel index. */
4
+ channel: number;
5
+ /** First sample of the run. */
6
+ startSample: number;
7
+ /** Samples in the run. */
8
+ length: number;
9
+ /** Seconds from the start: `startSample / sampleRate`. */
10
+ start: number;
11
+ /** Seconds: `(startSample + length) / sampleRate`. */
12
+ end: number;
13
+ }
14
+ /** Options of {@link scanClipping}. */
15
+ export interface ClippingOptions {
16
+ /** Share of full scale (0 to 1) a sample's magnitude must reach to sit at the rail. Default 0.999 (-0.009 dBFS
17
+ * sample peak), which also catches clipped audio that went through a lossy codec. */
18
+ level?: number;
19
+ /** Samples in a row at the rail that make a clip. Default 3: one full-scale sample is a peak, not a clip. */
20
+ run?: number;
21
+ }
22
+ /** What {@link scanClipping} gives back. */
23
+ export interface ClippingReport {
24
+ /** Every clipped run, ordered by start time, then channel. */
25
+ runs: ClipRun[];
26
+ /** Samples inside the runs, all channels. */
27
+ clippedSamples: number;
28
+ /** The largest |sample| anywhere (the sample peak, linear). */
29
+ peak: number;
30
+ }
31
+ /** Options of {@link findSilences}. */
32
+ export interface SilenceOptions {
33
+ /** A window whose level (dBFS, AES17: a full-scale sine is 0) is under this is silent. Default -50. */
34
+ floorDb?: number;
35
+ /** Silences shorter than this many seconds are not reported (pauses between words). Default 2. */
36
+ minSeconds?: number;
37
+ /** Window length in seconds. Default 0.01: 10 ms, so edges are found to within 10 ms. */
38
+ window?: number;
39
+ /** Only look inside these `[start, end]` spans in seconds (a section of a script, say); a silence never crosses a
40
+ * span's edge. Default: the whole signal. */
41
+ spans?: ReadonlyArray<readonly [number, number]>;
42
+ }
43
+ /** One silence: windows under the floor, in a row, lasting at least `minSeconds`. */
44
+ export interface Silence {
45
+ /** Seconds from the start of the signal. */
46
+ start: number;
47
+ /** Seconds from the start of the signal. */
48
+ end: number;
49
+ /** `end - start`. */
50
+ duration: number;
51
+ /** The loudest window inside, in dBFS (AES17); -Infinity when every sample is zero. */
52
+ level: number;
53
+ }
54
+ type Channels = ArrayLike<number> | ReadonlyArray<ArrayLike<number>>;
55
+ /**
56
+ * Finds clipped runs in decoded audio: `run` or more samples in a row on one channel whose magnitude is at or over
57
+ * `level` of full scale. `channels` is one channel or an array of them (`decodeWav(file).channels`).
58
+ */
59
+ export declare function scanClipping(channels: Channels, sampleRate: number, options?: ClippingOptions): ClippingReport;
60
+ /**
61
+ * Finds silences: runs of windows whose level (the mean square of all channels, in dBFS as AES17 defines it) is under
62
+ * `floorDb`, lasting at least `minSeconds`, inside `spans` when given. A trailing partial window counts by its own
63
+ * samples.
64
+ */
65
+ export declare function findSilences(samples: Channels, sampleRate: number, options?: SilenceOptions): Silence[];
66
+ export {};
package/dist/scan.js ADDED
@@ -0,0 +1,75 @@
1
+ import { i as e, r as t } from "./errors-D8rc8kzg.js";
2
+ //#region src/scan.ts
3
+ function n(n, r) {
4
+ let i = (e) => ArrayBuffer.isView(e) || Array.isArray(e) && e.length > 0 && typeof e[0] == "number", a = i(n) ? [n] : Array.isArray(n) ? n : null;
5
+ if (!a || !a.length || !a.every(i)) throw e(`${r} needs a channel (a Float32Array) or an array of channels`, "audio-scan");
6
+ if (a.some((e) => e.length !== a[0].length)) throw t(`${r} needs channels of one length`, "audio-scan");
7
+ return a;
8
+ }
9
+ var r = (e, n) => {
10
+ if (!(Number.isFinite(e) && e > 0)) throw t(`${n} needs a sampleRate above 0`, "audio-scan");
11
+ };
12
+ function i(e, i, a = {}) {
13
+ let o = n(e, "scanClipping");
14
+ r(i, "scanClipping");
15
+ let { level: s = .999, run: c = 3 } = a;
16
+ if (!(s > 0 && s <= 1)) throw t("scanClipping needs a level above 0 and at most 1", "audio-scan");
17
+ if (!(Number.isInteger(c) && c >= 1)) throw t("scanClipping needs a run of 1 or more samples", "audio-scan");
18
+ let l = [], u = 0, d = 0;
19
+ return o.forEach((e, t) => {
20
+ let n = 0;
21
+ for (let r = 0; r <= e.length; r++) {
22
+ let a = r < e.length ? Math.abs(e[r]) : 0;
23
+ if (a > d && (d = a), a >= s) n++;
24
+ else {
25
+ if (n >= c) {
26
+ let e = r - n;
27
+ l.push({
28
+ channel: t,
29
+ startSample: e,
30
+ length: n,
31
+ start: e / i,
32
+ end: r / i
33
+ }), u += n;
34
+ }
35
+ n = 0;
36
+ }
37
+ }
38
+ }), l.sort((e, t) => e.startSample - t.startSample || e.channel - t.channel), {
39
+ runs: l,
40
+ clippedSamples: u,
41
+ peak: d
42
+ };
43
+ }
44
+ function a(e, i, a = {}) {
45
+ let o = n(e, "findSilences");
46
+ r(i, "findSilences");
47
+ let { floorDb: s = -50, minSeconds: c = 2, window: l = .01 } = a;
48
+ if (!Number.isFinite(s)) throw t("findSilences needs a finite floorDb", "audio-scan");
49
+ if (!(c >= 0 && c < Infinity)) throw t("findSilences needs minSeconds of 0 or more", "audio-scan");
50
+ let u = Math.round(l * i);
51
+ if (!(u >= 1)) throw t("findSilences needs a window of at least one sample", "audio-scan");
52
+ let d = o[0].length, f = d / i, p = a.spans ?? [[0, f]];
53
+ for (let e of p) if (!(e.length === 2 && Number.isFinite(e[0]) && Number.isFinite(e[1]) && e[0] <= e[1])) throw t("findSilences needs spans of [start, end] seconds with start <= end", "audio-scan");
54
+ let m = .5 * 10 ** (s / 10), h = (e) => 10 * Math.log10(2 * e), g = [];
55
+ for (let [e, t] of p) {
56
+ let n = Math.max(0, Math.round(e * i)), r = Math.min(d, Math.round(t * i)), a = -1, s = 0, l = (e) => {
57
+ a >= 0 && (e - a) / i >= c && g.push({
58
+ start: a / i,
59
+ end: e / i,
60
+ duration: (e - a) / i,
61
+ level: s > 0 ? h(s) : -Infinity
62
+ }), a = -1, s = 0;
63
+ };
64
+ for (let e = n; e < r; e += u) {
65
+ let t = Math.min(e + u, r), n = 0;
66
+ for (let r of o) for (let i = e; i < t; i++) n += r[i] * r[i];
67
+ let i = n / ((t - e) * o.length);
68
+ i < m ? (a < 0 && (a = e), i > s && (s = i)) : l(e);
69
+ }
70
+ l(r);
71
+ }
72
+ return g.sort((e, t) => e.start - t.start);
73
+ }
74
+ //#endregion
75
+ export { a as findSilences, i as scanClipping };
@@ -0,0 +1,36 @@
1
+ //#region src/sinc.ts
2
+ var e = 1024;
3
+ function t(e) {
4
+ let t = 1, n = 1, r = e * e / 4;
5
+ for (let e = 1; e < 64 && (n *= r / (e * e), t += n, !(n < t * 1e-17)); e++);
6
+ return t;
7
+ }
8
+ function n(n, r) {
9
+ let i = n * e, a = new Float64Array(i + 2), o = t(r);
10
+ a[0] = 1;
11
+ for (let s = 1; s <= i; s++) {
12
+ let i = s / e, c = i / n, l = c < 1 ? t(r * Math.sqrt(1 - c * c)) / o : 0;
13
+ a[s] = Math.sin(Math.PI * i) / (Math.PI * i) * l;
14
+ }
15
+ return a;
16
+ }
17
+ function r(t, r, i = 32, a = .92, o = 90) {
18
+ let s = a * Math.min(1, r / t), c = i / s, l = n(i, o > 50 ? .1102 * (o - 8.7) : .5842 * (o - 21) ** .4 + .07886 * (o - 21)), u = new Float64Array(Math.ceil(2 * c) + 2);
19
+ return (n, i, a, o) => {
20
+ let d = n[0].length;
21
+ for (let f = a; f < o; f++) {
22
+ let a = f * t / r, o = Math.max(0, Math.ceil(a - c)), p = Math.min(d - 1, Math.floor(a + c)), m = 0;
23
+ for (let t = o; t <= p; t++) {
24
+ let n = Math.abs(a - t) * s * e, r = Math.floor(n), i = n - r;
25
+ u[m++] = r >= l.length - 1 ? 0 : s * (l[r] + (l[r + 1] - l[r]) * i);
26
+ }
27
+ for (let e = 0; e < n.length; e++) {
28
+ let t = n[e], r = 0;
29
+ for (let e = 0; e < m; e++) r += u[e] * t[o + e];
30
+ i[e][f] = r;
31
+ }
32
+ }
33
+ };
34
+ }
35
+ //#endregion
36
+ export { r as t };
package/dist/sinc.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * A renderer whose output sample `n` is the input read at time `n * step / per` samples. `render` fills output samples
3
+ * `from` to `to` (exclusive) of every channel, so a long signal can be converted in blocks with the same result.
4
+ */
5
+ export declare function sincRenderer(step: number, per: number, zeroCrossings?: number, rolloff?: number, attenuation?: number): (channels: readonly Float32Array[], out: readonly Float32Array[], from: number, to: number) => void;
@@ -0,0 +1,59 @@
1
+ import { r as e } from "./errors-D8rc8kzg.js";
2
+ //#region src/spectrogram-fft.ts
3
+ var t = /* @__PURE__ */ new Map();
4
+ function n(n) {
5
+ let r = t.get(n);
6
+ if (r) return r;
7
+ if (!Number.isInteger(n) || n < 2 || n > 65536 || n & n - 1) throw e(`FFT size must be a power of two from 2 to 65536, got ${n}`, "state");
8
+ let i = new Float64Array(n / 2), a = new Float64Array(n / 2), o = new Uint32Array(n), s = Math.log2(n);
9
+ for (let e = 0; e < n / 2; e++) i[e] = Math.cos(2 * Math.PI * e / n), a[e] = -Math.sin(2 * Math.PI * e / n);
10
+ for (let e = 0; e < n; e++) {
11
+ let t = 0;
12
+ for (let n = 0; n < s; n++) t |= (e >> n & 1) << s - 1 - n;
13
+ o[e] = t;
14
+ }
15
+ return t.size >= 8 && t.delete(t.keys().next().value), t.set(n, r = {
16
+ size: n,
17
+ cos: i,
18
+ sin: a,
19
+ reverse: o
20
+ }), r;
21
+ }
22
+ function r(e, t, n, r = !1) {
23
+ let { size: i, cos: a, sin: o, reverse: s } = e;
24
+ for (let e = 0; e < i; e++) {
25
+ let r = s[e];
26
+ if (r > e) {
27
+ let i = t[e];
28
+ t[e] = t[r], t[r] = i, i = n[e], n[e] = n[r], n[r] = i;
29
+ }
30
+ }
31
+ let c = r ? -1 : 1;
32
+ for (let e = 2; e <= i; e *= 2) {
33
+ let r = e / 2, s = i / e;
34
+ for (let l = 0; l < i; l += e) for (let e = 0, i = 0; e < r; e++, i += s) {
35
+ let s = a[i], u = c * o[i], d = l + e, f = d + r, p = t[f] * s - n[f] * u, m = t[f] * u + n[f] * s;
36
+ t[f] = t[d] - p, n[f] = n[d] - m, t[d] += p, n[d] += m;
37
+ }
38
+ }
39
+ }
40
+ function i(e, t, n, i, a, o, s, c, l) {
41
+ let u = e.size;
42
+ i.set(t), a.set(n), r(e, i, a);
43
+ for (let e = 0; e <= u / 2; e++) {
44
+ let t = u - e & u - 1;
45
+ o[e] = (i[e] + i[t]) / 2, s[e] = (a[e] - a[t]) / 2, c[e] = (a[e] + a[t]) / 2, l[e] = (i[t] - i[e]) / 2;
46
+ }
47
+ }
48
+ function a(e, t, n, i, a, o, s, c, l) {
49
+ let u = e.size;
50
+ for (let e = 0; e <= u / 2; e++) {
51
+ let r = u - e & u - 1, c = e === 0 || e === u / 2 ? 0 : n[e], l = e === 0 || e === u / 2 ? 0 : a[e];
52
+ o[e] = t[e] - l, s[e] = c + i[e], o[r] = t[e] + l, s[r] = -c + i[e];
53
+ }
54
+ r(e, o, s, !0);
55
+ let d = 1 / u;
56
+ for (let e = 0; e < u; e++) c[e] = o[e] * d, l[e] = s[e] * d;
57
+ }
58
+ //#endregion
59
+ export { i, n, a as r, r as t };
@@ -0,0 +1,26 @@
1
+ /** Twiddles and bit-reversal order of one transform size. */
2
+ export interface FftPlan {
3
+ readonly size: number;
4
+ readonly cos: Float64Array;
5
+ readonly sin: Float64Array;
6
+ readonly reverse: Uint32Array;
7
+ }
8
+ /** The cached plan for a power-of-two `size` from 2 to 65536 (an FFT of 32 to 65536 points is what the entry accepts). */
9
+ export declare function fftPlan(size: number): FftPlan;
10
+ /**
11
+ * Complex transform of re/im in place: forward (exp(-2 pi i k n / N), unscaled) or inverse (conjugate twiddles, unscaled).
12
+ * Work: (N/2) log2 N butterflies.
13
+ */
14
+ export declare function fft(plan: FftPlan, re: Float64Array, im: Float64Array, inverse?: boolean): void;
15
+ /** Butterflies of one complex transform of `size` points, the unit of the entry's work counts. */
16
+ export declare const butterflies: (size: number) => number;
17
+ /**
18
+ * Forward transforms of two real frames `a` and `b` (length N) at once; writes bins 0..N/2 of each into
19
+ * (aRe, aIm) and (bRe, bIm), which hold at least N/2 + 1 values. `zr` and `zi` are N-point scratch.
20
+ */
21
+ export declare function realPair(plan: FftPlan, a: Float64Array, b: Float64Array, zr: Float64Array, zi: Float64Array, aRe: Float64Array, aIm: Float64Array, bRe: Float64Array, bIm: Float64Array): void;
22
+ /**
23
+ * Inverse transforms of two Hermitian spectra (bins 0..N/2 given) at once, scaled by 1/N: the real frames land in
24
+ * `a` and `b` (length N). `zr` and `zi` are N-point scratch.
25
+ */
26
+ export declare function inverseRealPair(plan: FftPlan, aRe: Float64Array, aIm: Float64Array, bRe: Float64Array, bIm: Float64Array, zr: Float64Array, zi: Float64Array, a: Float64Array, b: Float64Array): void;
@@ -0,0 +1,24 @@
1
+ /** Window names understood by `stft`: `'hann'` (default), `'hamming'`, `'blackman'`, `'blackman-harris'`, `'kaiser'` or `'rectangular'`. */
2
+ export type WindowName = 'hann' | 'hamming' | 'blackman' | 'blackman-harris' | 'kaiser' | 'rectangular';
3
+ /** Options for the window builders. */
4
+ export interface WindowOptions {
5
+ /** Symmetric (filter design) instead of periodic (spectral analysis) form. Default false. */
6
+ readonly symmetric?: boolean;
7
+ }
8
+ /** Window builders. Each returns a Float64Array of `size` coefficients. */
9
+ export declare const windows: {
10
+ /** Rectangular window, all ones. */
11
+ readonly rectangular: (size: number) => Float64Array;
12
+ /** Hann: 0.5 - 0.5 cos(2 pi n / N). */
13
+ readonly hann: (size: number, options?: WindowOptions) => Float64Array<ArrayBufferLike>;
14
+ /** Hamming: 0.54 - 0.46 cos(2 pi n / N). */
15
+ readonly hamming: (size: number, options?: WindowOptions) => Float64Array<ArrayBufferLike>;
16
+ /** Blackman, the three-term form with Harris's rounded coefficients 0.42, 0.5, 0.08. */
17
+ readonly blackman: (size: number, options?: WindowOptions) => Float64Array<ArrayBufferLike>;
18
+ /** The four-term minimum-sidelobe Blackman-Harris window (-92 dB sidelobes). */
19
+ readonly blackmanHarris: (size: number, options?: WindowOptions) => Float64Array<ArrayBufferLike>;
20
+ /** Kaiser-Bessel with shape `beta` (default 8.6, about -80 dB sidelobes): I0(beta sqrt(1 - r^2)) / I0(beta). */
21
+ readonly kaiser: (size: number, beta?: number, options?: WindowOptions) => Float64Array;
22
+ };
23
+ /** A window by name, or the coefficients themselves (which must hold `size` numbers). */
24
+ export declare function windowOf(window: WindowName | ArrayLike<number> | undefined, size: number): Float64Array;