@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,116 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel, EnginePlugin, Mesh } from '@margoengine/core';
|
|
3
|
+
import { type FrameGraph } from '@margoengine/core/extension';
|
|
4
|
+
import { type Vec3 } from './math.js';
|
|
5
|
+
import { type LoadAudioOptions } from './loader.js';
|
|
6
|
+
/**
|
|
7
|
+
* Options for {@link AudioListener}. Every field is optional.
|
|
8
|
+
* @example
|
|
9
|
+
* AudioListener.attach(engine, {volume: 0.8});
|
|
10
|
+
*/
|
|
11
|
+
export interface AudioListenerOptions {
|
|
12
|
+
/** An existing context (for example an OfflineAudioContext) or a factory. Default: `new AudioContext()` on first use. */
|
|
13
|
+
context?: BaseAudioContext | (() => BaseAudioContext);
|
|
14
|
+
/** Master volume, default 1. */
|
|
15
|
+
volume?: number;
|
|
16
|
+
/** Resume a suspended context on the first pointer, key or touch gesture. Default true. */
|
|
17
|
+
autoResume?: boolean;
|
|
18
|
+
/** Time constant in seconds for parameter smoothing. 0 sets values immediately. Default 0.02. */
|
|
19
|
+
smoothing?: number;
|
|
20
|
+
/** Plugin and frame-pass name. Default `'margoengine-audio'`. */
|
|
21
|
+
name?: string;
|
|
22
|
+
/** Frame-pass order. Default 900, after the scene and overlay passes. */
|
|
23
|
+
order?: number;
|
|
24
|
+
}
|
|
25
|
+
/** Internal contract between the listener and the sounds it owns. */
|
|
26
|
+
export interface ListenerSource {
|
|
27
|
+
update(engine: EngineKernel | null): void;
|
|
28
|
+
dispose(): void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The ears of the scene. As an engine plugin it follows `engine.camera` every frame (position, forward from the
|
|
32
|
+
* camera position toward its target, camera up), or a mesh such as the player character (`follow`). Owns the master gain that every Audio and PositionalAudio feeds.
|
|
33
|
+
* The AudioContext is created lazily on first use, never at import time.
|
|
34
|
+
*/
|
|
35
|
+
export declare class AudioListener implements EnginePlugin {
|
|
36
|
+
readonly name: string;
|
|
37
|
+
/** Last pose written to the Web Audio listener (world space). */
|
|
38
|
+
readonly position: Float64Array<ArrayBuffer>;
|
|
39
|
+
readonly forward: Float64Array<ArrayBuffer>;
|
|
40
|
+
readonly up: Float64Array<ArrayBuffer>;
|
|
41
|
+
smoothing: number;
|
|
42
|
+
readonly autoResume: boolean;
|
|
43
|
+
private readonly order;
|
|
44
|
+
private readonly source?;
|
|
45
|
+
private audioContext?;
|
|
46
|
+
private owned;
|
|
47
|
+
private master?;
|
|
48
|
+
private volumeValue;
|
|
49
|
+
private engine;
|
|
50
|
+
private readonly sources;
|
|
51
|
+
private gestureTarget?;
|
|
52
|
+
private written;
|
|
53
|
+
private readonly cameraForward;
|
|
54
|
+
private readonly meshUp;
|
|
55
|
+
private readonly meshPosition;
|
|
56
|
+
private readonly localForward;
|
|
57
|
+
private readonly localUp;
|
|
58
|
+
private target;
|
|
59
|
+
private readonly forwardScratch;
|
|
60
|
+
private readonly upScratch;
|
|
61
|
+
private readonly onGesture;
|
|
62
|
+
/** Same as `engine.use(new AudioListener(...))`; returns the AudioListener. */
|
|
63
|
+
static attach(engine: EngineKernel, options?: AudioListenerOptions): AudioListener;
|
|
64
|
+
constructor(options?: AudioListenerOptions);
|
|
65
|
+
/** The audio context, created on first access. */
|
|
66
|
+
get context(): BaseAudioContext;
|
|
67
|
+
/** True once the context exists. Reading `context` creates it; this does not. */
|
|
68
|
+
get hasContext(): boolean;
|
|
69
|
+
/** Master gain node; every sound connects here. Connected to `context.destination`. */
|
|
70
|
+
get input(): GainNode;
|
|
71
|
+
get volume(): number;
|
|
72
|
+
set volume(value: number);
|
|
73
|
+
/** The engine this listener is attached to, if any. */
|
|
74
|
+
get attachedEngine(): EngineKernel | null;
|
|
75
|
+
/** Resumes a suspended AudioContext. Resolves immediately for offline contexts. */
|
|
76
|
+
resume(): Promise<void>;
|
|
77
|
+
/** Suspends a running AudioContext (all sounds freeze in place). */
|
|
78
|
+
suspend(): Promise<void>;
|
|
79
|
+
/** Fetches and decodes a sound with this listener's context, cached by URL. */
|
|
80
|
+
load(url: string, options?: LoadAudioOptions): Promise<AudioBuffer>;
|
|
81
|
+
setup(engine: EngineKernel): void;
|
|
82
|
+
registerPasses(graph: FrameGraph): void;
|
|
83
|
+
/**
|
|
84
|
+
* Copies the camera pose into the Web Audio listener and updates every positional sound. Runs once per frame
|
|
85
|
+
* as a frame pass (after the camera and world matrices are current). Call it yourself when not using an engine.
|
|
86
|
+
*/
|
|
87
|
+
update(): void;
|
|
88
|
+
/** What the listener follows each frame: `'camera'` (the default), a mesh, or null (only `setPose` moves it). */
|
|
89
|
+
get following(): 'camera' | Mesh | null;
|
|
90
|
+
/**
|
|
91
|
+
* Puts the ears on something other than the camera: a mesh (its world position, its local `forward` axis and its
|
|
92
|
+
* local `up` axis, by default `[0, 0, 1]` and +Y, for example a third-person character), `'camera'` (the default),
|
|
93
|
+
* or null to keep the last pose and place the listener only with `setPose`. A disposed mesh keeps its last pose.
|
|
94
|
+
* `up` is made independent of `forward` as {@link AudioListener.setPose} does, so `follow(mesh, [0, 1, 0])` (a
|
|
95
|
+
* mesh that faces along its own +Y) hears with local +Z as up.
|
|
96
|
+
*/
|
|
97
|
+
follow(target: 'camera' | Mesh | null, forward?: Vec3, up?: Vec3): this;
|
|
98
|
+
private followTarget;
|
|
99
|
+
private followCamera;
|
|
100
|
+
/**
|
|
101
|
+
* Sets the listener pose directly (world position, forward direction, up direction). The listener keeps `forward`
|
|
102
|
+
* and stores the part of `up` at right angles to it, so the pose it hands Web Audio always has a right axis; an up
|
|
103
|
+
* along forward (looking straight down with up +Y) becomes the up of that view pitched from level: [0, 0, -1]
|
|
104
|
+
* looking down, [0, 0, 1] looking up.
|
|
105
|
+
*/
|
|
106
|
+
setPose(position: Vec3, forward: Vec3, up?: Vec3): this;
|
|
107
|
+
private writePose;
|
|
108
|
+
/** @internal Registers a sound so the listener updates and disposes it. Creates the context. */
|
|
109
|
+
addSource(source: ListenerSource): void;
|
|
110
|
+
/** @internal */
|
|
111
|
+
removeSource(source: ListenerSource): void;
|
|
112
|
+
private removeGestureListeners;
|
|
113
|
+
/** Stops and disposes every sound, disconnects the master gain and closes a context this listener created. */
|
|
114
|
+
dispose(): void;
|
|
115
|
+
[Symbol.dispose]: () => void;
|
|
116
|
+
}
|
package/dist/loader.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for {@link loadAudioBuffer} and `AudioListener.load`.
|
|
3
|
+
* @example
|
|
4
|
+
* const buffer = await listener.load('/sounds/step.ogg', {signal: AbortSignal.timeout(10_000)});
|
|
5
|
+
*/
|
|
6
|
+
export interface LoadAudioOptions {
|
|
7
|
+
/** Rejects this caller with an AbortError. The download itself is cancelled once every waiting caller aborted. */
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
/** Share and keep the decoded buffer per context and URL. Default true. */
|
|
10
|
+
cache?: boolean;
|
|
11
|
+
/** Custom fetch, for example one that adds headers. Default `globalThis.fetch`. */
|
|
12
|
+
fetch?: (input: string, init?: RequestInit) => Promise<Response>;
|
|
13
|
+
}
|
|
14
|
+
/** Wraps both the promise and the legacy callback forms of `decodeAudioData`. */
|
|
15
|
+
export declare function decodeAudio(context: BaseAudioContext, bytes: ArrayBuffer): Promise<AudioBuffer>;
|
|
16
|
+
/**
|
|
17
|
+
* Fetches and decodes an audio file into an AudioBuffer. Concurrent and repeated
|
|
18
|
+
* requests for the same URL on the same context share one download and one decoded buffer.
|
|
19
|
+
*/
|
|
20
|
+
export declare function loadAudioBuffer(context: BaseAudioContext, url: string, options?: LoadAudioOptions): Promise<AudioBuffer>;
|
|
21
|
+
/** Drops cached buffers for one context, or for one URL on that context. */
|
|
22
|
+
export declare function clearAudioBufferCache(context: BaseAudioContext, url?: string): void;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//#region src/math.ts
|
|
2
|
+
function e(e, t, n = 1, r = 1e4, i = 1) {
|
|
3
|
+
if (![
|
|
4
|
+
t,
|
|
5
|
+
n,
|
|
6
|
+
r,
|
|
7
|
+
i
|
|
8
|
+
].every(Number.isFinite)) throw RangeError("Distance parameters must be finite");
|
|
9
|
+
if (e === "linear") {
|
|
10
|
+
let e = Math.min(1, Math.max(0, i)), a = Math.min(n, r), o = Math.max(n, r);
|
|
11
|
+
return a === o ? 1 - e : 1 - e * (Math.min(o, Math.max(a, t)) - n) / (r - n);
|
|
12
|
+
}
|
|
13
|
+
if (n === 0) return 0;
|
|
14
|
+
let a = Math.max(t, n);
|
|
15
|
+
if (e === "inverse") return n / (n + i * (a - n));
|
|
16
|
+
if (e === "exponential") return (a / n) ** +-i;
|
|
17
|
+
throw TypeError(`Unknown distance model ${String(e)}; use 'linear', 'inverse' or 'exponential'`);
|
|
18
|
+
}
|
|
19
|
+
function t(e, t, n, r = 360, i = 360, a = 0) {
|
|
20
|
+
let o = Math.hypot(t[0], t[1], t[2]);
|
|
21
|
+
if (!o || r === 360 && i === 360) return 1;
|
|
22
|
+
let s = n[0] - e[0], c = n[1] - e[1], l = n[2] - e[2], u = Math.hypot(s, c, l);
|
|
23
|
+
if (!u) return 1;
|
|
24
|
+
let d = (s * t[0] + c * t[1] + l * t[2]) / (u * o), f = Math.acos(Math.max(-1, Math.min(1, d))) * 180 / Math.PI, p = Math.abs(r) / 2, m = Math.abs(i) / 2;
|
|
25
|
+
if (f <= p) return 1;
|
|
26
|
+
if (f >= m) return a;
|
|
27
|
+
let h = (f - p) / (m - p);
|
|
28
|
+
return 1 - h + a * h;
|
|
29
|
+
}
|
|
30
|
+
function n(e, t, n, r, i = [
|
|
31
|
+
0,
|
|
32
|
+
0,
|
|
33
|
+
-1
|
|
34
|
+
]) {
|
|
35
|
+
let a = Math.hypot(e, t, n);
|
|
36
|
+
return !a || !Number.isFinite(a) ? (r[0] = i[0], r[1] = i[1], r[2] = i[2], !1) : (r[0] = e / a, r[1] = t / a, r[2] = n / a, !0);
|
|
37
|
+
}
|
|
38
|
+
function r(e, t, n, r, i = !1) {
|
|
39
|
+
if (!Number.isFinite(t)) throw RangeError("Audio parameter value must be finite");
|
|
40
|
+
let a = n.currentTime;
|
|
41
|
+
e.cancelScheduledValues(a), i || r <= 0 || n.state !== "running" ? (e.setValueAtTime(t, a), e.value = t) : e.setTargetAtTime(t, a, r);
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
export { r as i, e as n, n as r, t };
|
package/dist/math.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** A read-only `[x, y, z]` vector in metres. */
|
|
2
|
+
export type Vec3 = readonly [number, number, number];
|
|
3
|
+
/** How volume falls with distance, as in the Web Audio PannerNode. */
|
|
4
|
+
export type DistanceModel = 'linear' | 'inverse' | 'exponential';
|
|
5
|
+
/**
|
|
6
|
+
* Distance attenuation exactly as the Web Audio PannerNode applies it (spec section "Distance Effects").
|
|
7
|
+
* Useful for UI meters and for tests that check rendered output against the selected model.
|
|
8
|
+
*/
|
|
9
|
+
export declare function distanceGain(model: DistanceModel, distance: number, refDistance?: number, maxDistance?: number, rolloffFactor?: number): number;
|
|
10
|
+
/** Sound cone gain as the Web Audio PannerNode computes it, with angles in degrees. */
|
|
11
|
+
export declare function coneGain(source: Vec3, direction: Vec3, listener: Vec3, innerAngle?: number, outerAngle?: number, outerGain?: number): number;
|
|
12
|
+
/** Normalizes `value`, returning `fallback` for a zero vector. */
|
|
13
|
+
export declare function normalize(x: number, y: number, z: number, out: number[] | Float64Array, fallback?: Vec3): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Moves an AudioParam to `value`. With a positive time constant and a running context it eases there with
|
|
16
|
+
* `setTargetAtTime`, which avoids zipper noise and clicks; otherwise (first write, suspended or offline context)
|
|
17
|
+
* it sets the value at the current time so nothing sweeps in from the previous value.
|
|
18
|
+
*/
|
|
19
|
+
export declare function smoothParam(param: AudioParam, value: number, context: BaseAudioContext, timeConstant: number, immediate?: boolean): void;
|
package/dist/music.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { AudioListener } from './listener.js';
|
|
3
|
+
/** Stem levels in dB, keyed by stem name. Omitted stems are silent; -Infinity is silence. */
|
|
4
|
+
export type MusicState = Readonly<Record<string, number>>;
|
|
5
|
+
/** Fixed tempo, shared loop length and audio-clock scheduling defaults. */
|
|
6
|
+
export interface AudioMusicOptions {
|
|
7
|
+
/** Tempo in quarter-note beats per minute. Default 120. Fixed while playing. */
|
|
8
|
+
bpm?: number;
|
|
9
|
+
/** Positive integer beats per bar. Default 4. */
|
|
10
|
+
beatsPerBar?: number;
|
|
11
|
+
/** Positive integer beats in every stem loop. Default 4. Buffers must cover this interval. */
|
|
12
|
+
loopBeats?: number;
|
|
13
|
+
/** Audio-clock scheduling headroom in seconds. Default 0.05. */
|
|
14
|
+
leadTime?: number;
|
|
15
|
+
/** Mixer exponential fade seconds (about 98 percent settled). Default 0.02; 0 jumps. */
|
|
16
|
+
fade?: number;
|
|
17
|
+
}
|
|
18
|
+
/** Grid and fade settings for one game-state transition. */
|
|
19
|
+
export interface MusicTransitionOptions {
|
|
20
|
+
/** Grid relative to the start time. Default 'bar'. */
|
|
21
|
+
quantize?: 'beat' | 'bar';
|
|
22
|
+
/** Earliest audio-clock time, also respecting currentTime + leadTime. Default now. */
|
|
23
|
+
after?: number;
|
|
24
|
+
/** Fade duration, overriding the constructor default. */
|
|
25
|
+
fade?: number;
|
|
26
|
+
}
|
|
27
|
+
/** Phase-aligned looping stems with game-state gain changes on an audio-clock beat/bar grid. */
|
|
28
|
+
export declare class AudioMusic {
|
|
29
|
+
readonly listener: AudioListener;
|
|
30
|
+
private readonly mixer;
|
|
31
|
+
private readonly stems;
|
|
32
|
+
private readonly states;
|
|
33
|
+
private readonly beat;
|
|
34
|
+
private readonly bar;
|
|
35
|
+
private readonly loop;
|
|
36
|
+
private readonly leadTime;
|
|
37
|
+
private readonly fade;
|
|
38
|
+
private readonly sources;
|
|
39
|
+
private origin;
|
|
40
|
+
/** Index of the next loop to schedule, counted from origin. */
|
|
41
|
+
private next;
|
|
42
|
+
/** Audio time of the offline render's next refill (a suspend point), or null when none is pending. */
|
|
43
|
+
private checkpoint;
|
|
44
|
+
/** An offline render that cannot refill at suspend points is scheduled whole. */
|
|
45
|
+
private whole;
|
|
46
|
+
private disposed;
|
|
47
|
+
constructor(listener: AudioListener, stems: Readonly<Record<string, AudioBuffer>>, states: Readonly<Record<string, MusicState>>, options?: AudioMusicOptions);
|
|
48
|
+
/**
|
|
49
|
+
* Start every stem at the same audio time and loop phase. Returns that time. Already playing raises state.
|
|
50
|
+
* Each loop k of each stem is its own one-shot source started at `at + k * loop` on the audio clock, so a browser
|
|
51
|
+
* that rounds start times to whole frames stays within a frame of the grid and never accumulates drift.
|
|
52
|
+
*/
|
|
53
|
+
start(state: string, at?: number): number;
|
|
54
|
+
/** Schedule a state on the next grid boundary. A new request replaces automation at/after its boundary. */
|
|
55
|
+
transition(state: string, options?: MusicTransitionOptions): number;
|
|
56
|
+
/** Stop immediately, including scheduled starts. A later start resets the grid and all pending state automation. */
|
|
57
|
+
stop(): void;
|
|
58
|
+
/** @internal Keeps loops scheduled ahead of the audio clock; every loop end refills too, so hidden tabs keep time. */
|
|
59
|
+
update(): void;
|
|
60
|
+
/**
|
|
61
|
+
* Schedule loops whose start lies before the horizon: max(1 s, 2 loops) past currentTime + leadTime on a live context.
|
|
62
|
+
* An OfflineAudioContext renders in one go, so main-thread timers cannot refill it. Where it has suspend(time)
|
|
63
|
+
* (Chromium, Safari) the loops are scheduled a window at a time and refilled at a suspend point: every scheduled
|
|
64
|
+
* source costs the renderer work on every render quantum until it ends, so scheduling an hour of loops at once made
|
|
65
|
+
* the render about 40 times slower (KB5029b). Without suspend(time) (Firefox) the whole render is scheduled at once.
|
|
66
|
+
* A loop whose start already passed (a stalled main thread) starts now at its matching offset, so the grid holds.
|
|
67
|
+
*/
|
|
68
|
+
private schedule;
|
|
69
|
+
/** Suspend the offline render at `at`, schedule the next window there and resume it. */
|
|
70
|
+
private refillAt;
|
|
71
|
+
/** Stop sources, release owned mixer buses, keep shared buffers. Listener teardown also disposes music. */
|
|
72
|
+
dispose(): void;
|
|
73
|
+
private levels;
|
|
74
|
+
private assertLive;
|
|
75
|
+
[Symbol.dispose]: () => void;
|
|
76
|
+
}
|
package/dist/music.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { r as e, t } from "./errors-D8rc8kzg.js";
|
|
2
|
+
import { Mixer as n } from "@margoengine/mixer";
|
|
3
|
+
//#region src/music.ts
|
|
4
|
+
var r = 128, i = 8, a = 8;
|
|
5
|
+
function o(t, n) {
|
|
6
|
+
if (!Number.isFinite(t) || t < 0) throw e(`Music ${n} must be finite and nonnegative; use audio-clock seconds`, "state");
|
|
7
|
+
return t;
|
|
8
|
+
}
|
|
9
|
+
var s = class {
|
|
10
|
+
constructor(r, i, a, s = {}) {
|
|
11
|
+
this.states = /* @__PURE__ */ new Map(), this.sources = /* @__PURE__ */ new Set(), this.origin = null, this.next = 0, this.checkpoint = null, this.whole = !1, this.disposed = !1, this.listener = r;
|
|
12
|
+
let c = s.bpm ?? 120, l = s.beatsPerBar ?? 4, u = s.loopBeats ?? 4;
|
|
13
|
+
if (!Number.isFinite(c) || c <= 0 || !Number.isInteger(l) || l < 1 || !Number.isInteger(u) || u < 1) throw e("Music needs positive finite bpm and positive integer beatsPerBar/loopBeats", "state");
|
|
14
|
+
if (this.beat = 60 / c, this.bar = this.beat * l, this.loop = this.beat * u, !Number.isFinite(this.bar) || !Number.isFinite(this.loop) || this.beat <= 0) throw e("Music grid must fit finite audio-clock seconds; use a practical tempo and loop length", "state");
|
|
15
|
+
if (this.leadTime = o(s.leadTime ?? .05, "leadTime"), this.fade = o(s.fade ?? .02, "fade"), this.stems = Object.entries(i), !this.stems.length || this.stems.some(([e, t]) => !e || !Number.isFinite(t?.duration) || t.duration < this.loop)) throw t("state", "Music needs named decoded stems covering loopBeats; load buffers before construction");
|
|
16
|
+
let d = new Set(this.stems.map(([e]) => e));
|
|
17
|
+
for (let [e, n] of Object.entries(a)) {
|
|
18
|
+
if (!e || !n || Object.entries(n).some(([e, t]) => !d.has(e) || t !== -Infinity && (!Number.isFinite(t) || !Number.isFinite(Math.fround(10 ** (t / 20)))))) throw t("state", "Music states need known stem names and finite float32 gains in dB, or -Infinity");
|
|
19
|
+
this.states.set(e, this.stems.map(([e]) => Object.hasOwn(n, e) ? n[e] : -Infinity));
|
|
20
|
+
}
|
|
21
|
+
if (!this.states.size) throw t("state", "Music needs at least one named state with stem dB levels");
|
|
22
|
+
this.mixer = new n(r.context, { destination: r.input }), this.stems.forEach((e, t) => this.mixer.createBus(String(t), { gain: -Infinity })), r.addSource(this);
|
|
23
|
+
}
|
|
24
|
+
start(e, n = this.listener.context.currentTime + this.leadTime) {
|
|
25
|
+
this.assertLive();
|
|
26
|
+
let r = this.levels(e), i = this.listener.context;
|
|
27
|
+
if (o(n, "start time"), this.origin !== null || n < i.currentTime || !Number.isFinite(n + this.loop)) throw t("state", "Music start needs a stopped transport and a finite future time; stop first or use transition");
|
|
28
|
+
this.stems.forEach((e, t) => this.mixer.bus(String(t)).setGain(r[t], {
|
|
29
|
+
at: i.currentTime,
|
|
30
|
+
seconds: 0
|
|
31
|
+
})), this.origin = n, this.next = 0;
|
|
32
|
+
try {
|
|
33
|
+
this.schedule();
|
|
34
|
+
} catch (e) {
|
|
35
|
+
throw this.stop(), e;
|
|
36
|
+
}
|
|
37
|
+
return n;
|
|
38
|
+
}
|
|
39
|
+
transition(n, r = {}) {
|
|
40
|
+
this.assertLive();
|
|
41
|
+
let i = this.levels(n);
|
|
42
|
+
if (this.origin === null) throw t("state", "Music is stopped; call start(state) before transitioning");
|
|
43
|
+
let a = r.quantize ?? "bar";
|
|
44
|
+
if (a !== "beat" && a !== "bar") throw t("state", "Music quantize must be 'beat' or 'bar'");
|
|
45
|
+
let s = o(r.after ?? this.listener.context.currentTime, "after"), c = o(r.fade ?? this.fade, "fade"), l = a === "beat" ? this.beat : this.bar, u = Math.max(this.origin, s, this.listener.context.currentTime + this.leadTime), d = this.origin + Math.ceil((u - this.origin) / l) * l;
|
|
46
|
+
if (!Number.isFinite(d)) throw e("Music transition time overflow; use a nearer audio-clock time", "state");
|
|
47
|
+
return i.forEach((e, t) => this.mixer.bus(String(t)).setGain(e, {
|
|
48
|
+
at: d,
|
|
49
|
+
seconds: c
|
|
50
|
+
})), d;
|
|
51
|
+
}
|
|
52
|
+
stop() {
|
|
53
|
+
this.assertLive();
|
|
54
|
+
for (let e of this.sources) e.onended = null, e.stop(), e.disconnect();
|
|
55
|
+
this.sources.clear(), this.origin = null;
|
|
56
|
+
}
|
|
57
|
+
update() {
|
|
58
|
+
!this.disposed && this.origin !== null && this.schedule();
|
|
59
|
+
}
|
|
60
|
+
schedule() {
|
|
61
|
+
let e = this.listener.context, t = this.origin, n = e.currentTime, o = e, s = n + this.leadTime + Math.max(1, 2 * this.loop);
|
|
62
|
+
if (typeof o.startRendering == "function" && typeof o.length == "number") {
|
|
63
|
+
let t = o.length / e.sampleRate;
|
|
64
|
+
if (s = t, !this.whole && typeof o.suspend == "function") {
|
|
65
|
+
let c = Math.max(i, a * this.loop), l = e.sampleRate, u = Math.floor((n + c / 2) * l / r) * r, d = this.checkpoint ?? Math.max(n + r / l, u / l);
|
|
66
|
+
d + c / 2 + this.leadTime < t && (s = d + c / 2 + this.leadTime, this.checkpoint === null && this.refillAt(o, d));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
for (let r = t + this.next * this.loop; r < s; r = t + ++this.next * this.loop) {
|
|
70
|
+
let t = Math.max(0, n - r);
|
|
71
|
+
t >= this.loop || this.stems.forEach(([, n], i) => {
|
|
72
|
+
let a = e.createBufferSource();
|
|
73
|
+
a.buffer = n, a.connect(this.mixer.bus(String(i)).input), a.onended = () => {
|
|
74
|
+
this.sources.delete(a), a.disconnect(), this.update();
|
|
75
|
+
};
|
|
76
|
+
try {
|
|
77
|
+
a.start(r + t, t, this.loop - t);
|
|
78
|
+
} catch (e) {
|
|
79
|
+
throw a.disconnect(), e;
|
|
80
|
+
}
|
|
81
|
+
this.sources.add(a);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
refillAt(e, t) {
|
|
86
|
+
this.checkpoint = t, e.suspend(t).then(() => (this.checkpoint = null, this.update(), e.resume()), () => {
|
|
87
|
+
this.checkpoint = null, this.whole = !0, this.update();
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
dispose() {
|
|
91
|
+
this.disposed || (this.stop(), this.disposed = !0, this.listener.removeSource(this), this.mixer.dispose());
|
|
92
|
+
}
|
|
93
|
+
levels(e) {
|
|
94
|
+
let n = this.states.get(e);
|
|
95
|
+
if (!n) throw t("state", `Unknown music state ${e}; define it in the constructor`);
|
|
96
|
+
return n;
|
|
97
|
+
}
|
|
98
|
+
assertLive() {
|
|
99
|
+
if (this.disposed) throw t("disposed", "AudioMusic is disposed; create a new transport");
|
|
100
|
+
}
|
|
101
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
102
|
+
this.dispose();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
//#endregion
|
|
106
|
+
export { s as AudioMusic };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel, Mesh } from '@margoengine/core';
|
|
3
|
+
import type { PositionalAudio } from './audio.js';
|
|
4
|
+
/** Geometry-driven attenuation settings. Distances use scene metres. */
|
|
5
|
+
export interface AudioOcclusionOptions {
|
|
6
|
+
/** Radius of the five-ray source cross. Zero traces only the centre. Default 0.25 m. */
|
|
7
|
+
radius?: number;
|
|
8
|
+
/** Linear gain with every ray blocked. Default 0.25, range 0..1. */
|
|
9
|
+
blockedGain?: number;
|
|
10
|
+
/** Low-pass cutoff with every ray blocked. Default 800 Hz, range 10..min(20000, Nyquist). */
|
|
11
|
+
blockedFrequency?: number;
|
|
12
|
+
/** Smoothing time constant in seconds. Default listener.smoothing. */
|
|
13
|
+
smoothing?: number;
|
|
14
|
+
/** Additional blocker predicate. Default all visible meshes except the source and followed listener mesh. */
|
|
15
|
+
filter?: (mesh: Mesh) => boolean;
|
|
16
|
+
/** Cancel creation while the optional BVH module loads. Default none. */
|
|
17
|
+
signal?: AbortSignal;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Five CPU geometry rays control a low-pass filter and a separate gain in a positional sound's filter chain.
|
|
21
|
+
* Registered with the sound's listener, after the sound's pose update. Works with WebGPU and WebGL2.
|
|
22
|
+
* This is a tunable obstruction model, not a diffraction or acoustic material simulation.
|
|
23
|
+
*/
|
|
24
|
+
export declare class AudioOcclusion {
|
|
25
|
+
/** The controlled sound, whose ordinary volume and filters remain independent. */
|
|
26
|
+
readonly sound: PositionalAudio;
|
|
27
|
+
/** Owned low-pass node. Do not disconnect it or replace the controlled sound's filters while active. */
|
|
28
|
+
readonly lowpass: BiquadFilterNode;
|
|
29
|
+
/** Owned attenuation node. */
|
|
30
|
+
readonly attenuation: GainNode;
|
|
31
|
+
private readonly ray;
|
|
32
|
+
private readonly radius;
|
|
33
|
+
private readonly level;
|
|
34
|
+
private readonly frequency;
|
|
35
|
+
private readonly openFrequency;
|
|
36
|
+
private readonly smoothing;
|
|
37
|
+
private amount;
|
|
38
|
+
private disposed;
|
|
39
|
+
private written;
|
|
40
|
+
/** Load the existing BVH implementation lazily, without pulling it into the audio root or import-time graph. */
|
|
41
|
+
static create(sound: PositionalAudio, options?: AudioOcclusionOptions): Promise<AudioOcclusion>;
|
|
42
|
+
private constructor();
|
|
43
|
+
/** Fraction of blocked rays, 0 clear through 1 fully occluded. Partial coverage models obstruction. */
|
|
44
|
+
get blocked(): number;
|
|
45
|
+
/** Recompute after current world matrices and sound/listener poses. Called automatically by listener.update(). */
|
|
46
|
+
update(engine?: EngineKernel | null): void;
|
|
47
|
+
/** Remove only the owned nodes, preserving caller filters and restoring the sound's direct path. Idempotent. */
|
|
48
|
+
dispose(): void;
|
|
49
|
+
[Symbol.dispose]: () => void;
|
|
50
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { t as e } from "./errors-D8rc8kzg.js";
|
|
2
|
+
import { i as t } from "./math-C82bLsED.js";
|
|
3
|
+
//#region src/occlusion.ts
|
|
4
|
+
function n(e, t, n, r) {
|
|
5
|
+
if (!Number.isFinite(e) || e < t || e > n) throw RangeError(`${r} needs a finite value in ${t}..${n}; choose a value in that range`);
|
|
6
|
+
return e;
|
|
7
|
+
}
|
|
8
|
+
var r = class r {
|
|
9
|
+
static async create(t, i = {}) {
|
|
10
|
+
let a = i.signal, o = i.filter;
|
|
11
|
+
if (a?.throwIfAborted(), t.isDisposed) throw e("disposed", "AudioOcclusion needs a live sound; create a new sound");
|
|
12
|
+
let s = n(i.radius ?? .25, 0, 100, "Occlusion radius"), c = n(i.blockedGain ?? .25, 0, 1, "Occlusion blockedGain"), l = n(i.blockedFrequency ?? 800, 10, Math.min(2e4, t.context.sampleRate / 2), "Occlusion blockedFrequency"), u = n(i.smoothing ?? t.listener.smoothing, 0, 60, "Occlusion smoothing");
|
|
13
|
+
if (o !== void 0 && typeof o != "function") throw TypeError("Occlusion filter needs a predicate; pass a function accepting a mesh");
|
|
14
|
+
let { Raycaster: d } = await import("@margoengine/bvh");
|
|
15
|
+
if (a?.throwIfAborted(), t.isDisposed) throw e("disposed", "AudioOcclusion needs a live sound; create a new sound");
|
|
16
|
+
return new r(t, new d(), {
|
|
17
|
+
filter: o,
|
|
18
|
+
radius: s,
|
|
19
|
+
blockedGain: c,
|
|
20
|
+
blockedFrequency: l,
|
|
21
|
+
smoothing: u
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
constructor(e, t, n) {
|
|
25
|
+
this.amount = 0, this.disposed = !1, this.written = !1, this.sound = e, this.ray = t, this.radius = n.radius, this.level = n.blockedGain, this.frequency = n.blockedFrequency, this.smoothing = n.smoothing, this.openFrequency = Math.min(2e4, e.context.sampleRate / 2), this.lowpass = e.context.createBiquadFilter(), this.lowpass.type = "lowpass", this.lowpass.Q.value = -3.01029995664, this.lowpass.frequency.value = this.openFrequency, this.attenuation = e.context.createGain(), t.firstHitOnly = !0, t.side = "double", t.near = .001, t.filter = (t) => t !== e.mesh && t !== e.listener.following && (n.filter?.(t) ?? !0), e.setFilters([
|
|
26
|
+
...e.getFilters(),
|
|
27
|
+
this.lowpass,
|
|
28
|
+
this.attenuation
|
|
29
|
+
]), e.listener.addSource(this);
|
|
30
|
+
}
|
|
31
|
+
get blocked() {
|
|
32
|
+
return this.amount;
|
|
33
|
+
}
|
|
34
|
+
update(n = this.sound.listener.attachedEngine) {
|
|
35
|
+
if (this.disposed) throw e("disposed", "AudioOcclusion is disposed; create a new controller");
|
|
36
|
+
if (this.sound.isDisposed) {
|
|
37
|
+
this.dispose();
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (!n || n.disposed) return;
|
|
41
|
+
let r = this.sound.worldPosition, i = this.sound.listener.position, a = r[0] - i[0], o = r[1] - i[1], s = r[2] - i[2], c = Math.hypot(a, o, s), l = 0, u = this.radius === 0 ? 1 : 5;
|
|
42
|
+
if (c > .002) {
|
|
43
|
+
let e = a / c, t = o / c, r = s / c, d = Math.abs(t) < .9, f = d ? r : 0, p = d ? 0 : -r, m = d ? -e : t, h = Math.hypot(f, p, m), g = [
|
|
44
|
+
f / h,
|
|
45
|
+
p / h,
|
|
46
|
+
m / h
|
|
47
|
+
], _ = [
|
|
48
|
+
t * g[2] - r * g[1],
|
|
49
|
+
r * g[0] - e * g[2],
|
|
50
|
+
e * g[1] - t * g[0]
|
|
51
|
+
];
|
|
52
|
+
for (let e = 0; e < u; e++) {
|
|
53
|
+
let t = e < 3 ? g : _, r = e === 0 ? 0 : e % 2 == 1 ? this.radius : -this.radius, c = [
|
|
54
|
+
a + t[0] * r,
|
|
55
|
+
o + t[1] * r,
|
|
56
|
+
s + t[2] * r
|
|
57
|
+
];
|
|
58
|
+
this.ray.set(i, c), this.ray.far = Math.hypot(...c) - .001, this.ray.intersectEngine(n, { refresh: e === 0 }).length && l++;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
this.amount = l / u;
|
|
62
|
+
let d = 1 + (this.level - 1) * this.amount, f = this.openFrequency * (this.frequency / this.openFrequency) ** this.amount;
|
|
63
|
+
t(this.attenuation.gain, d, this.sound.context, this.smoothing, !this.written), t(this.lowpass.frequency, f, this.sound.context, this.smoothing, !this.written), this.written = !0;
|
|
64
|
+
}
|
|
65
|
+
dispose() {
|
|
66
|
+
if (this.disposed) return;
|
|
67
|
+
let e = this.sound.getFilters();
|
|
68
|
+
!this.sound.isDisposed && (e.includes(this.lowpass) || e.includes(this.attenuation)) && this.sound.setFilters(e.filter((e) => e !== this.lowpass && e !== this.attenuation)), this.lowpass.disconnect(), this.attenuation.disconnect(), this.sound.listener.removeSource(this), this.disposed = !0;
|
|
69
|
+
}
|
|
70
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
71
|
+
this.dispose();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
//#endregion
|
|
75
|
+
export { r as AudioOcclusion };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { PositionalAudio } from './audio.js';
|
|
3
|
+
/** Structural reading so an Audio scene can use a physics path without loading the physics package itself. */
|
|
4
|
+
export interface OutdoorAudioPath {
|
|
5
|
+
readonly delaySeconds: number;
|
|
6
|
+
/** Signed gain at 63, 125, 250, 500, 1000, 2000, 4000 and 8000 Hz, in dB. */
|
|
7
|
+
readonly gainDb: ArrayLike<number>;
|
|
8
|
+
}
|
|
9
|
+
/** Settings for the outdoor audio controller. */
|
|
10
|
+
export interface OutdoorAudioOptions {
|
|
11
|
+
/** Maximum travel time supported by the delay line (default 30 s, range above 0 through 180 s). */
|
|
12
|
+
maxDelay?: number;
|
|
13
|
+
/** Smoothing time constant for path changes (default the listener's smoothing setting). */
|
|
14
|
+
smoothing?: number;
|
|
15
|
+
}
|
|
16
|
+
/** Gives one positional sound the solved path's time of flight, spreading and a broad spectral colour. */
|
|
17
|
+
export declare class OutdoorAudio {
|
|
18
|
+
readonly sound: PositionalAudio;
|
|
19
|
+
private readonly path;
|
|
20
|
+
private static readonly owners;
|
|
21
|
+
private readonly delay;
|
|
22
|
+
private readonly level;
|
|
23
|
+
private readonly bass;
|
|
24
|
+
private readonly treble;
|
|
25
|
+
private readonly upperDelay;
|
|
26
|
+
private readonly smoothing;
|
|
27
|
+
private readonly previousRolloff;
|
|
28
|
+
private readonly filters;
|
|
29
|
+
private initial;
|
|
30
|
+
private disposed;
|
|
31
|
+
constructor(sound: PositionalAudio, path: () => OutdoorAudioPath, options?: OutdoorAudioOptions);
|
|
32
|
+
/** Apply the latest world reading. The listener calls this on subsequent audio updates. */
|
|
33
|
+
update(): void;
|
|
34
|
+
/** Remove owned filters and restore the positional distance rolloff. */
|
|
35
|
+
dispose(): void;
|
|
36
|
+
[Symbol.dispose](): void;
|
|
37
|
+
}
|
package/dist/outdoor.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { r as e, t } from "./errors-D8rc8kzg.js";
|
|
2
|
+
import { i as n } from "./math-C82bLsED.js";
|
|
3
|
+
//#region src/outdoor.ts
|
|
4
|
+
var r = class r {
|
|
5
|
+
static {
|
|
6
|
+
this.owners = /* @__PURE__ */ new WeakSet();
|
|
7
|
+
}
|
|
8
|
+
constructor(n, i, a = {}) {
|
|
9
|
+
if (this.sound = n, this.path = i, this.initial = !0, this.disposed = !1, n.isDisposed || r.owners.has(n)) throw t("attached", "Outdoor audio needs a live, uncontrolled sound; dispose its old controller or use a fresh sound.");
|
|
10
|
+
if (this.upperDelay = a.maxDelay ?? 30, !(this.upperDelay > 0 && this.upperDelay <= 180)) throw e("Outdoor maxDelay must be above 0 and at most 180 seconds; pass a sufficient delay.", "state");
|
|
11
|
+
if (this.smoothing = a.smoothing ?? n.listener.smoothing, !(this.smoothing >= 0 && Number.isFinite(this.smoothing))) throw e("Outdoor smoothing must be finite and nonnegative; pass seconds or use the listener value.", "state");
|
|
12
|
+
this.previousRolloff = n.rolloffFactor;
|
|
13
|
+
let o = n.context;
|
|
14
|
+
this.delay = o.createDelay(this.upperDelay), this.level = o.createGain(), this.bass = o.createBiquadFilter(), this.treble = o.createBiquadFilter(), this.bass.type = "lowshelf", this.treble.type = "highshelf", this.bass.frequency.value = Math.min(250, o.sampleRate / 2), this.treble.frequency.value = Math.min(2800, o.sampleRate / 2), this.filters = [
|
|
15
|
+
this.delay,
|
|
16
|
+
this.bass,
|
|
17
|
+
this.treble,
|
|
18
|
+
this.level
|
|
19
|
+
], this.update(), n.setFilters([...n.getFilters(), ...this.filters]), n.rolloffFactor = 0, n.listener.addSource(this), r.owners.add(n);
|
|
20
|
+
}
|
|
21
|
+
update() {
|
|
22
|
+
if (this.disposed) return;
|
|
23
|
+
if (this.sound.isDisposed) {
|
|
24
|
+
this.dispose();
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
let { delaySeconds: t, gainDb: r } = this.path(), i = r[0], a = r[4], o = r[7];
|
|
28
|
+
if (!(t >= 0 && t <= this.upperDelay)) throw e("Outdoor travel time exceeds the delay line; increase maxDelay or shorten the path.", "state");
|
|
29
|
+
if (i === void 0 || a === void 0 || o === void 0 || !Number.isFinite(i) || !Number.isFinite(a) || !Number.isFinite(o)) throw e("Outdoor audio needs finite octave gains; pass a solved eight-band path.", "state");
|
|
30
|
+
let s = this.sound.context, c = this.initial ? 0 : this.smoothing;
|
|
31
|
+
n(this.delay.delayTime, t, s, c), n(this.level.gain, 10 ** (a / 20), s, c), n(this.bass.gain, i - a, s, c), n(this.treble.gain, o - a, s, c), this.initial = !1;
|
|
32
|
+
}
|
|
33
|
+
dispose() {
|
|
34
|
+
if (this.disposed) return;
|
|
35
|
+
this.disposed = !0;
|
|
36
|
+
let e = this.sound;
|
|
37
|
+
e.listener.removeSource(this), e.isDisposed || (e.setFilters(e.getFilters().filter((e) => !this.filters.includes(e))), e.rolloffFactor === 0 && (e.rolloffFactor = this.previousRolloff));
|
|
38
|
+
for (let e of this.filters) e.disconnect();
|
|
39
|
+
r.owners.delete(e);
|
|
40
|
+
}
|
|
41
|
+
[Symbol.dispose]() {
|
|
42
|
+
this.dispose();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
//#endregion
|
|
46
|
+
export { r as OutdoorAudio };
|
package/dist/pan.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Audio, type AudioOptions } from '@margoengine/audio';
|
|
2
|
+
import type { AudioListener } from '@margoengine/audio';
|
|
3
|
+
/** Non-positional stereo playback options. */
|
|
4
|
+
export interface StereoAudioOptions extends AudioOptions {
|
|
5
|
+
/** Equal-power pan from -1 (left) to 1 (right). Default 0. */
|
|
6
|
+
pan?: number;
|
|
7
|
+
}
|
|
8
|
+
/** A non-positional sound through a native StereoPannerNode, including stereo input. */
|
|
9
|
+
export declare class StereoAudio extends Audio {
|
|
10
|
+
/** Native panner; its AudioParam also supports sample-accurate automation. */
|
|
11
|
+
readonly panner: StereoPannerNode;
|
|
12
|
+
private panValue;
|
|
13
|
+
constructor(listener: AudioListener, options?: StereoAudioOptions);
|
|
14
|
+
/** Finite stereo pan -1..1, smoothed using listener.smoothing. */
|
|
15
|
+
get pan(): number;
|
|
16
|
+
set pan(value: number);
|
|
17
|
+
/** Last graph node, for analysers and wet sends. */
|
|
18
|
+
getOutput(): AudioNode;
|
|
19
|
+
/** Release the source and stereo graph; safe to call repeatedly. */
|
|
20
|
+
dispose(): void;
|
|
21
|
+
}
|
package/dist/pan.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { r as e, t } from "./errors-D8rc8kzg.js";
|
|
2
|
+
import { i as n } from "./math-C82bLsED.js";
|
|
3
|
+
import { Audio as r } from "@margoengine/audio";
|
|
4
|
+
//#region src/pan.ts
|
|
5
|
+
var i = (t) => {
|
|
6
|
+
if (!Number.isFinite(t) || t < -1 || t > 1) throw e("Pan must be finite in -1..1; choose a stereo position", "state");
|
|
7
|
+
return t;
|
|
8
|
+
}, a = (e) => (i(e.pan ?? 0), e), o = class extends r {
|
|
9
|
+
constructor(e, n = {}) {
|
|
10
|
+
if (a(n), typeof e.context.createStereoPanner != "function") throw t("unsupported", "Stereo panning needs createStereoPanner; use a supporting Web Audio context");
|
|
11
|
+
super(e, n), this.panValue = n.pan ?? 0, this.panner = this.context.createStereoPanner(), this.panner.pan.setValueAtTime(this.panValue, this.context.currentTime), this.gain.disconnect(e.input), this.gain.connect(this.panner).connect(e.input);
|
|
12
|
+
}
|
|
13
|
+
get pan() {
|
|
14
|
+
return this.panValue;
|
|
15
|
+
}
|
|
16
|
+
set pan(e) {
|
|
17
|
+
if (this.isDisposed) throw t("disposed", "StereoAudio is disposed; create a new sound");
|
|
18
|
+
this.panValue = i(e), n(this.panner.pan, e, this.context, this.listener.smoothing);
|
|
19
|
+
}
|
|
20
|
+
getOutput() {
|
|
21
|
+
return this.panner ?? this.gain;
|
|
22
|
+
}
|
|
23
|
+
dispose() {
|
|
24
|
+
super.dispose(), this.panner?.disconnect();
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
//#endregion
|
|
28
|
+
export { o as StereoAudio };
|