@margoengine/audio 0.1.0 → 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.
@@ -1,64 +1,8 @@
1
1
  import { i as e, r as t, t as n } from "./errors-D8rc8kzg.js";
2
- //#region src/spectrogram-fft.ts
3
- var r = /* @__PURE__ */ new Map();
4
- function i(e) {
5
- let n = r.get(e);
6
- if (n) return n;
7
- if (!Number.isInteger(e) || e < 2 || e > 65536 || e & e - 1) throw t(`FFT size must be a power of two from 2 to 65536, got ${e}`, "state");
8
- let i = new Float64Array(e / 2), a = new Float64Array(e / 2), o = new Uint32Array(e), s = Math.log2(e);
9
- for (let t = 0; t < e / 2; t++) i[t] = Math.cos(2 * Math.PI * t / e), a[t] = -Math.sin(2 * Math.PI * t / e);
10
- for (let t = 0; t < e; t++) {
11
- let e = 0;
12
- for (let n = 0; n < s; n++) e |= (t >> n & 1) << s - 1 - n;
13
- o[t] = e;
14
- }
15
- return r.size >= 8 && r.delete(r.keys().next().value), r.set(e, n = {
16
- size: e,
17
- cos: i,
18
- sin: a,
19
- reverse: o
20
- }), n;
21
- }
22
- function a(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 o(e, t, n, r, i, o, s, c, l) {
41
- let u = e.size;
42
- r.set(t), i.set(n), a(e, r, i);
43
- for (let e = 0; e <= u / 2; e++) {
44
- let t = u - e & u - 1;
45
- o[e] = (r[e] + r[t]) / 2, s[e] = (i[e] - i[t]) / 2, c[e] = (i[e] + i[t]) / 2, l[e] = (r[t] - r[e]) / 2;
46
- }
47
- }
48
- function s(e, t, n, r, i, o, s, c, l) {
49
- let u = e.size;
50
- for (let e = 0; e <= u / 2; e++) {
51
- let a = u - e & u - 1, c = e === 0 || e === u / 2 ? 0 : n[e], l = e === 0 || e === u / 2 ? 0 : i[e];
52
- o[e] = t[e] - l, s[e] = c + r[e], o[a] = t[e] + l, s[a] = -c + r[e];
53
- }
54
- a(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
2
+ import { i as r, n as i, r as a } from "./spectrogram-fft-CRLetuHA.js";
59
3
  //#region src/spectrogram-windows.ts
60
- function c(e, t, n) {
61
- l(e);
4
+ function o(e, t, n) {
5
+ s(e);
62
6
  let r = new Float64Array(e), i = n.symmetric ? Math.max(e - 1, 1) : e;
63
7
  for (let n = 0; n < e; n++) {
64
8
  let e = 0;
@@ -67,59 +11,59 @@ function c(e, t, n) {
67
11
  }
68
12
  return r;
69
13
  }
70
- function l(e) {
14
+ function s(e) {
71
15
  if (!Number.isInteger(e) || e < 1) throw t(`Window size must be a whole number of at least 1, got ${e}`, "state");
72
16
  }
73
- function u(e) {
17
+ function c(e) {
74
18
  let t = 1, n = 1;
75
19
  for (let r = 1; r < 200 && n > 1e-17 * t; r++) n *= (e / (2 * r)) ** 2, t += n;
76
20
  return t;
77
21
  }
78
- var d = {
22
+ var l = {
79
23
  rectangular(e) {
80
- return l(e), new Float64Array(e).fill(1);
24
+ return s(e), new Float64Array(e).fill(1);
81
25
  },
82
- hann: (e, t = {}) => c(e, [.5, .5], t),
83
- hamming: (e, t = {}) => c(e, [.54, .46], t),
84
- blackman: (e, t = {}) => c(e, [
26
+ hann: (e, t = {}) => o(e, [.5, .5], t),
27
+ hamming: (e, t = {}) => o(e, [.54, .46], t),
28
+ blackman: (e, t = {}) => o(e, [
85
29
  .42,
86
30
  .5,
87
31
  .08
88
32
  ], t),
89
- blackmanHarris: (e, t = {}) => c(e, [
33
+ blackmanHarris: (e, t = {}) => o(e, [
90
34
  .35875,
91
35
  .48829,
92
36
  .14128,
93
37
  .01168
94
38
  ], t),
95
39
  kaiser(e, n = 8.6, r = {}) {
96
- if (l(e), !(n >= 0) || !Number.isFinite(n)) throw t(`Kaiser beta must be 0 or more, got ${n}`, "state");
97
- let i = new Float64Array(e), a = (r.symmetric ? Math.max(e - 1, 1) : e) / 2, o = u(n);
40
+ if (s(e), !(n >= 0) || !Number.isFinite(n)) throw t(`Kaiser beta must be 0 or more, got ${n}`, "state");
41
+ let i = new Float64Array(e), a = (r.symmetric ? Math.max(e - 1, 1) : e) / 2, o = c(n);
98
42
  for (let t = 0; t < e; t++) {
99
43
  let e = (t - a) / a;
100
- i[t] = u(n * Math.sqrt(Math.max(0, 1 - e * e))) / o;
44
+ i[t] = c(n * Math.sqrt(Math.max(0, 1 - e * e))) / o;
101
45
  }
102
46
  return i;
103
47
  }
104
48
  };
105
- function f(e, n) {
106
- if (e === void 0 || e === "hann") return d.hann(n);
49
+ function u(e, n) {
50
+ if (e === void 0 || e === "hann") return l.hann(n);
107
51
  if (typeof e != "string") {
108
52
  if (e.length !== n) throw t(`A window array must hold ${n} numbers, got ${e.length}`, "state");
109
53
  return Float64Array.from(e);
110
54
  }
111
55
  switch (e) {
112
- case "hamming": return d.hamming(n);
113
- case "blackman": return d.blackman(n);
114
- case "blackman-harris": return d.blackmanHarris(n);
115
- case "kaiser": return d.kaiser(n);
116
- case "rectangular": return d.rectangular(n);
56
+ case "hamming": return l.hamming(n);
57
+ case "blackman": return l.blackman(n);
58
+ case "blackman-harris": return l.blackmanHarris(n);
59
+ case "kaiser": return l.kaiser(n);
60
+ case "rectangular": return l.rectangular(n);
117
61
  }
118
62
  throw t(`Unknown window "${String(e)}"; use hann, hamming, blackman, blackman-harris, kaiser or rectangular`, "state");
119
63
  }
120
64
  //#endregion
121
65
  //#region \0@oxc-project+runtime@0.153.0/helpers/esm/usingCtx.js
122
- function p() {
66
+ function d() {
123
67
  var e = typeof SuppressedError == "function" ? SuppressedError : function(e, t) {
124
68
  var n = Error();
125
69
  return n.name = "SuppressedError", n.error = e, n.suppressed = t, n;
@@ -175,17 +119,17 @@ function p() {
175
119
  }
176
120
  //#endregion
177
121
  //#region src/spectrogram.ts
178
- var m = (e) => {
122
+ var f = (e) => {
179
123
  throw t(e, "state");
180
- }, h = 2 ** 27;
181
- function g(e, t) {
124
+ }, p = 2 ** 27;
125
+ function m(e, t) {
182
126
  let n = t.size ?? 2048, r = t.hop ?? Math.max(1, n / 4), i = t.center ?? !0, a = t.sampleRate ?? 44100, o = t.pad ?? "reflect";
183
- (!Number.isInteger(n) || n < 32 || n > 65536 || n & n - 1) && m(`STFT size must be a power of two from 32 to 65536, got ${n}`), (!Number.isInteger(r) || r < 1 || r > n) && m(`STFT hop must be a whole number from 1 to size (${n}), got ${r}`), (!(a > 0) || !Number.isFinite(a)) && m(`Sample rate must be a positive number, got ${a}`), o !== "reflect" && o !== "zero" && o !== "edge" && m(`Unknown pad "${String(o)}"; use reflect, zero or edge`), e < 1 && m("STFT needs at least one sample"), !i && e < n && m(`Without centring the signal needs at least size (${n}) samples, got ${e}`);
127
+ (!Number.isInteger(n) || n < 32 || n > 65536 || n & n - 1) && f(`STFT size must be a power of two from 32 to 65536, got ${n}`), (!Number.isInteger(r) || r < 1 || r > n) && f(`STFT hop must be a whole number from 1 to size (${n}), got ${r}`), (!(a > 0) || !Number.isFinite(a)) && f(`Sample rate must be a positive number, got ${a}`), o !== "reflect" && o !== "zero" && o !== "edge" && f(`Unknown pad "${String(o)}"; use reflect, zero or edge`), e < 1 && f("STFT needs at least one sample"), !i && e < n && f(`Without centring the signal needs at least size (${n}) samples, got ${e}`);
184
128
  let s = i ? 1 + Math.floor(e / r) : 1 + Math.floor((e - n) / r), c = n / 2 + 1;
185
- return s * c > h && m(`${s} frames of ${c} bins is over ${h} values; use a larger hop or a shorter signal`), {
129
+ return s * c > p && f(`${s} frames of ${c} bins is over ${p} values; use a larger hop or a shorter signal`), {
186
130
  size: n,
187
131
  hop: r,
188
- window: f(t.window, n),
132
+ window: u(t.window, n),
189
133
  center: i,
190
134
  sampleRate: a,
191
135
  frames: s,
@@ -193,25 +137,25 @@ function g(e, t) {
193
137
  pad: o
194
138
  };
195
139
  }
196
- function _(t) {
140
+ function h(t) {
197
141
  if (typeof t != "object" || !t || typeof t.length != "number") throw e("Samples must be a Float32Array, Float64Array or array of numbers", "state");
198
- for (let e = 0; e < t.length; e++) Number.isFinite(t[e]) || m(`Sample ${e} is ${t[e]}; samples must be finite numbers`);
142
+ for (let e = 0; e < t.length; e++) Number.isFinite(t[e]) || f(`Sample ${e} is ${t[e]}; samples must be finite numbers`);
199
143
  return t;
200
144
  }
201
- var v = (e, t) => {
145
+ var g = (e, t) => {
202
146
  if (t === 1) return 0;
203
147
  let n = 2 * (t - 1), r = (e % n + n) % n;
204
148
  return r < t ? r : n - r;
205
149
  };
206
- function y(e, t) {
150
+ function _(e, t) {
207
151
  let n = e.length, r = t.center ? t.size / 2 : 0, i = new Float64Array(t.center ? n + t.size : n);
208
152
  for (let a = 0; a < i.length; a++) {
209
153
  let o = a - r;
210
- o >= 0 && o < n ? i[a] = e[o] : t.pad === "reflect" ? i[a] = e[v(o, n)] : t.pad === "edge" && (i[a] = e[o < 0 ? 0 : n - 1]);
154
+ o >= 0 && o < n ? i[a] = e[o] : t.pad === "reflect" ? i[a] = e[g(o, n)] : t.pad === "edge" && (i[a] = e[o < 0 ? 0 : n - 1]);
211
155
  }
212
156
  return i;
213
157
  }
214
- var b = class {
158
+ var v = class {
215
159
  #e;
216
160
  #t;
217
161
  #n;
@@ -226,30 +170,30 @@ var b = class {
226
170
  for (let i = 0; i < r; i++) for (let r = 0; r < e; r++) a[i * t + r] += n[r] * n[r];
227
171
  this.inverseEnvelope = a.map((e) => e > 1e-12 ? 1 / e : 0);
228
172
  }
229
- analyse(e, t, n, r) {
230
- let { size: i, hop: a, window: s, frames: c, bins: l } = this;
173
+ analyse(e, t, n, i) {
174
+ let { size: a, hop: o, window: s, frames: c, bins: l } = this;
231
175
  for (let u = 0; u < c; u += 2) {
232
- r?.throwIfAborted();
176
+ i?.throwIfAborted();
233
177
  let d = u + 1 < c;
234
- for (let t = 0; t < i; t++) this.#e[t] = e[u * a + t] * s[t], this.#t[t] = d ? e[(u + 1) * a + t] * s[t] : 0;
235
- o(this.fft, this.#e, this.#t, this.#n, this.#r, this.#i, this.#a, this.#o, this.#s);
178
+ for (let t = 0; t < a; t++) this.#e[t] = e[u * o + t] * s[t], this.#t[t] = d ? e[(u + 1) * o + t] * s[t] : 0;
179
+ r(this.fft, this.#e, this.#t, this.#n, this.#r, this.#i, this.#a, this.#o, this.#s);
236
180
  for (let e = 0; e < l; e++) t[u * l + e] = this.#i[e], n[u * l + e] = this.#a[e], d && (t[(u + 1) * l + e] = this.#o[e], n[(u + 1) * l + e] = this.#s[e]);
237
181
  }
238
182
  }
239
183
  synthesise(e, t, n, r) {
240
- let { size: i, hop: a, window: o, frames: c, bins: l } = this;
184
+ let { size: i, hop: o, window: s, frames: c, bins: l } = this;
241
185
  n.fill(0);
242
186
  for (let u = 0; u < c; u += 2) {
243
187
  r?.throwIfAborted();
244
188
  let d = u + 1 < c;
245
189
  for (let n = 0; n < l; n++) this.#i[n] = e[u * l + n], this.#a[n] = t[u * l + n], this.#o[n] = d ? e[(u + 1) * l + n] : 0, this.#s[n] = d ? t[(u + 1) * l + n] : 0;
246
- s(this.fft, this.#i, this.#a, this.#o, this.#s, this.#n, this.#r, this.#e, this.#t);
247
- for (let e = 0; e < i; e++) n[u * a + e] += this.#e[e] * o[e], d && (n[(u + 1) * a + e] += this.#t[e] * o[e]);
190
+ a(this.fft, this.#i, this.#a, this.#o, this.#s, this.#n, this.#r, this.#e, this.#t);
191
+ for (let e = 0; e < i; e++) n[u * o + e] += this.#e[e] * s[e], d && (n[(u + 1) * o + e] += this.#t[e] * s[e]);
248
192
  }
249
193
  for (let e = 0; e < n.length; e++) n[e] *= this.inverseEnvelope[e];
250
194
  }
251
195
  };
252
- function x(e, t) {
196
+ function y(e, t) {
253
197
  let n = Infinity, r = 0;
254
198
  for (let i = 0; i < Math.min(t, e.length); i++) {
255
199
  let a = 0;
@@ -258,12 +202,12 @@ function x(e, t) {
258
202
  }
259
203
  return r > 0 ? n / r : 0;
260
204
  }
261
- function S(e, t) {
262
- let r = x(e, t);
205
+ function b(e, t) {
206
+ let r = y(e, t);
263
207
  if (!(r >= .1)) throw n("state", `Window and hop do not overlap-add evenly (smallest over largest summed squared window is ${r.toFixed(3)}, needs 0.1); use a hop of size / 4 or size / 2 with a Hann window`);
264
208
  }
265
- function C(e, t = {}) {
266
- let n = _(e), r = g(n.length, t), i = t.signal, a = y(n, r);
209
+ function x(e, t = {}) {
210
+ let n = h(e), r = m(n.length, t), i = t.signal, a = _(n, r);
267
211
  i?.throwIfAborted();
268
212
  let o = new Float32Array(r.frames * r.bins), s = new Float32Array(r.frames * r.bins), c = {
269
213
  real: o,
@@ -277,39 +221,39 @@ function C(e, t = {}) {
277
221
  length: n.length,
278
222
  window: r.window
279
223
  };
280
- return "gpu" in t && t.gpu ? w(t.gpu, a, r, c, i) : (new b(r.size, r.hop, r.window, r.frames).analyse(a, o, s, i), c);
224
+ return "gpu" in t && t.gpu ? S(t.gpu, a, r, c, i) : (new v(r.size, r.hop, r.window, r.frames).analyse(a, o, s, i), c);
281
225
  }
282
- async function w(e, t, n, r, i) {
226
+ async function S(e, t, n, r, i) {
283
227
  let { fft: a } = await import("@margoengine/compute/fft"), { size: o, hop: s, window: c, bins: l } = n, u = Math.max(1, Math.floor(2 ** 22 / o));
284
- for (let f = 0; f < n.frames; f += u) try {
285
- var d = p();
228
+ for (let p = 0; p < n.frames; p += u) try {
229
+ var f = d();
286
230
  i?.throwIfAborted();
287
- let m = Math.min(u, n.frames - f), h = new Float32Array(m * o);
288
- for (let e = 0; e < m; e++) for (let n = 0; n < o; n++) h[e * o + n] = t[(f + e) * s + n] * c[n];
289
- let { re: g, im: _ } = a(d.u(e.array(h, { shape: [m, o] })));
231
+ let m = Math.min(u, n.frames - p), h = new Float32Array(m * o);
232
+ for (let e = 0; e < m; e++) for (let n = 0; n < o; n++) h[e * o + n] = t[(p + e) * s + n] * c[n];
233
+ let { re: g, im: _ } = a(f.u(e.array(h, { shape: [m, o] })));
290
234
  try {
291
235
  let [e, t] = await Promise.all([g.read({ signal: i }), _.read({ signal: i })]);
292
- for (let n = 0; n < m; n++) for (let i = 0; i < l; i++) r.real[(f + n) * l + i] = e[n * o + i], r.imag[(f + n) * l + i] = t[n * o + i];
236
+ for (let n = 0; n < m; n++) for (let i = 0; i < l; i++) r.real[(p + n) * l + i] = e[n * o + i], r.imag[(p + n) * l + i] = t[n * o + i];
293
237
  } finally {
294
238
  g.dispose(), _.dispose();
295
239
  }
296
240
  } catch (e) {
297
- d.e = e;
241
+ f.e = e;
298
242
  } finally {
299
- d.d();
243
+ f.d();
300
244
  }
301
245
  return r;
302
246
  }
303
- function T(e, t = {}) {
304
- S(e.window, e.hop);
305
- let n = new b(e.size, e.hop, e.window, e.frames), r = new Float64Array(n.span);
306
- return n.synthesise(e.real, e.imag, r, t.signal), E(r, e, t.length);
247
+ function C(e, t = {}) {
248
+ b(e.window, e.hop);
249
+ let n = new v(e.size, e.hop, e.window, e.frames), r = new Float64Array(n.span);
250
+ return n.synthesise(e.real, e.imag, r, t.signal), w(r, e, t.length);
307
251
  }
308
- function E(e, t, n) {
252
+ function w(e, t, n) {
309
253
  let r = t.center ? t.size / 2 : 0, i = n ?? t.length;
310
- return (!Number.isInteger(i) || i < 0 || r + i > e.length) && m(`Length must be a whole number from 0 to ${e.length - r}, got ${i}`), Float32Array.from(e.subarray(r, r + i));
254
+ return (!Number.isInteger(i) || i < 0 || r + i > e.length) && f(`Length must be a whole number from 0 to ${e.length - r}, got ${i}`), Float32Array.from(e.subarray(r, r + i));
311
255
  }
312
- var D = [
256
+ var T = [
313
257
  [
314
258
  0,
315
259
  0,
@@ -335,27 +279,27 @@ var D = [
335
279
  250,
336
280
  200
337
281
  ]
338
- ], O = (e) => {
339
- let t = Math.min(.999999, Math.max(0, e)) * (D.length - 1), n = Math.floor(t), r = t - n;
282
+ ], E = (e) => {
283
+ let t = Math.min(.999999, Math.max(0, e)) * (T.length - 1), n = Math.floor(t), r = t - n;
340
284
  return [
341
285
  0,
342
286
  1,
343
287
  2
344
- ].map((e) => D[n][e] + r * (D[n + 1][e] - D[n][e]));
288
+ ].map((e) => T[n][e] + r * (T[n + 1][e] - T[n][e]));
345
289
  };
346
- function k(e, t = {}) {
290
+ function D(e, t = {}) {
347
291
  let n = t.dB ?? !0, r = t.floor ?? -100, [i, a] = t.range ?? [r, 0], o = e.frames * e.bins, s = new Float64Array(o);
348
- (!(r < 0) || !(i < a)) && m(`Floor must be below 0 and range [low, high] needs low < high, got floor ${r}, range [${i}, ${a}]`);
292
+ (!(r < 0) || !(i < a)) && f(`Floor must be below 0 and range [low, high] needs low < high, got floor ${r}, range [${i}, ${a}]`);
349
293
  let c = 0;
350
294
  for (let t = 0; t < o; t++) s[t] = e.real[t] ** 2 + e.imag[t] ** 2, s[t] > c && (c = s[t]);
351
295
  let l = t.ref === void 0 || t.ref === "max" ? c : t.ref;
352
- !(l >= 0) && l !== 0 && m(`Reference power must be a number of 0 or more, got ${String(t.ref)}`);
296
+ !(l >= 0) && l !== 0 && f(`Reference power must be a number of 0 or more, got ${String(t.ref)}`);
353
297
  let u = new Float32Array(o);
354
298
  for (let e = 0; e < o; e++) u[e] = n ? Math.max(r, 10 * Math.log10(Math.max(s[e], 1e-30) / Math.max(l, 1e-30))) : s[e];
355
- let d = t.origin === "high", f = (t) => d ? e.bins - 1 - t : t, p = t.colormap === "heat" ? O : t.colormap, h = (e) => Math.min(1, Math.max(0, (e - i) / (a - i)));
356
- if (t.format === "f32" && !p) {
299
+ let d = t.origin === "high", p = (t) => d ? e.bins - 1 - t : t, m = t.colormap === "heat" ? E : t.colormap, h = (e) => Math.min(1, Math.max(0, (e - i) / (a - i)));
300
+ if (t.format === "f32" && !m) {
357
301
  let t = new Float32Array(o);
358
- for (let n = 0; n < e.bins; n++) for (let r = 0; r < e.frames; r++) t[n * e.frames + r] = u[r * e.bins + f(n)];
302
+ for (let n = 0; n < e.bins; n++) for (let r = 0; r < e.frames; r++) t[n * e.frames + r] = u[r * e.bins + p(n)];
359
303
  return {
360
304
  width: e.frames,
361
305
  height: e.bins,
@@ -363,10 +307,10 @@ function k(e, t = {}) {
363
307
  data: t
364
308
  };
365
309
  }
366
- if (p) {
310
+ if (m) {
367
311
  let t = new Uint8Array(4 * o);
368
312
  for (let n = 0; n < e.bins; n++) for (let r = 0; r < e.frames; r++) {
369
- let [i, a, o] = p(h(u[r * e.bins + f(n)])), s = 4 * (n * e.frames + r);
313
+ let [i, a, o] = m(h(u[r * e.bins + p(n)])), s = 4 * (n * e.frames + r);
370
314
  t[s] = Math.round(i), t[s + 1] = Math.round(a), t[s + 2] = Math.round(o), t[s + 3] = 255;
371
315
  }
372
316
  return {
@@ -377,7 +321,7 @@ function k(e, t = {}) {
377
321
  };
378
322
  }
379
323
  let g = new Uint8Array(o);
380
- for (let t = 0; t < e.bins; t++) for (let n = 0; n < e.frames; n++) g[t * e.frames + n] = Math.round(255 * h(u[n * e.bins + f(t)]));
324
+ for (let t = 0; t < e.bins; t++) for (let n = 0; n < e.frames; n++) g[t * e.frames + n] = Math.round(255 * h(u[n * e.bins + p(t)]));
381
325
  return {
382
326
  width: e.frames,
383
327
  height: e.bins,
@@ -385,56 +329,56 @@ function k(e, t = {}) {
385
329
  data: g
386
330
  };
387
331
  }
388
- function A(e, t = {}) {
389
- let { size: n, hop: r, frames: i } = e, a = e.center ?? !0, o = t.iterations ?? 32, s = t.momentum ?? .99, c = f(e.window, n), l = n / 2 + 1;
390
- g(a ? Math.max(1, (i - 1) * r) : n + (i - 1) * r, {
332
+ function O(e, t = {}) {
333
+ let { size: n, hop: r, frames: i } = e, a = e.center ?? !0, o = t.iterations ?? 32, s = t.momentum ?? .99, c = u(e.window, n), l = n / 2 + 1;
334
+ m(a ? Math.max(1, (i - 1) * r) : n + (i - 1) * r, {
391
335
  size: n,
392
336
  hop: r,
393
337
  center: a,
394
338
  window: c
395
- }), (!Number.isInteger(i) || i < 1 || e.magnitude.length !== i * l) && m(`Magnitude needs frames x ${l} values; got ${e.magnitude.length} for ${i} frames`), (!Number.isInteger(o) || o < 1 || o > 1e4) && m(`Iterations must be a whole number from 1 to 10000, got ${o}`), s >= 0 && s <= .999 || m(`Momentum must be from 0 to 0.999, got ${s}`), S(c, r);
396
- let u = i * l, d = Float64Array.from(e.magnitude), p = 0;
397
- for (let e = 0; e < u; e++) (!(d[e] >= 0) || !Number.isFinite(d[e])) && m(`Magnitude ${e} is ${d[e]}; magnitudes must be finite and at least 0`), p += d[e] * d[e];
398
- let h = new b(n, r, c, i), _ = new Float64Array(u), v = new Float64Array(u), y = new Float64Array(u), x = new Float64Array(u), C = new Float64Array(u), w = new Float64Array(u), T = new Float64Array(h.span), D = new Float64Array(o), O = t.init ?? "random";
339
+ }), (!Number.isInteger(i) || i < 1 || e.magnitude.length !== i * l) && f(`Magnitude needs frames x ${l} values; got ${e.magnitude.length} for ${i} frames`), (!Number.isInteger(o) || o < 1 || o > 1e4) && f(`Iterations must be a whole number from 1 to 10000, got ${o}`), s >= 0 && s <= .999 || f(`Momentum must be from 0 to 0.999, got ${s}`), b(c, r);
340
+ let d = i * l, p = Float64Array.from(e.magnitude), h = 0;
341
+ for (let e = 0; e < d; e++) (!(p[e] >= 0) || !Number.isFinite(p[e])) && f(`Magnitude ${e} is ${p[e]}; magnitudes must be finite and at least 0`), h += p[e] * p[e];
342
+ let g = new v(n, r, c, i), _ = new Float64Array(d), y = new Float64Array(d), x = new Float64Array(d), S = new Float64Array(d), C = new Float64Array(d), T = new Float64Array(d), E = new Float64Array(g.span), D = new Float64Array(o), O = t.init ?? "random";
399
343
  if (typeof O == "object") {
400
- (O.frames !== i || O.size !== n) && m("The init spectrum must have the same frames and size as the magnitude");
401
- for (let e = 0; e < u; e++) {
344
+ (O.frames !== i || O.size !== n) && f("The init spectrum must have the same frames and size as the magnitude");
345
+ for (let e = 0; e < d; e++) {
402
346
  let t = Math.hypot(O.real[e], O.imag[e]);
403
- _[e] = t > 0 ? d[e] * O.real[e] / t : d[e], v[e] = t > 0 ? d[e] * O.imag[e] / t : 0;
347
+ _[e] = t > 0 ? p[e] * O.real[e] / t : p[e], y[e] = t > 0 ? p[e] * O.imag[e] / t : 0;
404
348
  }
405
- } else if (O === "zero") _.set(d);
349
+ } else if (O === "zero") _.set(p);
406
350
  else if (O === "random") {
407
351
  let e = (t.seed ?? 1) >>> 0;
408
- for (let t = 0; t < u; t++) {
352
+ for (let t = 0; t < d; t++) {
409
353
  e = e + 1831565813 >>> 0;
410
354
  let n = e;
411
355
  n = Math.imul(n ^ n >>> 15, n | 1), n ^= n + Math.imul(n ^ n >>> 7, n | 61);
412
356
  let r = 2 * Math.PI * ((n ^ n >>> 14) >>> 0) / 4294967296;
413
- _[t] = d[t] * Math.cos(r), v[t] = d[t] * Math.sin(r);
357
+ _[t] = p[t] * Math.cos(r), y[t] = p[t] * Math.sin(r);
414
358
  }
415
- } else m(`Unknown init "${String(O)}"; use random, zero or a Spectrum`);
416
- for (let e = 0; e < i; e++) v[e * l] = 0, v[e * l + l - 1] = 0;
417
- y.set(_), x.set(v);
359
+ } else f(`Unknown init "${String(O)}"; use random, zero or a Spectrum`);
360
+ for (let e = 0; e < i; e++) y[e * l] = 0, y[e * l + l - 1] = 0;
361
+ x.set(_), S.set(y);
418
362
  for (let e = 0; e < o; e++) {
419
- t.signal?.throwIfAborted(), h.synthesise(y, x, T, t.signal), h.analyse(T, C, w, t.signal);
363
+ t.signal?.throwIfAborted(), g.synthesise(x, S, E, t.signal), g.analyse(E, C, T, t.signal);
420
364
  let n = 0;
421
- for (let e = 0; e < u; e++) {
422
- let t = Math.hypot(C[e], w[e]), r = d[e] - t;
365
+ for (let e = 0; e < d; e++) {
366
+ let t = Math.hypot(C[e], T[e]), r = p[e] - t;
423
367
  n += r * r;
424
- let i = t > 1e-30 ? d[e] * C[e] / t : d[e], a = t > 1e-30 ? d[e] * w[e] / t : 0;
425
- y[e] = i + s * (i - _[e]), x[e] = a + s * (a - v[e]), _[e] = i, v[e] = a;
368
+ let i = t > 1e-30 ? p[e] * C[e] / t : p[e], a = t > 1e-30 ? p[e] * T[e] / t : 0;
369
+ x[e] = i + s * (i - _[e]), S[e] = a + s * (a - y[e]), _[e] = i, y[e] = a;
426
370
  }
427
- D[e] = p > 0 ? Math.sqrt(n / p) : 0;
371
+ D[e] = h > 0 ? Math.sqrt(n / h) : 0;
428
372
  }
429
- return h.synthesise(_, v, T, t.signal), {
430
- samples: E(T, {
373
+ return g.synthesise(_, y, E, t.signal), {
374
+ samples: w(E, {
431
375
  size: n,
432
376
  center: a,
433
- length: e.length ?? (a ? (i - 1) * r : h.span)
377
+ length: e.length ?? (a ? (i - 1) * r : g.span)
434
378
  }, e.length),
435
379
  convergence: D,
436
380
  transforms: o * 2 * i + i
437
381
  };
438
382
  }
439
383
  //#endregion
440
- export { T as istft, x as overlapRatio, A as resynthesize, k as spectrogram, C as stft, d as windows };
384
+ export { C as istft, y as overlapRatio, O as resynthesize, D as spectrogram, x as stft, l as windows };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * A per-frame magnitude correction for a pitch ratio: `fill(power, gains)` writes `E(ratio * f) / E(f)` for each bin of
3
+ * a transform of `points`, E the frame's envelope with cepstral cutoff `quefrency` samples.
4
+ */
5
+ export declare function envelopeCorrection(points: number, ratio: number, quefrency: number): (power: Float64Array, gains: Float64Array) => void;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Adds `input` stretched by `stretch` into `output` (whose length sets the stretched length). `size` is the window
3
+ * length, a power of two; the transform is twice as long. `gain`, when given, fills a per-bin magnitude correction from
4
+ * each frame's power. Every channel is rotated by the same per-bin angle, so level and phase differences between
5
+ * channels are kept.
6
+ */
7
+ export declare function phaseStretch(input: readonly Float32Array[], output: readonly Float32Array[], stretch: number, size: number, pause: () => Promise<void>, gain?: (power: Float64Array, gains: Float64Array) => void): Promise<void>;
@@ -0,0 +1,10 @@
1
+ /** The power of two nearest to `n` on a log scale, within [low, high]. */
2
+ export declare const nearestPowerOfTwo: (n: number, low: number, high: number) => number;
3
+ /** The percussive part of every channel; the harmonic part is the input minus it, sample for sample. */
4
+ export declare function percussivePart(channels: readonly Float32Array[], sampleRate: number, pause: () => Promise<void>): Promise<Float32Array[]>;
5
+ /**
6
+ * Adds `input`, made longer by `stretch` (above 1), into `output` by plain overlap-add with a window of about 6 ms (OLA,
7
+ * Driedger and Mueller, "A review of time-scale modification of music signals", Applied Sciences 6(2):57, 2016,
8
+ * section 3).
9
+ */
10
+ export declare function overlapStretch(input: readonly Float32Array[], output: readonly Float32Array[], stretch: number, sampleRate: number, pause: () => Promise<void>): Promise<void>;
@@ -0,0 +1,59 @@
1
+ /** Audio as `decodeWav` returns it: a rate and one Float32Array per channel. */
2
+ export interface VocoderAudio {
3
+ sampleRate: number;
4
+ channels: Float32Array[];
5
+ }
6
+ /** Options shared by {@link timeStretch} and {@link pitchShift}. */
7
+ export interface VocoderOptions {
8
+ /**
9
+ * When lengthening (`factor * 2 ** (semitones / 12)` above 1), stretch drum hits, consonants and other attacks apart
10
+ * from the sustained sound, so they stay sharp. Default true; false runs the phase vocoder on everything, which
11
+ * softens attacks.
12
+ */
13
+ transients?: boolean;
14
+ /** Keep the spectral envelope (a voice's or instrument's formants) where it is while the pitch moves. Default false. */
15
+ preserveFormants?: boolean;
16
+ /**
17
+ * With `preserveFormants`, the finest detail of the envelope as a time in milliseconds, 0.2 to 10: shorter follows
18
+ * the envelope more loosely. Keep it under the pitch period of the highest note. Default 2.
19
+ */
20
+ envelopeMs?: number;
21
+ /**
22
+ * Window of the phase vocoder in milliseconds, 20 to 200, rounded to the nearest power of two in samples. Longer
23
+ * separates close low notes better; shorter follows fast changes. Default 85 (4096 samples at 44.1 and 48 kHz).
24
+ */
25
+ frameMs?: number;
26
+ /** Stops the work between blocks of frames; the promise rejects with the signal's reason. */
27
+ signal?: AbortSignal;
28
+ }
29
+ /** Options of {@link timeStretch}. */
30
+ export interface TimeStretchOptions extends VocoderOptions {
31
+ /** Pitch change applied in the same pass, in semitones from -24 to 24 (fractions allowed). Default 0. */
32
+ semitones?: number;
33
+ }
34
+ /**
35
+ * Makes audio `factor` times as long at the same pitch with a phase vocoder, for music and dense mixes: 1.25 is 25 %
36
+ * longer (slower), 0.8 is 20 % shorter (faster). The output holds `round(length * factor)` samples; `semitones` moves
37
+ * the pitch in the same pass. All channels are turned by the same phase, so the stereo image stays. The input is not
38
+ * changed. The work yields to the event loop as it goes, so a page stays responsive and `signal` can stop it.
39
+ *
40
+ * @example
41
+ * import {timeStretch} from '@margoengine/audio/vocoder';
42
+ * const take = {sampleRate: 48000, channels: [new Float32Array(48000)]};
43
+ * const slower = await timeStretch(take, 1.25);
44
+ * console.log(slower.channels[0].length); // 60000
45
+ * @throws RangeError or TypeError (as a rejection) for bad audio or an option out of range; the signal's reason.
46
+ */
47
+ export declare function timeStretch(audio: VocoderAudio, factor: number, options?: TimeStretchOptions): Promise<VocoderAudio>;
48
+ /**
49
+ * Moves the pitch of audio by `semitones` (12 is an octave up, -12 down, fractions allowed) at the same length:
50
+ * {@link timeStretch} with a factor of 1.
51
+ *
52
+ * @example
53
+ * import {pitchShift} from '@margoengine/audio/vocoder';
54
+ * const voice = {sampleRate: 48000, channels: [new Float32Array(48000)]};
55
+ * const higher = await pitchShift(voice, 3, {preserveFormants: true});
56
+ * console.log(higher.channels[0].length); // 48000
57
+ * @throws RangeError or TypeError (as a rejection) for bad audio or an option out of range; the signal's reason.
58
+ */
59
+ export declare function pitchShift(audio: VocoderAudio, semitones: number, options?: VocoderOptions): Promise<VocoderAudio>;