@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,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
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { r as e, t } from "./errors-D8rc8kzg.js";
|
|
2
|
+
import { PositionalAudio as n, coneGain as r, distanceGain as i } from "@margoengine/audio";
|
|
3
|
+
//#region src/voice-manager.ts
|
|
4
|
+
var a = /* @__PURE__ */ new WeakMap(), o = (t, n) => {
|
|
5
|
+
if (!Number.isFinite(t) || t < 0) throw e(`${n} must be finite and nonnegative; choose a valid value`, "state");
|
|
6
|
+
return t;
|
|
7
|
+
}, s = (t) => {
|
|
8
|
+
if (!Number.isFinite(t)) throw e("Voice priority must be finite; choose a numeric priority", "state");
|
|
9
|
+
return t;
|
|
10
|
+
}, c = class {
|
|
11
|
+
constructor(e, t = {}) {
|
|
12
|
+
this.listener = e, this.voices = /* @__PURE__ */ new Set(), this.disposed = !1, this.cap = this.validCap(t.maxVoices ?? 32), this.quiet = o(t.threshold ?? 1e-4, "Voice threshold"), e.addSource(this);
|
|
13
|
+
}
|
|
14
|
+
get maxVoices() {
|
|
15
|
+
return this.cap;
|
|
16
|
+
}
|
|
17
|
+
set maxVoices(e) {
|
|
18
|
+
this.assertLive(), this.cap = this.validCap(e), this.update();
|
|
19
|
+
}
|
|
20
|
+
get threshold() {
|
|
21
|
+
return this.quiet;
|
|
22
|
+
}
|
|
23
|
+
set threshold(e) {
|
|
24
|
+
this.assertLive(), this.quiet = o(e, "Voice threshold"), this.update();
|
|
25
|
+
}
|
|
26
|
+
get activeCount() {
|
|
27
|
+
return [...this.voices].filter((e) => e.isPlaying && !e.isVirtual).length;
|
|
28
|
+
}
|
|
29
|
+
get virtualCount() {
|
|
30
|
+
return [...this.voices].filter((e) => e.isVirtual).length;
|
|
31
|
+
}
|
|
32
|
+
add(e, n = {}) {
|
|
33
|
+
if (this.assertLive(), a.has(e)) throw t("attached", "Sound already has a voice manager; dispose its handle before registering again", "state");
|
|
34
|
+
if (e.isDisposed || e.listener !== this.listener || e.sourceType !== "buffer" || !e.buffer || !Number.isFinite(e.buffer.duration) || e.buffer.duration <= 0) throw t("unsupported", "Voice management needs a live decoded-buffer sound on the same listener; set its buffer before registering");
|
|
35
|
+
if (s(n.priority ?? 0), o(e.playbackRate * 2 ** (e.detune / 1200), "Effective playback rate"), n.gain !== void 0 && typeof n.gain != "function") throw t("state", "Voice gain must be a function; return an additional linear gain");
|
|
36
|
+
let r = new l(this, e, n);
|
|
37
|
+
a.set(e, r), this.voices.add(r);
|
|
38
|
+
try {
|
|
39
|
+
this.update();
|
|
40
|
+
} catch (t) {
|
|
41
|
+
throw this.voices.delete(r), a.get(e) === r && a.delete(e), t;
|
|
42
|
+
}
|
|
43
|
+
return r;
|
|
44
|
+
}
|
|
45
|
+
update() {
|
|
46
|
+
if (this.disposed) return;
|
|
47
|
+
for (let e of this.voices) e.sound.isDisposed || e.sound.update(this.listener.attachedEngine), e.advance();
|
|
48
|
+
let e = [...this.voices].filter((e) => e.isPlaying).map((e) => ({
|
|
49
|
+
voice: e,
|
|
50
|
+
gain: e.audibility()
|
|
51
|
+
}));
|
|
52
|
+
e.sort((e, t) => t.voice.priority - e.voice.priority || t.gain - e.gain);
|
|
53
|
+
let t = new Set(e.filter((e) => e.gain > this.quiet).slice(0, this.cap).map((e) => e.voice));
|
|
54
|
+
for (let e of this.voices) t.has(e) || e.physical(!1);
|
|
55
|
+
for (let e of t) e.physical(!0);
|
|
56
|
+
}
|
|
57
|
+
remove(e) {
|
|
58
|
+
this.voices.delete(e) && (a.get(e.sound) === e && a.delete(e.sound), this.update());
|
|
59
|
+
}
|
|
60
|
+
dispose() {
|
|
61
|
+
if (!this.disposed) {
|
|
62
|
+
this.disposed = !0, this.listener.removeSource(this);
|
|
63
|
+
for (let e of [...this.voices]) e.dispose();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
validCap(t) {
|
|
67
|
+
if (!Number.isSafeInteger(t) || t < 0) throw e("Voice cap must be a nonnegative safe integer; choose a whole number", "state");
|
|
68
|
+
return t;
|
|
69
|
+
}
|
|
70
|
+
assertLive() {
|
|
71
|
+
if (this.disposed) throw t("disposed", "Voice manager is disposed; create a new manager");
|
|
72
|
+
}
|
|
73
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
74
|
+
this.dispose();
|
|
75
|
+
}
|
|
76
|
+
}, l = class {
|
|
77
|
+
constructor(e, t, n) {
|
|
78
|
+
this.manager = e, this.sound = t, this.allocated = !1, this.pending = !0, this.rank = n.priority ?? 0, this.extraGain = n.gain ?? (() => 1), this.phase = t.currentTime, this.clock = t.context.currentTime, this.rate = t.playbackRate, this.cents = t.detune, this.status = t.isPlaying ? "playing" : "stopped";
|
|
79
|
+
}
|
|
80
|
+
get priority() {
|
|
81
|
+
return this.rank;
|
|
82
|
+
}
|
|
83
|
+
set priority(e) {
|
|
84
|
+
this.assertLive(), this.rank = s(e), this.manager.update();
|
|
85
|
+
}
|
|
86
|
+
get isPlaying() {
|
|
87
|
+
return this.status === "playing";
|
|
88
|
+
}
|
|
89
|
+
get isVirtual() {
|
|
90
|
+
return this.isPlaying && !this.allocated;
|
|
91
|
+
}
|
|
92
|
+
get currentTime() {
|
|
93
|
+
return this.advance(), this.phase;
|
|
94
|
+
}
|
|
95
|
+
get playbackRate() {
|
|
96
|
+
return this.rate;
|
|
97
|
+
}
|
|
98
|
+
set playbackRate(e) {
|
|
99
|
+
this.pitch(e, this.cents);
|
|
100
|
+
}
|
|
101
|
+
get detune() {
|
|
102
|
+
return this.cents;
|
|
103
|
+
}
|
|
104
|
+
set detune(e) {
|
|
105
|
+
this.pitch(this.rate, e);
|
|
106
|
+
}
|
|
107
|
+
play() {
|
|
108
|
+
return this.assertLive(), this.isPlaying || (this.status === "ended" && (this.phase = this.sound.offset), this.clock = this.sound.context.currentTime, this.status = "playing"), this.manager.update(), this;
|
|
109
|
+
}
|
|
110
|
+
pause() {
|
|
111
|
+
return this.assertLive(), this.advance(), this.status = "paused", this.physical(!1), this.manager.update(), this;
|
|
112
|
+
}
|
|
113
|
+
stop() {
|
|
114
|
+
return this.assertLive(), this.status = "stopped", this.physical(!1), this.phase = this.sound.offset, this.manager.update(), this;
|
|
115
|
+
}
|
|
116
|
+
seek(e) {
|
|
117
|
+
return this.assertLive(), o(e, "Voice seek"), this.advance(), this.physical(!1), this.phase = this.wrap(e), this.status === "ended" && (this.status = "stopped"), this.clock = this.sound.context.currentTime, this.manager.update(), this;
|
|
118
|
+
}
|
|
119
|
+
advance() {
|
|
120
|
+
if (this.sound.isDisposed) {
|
|
121
|
+
this.status = "disposed", this.allocated = !1;
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
let e = this.sound.context.currentTime;
|
|
125
|
+
if (this.isPlaying && (this.phase = this.wrap(this.phase + Math.max(0, e - this.clock) * this.rate * 2 ** (this.cents / 1200)), !this.sound.loop && this.phase >= this.end)) {
|
|
126
|
+
let e = !this.allocated || this.sound.isPlaying;
|
|
127
|
+
this.status = "ended", this.physical(!1), e && this.sound.onEnded?.(this.sound);
|
|
128
|
+
}
|
|
129
|
+
this.clock = e;
|
|
130
|
+
}
|
|
131
|
+
audibility() {
|
|
132
|
+
let e = this.sound, t = this.manager.listener, a = e.gain.gain.value * e.fade.gain.value * t.volume * o(this.extraGain(), "Additional voice gain");
|
|
133
|
+
if (e instanceof n) {
|
|
134
|
+
let n = e.panner, o = e.worldPosition, s = Math.hypot(...t.position.map((e, t) => e - o[t]));
|
|
135
|
+
a *= i(n.distanceModel, s, n.refDistance, n.maxDistance, n.rolloffFactor), a *= r([
|
|
136
|
+
o[0],
|
|
137
|
+
o[1],
|
|
138
|
+
o[2]
|
|
139
|
+
], [
|
|
140
|
+
e.worldDirection[0],
|
|
141
|
+
e.worldDirection[1],
|
|
142
|
+
e.worldDirection[2]
|
|
143
|
+
], [
|
|
144
|
+
t.position[0],
|
|
145
|
+
t.position[1],
|
|
146
|
+
t.position[2]
|
|
147
|
+
], n.coneInnerAngle, n.coneOuterAngle, n.coneOuterGain);
|
|
148
|
+
}
|
|
149
|
+
return o(a, "Estimated voice gain");
|
|
150
|
+
}
|
|
151
|
+
physical(e) {
|
|
152
|
+
this.sound.isDisposed ? this.allocated = !1 : (this.pending && (this.pending = !1, this.sound.pause()), e && !this.allocated ? (this.sound.seek(this.phase).play(), this.allocated = !0) : !e && this.allocated && (this.sound.pause(), this.allocated = !1));
|
|
153
|
+
}
|
|
154
|
+
dispose() {
|
|
155
|
+
this.status === "disposed" || (this.status = "disposed", this.physical(!1)), this.manager.remove(this);
|
|
156
|
+
}
|
|
157
|
+
get end() {
|
|
158
|
+
return Math.min(this.sound.buffer.duration, this.sound.offset + (this.sound.duration ?? Infinity));
|
|
159
|
+
}
|
|
160
|
+
wrap(e) {
|
|
161
|
+
let t = this.sound, n = t.buffer.duration;
|
|
162
|
+
if (!t.loop) return Math.min(e, this.end);
|
|
163
|
+
let r = t.loopStart, i = t.loopEnd > 0 ? Math.min(t.loopEnd, n) : n;
|
|
164
|
+
return i > r ? e < i ? e : r + (e - r) % (i - r) : e % n;
|
|
165
|
+
}
|
|
166
|
+
pitch(t, n) {
|
|
167
|
+
if (this.assertLive(), o(t, "Voice playback rate"), o(t * 2 ** (n / 1200), "Effective voice playback rate"), !Number.isFinite(n)) throw e("Voice detune must be finite; use cents", "state");
|
|
168
|
+
this.advance(), this.physical(!1), this.rate = t, this.cents = n, this.sound.playbackRate = t, this.sound.detune = n, this.manager.update();
|
|
169
|
+
}
|
|
170
|
+
assertLive() {
|
|
171
|
+
if (this.status === "disposed" || this.sound.isDisposed) throw t("disposed", "Managed voice is disposed; register a live sound");
|
|
172
|
+
}
|
|
173
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
174
|
+
this.dispose();
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
//#endregion
|
|
178
|
+
export { l as AudioManagedVoice, c as AudioVoiceManager };
|