@margoengine/audio 0.0.0-stage → 0.1.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 +748 -3
- 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 +46 -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 +270 -0
- package/dist/scan.d.ts +66 -0
- package/dist/scan.js +75 -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 +440 -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/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 +127 -6
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { PositionalAudio } from './audio.js';
|
|
3
|
+
/** Optional Doppler controller settings. Scene distances are metres, time is seconds. */
|
|
4
|
+
export interface AudioDopplerOptions {
|
|
5
|
+
/** Sound speed in the stationary medium. Finite and positive. Default 343 m/s. */
|
|
6
|
+
speedOfSound?: number;
|
|
7
|
+
/** Authored playback rate, before Doppler. Default the sound's current playbackRate. */
|
|
8
|
+
playbackRate?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Estimates source and listener velocities after their poses update, then shifts buffer playback pitch.
|
|
12
|
+
* Authored sound/pool playbackRate writes compose with Doppler; the sound getter reports the effective rate.
|
|
13
|
+
* Independent of rendering backend. No retarded-time propagation or sonic booms.
|
|
14
|
+
*/
|
|
15
|
+
export declare class AudioDoppler {
|
|
16
|
+
readonly sound: PositionalAudio;
|
|
17
|
+
readonly speedOfSound: number;
|
|
18
|
+
private rate;
|
|
19
|
+
private amount;
|
|
20
|
+
private time;
|
|
21
|
+
private readonly previousSource;
|
|
22
|
+
private readonly previousListener;
|
|
23
|
+
private disposed;
|
|
24
|
+
private writing;
|
|
25
|
+
private readonly live;
|
|
26
|
+
private readonly onStateChange;
|
|
27
|
+
constructor(sound: PositionalAudio, options?: AudioDopplerOptions);
|
|
28
|
+
/** Current Doppler multiplier, independent of the authored playback rate. */
|
|
29
|
+
get ratio(): number;
|
|
30
|
+
/** Authored rate; writes on the sound and pool/event plays also update this value. */
|
|
31
|
+
get playbackRate(): number;
|
|
32
|
+
set playbackRate(value: number);
|
|
33
|
+
/** Sample current world poses. Live contexts use the monotonic frame clock, offline contexts use audio time. */
|
|
34
|
+
update(): void;
|
|
35
|
+
/** Clear velocity history after a teleport or camera cut; live context state changes reset automatically. */
|
|
36
|
+
reset(): void;
|
|
37
|
+
/** Unregister and restore authored playback rate. Idempotent; also called by listener teardown. */
|
|
38
|
+
dispose(): void;
|
|
39
|
+
private applyRate;
|
|
40
|
+
private checkSource;
|
|
41
|
+
private validateRate;
|
|
42
|
+
private assertLive;
|
|
43
|
+
[Symbol.dispose]: () => void;
|
|
44
|
+
}
|
package/dist/doppler.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { r as e, t } from "./errors-D8rc8kzg.js";
|
|
2
|
+
import { t as n } from "./rate-control-BiZ5FnNk.js";
|
|
3
|
+
//#region src/doppler.ts
|
|
4
|
+
var r = class {
|
|
5
|
+
constructor(r, i = {}) {
|
|
6
|
+
if (this.amount = 1, this.previousSource = /* @__PURE__ */ new Float64Array(3), this.previousListener = /* @__PURE__ */ new Float64Array(3), this.disposed = !1, this.writing = !1, this.onStateChange = () => {
|
|
7
|
+
this.reset();
|
|
8
|
+
}, this.sound = r, this.live = typeof r.context.startRendering != "function", this.speedOfSound = i.speedOfSound ?? 343, !Number.isFinite(this.speedOfSound) || this.speedOfSound <= 0) throw e("Doppler speedOfSound must be finite and positive; use 343 for air in metres", "state");
|
|
9
|
+
if (this.rate = i.playbackRate ?? r.playbackRate, this.validateRate(this.rate), this.checkSource(), n.has(r)) throw t("attached", "This sound already has Doppler; dispose its controller first", "state");
|
|
10
|
+
r.playbackRate = this.rate, n.set(r, {
|
|
11
|
+
validate: this.validateRate,
|
|
12
|
+
apply: (e) => this.writing ? e : (this.validateRate(e), this.rate = e, e * this.amount)
|
|
13
|
+
}), this.live && r.context.addEventListener("statechange", this.onStateChange), r.listener.addSource(this);
|
|
14
|
+
}
|
|
15
|
+
get ratio() {
|
|
16
|
+
return this.amount;
|
|
17
|
+
}
|
|
18
|
+
get playbackRate() {
|
|
19
|
+
return this.rate;
|
|
20
|
+
}
|
|
21
|
+
set playbackRate(e) {
|
|
22
|
+
this.assertLive(), this.validateRate(e), this.sound.playbackRate = e;
|
|
23
|
+
}
|
|
24
|
+
update() {
|
|
25
|
+
if (this.assertLive(), this.sound.isDisposed || this.sound.sourceType !== "buffer") {
|
|
26
|
+
this.dispose();
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
let e = this.sound.context;
|
|
30
|
+
if (this.live && e.state !== "running") {
|
|
31
|
+
this.reset();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
let t = this.live ? performance.now() / 1e3 : e.currentTime, n = this.sound.worldPosition, r = this.sound.listener.position;
|
|
35
|
+
if (this.time !== void 0 && t === this.time) return;
|
|
36
|
+
let i = 1;
|
|
37
|
+
if (this.time !== void 0 && t > this.time) {
|
|
38
|
+
let e = t - this.time, a = Math.hypot(n[0] - this.previousSource[0], n[1] - this.previousSource[1], n[2] - this.previousSource[2]), o = Math.hypot(r[0] - this.previousListener[0], r[1] - this.previousListener[1], r[2] - this.previousListener[2]), s = this.live && (e > .25 || Math.max(a, o) / e > .95 * this.speedOfSound), c = Math.hypot(n[0] - r[0], n[1] - r[1], n[2] - r[2]);
|
|
39
|
+
if (!s && c > 0) {
|
|
40
|
+
let t = 0, a = 0;
|
|
41
|
+
for (let i = 0; i < 3; i++) {
|
|
42
|
+
let o = (n[i] - r[i]) / c;
|
|
43
|
+
t += (n[i] - this.previousSource[i]) / e * o, a += (r[i] - this.previousListener[i]) / e * o;
|
|
44
|
+
}
|
|
45
|
+
let o = (e) => Math.max(-.95, Math.min(.95, e / this.speedOfSound));
|
|
46
|
+
i = (1 + o(a)) / (1 + o(t));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
this.previousSource.set(n), this.previousListener.set(r), this.time = t, this.amount = i, this.applyRate();
|
|
50
|
+
}
|
|
51
|
+
reset() {
|
|
52
|
+
this.assertLive(), this.time = void 0, this.amount = 1, this.applyRate();
|
|
53
|
+
}
|
|
54
|
+
dispose() {
|
|
55
|
+
this.disposed ||= (this.sound.listener.removeSource(this), this.live && this.sound.context.removeEventListener("statechange", this.onStateChange), n.delete(this.sound), this.sound.playbackRate = this.rate, !0);
|
|
56
|
+
}
|
|
57
|
+
applyRate() {
|
|
58
|
+
let e = this.rate * this.amount;
|
|
59
|
+
if (this.sound.playbackRate !== e) {
|
|
60
|
+
this.writing = !0;
|
|
61
|
+
try {
|
|
62
|
+
this.sound.playbackRate = e;
|
|
63
|
+
} finally {
|
|
64
|
+
this.writing = !1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
checkSource() {
|
|
69
|
+
if (this.sound.isDisposed || !this.sound.buffer || this.sound.sourceType !== "buffer") throw t("unsupported", "Doppler needs an AudioBuffer source; call sound.setBuffer before attaching");
|
|
70
|
+
}
|
|
71
|
+
validateRate(t) {
|
|
72
|
+
if (!Number.isFinite(t) || t < 0 || t > 34028234663852886e22 / 39) throw e("Doppler playbackRate must be finite, nonnegative and fit a float32 after a 39x shift", "state");
|
|
73
|
+
}
|
|
74
|
+
assertLive() {
|
|
75
|
+
if (this.disposed) throw t("disposed", "AudioDoppler is disposed; create a new controller");
|
|
76
|
+
}
|
|
77
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
78
|
+
this.dispose();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
//#endregion
|
|
82
|
+
export { r as AudioDoppler };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region ../shared/errors.js
|
|
2
|
+
var e = (e, t) => Object.assign(Error(t), {
|
|
3
|
+
name: "MargoError",
|
|
4
|
+
code: e
|
|
5
|
+
}), t = (e, t) => `${e}; see https://margoengine.com/errors#${t}`, n = (n, r, i = n) => e(n, t(r, i)), r = (e, n) => TypeError(t(e, n)), i = (e, n) => RangeError(t(e, n));
|
|
6
|
+
//#endregion
|
|
7
|
+
export { r as i, e as n, i as r, n as t };
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { AudioListener } from './listener.js';
|
|
3
|
+
import type { PositionalAudio } from './audio.js';
|
|
4
|
+
import { type AudioVoicePoolOptions } from './voices.js';
|
|
5
|
+
import type { Vec3 } from './math.js';
|
|
6
|
+
/** Random or ordered one-shot sound selection. No loading is performed by a container. */
|
|
7
|
+
export interface AudioEventOptions {
|
|
8
|
+
/** Selection order. Random avoids the immediately previous buffer index. Default 'random'. */
|
|
9
|
+
mode?: 'random' | 'sequence';
|
|
10
|
+
/** Independent voices per buffer, stealing that buffer's oldest when full. Default 4. */
|
|
11
|
+
voices?: number;
|
|
12
|
+
/** Uniform linear volume range, each endpoint in 0..1. Default [1, 1]. */
|
|
13
|
+
volume?: readonly [number, number];
|
|
14
|
+
/** Uniform pitch variation in cents, each endpoint in -4800..4800. Default [0, 0]. */
|
|
15
|
+
pitch?: readonly [number, number];
|
|
16
|
+
/** Minimum audio-clock seconds between accepted plays. Default 0. */
|
|
17
|
+
cooldown?: number;
|
|
18
|
+
/** Uniform draw in [0, 1), for reproducible choices. Default Math.random. */
|
|
19
|
+
random?: () => number;
|
|
20
|
+
/** Shared positional/clip options. Default AudioVoicePool defaults, with looping disabled. */
|
|
21
|
+
voiceOptions?: Omit<AudioVoicePoolOptions, 'voices' | 'volume' | 'playbackRate' | 'detune' | 'loop' | 'loopStart' | 'loopEnd'>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Random and sequence sound containers with per-play volume/pitch variation and an audio-clock cooldown.
|
|
25
|
+
* Reuses AudioVoicePool for overlapping playback, source ownership and voice stealing on every rendering backend.
|
|
26
|
+
*/
|
|
27
|
+
export declare class AudioEventContainer {
|
|
28
|
+
readonly listener: AudioListener;
|
|
29
|
+
private readonly pools;
|
|
30
|
+
private readonly mode;
|
|
31
|
+
private readonly volume;
|
|
32
|
+
private readonly pitch;
|
|
33
|
+
private readonly cooldown;
|
|
34
|
+
private readonly random;
|
|
35
|
+
private previous;
|
|
36
|
+
private next;
|
|
37
|
+
private lastTime;
|
|
38
|
+
private disposed;
|
|
39
|
+
constructor(listener: AudioListener, buffers: readonly AudioBuffer[], options?: AudioEventOptions);
|
|
40
|
+
/** Index of the last accepted buffer, or -1 before the first play. Cooldown rejection does not change it. */
|
|
41
|
+
get lastIndex(): number;
|
|
42
|
+
/** Play at a world position (default origin). Returns the voice, or null during cooldown. */
|
|
43
|
+
play(position?: Vec3): PositionalAudio | null;
|
|
44
|
+
/** Stop sounding voices, retaining sequence/random history and cooldown. */
|
|
45
|
+
stopAll(): void;
|
|
46
|
+
/** @internal The container has no frame work; registration provides listener-owned teardown. */
|
|
47
|
+
update(): void;
|
|
48
|
+
/** Release all voices and unregister from the listener. Shared buffers remain usable. Idempotent. */
|
|
49
|
+
dispose(): void;
|
|
50
|
+
private draw;
|
|
51
|
+
private sample;
|
|
52
|
+
private assertLive;
|
|
53
|
+
[Symbol.dispose]: () => void;
|
|
54
|
+
}
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { r as e, t } from "./errors-D8rc8kzg.js";
|
|
2
|
+
import { t as n } from "./voices-CdfPmzIo.js";
|
|
3
|
+
//#region src/events.ts
|
|
4
|
+
function r(t, n, r, i) {
|
|
5
|
+
if (t.length !== 2 || !Number.isFinite(t[0]) || !Number.isFinite(t[1]) || t[0] < n || t[1] > r || t[0] > t[1]) throw e(`Audio event ${i} needs an ordered pair in ${n}..${r}; pass [minimum, maximum]`, "state");
|
|
6
|
+
return [t[0], t[1]];
|
|
7
|
+
}
|
|
8
|
+
var i = class {
|
|
9
|
+
constructor(i, a, o = {}) {
|
|
10
|
+
if (this.pools = [], this.previous = -1, this.next = 0, this.lastTime = -Infinity, this.disposed = !1, this.listener = i, !a.length || a.some((e) => !Number.isFinite(e?.duration) || e.duration <= 0)) throw t("state", "Audio events need nonempty decoded buffers; use listener.load or context.createBuffer");
|
|
11
|
+
if (this.mode = o.mode ?? "random", this.mode !== "random" && this.mode !== "sequence") throw t("state", "Audio event mode must be 'random' or 'sequence'; choose one of those modes");
|
|
12
|
+
if (this.volume = r(o.volume ?? [1, 1], 0, 1, "volume"), this.pitch = r(o.pitch ?? [0, 0], -4800, 4800, "pitch"), this.cooldown = o.cooldown ?? 0, !Number.isFinite(this.cooldown) || this.cooldown < 0) throw e("Audio event cooldown must be finite and nonnegative; use audio-clock seconds", "state");
|
|
13
|
+
if (this.random = o.random ?? Math.random, typeof this.random != "function") throw t("state", "Audio event random needs a function returning a value in [0, 1)");
|
|
14
|
+
try {
|
|
15
|
+
for (let e of a) this.pools.push(new n(i, e, {
|
|
16
|
+
...o.voiceOptions,
|
|
17
|
+
voices: o.voices ?? 4,
|
|
18
|
+
volume: 1,
|
|
19
|
+
playbackRate: 1,
|
|
20
|
+
detune: 0,
|
|
21
|
+
loop: !1
|
|
22
|
+
}));
|
|
23
|
+
i.addSource(this);
|
|
24
|
+
} catch (e) {
|
|
25
|
+
for (let e of this.pools) e.dispose();
|
|
26
|
+
throw e;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
get lastIndex() {
|
|
30
|
+
return this.previous;
|
|
31
|
+
}
|
|
32
|
+
play(t = [
|
|
33
|
+
0,
|
|
34
|
+
0,
|
|
35
|
+
0
|
|
36
|
+
]) {
|
|
37
|
+
if (this.assertLive(), t.length !== 3 || !Number.isFinite(t[0]) || !Number.isFinite(t[1]) || !Number.isFinite(t[2])) throw e("Audio event position needs three finite coordinates; pass [x, y, z] in metres", "state");
|
|
38
|
+
let n = this.listener.context.currentTime;
|
|
39
|
+
if (n >= this.lastTime && n - this.lastTime < this.cooldown) return null;
|
|
40
|
+
let r = this.pools.length, i = this.next;
|
|
41
|
+
if (this.mode === "random") {
|
|
42
|
+
let e = r > 1 && this.previous >= 0;
|
|
43
|
+
i = r === 1 ? 0 : Math.floor(this.draw() * (r - Number(e))), e && i >= this.previous && i++;
|
|
44
|
+
}
|
|
45
|
+
let a = this.sample(this.volume), o = 2 ** (this.sample(this.pitch) / 1200), s = this.pools[i].play(t, {
|
|
46
|
+
volume: a,
|
|
47
|
+
playbackRate: o
|
|
48
|
+
});
|
|
49
|
+
return this.previous = i, this.next = (i + 1) % r, this.lastTime = n, s;
|
|
50
|
+
}
|
|
51
|
+
stopAll() {
|
|
52
|
+
this.assertLive();
|
|
53
|
+
for (let e of this.pools) e.stopAll();
|
|
54
|
+
}
|
|
55
|
+
update() {}
|
|
56
|
+
dispose() {
|
|
57
|
+
if (!this.disposed) {
|
|
58
|
+
this.disposed = !0, this.listener.removeSource(this);
|
|
59
|
+
for (let e of this.pools) e.dispose();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
draw() {
|
|
63
|
+
let t = this.random();
|
|
64
|
+
if (!Number.isFinite(t) || t < 0 || t >= 1) throw e("Audio event random must return a finite value in [0, 1); use a uniform random generator", "state");
|
|
65
|
+
return t;
|
|
66
|
+
}
|
|
67
|
+
sample(e) {
|
|
68
|
+
return e[0] === e[1] ? e[0] : e[0] + this.draw() * (e[1] - e[0]);
|
|
69
|
+
}
|
|
70
|
+
assertLive() {
|
|
71
|
+
if (this.disposed) throw t("disposed", "AudioEventContainer is disposed; create a new container");
|
|
72
|
+
}
|
|
73
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
74
|
+
this.dispose();
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
//#endregion
|
|
78
|
+
export { i as AudioEventContainer };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { AudioListener } from './listener.js';
|
|
2
|
+
export type { AudioListenerOptions } from './listener.js';
|
|
3
|
+
export { Audio, PositionalAudio } from './audio.js';
|
|
4
|
+
export type { AudioOptions, PositionalAudioOptions } from './audio.js';
|
|
5
|
+
export { AudioVoicePool } from './voices.js';
|
|
6
|
+
export type { AudioVoicePoolOptions } from './voices.js';
|
|
7
|
+
export { AudioAnalyser } from './analyser.js';
|
|
8
|
+
export type { AudioAnalyserOptions } from './analyser.js';
|
|
9
|
+
export { loadAudioBuffer, clearAudioBufferCache, decodeAudio } from './loader.js';
|
|
10
|
+
export type { LoadAudioOptions } from './loader.js';
|
|
11
|
+
export { distanceGain, coneGain, smoothParam } from './math.js';
|
|
12
|
+
export type { DistanceModel, Vec3 } from './math.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const c=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { n as e } from "./errors-D8rc8kzg.js";
|
|
5
|
+
import { i as t, n, r, t as i } from "./math-C82bLsED.js";
|
|
6
|
+
import { n as a, r as o, t as s } from "./voices-CdfPmzIo.js";
|
|
7
|
+
import { engineRuntime as __margoRuntime, meshRuntime as l } from "@margoengine/core/extension";
|
|
8
|
+
//#region src/loader.ts
|
|
9
|
+
var u = /* @__PURE__ */ new WeakMap(), d = (e) => e?.reason ?? new DOMException("The operation was aborted.", "AbortError");
|
|
10
|
+
function f(t, n) {
|
|
11
|
+
return new Promise((r, i) => {
|
|
12
|
+
let a = t.decodeAudioData(n, r, (t) => i(t ?? e("failed", "Audio decoding failed; use a file format this browser can decode (ogg, mp3, wav or m4a)")));
|
|
13
|
+
a && typeof a.then == "function" && a.then(r, i);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function p(t, n, r = {}) {
|
|
17
|
+
let { signal: i, cache: a = !0 } = r;
|
|
18
|
+
if (i?.aborted) return Promise.reject(d(i));
|
|
19
|
+
let o = r.fetch ?? globalThis.fetch;
|
|
20
|
+
if (!o) return Promise.reject(e("unsupported", "fetch is unavailable in this environment; pass options.fetch"));
|
|
21
|
+
let s = u.get(t);
|
|
22
|
+
s || (s = /* @__PURE__ */ new Map(), u.set(t, s));
|
|
23
|
+
let c = String(n), l = a ? s.get(c) : void 0;
|
|
24
|
+
if (!l) {
|
|
25
|
+
let n = new AbortController(), r = {
|
|
26
|
+
controller: n,
|
|
27
|
+
waiters: 0,
|
|
28
|
+
settled: !1,
|
|
29
|
+
promise: (async () => {
|
|
30
|
+
let r = await o(c, { signal: n.signal });
|
|
31
|
+
if (!r.ok) throw e("failed", `HTTP ${r.status} loading ${c}; check the URL and that the server allows this origin`);
|
|
32
|
+
let i = await r.arrayBuffer();
|
|
33
|
+
return n.signal.throwIfAborted(), f(t, i);
|
|
34
|
+
})()
|
|
35
|
+
};
|
|
36
|
+
r.promise.then(() => {
|
|
37
|
+
r.settled = !0;
|
|
38
|
+
}, () => {
|
|
39
|
+
r.settled = !0, s.get(c) === r && s.delete(c);
|
|
40
|
+
}), a && s.set(c, r), l = r;
|
|
41
|
+
}
|
|
42
|
+
let p = l;
|
|
43
|
+
return p.waiters++, new Promise((e, t) => {
|
|
44
|
+
let n = !1, r = () => !n && (n = !0, p.waiters--, i?.removeEventListener("abort", a), !0), a = () => {
|
|
45
|
+
r() && (t(d(i)), p.waiters === 0 && !p.settled && (p.controller.abort(), s.get(c) === p && s.delete(c)));
|
|
46
|
+
};
|
|
47
|
+
i?.addEventListener("abort", a, { once: !0 }), p.promise.then((t) => {
|
|
48
|
+
r() && e(t);
|
|
49
|
+
}, (e) => {
|
|
50
|
+
r() && t(e);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function m(e, t) {
|
|
55
|
+
let n = u.get(e);
|
|
56
|
+
n && (t === void 0 ? n.clear() : n.delete(String(t)));
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/listener.ts
|
|
60
|
+
var h = [
|
|
61
|
+
"pointerdown",
|
|
62
|
+
"mousedown",
|
|
63
|
+
"keydown",
|
|
64
|
+
"touchend"
|
|
65
|
+
], g = [
|
|
66
|
+
0,
|
|
67
|
+
0,
|
|
68
|
+
-1
|
|
69
|
+
], _ = [
|
|
70
|
+
0,
|
|
71
|
+
1,
|
|
72
|
+
0
|
|
73
|
+
], v = [
|
|
74
|
+
0,
|
|
75
|
+
0,
|
|
76
|
+
1
|
|
77
|
+
];
|
|
78
|
+
function y(e, t, n) {
|
|
79
|
+
let r = e[0], i = e[1], a = e[2], o = t[0] * r + t[1] * i + t[2] * a, s = t[0] - o * r, c = t[1] - o * i, l = t[2] - o * a, u = Math.hypot(s, c, l);
|
|
80
|
+
u > 1e-6 || (o = i, s = -o * r, c = 1 - o * i, l = -o * a, u = Math.hypot(s, c, l), u > 1e-6 || (s = 0, c = 0, l = i > 0 ? 1 : -1, u = 1)), n[0] = s / u, n[1] = c / u, n[2] = l / u;
|
|
81
|
+
}
|
|
82
|
+
var b = () => {
|
|
83
|
+
let t = globalThis.AudioContext;
|
|
84
|
+
if (!t) throw e("unsupported", "Web Audio is unavailable in this environment; run in a browser, or pass options.context (for example an OfflineAudioContext)");
|
|
85
|
+
return t;
|
|
86
|
+
}, x = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
87
|
+
static attach(t, n) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
88
|
+
let r = new e(n);
|
|
89
|
+
return t.use(r), r;
|
|
90
|
+
}
|
|
91
|
+
constructor(e = {}) {
|
|
92
|
+
if (this.position = /* @__PURE__ */ new Float64Array(3), this.forward = new Float64Array([
|
|
93
|
+
0,
|
|
94
|
+
0,
|
|
95
|
+
-1
|
|
96
|
+
]), this.up = new Float64Array([
|
|
97
|
+
0,
|
|
98
|
+
1,
|
|
99
|
+
0
|
|
100
|
+
]), this.owned = !1, this.engine = null, this.sources = /* @__PURE__ */ new Set(), this.written = !1, this.cameraForward = [
|
|
101
|
+
0,
|
|
102
|
+
0,
|
|
103
|
+
-1
|
|
104
|
+
], this.meshUp = [
|
|
105
|
+
0,
|
|
106
|
+
1,
|
|
107
|
+
0
|
|
108
|
+
], this.meshPosition = [
|
|
109
|
+
0,
|
|
110
|
+
0,
|
|
111
|
+
0
|
|
112
|
+
], this.localForward = new Float64Array([
|
|
113
|
+
0,
|
|
114
|
+
0,
|
|
115
|
+
1
|
|
116
|
+
]), this.localUp = new Float64Array([
|
|
117
|
+
0,
|
|
118
|
+
1,
|
|
119
|
+
0
|
|
120
|
+
]), this.target = "camera", this.forwardScratch = /* @__PURE__ */ new Float64Array(3), this.upScratch = /* @__PURE__ */ new Float64Array(3), this.onGesture = () => {
|
|
121
|
+
let e = this.audioContext;
|
|
122
|
+
e && (e.state === "running" ? this.removeGestureListeners() : this.resume().then(() => {
|
|
123
|
+
e.state === "running" && this.removeGestureListeners();
|
|
124
|
+
}, () => {}));
|
|
125
|
+
}, this.name = e.name ?? "margoengine-audio", this.order = e.order ?? 900, this.source = e.context, e.context && typeof e.context != "function" && (this.audioContext = e.context), this.autoResume = e.autoResume ?? !0, this.smoothing = e.smoothing ?? .02, this.volumeValue = e.volume ?? 1, !Number.isFinite(this.volumeValue) || this.volumeValue < 0) throw RangeError("Volume must be a finite number >= 0");
|
|
126
|
+
if (!Number.isFinite(this.smoothing) || this.smoothing < 0) throw RangeError("Smoothing must be >= 0");
|
|
127
|
+
}
|
|
128
|
+
get context() {
|
|
129
|
+
if (!this.audioContext) {
|
|
130
|
+
let e = this.source;
|
|
131
|
+
typeof e == "function" ? this.audioContext = e() : e ? this.audioContext = e : (this.audioContext = new (b())(), this.owned = !0);
|
|
132
|
+
}
|
|
133
|
+
return this.audioContext;
|
|
134
|
+
}
|
|
135
|
+
get hasContext() {
|
|
136
|
+
return this.audioContext !== void 0;
|
|
137
|
+
}
|
|
138
|
+
get input() {
|
|
139
|
+
if (!this.master) {
|
|
140
|
+
let e = this.context;
|
|
141
|
+
this.master = e.createGain(), this.master.gain.setValueAtTime(this.volumeValue, e.currentTime), this.master.connect(e.destination);
|
|
142
|
+
}
|
|
143
|
+
return this.master;
|
|
144
|
+
}
|
|
145
|
+
get volume() {
|
|
146
|
+
return this.volumeValue;
|
|
147
|
+
}
|
|
148
|
+
set volume(e) {
|
|
149
|
+
if (!Number.isFinite(e) || e < 0) throw RangeError("Volume must be a finite number >= 0");
|
|
150
|
+
this.volumeValue = e, this.master && t(this.master.gain, e, this.context, this.smoothing);
|
|
151
|
+
}
|
|
152
|
+
get attachedEngine() {
|
|
153
|
+
return this.engine;
|
|
154
|
+
}
|
|
155
|
+
async resume() {
|
|
156
|
+
let e = this.audioContext;
|
|
157
|
+
e && e.state === "suspended" && typeof e.close == "function" && await e.resume();
|
|
158
|
+
}
|
|
159
|
+
async suspend() {
|
|
160
|
+
let e = this.audioContext;
|
|
161
|
+
e && e.state === "running" && typeof e.close == "function" && await e.suspend();
|
|
162
|
+
}
|
|
163
|
+
load(e, t) {
|
|
164
|
+
return p(this.context, e, t);
|
|
165
|
+
}
|
|
166
|
+
setup(e) {
|
|
167
|
+
if (this.engine = e, this.autoResume) {
|
|
168
|
+
let t = e.canvas.ownerDocument ?? globalThis.document, n = t?.defaultView ?? t;
|
|
169
|
+
if (n) {
|
|
170
|
+
this.gestureTarget = n;
|
|
171
|
+
for (let e of h) n.addEventListener(e, this.onGesture, {
|
|
172
|
+
capture: !0,
|
|
173
|
+
passive: !0
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
registerPasses(e) {
|
|
179
|
+
e.add({
|
|
180
|
+
name: this.name,
|
|
181
|
+
order: this.order,
|
|
182
|
+
execute: () => this.update()
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
update() {
|
|
186
|
+
let e = this.engine;
|
|
187
|
+
e && !e.disposed && this.followTarget(e);
|
|
188
|
+
for (let t of this.sources) t.update(e && !e.disposed ? e : null);
|
|
189
|
+
}
|
|
190
|
+
get following() {
|
|
191
|
+
return this.target;
|
|
192
|
+
}
|
|
193
|
+
follow(e, t = v, n = _) {
|
|
194
|
+
if (e !== "camera" && e !== null && !Number.isInteger(l(e)?.id)) throw TypeError("The listener follows a core Mesh, 'camera' or null; pass one of those to follow()");
|
|
195
|
+
let i = this.forwardScratch, a = this.upScratch;
|
|
196
|
+
if (!r(t[0], t[1], t[2], i, v)) throw RangeError("Listener forward axis must be nonzero");
|
|
197
|
+
if (!r(n[0], n[1], n[2], a, _)) throw RangeError("Listener up axis must be nonzero");
|
|
198
|
+
return this.localForward.set(i), y(i, a, this.localUp), this.target = e, this;
|
|
199
|
+
}
|
|
200
|
+
followTarget(e) {
|
|
201
|
+
let t = this.target;
|
|
202
|
+
if (t === "camera") {
|
|
203
|
+
this.followCamera(e);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (!t) return;
|
|
207
|
+
let n = c(e), r = l(t).id;
|
|
208
|
+
if (n.meshes.get(r) !== t) return;
|
|
209
|
+
let i = n.store.matrices, a = r * 16, o = this.localForward, s = o[0], u = o[1], d = o[2], f = this.localUp, p = f[0], m = f[1], h = f[2], g = this.cameraForward, _ = this.meshUp;
|
|
210
|
+
g[0] = i[a] * s + i[a + 4] * u + i[a + 8] * d, g[1] = i[a + 1] * s + i[a + 5] * u + i[a + 9] * d, g[2] = i[a + 2] * s + i[a + 6] * u + i[a + 10] * d, _[0] = i[a] * p + i[a + 4] * m + i[a + 8] * h, _[1] = i[a + 1] * p + i[a + 5] * m + i[a + 9] * h, _[2] = i[a + 2] * p + i[a + 6] * m + i[a + 10] * h;
|
|
211
|
+
let v = this.meshPosition;
|
|
212
|
+
v[0] = i[a + 12], v[1] = i[a + 13], v[2] = i[a + 14], this.setPose(v, g, _);
|
|
213
|
+
}
|
|
214
|
+
followCamera(e) {
|
|
215
|
+
let t = e.camera, n = t.position, r = t.target, i = this.cameraForward;
|
|
216
|
+
i[0] = r[0] - n[0], i[1] = r[1] - n[1], i[2] = r[2] - n[2], this.setPose(n, i, t.up);
|
|
217
|
+
}
|
|
218
|
+
setPose(e, t, n = _) {
|
|
219
|
+
let i = this.forwardScratch, a = this.upScratch;
|
|
220
|
+
if (r(t[0], t[1], t[2], i, g), r(n[0], n[1], n[2], a, _), y(i, a, a), !Number.isFinite(e[0]) || !Number.isFinite(e[1]) || !Number.isFinite(e[2])) throw RangeError("Listener pose must be finite");
|
|
221
|
+
let o = !this.written;
|
|
222
|
+
for (let t = 0; t < 3; t++) (e[t] !== this.position[t] || i[t] !== this.forward[t] || a[t] !== this.up[t]) && (o = !0);
|
|
223
|
+
for (let t = 0; t < 3; t++) this.position[t] = e[t];
|
|
224
|
+
return this.forward.set(i), this.up.set(a), o && this.audioContext && this.writePose(), this;
|
|
225
|
+
}
|
|
226
|
+
writePose() {
|
|
227
|
+
let e = this.context, n = e.listener, r = !this.written;
|
|
228
|
+
this.written = !0;
|
|
229
|
+
let i = this.position[0], a = this.position[1], o = this.position[2], s = this.forward[0], c = this.forward[1], l = this.forward[2], u = this.up[0], d = this.up[1], f = this.up[2];
|
|
230
|
+
n.positionX ? (t(n.positionX, i, e, this.smoothing, r), t(n.positionY, a, e, this.smoothing, r), t(n.positionZ, o, e, this.smoothing, r), t(n.forwardX, s, e, this.smoothing, r), t(n.forwardY, c, e, this.smoothing, r), t(n.forwardZ, l, e, this.smoothing, r), t(n.upX, u, e, this.smoothing, r), t(n.upY, d, e, this.smoothing, r), t(n.upZ, f, e, this.smoothing, r)) : (n.setPosition(i, a, o), n.setOrientation(s, c, l, u, d, f));
|
|
231
|
+
}
|
|
232
|
+
addSource(e) {
|
|
233
|
+
this.sources.add(e), this.written || (this.engine && !this.engine.disposed && this.followTarget(this.engine), this.written || this.writePose());
|
|
234
|
+
}
|
|
235
|
+
removeSource(e) {
|
|
236
|
+
this.sources.delete(e);
|
|
237
|
+
}
|
|
238
|
+
removeGestureListeners() {
|
|
239
|
+
let e = this.gestureTarget;
|
|
240
|
+
if (e) {
|
|
241
|
+
for (let t of h) e.removeEventListener(t, this.onGesture, { capture: !0 });
|
|
242
|
+
this.gestureTarget = void 0;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
dispose() {
|
|
246
|
+
let e = this.engine?.sceneOf(this);
|
|
247
|
+
if (e) {
|
|
248
|
+
e.removePlugin(this.name);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
this.removeGestureListeners();
|
|
252
|
+
for (let e of [...this.sources]) e.dispose();
|
|
253
|
+
this.sources.clear(), this.master?.disconnect(), this.master = void 0;
|
|
254
|
+
let t = this.audioContext;
|
|
255
|
+
t && this.owned && t.state !== "closed" && t.close().catch(() => {}), this.owned && (this.audioContext = void 0, this.owned = !1, this.written = !1), this.engine = null;
|
|
256
|
+
}
|
|
257
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
258
|
+
this.dispose();
|
|
259
|
+
}
|
|
260
|
+
}, S = class {
|
|
261
|
+
constructor(e, t = {}) {
|
|
262
|
+
this.disposed = !1;
|
|
263
|
+
let n = typeof t == "number" ? { fftSize: t } : t;
|
|
264
|
+
this.input = "getOutput" in e ? e.getOutput() : e;
|
|
265
|
+
let r = this.input.context.createAnalyser();
|
|
266
|
+
r.fftSize = n.fftSize ?? 2048, n.smoothingTimeConstant !== void 0 && (r.smoothingTimeConstant = n.smoothingTimeConstant), n.minDecibels !== void 0 && (r.minDecibels = n.minDecibels), n.maxDecibels !== void 0 && (r.maxDecibels = n.maxDecibels), this.analyser = r, this.data = new Uint8Array(r.frequencyBinCount), this.waveform = new Uint8Array(r.fftSize), this.spectrum = new Float32Array(r.frequencyBinCount), this.input.connect(r);
|
|
267
|
+
}
|
|
268
|
+
getFrequencyData() {
|
|
269
|
+
return this.analyser.getByteFrequencyData(this.data), this.data;
|
|
270
|
+
}
|
|
271
|
+
getFloatFrequencyData(e = new Float32Array(this.analyser.frequencyBinCount)) {
|
|
272
|
+
return this.analyser.getFloatFrequencyData(e), e;
|
|
273
|
+
}
|
|
274
|
+
getWaveformData() {
|
|
275
|
+
return this.analyser.getByteTimeDomainData(this.waveform), this.waveform;
|
|
276
|
+
}
|
|
277
|
+
getFloatWaveformData(e = new Float32Array(this.analyser.fftSize)) {
|
|
278
|
+
return this.analyser.getFloatTimeDomainData(e), e;
|
|
279
|
+
}
|
|
280
|
+
getAverageFrequency() {
|
|
281
|
+
let e = this.getFrequencyData(), t = 0;
|
|
282
|
+
for (let n = 0; n < e.length; n++) t += e[n];
|
|
283
|
+
return e.length ? t / e.length : 0;
|
|
284
|
+
}
|
|
285
|
+
getDominantFrequency() {
|
|
286
|
+
let e = this.getFloatFrequencyData(this.spectrum), t = 0;
|
|
287
|
+
for (let n = 1; n < e.length; n++) e[n] > e[t] && (t = n);
|
|
288
|
+
return t * this.analyser.context.sampleRate / this.analyser.fftSize;
|
|
289
|
+
}
|
|
290
|
+
dispose() {
|
|
291
|
+
if (!this.disposed) {
|
|
292
|
+
this.disposed = !0;
|
|
293
|
+
try {
|
|
294
|
+
this.input.disconnect(this.analyser);
|
|
295
|
+
} catch {}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
299
|
+
this.dispose();
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
//#endregion
|
|
303
|
+
export { a as Audio, S as AudioAnalyser, x as AudioListener, s as AudioVoicePool, o as PositionalAudio, m as clearAudioBufferCache, i as coneGain, f as decodeAudio, n as distanceGain, p as loadAudioBuffer, t as smoothParam };
|