@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +987 -2
- package/dist/ambisonics.d.ts +54 -0
- package/dist/ambisonics.js +118 -0
- package/dist/analyser.d.ts +45 -0
- package/dist/audio.d.ts +282 -0
- package/dist/binaural.d.ts +35 -0
- package/dist/binaural.js +123 -0
- package/dist/doppler.d.ts +44 -0
- package/dist/doppler.js +82 -0
- package/dist/errors-D8rc8kzg.js +7 -0
- package/dist/events.d.ts +54 -0
- package/dist/events.js +78 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +303 -0
- package/dist/listener.d.ts +116 -0
- package/dist/loader.d.ts +22 -0
- package/dist/math-C82bLsED.js +44 -0
- package/dist/math.d.ts +19 -0
- package/dist/music.d.ts +76 -0
- package/dist/music.js +106 -0
- package/dist/occlusion.d.ts +50 -0
- package/dist/occlusion.js +75 -0
- package/dist/outdoor.d.ts +37 -0
- package/dist/outdoor.js +52 -0
- package/dist/pan.d.ts +21 -0
- package/dist/pan.js +28 -0
- package/dist/rate-control-BiZ5FnNk.js +4 -0
- package/dist/rate-control.d.ts +5 -0
- package/dist/resample.d.ts +53 -0
- package/dist/resample.js +245 -0
- package/dist/scan.d.ts +66 -0
- package/dist/scan.js +75 -0
- package/dist/sinc-CbjImCBh.js +36 -0
- package/dist/sinc.d.ts +5 -0
- package/dist/spectrogram-fft-CRLetuHA.js +59 -0
- package/dist/spectrogram-fft.d.ts +26 -0
- package/dist/spectrogram-windows.d.ts +24 -0
- package/dist/spectrogram.d.ts +143 -0
- package/dist/spectrogram.js +384 -0
- package/dist/sprites.d.ts +59 -0
- package/dist/sprites.js +99 -0
- package/dist/stretch.d.ts +25 -0
- package/dist/stretch.js +55 -0
- package/dist/vocoder-envelope.d.ts +5 -0
- package/dist/vocoder-phase.d.ts +7 -0
- package/dist/vocoder-split.d.ts +10 -0
- package/dist/vocoder.d.ts +59 -0
- package/dist/vocoder.js +291 -0
- package/dist/voice-manager.d.ts +100 -0
- package/dist/voice-manager.js +178 -0
- package/dist/voices-CdfPmzIo.js +425 -0
- package/dist/voices.d.ts +45 -0
- package/dist/wav.d.ts +47 -0
- package/dist/wav.js +156 -0
- package/dist/zones.d.ts +43 -0
- package/dist/zones.js +75 -0
- package/package.json +130 -4
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Audio as `decodeWav` returns it: a rate and one Float32Array per channel. */
|
|
2
|
+
export interface StretchInput {
|
|
3
|
+
sampleRate: number;
|
|
4
|
+
channels: Float32Array[];
|
|
5
|
+
}
|
|
6
|
+
/** Options of {@link timeStretch}. */
|
|
7
|
+
export interface StretchOptions {
|
|
8
|
+
/** Frame length in milliseconds, 10 to 200; the synthesis hop is half of it. Default 40, a few pitch periods of speech. */
|
|
9
|
+
frameMs?: number;
|
|
10
|
+
/** How far, in milliseconds, a frame may move from its nominal place to match the waveform, 1 to 50. Default 10. */
|
|
11
|
+
toleranceMs?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Makes audio `factor` times as long at the same pitch by WSOLA (Verhelst and Roelands 1993): 1.05 is 5 % longer
|
|
15
|
+
* (slower), 0.95 is 5 % shorter (faster). The output holds `round(length * factor)` samples. Every channel uses the
|
|
16
|
+
* same frame positions, chosen on their mean, so the stereo image stays put. A factor of 1 copies the samples.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* import {timeStretch} from '@margoengine/audio/stretch';
|
|
20
|
+
* const take = {sampleRate: 48000, channels: [new Float32Array(48000)]};
|
|
21
|
+
* const faster = timeStretch(take, 0.95);
|
|
22
|
+
* console.log(faster.channels[0].length); // 45600
|
|
23
|
+
* @throws RangeError for a factor outside 0.25 to 4 or an option out of range.
|
|
24
|
+
*/
|
|
25
|
+
export declare function timeStretch(audio: StretchInput, factor: number, options?: StretchOptions): StretchInput;
|
package/dist/stretch.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { i as e, r as t } from "./errors-D8rc8kzg.js";
|
|
2
|
+
//#region src/stretch.ts
|
|
3
|
+
var n = 4;
|
|
4
|
+
function r(r, i, a = {}) {
|
|
5
|
+
if (!r || typeof r != "object" || !Array.isArray(r.channels) || !r.channels.length) throw e("Pass {sampleRate, channels} audio with at least one channel", "audio-stretch");
|
|
6
|
+
let { sampleRate: o, channels: s } = r;
|
|
7
|
+
if (!s.every((e) => e instanceof Float32Array && e.length === s[0].length)) throw t("Every channel must be a Float32Array of the same length", "audio-stretch");
|
|
8
|
+
if (!Number.isInteger(o) || o < 1 || o > 768e3) throw t("sampleRate must be a whole number from 1 to 768000", "audio-stretch");
|
|
9
|
+
if (!(i >= .25 && i <= 4)) throw t("factor must be from 0.25 to 4", "audio-stretch");
|
|
10
|
+
let { frameMs: c = 40, toleranceMs: l = 10 } = a;
|
|
11
|
+
if (!(c >= 10 && c <= 200)) throw t("frameMs must be from 10 to 200", "audio-stretch");
|
|
12
|
+
if (!(l >= 1 && l <= 50)) throw t("toleranceMs must be from 1 to 50", "audio-stretch");
|
|
13
|
+
let u = s[0].length, d = Math.round(u * i);
|
|
14
|
+
if (i === 1) return {
|
|
15
|
+
sampleRate: o,
|
|
16
|
+
channels: s.map((e) => e.slice())
|
|
17
|
+
};
|
|
18
|
+
let f = Math.max(2, Math.round(c * o / 2e3)), p = 2 * f, m = f, h = m / i, g = Math.max(1, Math.round(l * o / 1e3)), _ = Float64Array.from({ length: p }, (e, t) => .5 - .5 * Math.cos(2 * Math.PI * t / p)), v = new Float32Array(u);
|
|
19
|
+
for (let e of s) for (let t = 0; t < u; t++) v[t] += e[t] / s.length;
|
|
20
|
+
let y = (e) => e >= 0 && e < u ? v[e] : 0, b = (e, t, n) => {
|
|
21
|
+
let r = 0;
|
|
22
|
+
for (let i = 0; i < p; i += n) r += y(e - f + i) * y(t - f + i);
|
|
23
|
+
return r;
|
|
24
|
+
}, x = Math.ceil(d / m) + 1, S = s.map(() => new Float32Array(d)), C = new Float64Array(d), w = 0;
|
|
25
|
+
for (let e = 0; e < x; e++) {
|
|
26
|
+
let t = Math.round(e * h), r = t;
|
|
27
|
+
if (e > 0) {
|
|
28
|
+
let e = w + m, i = 0, a = -Infinity;
|
|
29
|
+
for (let r = -g; r <= g; r += n) {
|
|
30
|
+
let o = b(e, t + r, n);
|
|
31
|
+
o > a && ([a, i] = [o, r]);
|
|
32
|
+
}
|
|
33
|
+
let o = i;
|
|
34
|
+
a = -Infinity;
|
|
35
|
+
for (let r = Math.max(-g, o - n + 1); r <= Math.min(g, o + n - 1); r++) {
|
|
36
|
+
let n = b(e, t + r, 1);
|
|
37
|
+
n > a && ([a, i] = [n, r]);
|
|
38
|
+
}
|
|
39
|
+
r = t + i;
|
|
40
|
+
}
|
|
41
|
+
w = r;
|
|
42
|
+
let i = e * m - f;
|
|
43
|
+
for (let e = 0; e < p; e++) {
|
|
44
|
+
let t = i + e, n = r - f + e;
|
|
45
|
+
if (!(t < 0 || t >= d) && (C[t] += _[e], !(n < 0 || n >= u))) for (let r = 0; r < s.length; r++) S[r][t] += _[e] * s[r][n];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
for (let e = 0; e < d; e++) if (C[e] > 1e-9) for (let t of S) t[e] /= C[e];
|
|
49
|
+
return {
|
|
50
|
+
sampleRate: o,
|
|
51
|
+
channels: S
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
export { r as timeStretch };
|
|
@@ -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>;
|
package/dist/vocoder.js
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { i as e, r as t } from "./errors-D8rc8kzg.js";
|
|
2
|
+
import { i as n, n as r, r as i, t as a } from "./spectrogram-fft-CRLetuHA.js";
|
|
3
|
+
import { t as o } from "./sinc-CbjImCBh.js";
|
|
4
|
+
//#region src/vocoder-envelope.ts
|
|
5
|
+
var s = 2 * Math.LN10 / 20, c = 32, l = 3 * Math.LN10, u = 6 * Math.LN10;
|
|
6
|
+
function d(e, t, n) {
|
|
7
|
+
let i = Math.max(2, Math.min(e / 4, Math.round(n))), o = Math.min(e, Math.max(1024, 2 ** Math.ceil(Math.log2(4 * i)))), d = e / o, f = o / 2, p = r(o), m = new Float64Array(f + 1), h = new Float64Array(f + 1), g = new Float64Array(o), _ = new Float64Array(o);
|
|
8
|
+
return (e, n) => {
|
|
9
|
+
let r = 0;
|
|
10
|
+
for (let t = 0; t <= f; t++) {
|
|
11
|
+
let n = 0, i = Math.max(0, Math.ceil((t - .5) * d)), a = Math.min(e.length - 1, Math.floor((t + .5) * d));
|
|
12
|
+
for (let t = i; t <= a; t++) e[t] > n && (n = e[t]);
|
|
13
|
+
m[t] = n, n > r && (r = n);
|
|
14
|
+
}
|
|
15
|
+
if (!(r > 0)) {
|
|
16
|
+
n.fill(1);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
let v = Math.log(r) / 2 - u;
|
|
20
|
+
for (let e = 0; e <= f; e++) m[e] = h[e] = Math.max(v, Math.log(m[e]) / 2);
|
|
21
|
+
for (let e = 0; e < c; e++) {
|
|
22
|
+
for (let e = 0; e < o; e++) g[e] = h[e <= f ? e : o - e], _[e] = 0;
|
|
23
|
+
a(p, g, _);
|
|
24
|
+
for (let e = 0; e < o; e++) {
|
|
25
|
+
let t = Math.min(e, o - e), n = t < i ? 1 : t === i ? .5 : 0;
|
|
26
|
+
g[e] = g[e] * n / o, _[e] = 0;
|
|
27
|
+
}
|
|
28
|
+
a(p, g, _);
|
|
29
|
+
let e = 0;
|
|
30
|
+
for (let t = 0; t <= f; t++) e = Math.max(e, m[t] - g[t]), h[t] = Math.max(m[t], g[t]);
|
|
31
|
+
if (e < s) break;
|
|
32
|
+
}
|
|
33
|
+
let y = (e) => {
|
|
34
|
+
let t = Math.min(f, e / d), n = Math.min(f - 1, Math.floor(t));
|
|
35
|
+
return g[n] + (g[n + 1] - g[n]) * (t - n);
|
|
36
|
+
};
|
|
37
|
+
for (let e = 0; e < n.length; e++) n[e] = Math.exp(Math.max(-l, Math.min(l, y(e * t) - y(e))));
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/vocoder-phase.ts
|
|
42
|
+
var f = 16, p = 1e-12, m = 2 * Math.PI, h = class {
|
|
43
|
+
#e;
|
|
44
|
+
#t;
|
|
45
|
+
constructor(e) {
|
|
46
|
+
this.size = 0, this.#e = new Float64Array(e), this.#t = new Int32Array(e);
|
|
47
|
+
}
|
|
48
|
+
push(e, t) {
|
|
49
|
+
let n = this.#e, r = this.#t, i = this.size++;
|
|
50
|
+
for (; i > 0;) {
|
|
51
|
+
let t = i - 1 >> 1;
|
|
52
|
+
if (n[t] >= e) break;
|
|
53
|
+
n[i] = n[t], r[i] = r[t], i = t;
|
|
54
|
+
}
|
|
55
|
+
n[i] = e, r[i] = t;
|
|
56
|
+
}
|
|
57
|
+
pop() {
|
|
58
|
+
let e = this.#e, t = this.#t, n = t[0], r = --this.size, i = e[r], a = t[r], o = 0;
|
|
59
|
+
for (;;) {
|
|
60
|
+
let n = 2 * o + 1;
|
|
61
|
+
if (n >= r || (n + 1 < r && e[n + 1] > e[n] && n++, e[n] <= i)) break;
|
|
62
|
+
e[o] = e[n], t[o] = t[n], o = n;
|
|
63
|
+
}
|
|
64
|
+
return e[o] = i, t[o] = a, n;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
async function g(e, t, a, o, s, c) {
|
|
68
|
+
let l = e.length, u = e[0].length, d = t[0].length, g = o / 2, _ = 2 * o, v = o + 1, y = m / _, b = r(_), x = Math.max(1, Math.round(Math.min(1, a) * o / 4)), S = Float64Array.from({ length: o }, (e, t) => .5 + .5 * Math.cos(m * (t - g) / o)), C = Float64Array.from({ length: o }, (e, t) => -Math.PI / o * Math.sin(m * (t - g) / o)), w = S.map((e, t) => (t - g) * e), T = new Float64Array(x), E = (e) => (e % x + x) % x;
|
|
69
|
+
for (let e = 0; e < o; e++) T[E(e - g)] += S[e] * S[e];
|
|
70
|
+
let D = () => ({
|
|
71
|
+
re: e.map(() => new Float64Array(v)),
|
|
72
|
+
im: e.map(() => new Float64Array(v)),
|
|
73
|
+
omega: e.map(() => new Float64Array(v)),
|
|
74
|
+
power: new Float64Array(v),
|
|
75
|
+
channel: new Uint8Array(v),
|
|
76
|
+
peak: 0
|
|
77
|
+
}), O = D(), k = D(), A = e.map(() => new Float64Array(v)), j = () => new Float64Array(_), M = j(), N = j(), P = j(), F = j(), I = j(), L = j(), R = new Float64Array(v), z = new Float64Array(v), B = new Float64Array(v), V = new Float64Array(v), H = new Float64Array(v), ee = new Float64Array(v), U = [new Float64Array(v), new Float64Array(v)], W = [new Float64Array(v), new Float64Array(v)], G = [j(), j()], K = c && new Float64Array(v), q = new Float64Array(v), J = new Float64Array(v), Y = new Uint8Array(v), X = new h(2 * v), Z = 4 * m / o, Q = 625341585, te = () => (Q = Math.imul(Q, 1664525) + 1013904223 >>> 0, Q / 4294967296 * m), ne = (t) => {
|
|
78
|
+
O.power.fill(0), O.channel.fill(0), O.peak = 0;
|
|
79
|
+
for (let r = 0; r < l; r++) {
|
|
80
|
+
let i = e[r];
|
|
81
|
+
P.fill(0), F.fill(0), I.fill(0);
|
|
82
|
+
let a = Math.max(0, g - t), s = Math.min(o, u + g - t);
|
|
83
|
+
for (let e = a; e < s; e++) {
|
|
84
|
+
let n = (e + _ - g) % _, r = i[t - g + e];
|
|
85
|
+
P[n] = r * S[e], F[n] = r * C[e], I[n] = r * w[e];
|
|
86
|
+
}
|
|
87
|
+
let c = O.re[r], l = O.im[r], d = O.omega[r], f = A[r];
|
|
88
|
+
n(b, P, F, M, N, c, l, R, z), n(b, I, L, M, N, B, V, H, ee);
|
|
89
|
+
for (let e = 0; e < v; e++) {
|
|
90
|
+
let t = c[e] * c[e] + l[e] * l[e], n = 0, i = 0;
|
|
91
|
+
t > 0 && (n = Math.max(-Z, Math.min(Z, -(z[e] * c[e] - R[e] * l[e]) / t)), i = Math.max(-g, Math.min(g, -(B[e] * c[e] + V[e] * l[e]) / t))), d[e] = y * e + n, f[e] = i, t > O.power[e] && (O.power[e] = t, O.channel[e] = r, t > O.peak && (O.peak = t));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}, re = () => {
|
|
95
|
+
let e = p * Math.max(O.peak, k.peak), t = 0;
|
|
96
|
+
X.size = 0;
|
|
97
|
+
for (let n = 0; n < v; n++) O.power[n] > e ? (Y[n] = 1, t++, X.push(k.power[n], 2 * n)) : (Y[n] = 0, J[n] = te());
|
|
98
|
+
for (; t > 0;) {
|
|
99
|
+
let n = X.pop(), r = n >> 1;
|
|
100
|
+
if (n & 1) for (let e = -1; e <= 1; e += 2) {
|
|
101
|
+
let n = r + e;
|
|
102
|
+
if (n < 0 || n >= v || !Y[n]) continue;
|
|
103
|
+
let i = O.channel[n], o = O.re[i], s = O.im[i];
|
|
104
|
+
J[n] = J[r] + Math.atan2(s[r], o[r]) - Math.atan2(s[n], o[n]) + e * a * y * (A[i][r] + A[i][n]) / 2, Y[n] = 0, t--, X.push(O.power[n], 2 * n + 1);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
if (!Y[r]) continue;
|
|
108
|
+
let n = O.channel[r];
|
|
109
|
+
J[r] = k.power[r] > e ? q[r] + Math.atan2(k.im[n][r], k.re[n][r]) - Math.atan2(O.im[n][r], O.re[n][r]) + x * (k.omega[n][r] + O.omega[n][r]) / 2 : 0, Y[r] = 0, t--, X.push(O.power[r], 2 * r + 1);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
for (let e = 0; e < v; e++) q[e] = J[e] - m * Math.round(J[e] / m);
|
|
113
|
+
q[0] = q[v - 1] = 0;
|
|
114
|
+
}, ie = (e) => {
|
|
115
|
+
c && c(O.power, K);
|
|
116
|
+
for (let n = 0; n < l; n += 2) {
|
|
117
|
+
let r = Math.min(2, l - n);
|
|
118
|
+
for (let e = 0; e < 2; e++) {
|
|
119
|
+
let t = U[e], i = W[e];
|
|
120
|
+
if (e >= r) {
|
|
121
|
+
t.fill(0), i.fill(0);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
let a = O.re[n + e], o = O.im[n + e];
|
|
125
|
+
for (let e = 0; e < v; e++) {
|
|
126
|
+
let n = K ? K[e] : 1, r = n * Math.cos(q[e]), s = n * Math.sin(q[e]);
|
|
127
|
+
t[e] = a[e] * r - o[e] * s, i[e] = a[e] * s + o[e] * r;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
i(b, U[0], W[0], U[1], W[1], M, N, G[0], G[1]);
|
|
131
|
+
let a = Math.max(0, g - e), s = Math.min(o, d + g - e);
|
|
132
|
+
for (let i = 0; i < r; i++) {
|
|
133
|
+
let r = t[n + i], o = G[i];
|
|
134
|
+
for (let t = a; t < s; t++) {
|
|
135
|
+
let n = e - g + t;
|
|
136
|
+
r[n] += S[t] * o[(t + _ - g) % _] / T[E(t - g)];
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}, $ = -Math.floor((g - 1) / x), ae = Math.floor((d + g - 1) / x);
|
|
141
|
+
for (let e = $; e <= ae; e++) ne(Math.round(e * x / a)), e === $ ? q.fill(0) : re(), ie(e * x), [O, k] = [k, O], (e - $) % f === 15 && await s();
|
|
142
|
+
}
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region src/vocoder-split.ts
|
|
145
|
+
var _ = .2, v = 500, y = 2, b = 64, x = (e, t, n) => Math.max(t, Math.min(n, 2 ** Math.round(Math.log2(e)))), S = (e) => Float64Array.from({ length: e }, (t, n) => .5 - .5 * Math.cos(2 * Math.PI * n / e));
|
|
146
|
+
function C(e, t, n, r, i) {
|
|
147
|
+
let a = t, o = t + n - 1;
|
|
148
|
+
for (; a < o && e[a] < r;) a++;
|
|
149
|
+
if (i > r) for (; a < o && e[a + 1] < i;) e[a] = e[++a];
|
|
150
|
+
else for (; a > t && e[a - 1] > i;) e[a] = e[--a];
|
|
151
|
+
e[a] = i;
|
|
152
|
+
}
|
|
153
|
+
async function w(e, t, a) {
|
|
154
|
+
let o = e.length, s = e[0].length, c = x(t * .023, 64, 8192), l = c / 4, u = c / 2 + 1, d = (e) => 2 * Math.max(1, Math.round((e - 1) / 2)) + 1, f = d(_ * t / l), p = (f - 1) / 2, m = (d(Math.min(u, v * c / t)) - 1) / 2, h = r(c), g = S(c), w = Math.floor((s + c / 2 - 1) / l) - -1 + 1, T = new Float64Array(l);
|
|
155
|
+
for (let e = 0; e < c; e++) T[e % l] += g[e] * g[e];
|
|
156
|
+
let E = Array.from({ length: p + 1 }, () => ({
|
|
157
|
+
re: e.map(() => new Float64Array(u)),
|
|
158
|
+
im: e.map(() => new Float64Array(u)),
|
|
159
|
+
magnitude: new Float64Array(u)
|
|
160
|
+
})), D = Array.from({ length: f }, () => new Float64Array(u)), O = new Float64Array(u * f), k = new Float64Array(2 * m + 1), A = new Float64Array(c), j = new Float64Array(c), M = new Float64Array(c), N = new Float64Array(c), P = [new Float64Array(u), new Float64Array(u)], F = [new Float64Array(u), new Float64Array(u)], I = {
|
|
161
|
+
re: new Float64Array(u),
|
|
162
|
+
im: new Float64Array(u)
|
|
163
|
+
}, L = new Uint8Array(u), R = e.map(() => new Float32Array(s)), z = (t) => {
|
|
164
|
+
let r = E[t % (p + 1)], i = (t + -1) * l - c / 2;
|
|
165
|
+
for (let t = 0; t < o; t += 2) {
|
|
166
|
+
let a = t + 1 < o;
|
|
167
|
+
for (let n = 0; n < c; n++) {
|
|
168
|
+
let r = i + n, o = r >= 0 && r < s;
|
|
169
|
+
A[n] = o ? e[t][r] * g[n] : 0, j[n] = o && a ? e[t + 1][r] * g[n] : 0;
|
|
170
|
+
}
|
|
171
|
+
n(h, A, j, M, N, r.re[t], r.im[t], a ? r.re[t + 1] : I.re, a ? r.im[t + 1] : I.im);
|
|
172
|
+
}
|
|
173
|
+
for (let e = 0; e < u; e++) {
|
|
174
|
+
let t = 0;
|
|
175
|
+
for (let n = 0; n < o; n++) t += r.re[n][e] ** 2 + r.im[n][e] ** 2;
|
|
176
|
+
r.magnitude[e] = Math.sqrt(t);
|
|
177
|
+
}
|
|
178
|
+
}, B = (e) => {
|
|
179
|
+
let t = E[e % (p + 1)], n = t.magnitude, r = (e) => n[Math.max(0, Math.min(u - 1, e))];
|
|
180
|
+
for (let e = 0; e < k.length; e++) k[e] = r(e - m);
|
|
181
|
+
k.sort();
|
|
182
|
+
for (let e = 0; e < u; e++) e > 0 && C(k, 0, k.length, r(e - 1 - m), r(e + m)), L[e] = +(k[m] > y * O[e * f + p]);
|
|
183
|
+
let a = (e + -1) * l - c / 2;
|
|
184
|
+
for (let e = 0; e < o; e += 2) {
|
|
185
|
+
let n = Math.min(2, o - e);
|
|
186
|
+
for (let r = 0; r < 2; r++) for (let i = 0; i < u; i++) {
|
|
187
|
+
let a = r < n && L[i] === 1;
|
|
188
|
+
P[r][i] = a ? t.re[e + r][i] : 0, F[r][i] = a ? t.im[e + r][i] : 0;
|
|
189
|
+
}
|
|
190
|
+
i(h, P[0], F[0], P[1], F[1], M, N, A, j);
|
|
191
|
+
for (let t = 0; t < n; t++) {
|
|
192
|
+
let n = t ? j : A, r = R[e + t];
|
|
193
|
+
for (let e = Math.max(0, -a); e < Math.min(c, s - a); e++) r[a + e] += n[e] * g[e] / T[e % l];
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
for (let e = 0; e < w + p; e++) {
|
|
198
|
+
e < w && z(e);
|
|
199
|
+
let t = E[Math.min(e, w - 1) % (p + 1)].magnitude;
|
|
200
|
+
if (e === 0) {
|
|
201
|
+
for (let e of D) e.set(t);
|
|
202
|
+
for (let e = 0; e < u; e++) O.fill(t[e], e * f, (e + 1) * f);
|
|
203
|
+
} else {
|
|
204
|
+
let n = D[e % f];
|
|
205
|
+
for (let e = 0; e < u; e++) C(O, e * f, f, n[e], t[e]);
|
|
206
|
+
n.set(t);
|
|
207
|
+
}
|
|
208
|
+
e >= p && B(e - p), e % b === 63 && await a();
|
|
209
|
+
}
|
|
210
|
+
return R;
|
|
211
|
+
}
|
|
212
|
+
async function T(e, t, n, r, i) {
|
|
213
|
+
let a = x(r * .0058, 16, 2048), o = a / 2, s = e[0].length, c = t[0].length, l = S(a), u = Math.floor((c + o - 1) / o);
|
|
214
|
+
for (let r = 0; r <= u; r++) {
|
|
215
|
+
let u = r * o, d = Math.round(u / n), f = Math.max(0, o - u, o - d), p = Math.min(a, c + o - u, s + o - d);
|
|
216
|
+
for (let n = 0; n < e.length; n++) for (let r = f; r < p; r++) t[n][u - o + r] += l[r] * e[n][d - o + r];
|
|
217
|
+
r % 1024 == 1023 && await i();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region src/vocoder.ts
|
|
222
|
+
var E = 16384, D = () => {
|
|
223
|
+
let e = globalThis.scheduler;
|
|
224
|
+
return e?.yield ? e.yield() : new Promise((e) => setTimeout(e));
|
|
225
|
+
};
|
|
226
|
+
function O(n, r, i) {
|
|
227
|
+
if (!n || typeof n != "object" || !Array.isArray(n.channels) || !n.channels.length) throw e("Pass {sampleRate, channels} audio with at least one channel", "audio-vocoder");
|
|
228
|
+
let { sampleRate: a, channels: o } = n;
|
|
229
|
+
if (o.length > 32 || !o.every((e) => e instanceof Float32Array && e.length === o[0].length)) throw t("Pass 1 to 32 channels, each a Float32Array of the same length", "audio-vocoder");
|
|
230
|
+
if (!Number.isInteger(a) || a < 1 || a > 768e3) throw t("sampleRate must be a whole number from 1 to 768000", "audio-vocoder");
|
|
231
|
+
if (!(r >= .25 && r <= 4)) throw t("factor must be from 0.25 to 4", "audio-vocoder");
|
|
232
|
+
let { semitones: s = 0, envelopeMs: c = 2, frameMs: l = 85 } = i;
|
|
233
|
+
if (!(s >= -24 && s <= 24)) throw t("semitones must be from -24 to 24", "audio-vocoder");
|
|
234
|
+
if (!(c >= .2 && c <= 10)) throw t("envelopeMs must be from 0.2 to 10", "audio-vocoder");
|
|
235
|
+
if (!(l >= 20 && l <= 200)) throw t("frameMs must be from 20 to 200", "audio-vocoder");
|
|
236
|
+
for (let e of o) for (let n = 0; n < e.length; n++) if (!Number.isFinite(e[n])) throw t("Every sample must be a finite number", "audio-vocoder");
|
|
237
|
+
return {
|
|
238
|
+
semitones: s,
|
|
239
|
+
envelopeMs: c,
|
|
240
|
+
frameMs: l
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
async function k(e, t, n = {}) {
|
|
244
|
+
let { semitones: r, envelopeMs: i, frameMs: a } = O(e, t, n), { transients: s = !0, preserveFormants: c = !1, signal: l } = n;
|
|
245
|
+
l?.throwIfAborted();
|
|
246
|
+
let { sampleRate: u, channels: f } = e, p = f[0].length, m = Math.round(p * t), h = 2 ** (r / 12), _ = h === 1 ? m : Math.round(m * h), v = t * h, y = c && h !== 1;
|
|
247
|
+
if (t === 1 && h === 1) return {
|
|
248
|
+
sampleRate: u,
|
|
249
|
+
channels: f.map((e) => e.slice())
|
|
250
|
+
};
|
|
251
|
+
if (!p || !m) return {
|
|
252
|
+
sampleRate: u,
|
|
253
|
+
channels: f.map(() => new Float32Array(m))
|
|
254
|
+
};
|
|
255
|
+
let b = async () => {
|
|
256
|
+
await D(), l?.throwIfAborted();
|
|
257
|
+
};
|
|
258
|
+
await b();
|
|
259
|
+
let S = f;
|
|
260
|
+
if (v !== 1 || y) {
|
|
261
|
+
let e = x(a * u / 1e3, 64, 32768), t = f.map(() => new Float32Array(Math.max(1, _))), n = y ? d(2 * e, h, i * u / 1e3) : void 0, r = f;
|
|
262
|
+
if (s && v > 1) {
|
|
263
|
+
let e = await w(f, u, b);
|
|
264
|
+
await T(e, t, v, u, b);
|
|
265
|
+
for (let t = 0; t < f.length; t++) {
|
|
266
|
+
let n = f[t], r = e[t];
|
|
267
|
+
for (let e = 0; e < p; e++) r[e] = n[e] - r[e];
|
|
268
|
+
}
|
|
269
|
+
r = e;
|
|
270
|
+
}
|
|
271
|
+
await g(r, t, v, e, b, n), S = t;
|
|
272
|
+
}
|
|
273
|
+
if (h === 1) return {
|
|
274
|
+
sampleRate: u,
|
|
275
|
+
channels: S
|
|
276
|
+
};
|
|
277
|
+
let C = f.map(() => new Float32Array(m)), k = o(h, 1);
|
|
278
|
+
for (let e = 0; e < m; e += E) k(S, C, e, Math.min(m, e + E)), await b();
|
|
279
|
+
return {
|
|
280
|
+
sampleRate: u,
|
|
281
|
+
channels: C
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
function A(e, t, n = {}) {
|
|
285
|
+
return k(e, 1, {
|
|
286
|
+
...n,
|
|
287
|
+
semitones: t
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
//#endregion
|
|
291
|
+
export { A as pitchShift, k as timeStretch };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import { Audio, type AudioListener } from '@margoengine/audio';
|
|
3
|
+
/** One listener-wide physical voice budget, shared across sounds and pools. */
|
|
4
|
+
export interface AudioVoiceManagerOptions {
|
|
5
|
+
/** Maximum simultaneous buffer sources. Default 32; 0 virtualizes every voice. */
|
|
6
|
+
maxVoices?: number;
|
|
7
|
+
/** Estimated linear gain at/below which a voice is virtual. Default 0.0001. */
|
|
8
|
+
threshold?: number;
|
|
9
|
+
}
|
|
10
|
+
/** Priority and optional app-specific occlusion/bus gain for a managed sound. */
|
|
11
|
+
export interface AudioManagedVoiceOptions {
|
|
12
|
+
/** Greater finite priority wins before estimated audibility. Default 0. */
|
|
13
|
+
priority?: number;
|
|
14
|
+
/** Additional nonnegative finite gain estimate. Default returns 1. No graph mutation. */
|
|
15
|
+
gain?: () => number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Shared cap with priority/audibility ranking. Virtual voices have no buffer source,
|
|
19
|
+
* but their audio-clock phase continues. Control transport and pitch through the returned handles.
|
|
20
|
+
*/
|
|
21
|
+
export declare class AudioVoiceManager {
|
|
22
|
+
readonly listener: AudioListener;
|
|
23
|
+
private readonly voices;
|
|
24
|
+
private cap;
|
|
25
|
+
private quiet;
|
|
26
|
+
private disposed;
|
|
27
|
+
constructor(listener: AudioListener, options?: AudioVoiceManagerOptions);
|
|
28
|
+
/** Physical budget; reducing it immediately virtualizes the lowest-ranked voices. */
|
|
29
|
+
get maxVoices(): number;
|
|
30
|
+
set maxVoices(value: number);
|
|
31
|
+
/** Linear quiet threshold; changes apply immediately. */
|
|
32
|
+
get threshold(): number;
|
|
33
|
+
set threshold(value: number);
|
|
34
|
+
/** Number of currently allocated playing buffer sources. */
|
|
35
|
+
get activeCount(): number;
|
|
36
|
+
/** Playing logical voices without a source node. */
|
|
37
|
+
get virtualCount(): number;
|
|
38
|
+
/** Register a decoded-buffer sound, including a positional/pool voice. The caller keeps ownership of the sound. */
|
|
39
|
+
add(sound: Audio, options?: AudioManagedVoiceOptions): AudioManagedVoice;
|
|
40
|
+
/** Re-rank after listener/sound pose updates. Called automatically by listener updates. */
|
|
41
|
+
update(): void;
|
|
42
|
+
/** @internal Remove a released registration. */
|
|
43
|
+
remove(voice: AudioManagedVoice): void;
|
|
44
|
+
/** Stop and release registrations, preserving caller-owned sounds/buffers. Idempotent. */
|
|
45
|
+
dispose(): void;
|
|
46
|
+
private validCap;
|
|
47
|
+
private assertLive;
|
|
48
|
+
[Symbol.dispose]: () => void;
|
|
49
|
+
}
|
|
50
|
+
/** A transport handle whose phase advances even while its source is virtual. */
|
|
51
|
+
export declare class AudioManagedVoice {
|
|
52
|
+
private readonly manager;
|
|
53
|
+
readonly sound: Audio;
|
|
54
|
+
private status;
|
|
55
|
+
private phase;
|
|
56
|
+
private clock;
|
|
57
|
+
private rank;
|
|
58
|
+
private allocated;
|
|
59
|
+
private pending;
|
|
60
|
+
private rate;
|
|
61
|
+
private cents;
|
|
62
|
+
private readonly extraGain;
|
|
63
|
+
constructor(manager: AudioVoiceManager, sound: Audio, options: AudioManagedVoiceOptions);
|
|
64
|
+
/** Greater priority wins before gain. */
|
|
65
|
+
get priority(): number;
|
|
66
|
+
set priority(value: number);
|
|
67
|
+
/** Logical playback, including virtual voices. */
|
|
68
|
+
get isPlaying(): boolean;
|
|
69
|
+
/** True only when logically playing without an allocated buffer source. */
|
|
70
|
+
get isVirtual(): boolean;
|
|
71
|
+
/** Current logical buffer position, with loop-region wrapping. */
|
|
72
|
+
get currentTime(): number;
|
|
73
|
+
/** Playback multiplier. A pitch change restarts only the physical source at the exact current phase. */
|
|
74
|
+
get playbackRate(): number;
|
|
75
|
+
set playbackRate(value: number);
|
|
76
|
+
/** Detune in cents; participates in virtual phase. Default is the registered sound's value. */
|
|
77
|
+
get detune(): number;
|
|
78
|
+
set detune(value: number);
|
|
79
|
+
/** Start/resume logical playback, re-evaluating the shared cap immediately. */
|
|
80
|
+
play(): this;
|
|
81
|
+
/** Freeze logical phase and release the physical source. */
|
|
82
|
+
pause(): this;
|
|
83
|
+
/** Stop and rewind to the sound's configured offset. */
|
|
84
|
+
stop(): this;
|
|
85
|
+
/** Seek in buffer seconds, preserving logical play/pause state. */
|
|
86
|
+
seek(seconds: number): this;
|
|
87
|
+
/** @internal Advance logical phase and end one-shots even while virtual. */
|
|
88
|
+
advance(): void;
|
|
89
|
+
/** @internal Estimate volume, distance/cone and caller gain, without changing the graph. */
|
|
90
|
+
audibility(): number;
|
|
91
|
+
/** @internal Allocate/release the source without changing logical playback. */
|
|
92
|
+
physical(active: boolean): void;
|
|
93
|
+
/** Stop/release this handle; the underlying sound remains owned by the caller. */
|
|
94
|
+
dispose(): void;
|
|
95
|
+
private get end();
|
|
96
|
+
private wrap;
|
|
97
|
+
private pitch;
|
|
98
|
+
private assertLive;
|
|
99
|
+
[Symbol.dispose]: () => void;
|
|
100
|
+
}
|