@schmooky/zvuk 0.2.0 → 1.0.1
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/README.md +9 -7
- package/dist/index.d.ts +53 -18
- package/dist/index.js +102 -36
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,11 +37,13 @@ await engine.unlock(); // call from a user gest
|
|
|
37
37
|
await engine.loadSound('coin', '/sfx/coin.webm', { bus: 'sfx' });
|
|
38
38
|
|
|
39
39
|
const v = engine.sound('coin').play({ volume: { jitter: 0.05 } });
|
|
40
|
-
await v.fade({ to: 0,
|
|
40
|
+
await v.fade({ to: 0, duration: 0.8 });
|
|
41
41
|
|
|
42
|
-
engine.bus('music').fadeTo(0.1,
|
|
42
|
+
engine.bus('music').fadeTo(0.1, 0.8);
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
+
> All time-valued options in zvuk are **seconds** (matching the Web Audio API).
|
|
46
|
+
|
|
45
47
|
## Why zvuk?
|
|
46
48
|
|
|
47
49
|
`HTMLAudioElement` doesn't scale past one menu sound. The Web Audio API does, but you end up writing your own bus graph, your own scheduler, your own iOS unlock dance, your own codec ladder, your own sidechain envelope, your own snapshot crossfader — every time.
|
|
@@ -86,7 +88,7 @@ await engine.loadSound('intro', '/music/intro.webm', { bus: 'music' });
|
|
|
86
88
|
await engine.loadSound('main', '/music/main.webm', { bus: 'music' });
|
|
87
89
|
|
|
88
90
|
engine.sound('intro').play({ loop: true });
|
|
89
|
-
engine.crossfade('intro', 'main', {
|
|
91
|
+
engine.crossfade('intro', 'main', { duration: 1.5 });
|
|
90
92
|
```
|
|
91
93
|
|
|
92
94
|
### Sidechain ducking — music breathes under VO
|
|
@@ -97,7 +99,7 @@ import { Ducker } from '@schmooky/zvuk';
|
|
|
97
99
|
const ducker = new Ducker(engine.context, {
|
|
98
100
|
source: engine.bus('voice').output,
|
|
99
101
|
target: engine.bus('music'),
|
|
100
|
-
amount: 0.7, attack:
|
|
102
|
+
amount: 0.7, attack: 0.08, release: 0.6,
|
|
101
103
|
});
|
|
102
104
|
engine.bus('music').addFx(ducker);
|
|
103
105
|
```
|
|
@@ -121,10 +123,10 @@ requestAnimationFrame(function tick() {
|
|
|
121
123
|
```ts
|
|
122
124
|
const calm = engine.captureSnapshot('calm');
|
|
123
125
|
|
|
124
|
-
engine.bus('music').fadeTo(0.2,
|
|
125
|
-
engine.bus('voice').fadeTo(1.5,
|
|
126
|
+
engine.bus('music').fadeTo(0.2, 0.2);
|
|
127
|
+
engine.bus('voice').fadeTo(1.5, 0.2);
|
|
126
128
|
|
|
127
|
-
await calm.apply({
|
|
129
|
+
await calm.apply({ fade: 0.6 }); // restore everything in one call
|
|
128
130
|
```
|
|
129
131
|
|
|
130
132
|
## Browser support
|
package/dist/index.d.ts
CHANGED
|
@@ -16,6 +16,19 @@ declare class BankNotLoadedError extends ZvukError {
|
|
|
16
16
|
declare class DecodeError extends ZvukError {
|
|
17
17
|
constructor(url: string, cause: unknown);
|
|
18
18
|
}
|
|
19
|
+
interface DecodeAttempt {
|
|
20
|
+
readonly url: string;
|
|
21
|
+
readonly cause: unknown;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Thrown when every URL in a fallback list fails to load. Subclass of
|
|
25
|
+
* DecodeError so existing `catch (e instanceof DecodeError)` paths still
|
|
26
|
+
* fire — `attempts` exposes the per-URL causes for diagnostics.
|
|
27
|
+
*/
|
|
28
|
+
declare class AggregateDecodeError extends DecodeError {
|
|
29
|
+
readonly attempts: readonly DecodeAttempt[];
|
|
30
|
+
constructor(urls: readonly string[], attempts: readonly DecodeAttempt[]);
|
|
31
|
+
}
|
|
19
32
|
|
|
20
33
|
/**
|
|
21
34
|
* Common FX insert contract. Every FX exposes a single input + a single
|
|
@@ -78,9 +91,9 @@ interface SidechainConfig {
|
|
|
78
91
|
from: string;
|
|
79
92
|
/** Amount of duck (0..1, where 1 = fully muted at peak source). */
|
|
80
93
|
amount: number;
|
|
81
|
-
/** Attack
|
|
94
|
+
/** Attack in seconds. */
|
|
82
95
|
attack?: number;
|
|
83
|
-
/** Release
|
|
96
|
+
/** Release in seconds. */
|
|
84
97
|
release?: number;
|
|
85
98
|
}
|
|
86
99
|
interface BusConfig {
|
|
@@ -111,7 +124,8 @@ interface EngineConfig {
|
|
|
111
124
|
}
|
|
112
125
|
interface FadeOptions {
|
|
113
126
|
to: number;
|
|
114
|
-
|
|
127
|
+
/** Fade duration in seconds. */
|
|
128
|
+
duration: number;
|
|
115
129
|
curve?: FadeCurve;
|
|
116
130
|
}
|
|
117
131
|
interface VoiceJitter {
|
|
@@ -247,7 +261,7 @@ declare class Voice {
|
|
|
247
261
|
* Setting while paused only updates the value used on the next start.
|
|
248
262
|
*/
|
|
249
263
|
setPlaybackRate(rate: number, opts?: {
|
|
250
|
-
|
|
264
|
+
duration?: number;
|
|
251
265
|
curve?: FadeOptions['curve'];
|
|
252
266
|
}): void;
|
|
253
267
|
get playbackRate(): number;
|
|
@@ -270,7 +284,8 @@ declare class Voice {
|
|
|
270
284
|
* Voices are connected to bus.input. The master receives bus.output.
|
|
271
285
|
*
|
|
272
286
|
* level/mute use 10ms ramps to avoid clicks; raw gain.value writes would
|
|
273
|
-
* pop audibly on browsers that don't smooth the parameter.
|
|
287
|
+
* pop audibly on browsers that don't smooth the parameter. Public time
|
|
288
|
+
* arguments (fadeTo) are seconds.
|
|
274
289
|
*/
|
|
275
290
|
declare class Bus {
|
|
276
291
|
readonly name: string;
|
|
@@ -291,7 +306,8 @@ declare class Bus {
|
|
|
291
306
|
set level(v: number);
|
|
292
307
|
get muted(): boolean;
|
|
293
308
|
set muted(v: boolean);
|
|
294
|
-
|
|
309
|
+
/** Fade the bus output to `target` over `duration` seconds. */
|
|
310
|
+
fadeTo(target: number, duration: number, curve?: FadeCurve): Promise<void>;
|
|
295
311
|
/** Active voice count on this bus. */
|
|
296
312
|
get voiceCount(): number;
|
|
297
313
|
/** Read-only iterator over active voices. */
|
|
@@ -323,9 +339,9 @@ declare class Bus {
|
|
|
323
339
|
interface DuckerConfig {
|
|
324
340
|
/** How much to attenuate when fully ducking (0..1). Default 0.5 = -6 dB. */
|
|
325
341
|
amount?: number;
|
|
326
|
-
/** Attack
|
|
342
|
+
/** Attack in seconds. Default 0.08. */
|
|
327
343
|
attack?: number;
|
|
328
|
-
/** Release
|
|
344
|
+
/** Release in seconds. Default 0.4. */
|
|
329
345
|
release?: number;
|
|
330
346
|
/** Threshold (linear amplitude) on the source bus's RMS to trigger ducking. Default 0.05. */
|
|
331
347
|
threshold?: number;
|
|
@@ -623,12 +639,12 @@ interface SnapshotState {
|
|
|
623
639
|
parameters: Record<string, number>;
|
|
624
640
|
}
|
|
625
641
|
interface ApplyOptions {
|
|
626
|
-
/** Crossfade duration in
|
|
627
|
-
|
|
642
|
+
/** Crossfade duration in seconds. Default 0 (snap). */
|
|
643
|
+
fade?: number;
|
|
628
644
|
}
|
|
629
645
|
/**
|
|
630
646
|
* A captured mix-state preset. Capture it once with the engine in a known
|
|
631
|
-
* good state ("menu mood"), then `apply({
|
|
647
|
+
* good state ("menu mood"), then `apply({ fade: 0.25 })` to crossfade the
|
|
632
648
|
* entire mix back to that snapshot — bus levels, mutes, parameter values,
|
|
633
649
|
* everything in one call.
|
|
634
650
|
*
|
|
@@ -655,8 +671,15 @@ interface Engine {
|
|
|
655
671
|
* Load and decode an audio asset.
|
|
656
672
|
*
|
|
657
673
|
* Pass an array of URLs to ship a codec ladder — e.g. ['coin.webm', 'coin.m4a'].
|
|
658
|
-
* The
|
|
659
|
-
*
|
|
674
|
+
* The list is walked in order (codecs the browser claims it can play
|
|
675
|
+
* float to the front), and the first URL that successfully fetches AND
|
|
676
|
+
* decodes wins. A 404, network error, or decode failure on one URL falls
|
|
677
|
+
* through to the next — so a broken CDN entry or under-reported codec
|
|
678
|
+
* doesn't bring the whole sound down.
|
|
679
|
+
*
|
|
680
|
+
* If every URL fails, throws AggregateDecodeError (a subclass of
|
|
681
|
+
* DecodeError) with per-URL causes attached. If only one URL was given,
|
|
682
|
+
* the underlying DecodeError is rethrown verbatim.
|
|
660
683
|
*/
|
|
661
684
|
loadSound(name: string, url: string | readonly string[], options?: LoadSoundOptions): Promise<Sound>;
|
|
662
685
|
/** Register a Sound from an AudioBuffer you constructed yourself (procedural, custom-decoded, time-stretched). */
|
|
@@ -687,8 +710,9 @@ interface Engine {
|
|
|
687
710
|
sound(name: string): Sound;
|
|
688
711
|
bus(name: string): Bus;
|
|
689
712
|
/**
|
|
690
|
-
* Crossfade helper. Stops `from` over `
|
|
691
|
-
* window with `equal-power` curves so the perceived loudness
|
|
713
|
+
* Crossfade helper. Stops `from` over `duration` seconds while playing `to`
|
|
714
|
+
* over the same window with `equal-power` curves so the perceived loudness
|
|
715
|
+
* stays flat.
|
|
692
716
|
*
|
|
693
717
|
* Returns the new Voice. If `to` is already playing, its level is faded up
|
|
694
718
|
* from 0 instead of starting fresh.
|
|
@@ -706,8 +730,8 @@ interface Engine {
|
|
|
706
730
|
snapshot(name: string, state: SnapshotState): Snapshot;
|
|
707
731
|
}
|
|
708
732
|
interface CrossfadeOptions {
|
|
709
|
-
/** Crossfade duration in
|
|
710
|
-
|
|
733
|
+
/** Crossfade duration in seconds. Default 1.5. */
|
|
734
|
+
duration?: number;
|
|
711
735
|
/** Bus to play `to` on (and to look up `from` on). Defaults to the sound's defaults. */
|
|
712
736
|
bus?: string;
|
|
713
737
|
/** Loop the new voice. Default true (music swap is the canonical use case). */
|
|
@@ -777,6 +801,17 @@ declare function mimeForUrl(url: string): AudioMimeType | null;
|
|
|
777
801
|
* during SSR — return the first URL and let decodeAudioData decide.
|
|
778
802
|
*/
|
|
779
803
|
declare function pickSource(urls: readonly string[]): string;
|
|
804
|
+
/**
|
|
805
|
+
* Return the URL list reordered for fallback loading: codecs the browser
|
|
806
|
+
* claims it can play first (in user-given order), unknowns/unsupported last.
|
|
807
|
+
*
|
|
808
|
+
* canPlayType is a hint, not a hard filter — some browsers under-report
|
|
809
|
+
* support, and decodeAudioData accepts a few extras. So we keep all URLs in
|
|
810
|
+
* the result; ordering merely biases the first attempts toward what's most
|
|
811
|
+
* likely to succeed. Pair with Decoder.loadFirst() to walk the list and fall
|
|
812
|
+
* through on per-URL fetch/decode failures.
|
|
813
|
+
*/
|
|
814
|
+
declare function pickSourceOrder(urls: readonly string[]): readonly string[];
|
|
780
815
|
|
|
781
816
|
/**
|
|
782
817
|
* RMS-based loudness normalization on a decoded AudioBuffer.
|
|
@@ -796,4 +831,4 @@ type NormalizeFlag = boolean | LoudnessOptions;
|
|
|
796
831
|
declare function applyLoudnessNormalization(buffer: AudioBuffer, flag: NormalizeFlag): AudioBuffer;
|
|
797
832
|
declare function computeNormalizationGain(buffer: AudioBuffer, opts: Required<LoudnessOptions>): number;
|
|
798
833
|
|
|
799
|
-
export { type ApplyOptions, type AudioMimeType, BankNotLoadedError, Bus, type BusConfig, BusNotFoundError, Compressor, type CompressorConfig, type ConcurrencyConfig, type CrossfadeOptions, DecodeError, Ducker, type DuckerConfig, type Engine, EngineClosedError, type EngineConfig, type EngineState, type FadeCurve, type FadeOptions, Filter, type FilterConfig, type FilterKind, type FxInsert, type LoadSoundOptions, type LoudnessOptions, Master, type MasterConfig, type MasterLimiterConfig, type NormalizeFlag, Parameter, type ParameterCurve, type PlayOptions, Reverb, type ReverbConfig, type SidechainConfig, Snapshot, type SnapshotState, Sound, SoundNotFoundError, type SpatialOptions, Spatializer, Sprite, type SpriteMap, type SpriteRegion, type SpriteRegionPlayOptions, StreamSound, StretchProcessor, type StretchWorkletNode, type StretchWorkletOptions, Voice, type VoiceJitter, ZvukError, applyLoudnessNormalization, canPlay, computeNormalizationGain, createEngine, createStretchWorkletNode, ensureStretchWorklet, mimeForUrl, pickSource };
|
|
834
|
+
export { AggregateDecodeError, type ApplyOptions, type AudioMimeType, BankNotLoadedError, Bus, type BusConfig, BusNotFoundError, Compressor, type CompressorConfig, type ConcurrencyConfig, type CrossfadeOptions, type DecodeAttempt, DecodeError, Ducker, type DuckerConfig, type Engine, EngineClosedError, type EngineConfig, type EngineState, type FadeCurve, type FadeOptions, Filter, type FilterConfig, type FilterKind, type FxInsert, type LoadSoundOptions, type LoudnessOptions, Master, type MasterConfig, type MasterLimiterConfig, type NormalizeFlag, Parameter, type ParameterCurve, type PlayOptions, Reverb, type ReverbConfig, type SidechainConfig, Snapshot, type SnapshotState, Sound, SoundNotFoundError, type SpatialOptions, Spatializer, Sprite, type SpriteMap, type SpriteRegion, type SpriteRegionPlayOptions, StreamSound, StretchProcessor, type StretchWorkletNode, type StretchWorkletOptions, Voice, type VoiceJitter, ZvukError, applyLoudnessNormalization, canPlay, computeNormalizationGain, createEngine, createStretchWorkletNode, ensureStretchWorklet, mimeForUrl, pickSource, pickSourceOrder };
|
package/dist/index.js
CHANGED
|
@@ -35,6 +35,18 @@ var DecodeError = class extends ZvukError {
|
|
|
35
35
|
this.name = "DecodeError";
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
|
+
var AggregateDecodeError = class extends DecodeError {
|
|
39
|
+
attempts;
|
|
40
|
+
constructor(urls, attempts) {
|
|
41
|
+
const last = attempts[attempts.length - 1];
|
|
42
|
+
super(urls[urls.length - 1] ?? "<empty>", last?.cause);
|
|
43
|
+
this.name = "AggregateDecodeError";
|
|
44
|
+
this.attempts = attempts;
|
|
45
|
+
const summary = attempts.map((a) => ` - ${a.url}: ${a.cause instanceof Error ? a.cause.message : String(a.cause)}`).join("\n");
|
|
46
|
+
this.message = `Failed to load any of ${urls.length} fallback URLs:
|
|
47
|
+
${summary}`;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
38
50
|
|
|
39
51
|
// src/fx/compressor.ts
|
|
40
52
|
var Compressor = class {
|
|
@@ -121,8 +133,8 @@ var Ducker = class {
|
|
|
121
133
|
this.sourceBus = sourceBus;
|
|
122
134
|
this.cfg = {
|
|
123
135
|
amount: config.amount ?? 0.5,
|
|
124
|
-
attack: config.attack ??
|
|
125
|
-
release: config.release ??
|
|
136
|
+
attack: config.attack ?? 0.08,
|
|
137
|
+
release: config.release ?? 0.4,
|
|
126
138
|
threshold: config.threshold ?? 0.05
|
|
127
139
|
};
|
|
128
140
|
this.input = ctx.createGain();
|
|
@@ -178,8 +190,8 @@ var Ducker = class {
|
|
|
178
190
|
const exceeded = Math.max(0, rms - this.cfg.threshold);
|
|
179
191
|
const target = exceeded > 0 ? 1 - this.cfg.amount : 1;
|
|
180
192
|
const useAttack = target < this.envelope;
|
|
181
|
-
const tau = (useAttack ? this.cfg.attack : this.cfg.release) ||
|
|
182
|
-
const alpha = 1 - Math.exp(-
|
|
193
|
+
const tau = (useAttack ? this.cfg.attack : this.cfg.release) || 1e-3;
|
|
194
|
+
const alpha = 1 - Math.exp(-0.016666666666666666 / tau);
|
|
183
195
|
this.envelope = this.envelope + alpha * (target - this.envelope);
|
|
184
196
|
if (Math.abs(this.envelope - this.targetGain) > 1e-3) {
|
|
185
197
|
this.targetGain = this.envelope;
|
|
@@ -541,21 +553,21 @@ function createStretchWorkletNode(ctx, options = {}) {
|
|
|
541
553
|
}
|
|
542
554
|
|
|
543
555
|
// src/mixer/curve.ts
|
|
544
|
-
function applyRamp(param, now, to,
|
|
545
|
-
const
|
|
556
|
+
function applyRamp(param, now, to, duration, curve) {
|
|
557
|
+
const seconds = Math.max(0, duration);
|
|
546
558
|
const from = param.value;
|
|
547
559
|
param.cancelScheduledValues(now);
|
|
548
560
|
param.setValueAtTime(from, now);
|
|
549
|
-
if (
|
|
561
|
+
if (seconds === 0) {
|
|
550
562
|
param.setValueAtTime(to, now);
|
|
551
563
|
return;
|
|
552
564
|
}
|
|
553
565
|
if (curve === "linear") {
|
|
554
|
-
param.linearRampToValueAtTime(to, now +
|
|
566
|
+
param.linearRampToValueAtTime(to, now + seconds);
|
|
555
567
|
return;
|
|
556
568
|
}
|
|
557
569
|
const samples = sampleCurve(from, to, curve, 64);
|
|
558
|
-
param.setValueCurveAtTime(samples, now,
|
|
570
|
+
param.setValueCurveAtTime(samples, now, seconds);
|
|
559
571
|
}
|
|
560
572
|
function sampleCurve(from, to, curve, n) {
|
|
561
573
|
const out = new Float32Array(n);
|
|
@@ -616,20 +628,21 @@ var Bus = class {
|
|
|
616
628
|
set level(v) {
|
|
617
629
|
this._level = clamp01(v);
|
|
618
630
|
this._baseLevel = this._level;
|
|
619
|
-
if (!this._muted) this.rampOutput(this._level,
|
|
631
|
+
if (!this._muted) this.rampOutput(this._level, 0.01);
|
|
620
632
|
}
|
|
621
633
|
get muted() {
|
|
622
634
|
return this._muted;
|
|
623
635
|
}
|
|
624
636
|
set muted(v) {
|
|
625
637
|
this._muted = v;
|
|
626
|
-
this.rampOutput(v ? 0 : this._level,
|
|
638
|
+
this.rampOutput(v ? 0 : this._level, 0.01);
|
|
627
639
|
}
|
|
628
|
-
|
|
640
|
+
/** Fade the bus output to `target` over `duration` seconds. */
|
|
641
|
+
fadeTo(target, duration, curve = "linear") {
|
|
629
642
|
const to = clamp01(target);
|
|
630
643
|
this._level = to;
|
|
631
644
|
this._baseLevel = to;
|
|
632
|
-
return this.ramp(to,
|
|
645
|
+
return this.ramp(to, duration, curve);
|
|
633
646
|
}
|
|
634
647
|
/** Active voice count on this bus. */
|
|
635
648
|
get voiceCount() {
|
|
@@ -704,14 +717,14 @@ var Bus = class {
|
|
|
704
717
|
}
|
|
705
718
|
head.connect(this.output);
|
|
706
719
|
}
|
|
707
|
-
rampOutput(to,
|
|
708
|
-
void this.ramp(to,
|
|
720
|
+
rampOutput(to, duration) {
|
|
721
|
+
void this.ramp(to, duration, "linear");
|
|
709
722
|
}
|
|
710
|
-
ramp(to,
|
|
723
|
+
ramp(to, duration, curve) {
|
|
711
724
|
const param = this.output.gain;
|
|
712
725
|
const now = this.ctx.currentTime;
|
|
713
|
-
applyRamp(param, now, to,
|
|
714
|
-
return wait(
|
|
726
|
+
applyRamp(param, now, to, duration, curve);
|
|
727
|
+
return wait(duration);
|
|
715
728
|
}
|
|
716
729
|
dispose() {
|
|
717
730
|
try {
|
|
@@ -726,9 +739,9 @@ function clamp01(v) {
|
|
|
726
739
|
if (Number.isNaN(v)) return 0;
|
|
727
740
|
return Math.min(1, Math.max(0, v));
|
|
728
741
|
}
|
|
729
|
-
function wait(
|
|
730
|
-
if (
|
|
731
|
-
return new Promise((res) => setTimeout(res,
|
|
742
|
+
function wait(seconds) {
|
|
743
|
+
if (seconds <= 0) return Promise.resolve();
|
|
744
|
+
return new Promise((res) => setTimeout(res, seconds * 1e3));
|
|
732
745
|
}
|
|
733
746
|
function pickVictim(voices, strategy, newVoice) {
|
|
734
747
|
let victim = null;
|
|
@@ -845,6 +858,17 @@ function pickSource(urls) {
|
|
|
845
858
|
}
|
|
846
859
|
return urls[0];
|
|
847
860
|
}
|
|
861
|
+
function pickSourceOrder(urls) {
|
|
862
|
+
if (urls.length === 0) return urls;
|
|
863
|
+
const playable = [];
|
|
864
|
+
const unknown = [];
|
|
865
|
+
for (const url of urls) {
|
|
866
|
+
const mime = mimeForUrl(url);
|
|
867
|
+
if (mime && canPlay(mime)) playable.push(url);
|
|
868
|
+
else unknown.push(url);
|
|
869
|
+
}
|
|
870
|
+
return [...playable, ...unknown];
|
|
871
|
+
}
|
|
848
872
|
|
|
849
873
|
// src/runtime/context.ts
|
|
850
874
|
var AudioContextHost = class {
|
|
@@ -1000,6 +1024,48 @@ var Decoder = class {
|
|
|
1000
1024
|
this.touch(url, buffer);
|
|
1001
1025
|
return buffer;
|
|
1002
1026
|
}
|
|
1027
|
+
/**
|
|
1028
|
+
* Walk a list of URLs, returning the first successful load. Falls through
|
|
1029
|
+
* on per-URL fetch failures (404, 5xx, network) AND decode failures
|
|
1030
|
+
* (decodeAudioData rejections), so a single broken codec or stale CDN
|
|
1031
|
+
* entry doesn't bring the whole sound down.
|
|
1032
|
+
*
|
|
1033
|
+
* Cache fast-path: if any URL in the list is already cached, that buffer
|
|
1034
|
+
* is returned immediately without any fetch — useful when a previous load
|
|
1035
|
+
* already resolved a fallback for the same asset.
|
|
1036
|
+
*
|
|
1037
|
+
* AbortError (from opts.signal) is fatal and propagates verbatim — once
|
|
1038
|
+
* the caller pulled the plug, we don't keep trying.
|
|
1039
|
+
*
|
|
1040
|
+
* On total failure: if a single URL was given, the underlying DecodeError
|
|
1041
|
+
* is rethrown verbatim. If multiple URLs were given, an
|
|
1042
|
+
* AggregateDecodeError is thrown with per-URL causes attached.
|
|
1043
|
+
*/
|
|
1044
|
+
async loadFirst(urls, opts = {}) {
|
|
1045
|
+
if (urls.length === 0) throw new Error("loadFirst requires at least one URL");
|
|
1046
|
+
for (const url of urls) {
|
|
1047
|
+
const cached = this.cache.get(url);
|
|
1048
|
+
if (cached) {
|
|
1049
|
+
this.touch(url, cached);
|
|
1050
|
+
return cached;
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
const attempts = [];
|
|
1054
|
+
for (const url of urls) {
|
|
1055
|
+
try {
|
|
1056
|
+
return await this.load(url, opts);
|
|
1057
|
+
} catch (e) {
|
|
1058
|
+
if (e.name === "AbortError") throw e;
|
|
1059
|
+
attempts.push({ url, cause: e });
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
if (urls.length === 1) {
|
|
1063
|
+
const cause = attempts[0]?.cause;
|
|
1064
|
+
if (cause instanceof Error) throw cause;
|
|
1065
|
+
throw new DecodeError(urls[0], cause);
|
|
1066
|
+
}
|
|
1067
|
+
throw new AggregateDecodeError(urls, attempts);
|
|
1068
|
+
}
|
|
1003
1069
|
evict(url) {
|
|
1004
1070
|
this.cache.delete(url);
|
|
1005
1071
|
}
|
|
@@ -1198,8 +1264,8 @@ var Voice = class _Voice {
|
|
|
1198
1264
|
fade(opts) {
|
|
1199
1265
|
const param = this.gain.gain;
|
|
1200
1266
|
const now = this.ctx.currentTime;
|
|
1201
|
-
applyRamp(param, now, clamp012(opts.to), opts.
|
|
1202
|
-
return new Promise((res) => setTimeout(res, Math.max(0, opts.
|
|
1267
|
+
applyRamp(param, now, clamp012(opts.to), opts.duration, opts.curve ?? "linear");
|
|
1268
|
+
return new Promise((res) => setTimeout(res, Math.max(0, opts.duration) * 1e3));
|
|
1203
1269
|
}
|
|
1204
1270
|
stop() {
|
|
1205
1271
|
if (this.done) return;
|
|
@@ -1264,7 +1330,7 @@ var Voice = class _Voice {
|
|
|
1264
1330
|
setPlaybackRate(rate, opts = {}) {
|
|
1265
1331
|
if (this.done) return;
|
|
1266
1332
|
const r = Math.max(0, rate);
|
|
1267
|
-
const
|
|
1333
|
+
const duration = Math.max(0, opts.duration ?? 0);
|
|
1268
1334
|
if (!this.paused) {
|
|
1269
1335
|
this.currentOffset = this.computeOffset();
|
|
1270
1336
|
this.startCtxTime = this.ctx.currentTime;
|
|
@@ -1272,11 +1338,11 @@ var Voice = class _Voice {
|
|
|
1272
1338
|
this.basePitch = r;
|
|
1273
1339
|
if (this.paused) return;
|
|
1274
1340
|
const param = this.source.playbackRate;
|
|
1275
|
-
if (
|
|
1341
|
+
if (duration === 0) {
|
|
1276
1342
|
param.value = r;
|
|
1277
1343
|
return;
|
|
1278
1344
|
}
|
|
1279
|
-
applyRamp(param, this.ctx.currentTime, r,
|
|
1345
|
+
applyRamp(param, this.ctx.currentTime, r, duration, opts.curve ?? "linear");
|
|
1280
1346
|
}
|
|
1281
1347
|
get playbackRate() {
|
|
1282
1348
|
return this.basePitch;
|
|
@@ -1552,8 +1618,8 @@ var StreamSound = class {
|
|
|
1552
1618
|
}
|
|
1553
1619
|
fade(opts) {
|
|
1554
1620
|
if (!this.gain) return Promise.resolve();
|
|
1555
|
-
applyRamp(this.gain.gain, this.ctx.currentTime, clamp013(opts.to), opts.
|
|
1556
|
-
return new Promise((res) => setTimeout(res, Math.max(0, opts.
|
|
1621
|
+
applyRamp(this.gain.gain, this.ctx.currentTime, clamp013(opts.to), opts.duration, opts.curve ?? "linear");
|
|
1622
|
+
return new Promise((res) => setTimeout(res, Math.max(0, opts.duration) * 1e3));
|
|
1557
1623
|
}
|
|
1558
1624
|
dispose() {
|
|
1559
1625
|
this.disposed = true;
|
|
@@ -1718,13 +1784,13 @@ var Snapshot = class {
|
|
|
1718
1784
|
this.getParam = getParam;
|
|
1719
1785
|
}
|
|
1720
1786
|
apply(opts = {}) {
|
|
1721
|
-
const
|
|
1787
|
+
const fade = opts.fade ?? 0;
|
|
1722
1788
|
const tasks = [];
|
|
1723
1789
|
for (const [name, bs] of Object.entries(this.state.buses)) {
|
|
1724
1790
|
const bus = this.getBus(name);
|
|
1725
1791
|
if (!bus) continue;
|
|
1726
1792
|
bus.muted = bs.muted;
|
|
1727
|
-
tasks.push(bus.fadeTo(bs.level,
|
|
1793
|
+
tasks.push(bus.fadeTo(bs.level, fade));
|
|
1728
1794
|
}
|
|
1729
1795
|
for (const [name, value] of Object.entries(this.state.parameters)) {
|
|
1730
1796
|
const param = this.getParam(name);
|
|
@@ -1798,9 +1864,9 @@ var EngineImpl = class {
|
|
|
1798
1864
|
if (this.host.state === "closed") throw new EngineClosedError();
|
|
1799
1865
|
this.host.touch();
|
|
1800
1866
|
this.ensureGraph();
|
|
1801
|
-
const
|
|
1867
|
+
const urls = typeof url === "string" ? [url] : pickSourceOrder(url);
|
|
1802
1868
|
const decodeOpts = { signal: options.signal };
|
|
1803
|
-
let buffer = await this.decoder.
|
|
1869
|
+
let buffer = await this.decoder.loadFirst(urls, decodeOpts);
|
|
1804
1870
|
if (options.normalize) {
|
|
1805
1871
|
buffer = applyLoudnessNormalization(buffer, options.normalize);
|
|
1806
1872
|
}
|
|
@@ -1841,7 +1907,7 @@ var EngineImpl = class {
|
|
|
1841
1907
|
return this.streams.has(name);
|
|
1842
1908
|
}
|
|
1843
1909
|
crossfade(from, to, options = {}) {
|
|
1844
|
-
const
|
|
1910
|
+
const duration = options.duration ?? 1.5;
|
|
1845
1911
|
const curve = options.curve ?? "equal-power";
|
|
1846
1912
|
const toVolume = options.toVolume ?? 1;
|
|
1847
1913
|
const target = this.sound(to);
|
|
@@ -1850,10 +1916,10 @@ var EngineImpl = class {
|
|
|
1850
1916
|
loop: options.loop ?? true,
|
|
1851
1917
|
volume: 0
|
|
1852
1918
|
});
|
|
1853
|
-
void newVoice.fade({ to: toVolume,
|
|
1919
|
+
void newVoice.fade({ to: toVolume, duration, curve });
|
|
1854
1920
|
const outgoing = this.activeVoices().filter((v) => v !== newVoice && v.sourceName === from);
|
|
1855
1921
|
for (const v of outgoing) {
|
|
1856
|
-
void v.fade({ to: 0,
|
|
1922
|
+
void v.fade({ to: 0, duration, curve }).then(() => v.stop());
|
|
1857
1923
|
}
|
|
1858
1924
|
return newVoice;
|
|
1859
1925
|
}
|
|
@@ -2012,6 +2078,6 @@ function levenshtein(a, b) {
|
|
|
2012
2078
|
return prev[b.length];
|
|
2013
2079
|
}
|
|
2014
2080
|
|
|
2015
|
-
export { BankNotLoadedError, Bus, BusNotFoundError, Compressor, DecodeError, Ducker, EngineClosedError, Filter, Master, Parameter, Reverb, Snapshot, Sound, SoundNotFoundError, Spatializer, Sprite, StreamSound, StretchProcessor, Voice, ZvukError, applyLoudnessNormalization, canPlay, computeNormalizationGain, createEngine, createStretchWorkletNode, ensureStretchWorklet, mimeForUrl, pickSource };
|
|
2081
|
+
export { AggregateDecodeError, BankNotLoadedError, Bus, BusNotFoundError, Compressor, DecodeError, Ducker, EngineClosedError, Filter, Master, Parameter, Reverb, Snapshot, Sound, SoundNotFoundError, Spatializer, Sprite, StreamSound, StretchProcessor, Voice, ZvukError, applyLoudnessNormalization, canPlay, computeNormalizationGain, createEngine, createStretchWorkletNode, ensureStretchWorklet, mimeForUrl, pickSource, pickSourceOrder };
|
|
2016
2082
|
//# sourceMappingURL=index.js.map
|
|
2017
2083
|
//# sourceMappingURL=index.js.map
|