@schmooky/zvuk 1.12.0 → 1.13.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/README.md +8 -8
- package/dist/index.d.ts +54 -26
- package/dist/index.js +104 -44
- package/dist/index.js.map +1 -1
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -55,9 +55,9 @@ zvuk is the layer above. It gives you the routing primitives a real game audio t
|
|
|
55
55
|
| Mixer | FX | Loading | DX |
|
|
56
56
|
| --- | --- | --- | --- |
|
|
57
57
|
| Named buses with FX inserts | Compressor (DynamicsCompressor + makeup) | Codec ladder (`['x.webm', 'x.m4a']`) | Lazy AudioContext (no constructor side-effects) |
|
|
58
|
-
| Master headroom +
|
|
59
|
-
| Voice concurrency + stealing | Reverb (convolution + synthetic IR) | Stream long media via MediaElementSource |
|
|
60
|
-
| Sidechain ducking (Ducker) |
|
|
58
|
+
| Master headroom + soft limiter (DynamicsCompressor) | Filter (BiquadFilter, all 6 modes) | Audio sprites (one buffer, N regions) | iOS Safari unlock + visibility resume |
|
|
59
|
+
| Voice concurrency + stealing | Reverb (convolution + synthetic IR) | Stream long media via MediaElementSource | Audio-clock scheduler (`scheduleAt`) |
|
|
60
|
+
| Sidechain ducking (Ducker) | Pitch-preserving time-stretch (offline SOLA) + realtime varispeed Worklet | Loudness normalization on load | Async cues iterator on every Voice |
|
|
61
61
|
| Snapshots — capture & crossfade the mix | Spatializer (2D pan + 3D HRTF) | AbortSignal cancellation | "Did you mean…" on bus/sound name typos |
|
|
62
62
|
|
|
63
63
|
## Examples
|
|
@@ -96,9 +96,9 @@ engine.crossfade('intro', 'main', { duration: 1.5 });
|
|
|
96
96
|
```ts
|
|
97
97
|
import { Ducker } from '@schmooky/zvuk';
|
|
98
98
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
// The source bus (keyed from) is the 2nd argument; the target bus is
|
|
100
|
+
// whichever bus you add the ducker to.
|
|
101
|
+
const ducker = new Ducker(engine.context, engine.bus('voice'), {
|
|
102
102
|
amount: 0.7, attack: 0.08, release: 0.6,
|
|
103
103
|
});
|
|
104
104
|
engine.bus('music').addFx(ducker);
|
|
@@ -182,9 +182,9 @@ Releases are driven by [Changesets](https://github.com/changesets/changesets) on
|
|
|
182
182
|
1. Add a changeset describing your change: `pnpm changeset`
|
|
183
183
|
2. Open a PR; CI gates on lint + typecheck + tests + lib build + docs build.
|
|
184
184
|
3. On merge to `main`, the release workflow opens (or updates) a `Version Packages` PR that bumps the version and regenerates `CHANGELOG.md`.
|
|
185
|
-
4. Merging that PR publishes to npm with provenance attestation and creates a GitHub Release.
|
|
185
|
+
4. Merging that PR publishes to npm with provenance attestation and creates a GitHub Release. Auth uses **npm OIDC trusted publishing** — no `NPM_TOKEN` secret; the workflow exchanges a GitHub OIDC token for a short-lived npm publish token.
|
|
186
186
|
|
|
187
|
-
|
|
187
|
+
The same `release.yml` workflow handles per-branch preview publishes: pushing a non-`main` branch (or running it via `workflow_dispatch`) snapshot-publishes the pending changesets under a sanitized branch dist-tag — install with `pnpm add @schmooky/zvuk@<branch>`.
|
|
188
188
|
|
|
189
189
|
## CLI
|
|
190
190
|
|
package/dist/index.d.ts
CHANGED
|
@@ -145,7 +145,7 @@ interface SendOptions {
|
|
|
145
145
|
interface MasterLimiterConfig {
|
|
146
146
|
/** Threshold in dB. Default -1 (just below 0 dBFS). */
|
|
147
147
|
threshold?: number;
|
|
148
|
-
/** Compression ratio. Default 20 (brick
|
|
148
|
+
/** Compression ratio. Default 20 (high — limiter-like, but not a true brick wall). */
|
|
149
149
|
ratio?: number;
|
|
150
150
|
/** Attack in seconds. Default 0.001 (fast — catches the transient). */
|
|
151
151
|
attack?: number;
|
|
@@ -155,7 +155,7 @@ interface MasterLimiterConfig {
|
|
|
155
155
|
interface MasterConfig {
|
|
156
156
|
/** Headroom in dB applied to the master gain (negative). Default: 0. */
|
|
157
157
|
headroom?: number;
|
|
158
|
-
/** Optional
|
|
158
|
+
/** Optional fast-attack soft limiter on the master output (best-effort peak control). */
|
|
159
159
|
limiter?: MasterLimiterConfig;
|
|
160
160
|
}
|
|
161
161
|
/**
|
|
@@ -312,6 +312,13 @@ interface StopOptions {
|
|
|
312
312
|
fade?: number;
|
|
313
313
|
}
|
|
314
314
|
interface VoiceJitter {
|
|
315
|
+
/**
|
|
316
|
+
* Center value the jitter varies around. Defaults to 1 (the neutral
|
|
317
|
+
* volume / playback rate). Set it to combine a base with jitter, e.g.
|
|
318
|
+
* `{ base: 1.5, jitter: 0.1 }` plays at 1.5 ± 0.1 per voice.
|
|
319
|
+
*/
|
|
320
|
+
base?: number;
|
|
321
|
+
/** Maximum ± random deviation from `base`, drawn once per voice. */
|
|
315
322
|
jitter?: number;
|
|
316
323
|
}
|
|
317
324
|
/**
|
|
@@ -416,8 +423,9 @@ interface LoadSoundOptions {
|
|
|
416
423
|
signal?: AbortSignal;
|
|
417
424
|
/**
|
|
418
425
|
* Run RMS-based loudness normalization on the decoded buffer so it sits at
|
|
419
|
-
* the same
|
|
420
|
-
* defaults or an options object to tune
|
|
426
|
+
* the same RMS level as other normalized sounds. (Full-band RMS, not
|
|
427
|
+
* perceptual/LUFS.) Pass `true` for defaults or an options object to tune
|
|
428
|
+
* the target.
|
|
421
429
|
*/
|
|
422
430
|
normalize?: boolean | LoudnessOptions$1;
|
|
423
431
|
}
|
|
@@ -709,13 +717,15 @@ declare class Send {
|
|
|
709
717
|
declare class Bus {
|
|
710
718
|
readonly name: string;
|
|
711
719
|
readonly input: GainNode;
|
|
712
|
-
/** Sub-bus
|
|
720
|
+
/** Sub-bus the FX chain splices into: input → fxInput → (fx…) → output. */
|
|
713
721
|
readonly fxInput: GainNode;
|
|
714
722
|
/** Output node — connect to master.input or another bus.input for sends. */
|
|
715
723
|
readonly output: GainNode;
|
|
716
724
|
private _level;
|
|
717
725
|
private _muted;
|
|
718
726
|
private _soloed;
|
|
727
|
+
/** True while the engine's solo rule is silencing this (non-soloed) bus. */
|
|
728
|
+
private _soloVeiled;
|
|
719
729
|
private readonly ctx;
|
|
720
730
|
private _concurrency;
|
|
721
731
|
private _voices;
|
|
@@ -845,6 +855,8 @@ declare class Ducker implements FxInsert {
|
|
|
845
855
|
private analyser;
|
|
846
856
|
private buf;
|
|
847
857
|
private rafId;
|
|
858
|
+
/** Timestamp of the previous tick (ms), for measuring the real frame delta. */
|
|
859
|
+
private lastTickMs;
|
|
848
860
|
private envelope;
|
|
849
861
|
private targetGain;
|
|
850
862
|
private ctx;
|
|
@@ -886,6 +898,8 @@ declare class Filter implements FxInsert {
|
|
|
886
898
|
setFrequency(hz: number): void;
|
|
887
899
|
setQ(q: number): void;
|
|
888
900
|
setType(t: FilterKind): void;
|
|
901
|
+
/** Set the filter gain in dB. Only affects `peaking` (the one shelf-like mode exposed). */
|
|
902
|
+
setGain(db: number): void;
|
|
889
903
|
get bypassed(): boolean;
|
|
890
904
|
set bypassed(v: boolean);
|
|
891
905
|
dispose(): void;
|
|
@@ -919,6 +933,8 @@ declare class Reverb implements FxInsert {
|
|
|
919
933
|
private preDelay;
|
|
920
934
|
private ctx;
|
|
921
935
|
private _bypassed;
|
|
936
|
+
/** Active wet mix (0..1), so bypass can restore it instead of guessing. */
|
|
937
|
+
private _wet;
|
|
922
938
|
constructor(ctx: AudioContext, config?: ReverbConfig);
|
|
923
939
|
setWet(mix: number): void;
|
|
924
940
|
setImpulse(buffer: AudioBuffer): void;
|
|
@@ -949,21 +965,25 @@ declare class StretchProcessor {
|
|
|
949
965
|
}
|
|
950
966
|
|
|
951
967
|
/**
|
|
952
|
-
* Realtime
|
|
968
|
+
* Realtime *varispeed* via AudioWorklet.
|
|
953
969
|
*
|
|
954
|
-
* The companion to the offline `StretchProcessor`. Loads a worklet
|
|
955
|
-
* into the AudioContext and exposes a node whose `stretch`
|
|
956
|
-
* automated live — boss intros that bend in real time,
|
|
970
|
+
* The realtime companion to the offline `StretchProcessor`. Loads a worklet
|
|
971
|
+
* processor into the AudioContext and exposes a node whose `stretch`
|
|
972
|
+
* AudioParam can be automated live — boss intros that bend in real time,
|
|
973
|
+
* slow-mo stings, etc.
|
|
957
974
|
*
|
|
958
|
-
*
|
|
959
|
-
*
|
|
960
|
-
*
|
|
961
|
-
*
|
|
975
|
+
* IMPORTANT: unlike the offline `StretchProcessor`, this node is NOT
|
|
976
|
+
* pitch-preserving. It resamples a ring buffer at a variable read rate via
|
|
977
|
+
* linear interpolation, so changing `stretch` shifts pitch *and* tempo
|
|
978
|
+
* together — classic varispeed / tape-style speed change. It is cheap and
|
|
979
|
+
* sounds fine for live ramps across the 0.25×–4× range when you don't need
|
|
980
|
+
* pitch held constant. For pitch-preserving (offline) stretching, use
|
|
981
|
+
* `StretchProcessor`.
|
|
962
982
|
*/
|
|
963
983
|
interface StretchWorkletOptions {
|
|
964
|
-
/** Initial stretch factor. 1 = play at source rate. > 1 = faster. */
|
|
984
|
+
/** Initial stretch factor. 1 = play at source rate. > 1 = faster (higher pitch). */
|
|
965
985
|
stretchFactor?: number;
|
|
966
|
-
/**
|
|
986
|
+
/** Ring-buffer sizing hint in samples (ring = 8×). Default 1024. */
|
|
967
987
|
grainSize?: number;
|
|
968
988
|
}
|
|
969
989
|
interface StretchWorkletNode extends AudioNode {
|
|
@@ -1492,8 +1512,10 @@ interface Engine<TBusName extends string = string> {
|
|
|
1492
1512
|
* over the same window with `equal-power` curves so the perceived loudness
|
|
1493
1513
|
* stays flat.
|
|
1494
1514
|
*
|
|
1495
|
-
* Returns the new Voice
|
|
1496
|
-
*
|
|
1515
|
+
* Returns the new Voice — a fresh voice for `to` is always started at
|
|
1516
|
+
* volume 0 and faded up. Only voices of `from` on the same bus the new
|
|
1517
|
+
* voice plays on are faded out; instances of `from` on other buses are
|
|
1518
|
+
* left untouched.
|
|
1497
1519
|
*/
|
|
1498
1520
|
crossfade(from: string, to: string, options?: CrossfadeOptions): Voice;
|
|
1499
1521
|
scheduleAt(audioTime: number, fn: () => void): () => void;
|
|
@@ -1545,12 +1567,14 @@ interface CrossfadeOptions {
|
|
|
1545
1567
|
declare function createEngine<TBusName extends string = string>(config?: EngineConfig<TBusName>): Engine<TBusName>;
|
|
1546
1568
|
|
|
1547
1569
|
/**
|
|
1548
|
-
* The Master stage. Headroom-aware gain into an optional
|
|
1549
|
-
*
|
|
1570
|
+
* The Master stage. Headroom-aware gain into an optional soft limiter, then to
|
|
1571
|
+
* destination. Buses connect their output to master.input.
|
|
1550
1572
|
*
|
|
1551
1573
|
* Headroom is a static gain offset; the limiter is a fast-attack
|
|
1552
|
-
* DynamicsCompressorNode that catches peaks the headroom can't tame when
|
|
1553
|
-
* heavy FX stack on top of busy mixes.
|
|
1574
|
+
* DynamicsCompressorNode that catches most peaks the headroom can't tame when
|
|
1575
|
+
* heavy FX stack on top of busy mixes. Note it is best-effort, not a true
|
|
1576
|
+
* brick-wall limiter — with a finite attack and no lookahead, transient peaks
|
|
1577
|
+
* above the threshold can still pass, so it does not guarantee a hard ceiling.
|
|
1554
1578
|
*/
|
|
1555
1579
|
declare class Master {
|
|
1556
1580
|
readonly input: GainNode;
|
|
@@ -1622,10 +1646,14 @@ declare function pickSourceOrder(urls: readonly string[]): readonly string[];
|
|
|
1622
1646
|
/**
|
|
1623
1647
|
* RMS-based loudness normalization on a decoded AudioBuffer.
|
|
1624
1648
|
*
|
|
1625
|
-
* `engine.loadSound(..., { normalize: true })` runs this at decode-time
|
|
1626
|
-
*
|
|
1627
|
-
*
|
|
1628
|
-
*
|
|
1649
|
+
* `engine.loadSound(..., { normalize: true })` runs this at decode-time and
|
|
1650
|
+
* produces a buffer pre-scaled so all sounds sit at the same RMS level —
|
|
1651
|
+
* removing a workflow tax that game-audio teams currently pay in their DAW.
|
|
1652
|
+
* Pass an object to override the target RMS or peak ceiling.
|
|
1653
|
+
*
|
|
1654
|
+
* NOTE: this is full-band RMS, not perceptual (K-weighted / LUFS) loudness —
|
|
1655
|
+
* two spectrally different clips at equal RMS may still differ in perceived
|
|
1656
|
+
* loudness.
|
|
1629
1657
|
*/
|
|
1630
1658
|
interface LoudnessOptions {
|
|
1631
1659
|
/** Target RMS (linear, 0..1). Default 0.1 (~ -20 dBFS). */
|
|
@@ -1634,7 +1662,7 @@ interface LoudnessOptions {
|
|
|
1634
1662
|
peakCeiling?: number;
|
|
1635
1663
|
}
|
|
1636
1664
|
type NormalizeFlag = boolean | LoudnessOptions;
|
|
1637
|
-
declare function applyLoudnessNormalization(buffer: AudioBuffer, flag: NormalizeFlag): AudioBuffer;
|
|
1665
|
+
declare function applyLoudnessNormalization(buffer: AudioBuffer, flag: NormalizeFlag, ctx?: BaseAudioContext): AudioBuffer;
|
|
1638
1666
|
declare function computeNormalizationGain(buffer: AudioBuffer, opts: Required<LoudnessOptions>): number;
|
|
1639
1667
|
|
|
1640
1668
|
export { AggregateDecodeError, type ApplyOptions, type AssetResolver, type AudioLevel, type AudioMimeType, Bus, type BusConfig, BusGroup, BusNotFoundError, Compressor, type CompressorConfig, type ConcurrencyConfig, type CrossfadeOptions, type DecodeAttempt, DecodeError, type DistanceModel, 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, Music, type MusicLoadOptions, type MusicParts, type MusicPlayOptions, type MusicState, MusicVoice, type NormalizeFlag, Parameter, type ParameterCurve, type PlayOptions, PreloadError, type PreloadFailure, type PreloadItem, type PreloadOptions, type PreloadProgressEvent, type ResolveAssetContext, type ResolvedAsset, Reverb, type ReverbConfig, Send, type SendOptions, type SidechainConfig, type SkipToOutroOptions, Snapshot, type SnapshotState, Sound, SoundNotFoundError, type SpatialOptions, Spatializer, Sprite, type SpriteMap, type SpriteRegion, type SpriteRegionPlayOptions, type StopOptions, StreamSound, StretchProcessor, type StretchWorkletNode, type StretchWorkletOptions, type TickSource, type VariantStrategy, Variants, type VariantsOptions, Voice, type VoiceDefaults, type VoiceJitter, ZvukError, applyLoudnessNormalization, canPlay, computeNormalizationGain, createEngine, createStretchWorkletNode, ensureStretchWorklet, mimeForUrl, pickSource, pickSourceOrder };
|
package/dist/index.js
CHANGED
|
@@ -126,6 +126,8 @@ var Ducker = class {
|
|
|
126
126
|
analyser;
|
|
127
127
|
buf;
|
|
128
128
|
rafId = null;
|
|
129
|
+
/** Timestamp of the previous tick (ms), for measuring the real frame delta. */
|
|
130
|
+
lastTickMs = 0;
|
|
129
131
|
envelope = 0;
|
|
130
132
|
targetGain = 1;
|
|
131
133
|
ctx;
|
|
@@ -179,7 +181,12 @@ var Ducker = class {
|
|
|
179
181
|
} catch {
|
|
180
182
|
}
|
|
181
183
|
}
|
|
182
|
-
tick = () => {
|
|
184
|
+
tick = (timeMs) => {
|
|
185
|
+
let dtSec = 1 / 60;
|
|
186
|
+
if (timeMs != null && this.lastTickMs > 0) {
|
|
187
|
+
dtSec = Math.min(0.1, Math.max(1e-3, (timeMs - this.lastTickMs) / 1e3));
|
|
188
|
+
}
|
|
189
|
+
if (timeMs != null) this.lastTickMs = timeMs;
|
|
183
190
|
if (this._bypassed) {
|
|
184
191
|
this.rafId = requestAnimationFrame(this.tick);
|
|
185
192
|
return;
|
|
@@ -195,7 +202,7 @@ var Ducker = class {
|
|
|
195
202
|
const target = exceeded > 0 ? 1 - this.cfg.amount : 1;
|
|
196
203
|
const useAttack = target < this.envelope;
|
|
197
204
|
const tau = (useAttack ? this.cfg.attack : this.cfg.release) || 1e-3;
|
|
198
|
-
const alpha = 1 - Math.exp(-
|
|
205
|
+
const alpha = 1 - Math.exp(-dtSec / tau);
|
|
199
206
|
this.envelope = this.envelope + alpha * (target - this.envelope);
|
|
200
207
|
if (Math.abs(this.envelope - this.targetGain) > 1e-3) {
|
|
201
208
|
this.targetGain = this.envelope;
|
|
@@ -232,6 +239,10 @@ var Filter = class {
|
|
|
232
239
|
setType(t) {
|
|
233
240
|
this.filter.type = t;
|
|
234
241
|
}
|
|
242
|
+
/** Set the filter gain in dB. Only affects `peaking` (the one shelf-like mode exposed). */
|
|
243
|
+
setGain(db) {
|
|
244
|
+
this.filter.gain.setValueAtTime(db, this.ctx.currentTime);
|
|
245
|
+
}
|
|
235
246
|
get bypassed() {
|
|
236
247
|
return this._bypassed;
|
|
237
248
|
}
|
|
@@ -273,6 +284,8 @@ var Reverb = class {
|
|
|
273
284
|
preDelay;
|
|
274
285
|
ctx;
|
|
275
286
|
_bypassed = false;
|
|
287
|
+
/** Active wet mix (0..1), so bypass can restore it instead of guessing. */
|
|
288
|
+
_wet;
|
|
276
289
|
constructor(ctx, config = {}) {
|
|
277
290
|
this.ctx = ctx;
|
|
278
291
|
this.input = ctx.createGain();
|
|
@@ -282,6 +295,7 @@ var Reverb = class {
|
|
|
282
295
|
this.preDelay = ctx.createDelay(1);
|
|
283
296
|
this.convolver = ctx.createConvolver();
|
|
284
297
|
const wetMix = config.wet ?? 0.3;
|
|
298
|
+
this._wet = wetMix;
|
|
285
299
|
this.dry.gain.value = 1 - wetMix;
|
|
286
300
|
this.wet.gain.value = wetMix;
|
|
287
301
|
if (config.impulse) {
|
|
@@ -295,6 +309,8 @@ var Reverb = class {
|
|
|
295
309
|
}
|
|
296
310
|
setWet(mix) {
|
|
297
311
|
const m = Math.max(0, Math.min(1, mix));
|
|
312
|
+
this._wet = m;
|
|
313
|
+
if (this._bypassed) return;
|
|
298
314
|
const t = this.ctx.currentTime;
|
|
299
315
|
this.dry.gain.setValueAtTime(1 - m, t);
|
|
300
316
|
this.wet.gain.setValueAtTime(m, t);
|
|
@@ -308,7 +324,14 @@ var Reverb = class {
|
|
|
308
324
|
set bypassed(v) {
|
|
309
325
|
if (this._bypassed === v) return;
|
|
310
326
|
this._bypassed = v;
|
|
311
|
-
|
|
327
|
+
const t = this.ctx.currentTime;
|
|
328
|
+
if (v) {
|
|
329
|
+
this.dry.gain.setValueAtTime(1, t);
|
|
330
|
+
this.wet.gain.setValueAtTime(0, t);
|
|
331
|
+
} else {
|
|
332
|
+
this.dry.gain.setValueAtTime(1 - this._wet, t);
|
|
333
|
+
this.wet.gain.setValueAtTime(this._wet, t);
|
|
334
|
+
}
|
|
312
335
|
}
|
|
313
336
|
dispose() {
|
|
314
337
|
try {
|
|
@@ -466,18 +489,11 @@ class ZvukRealtimeStretchProcessor extends AudioWorkletProcessor {
|
|
|
466
489
|
constructor(options) {
|
|
467
490
|
super();
|
|
468
491
|
const grain = (options && options.processorOptions && options.processorOptions.grainSize) || 1024;
|
|
469
|
-
this.grainSize = grain;
|
|
470
492
|
this.ringSize = grain * 8;
|
|
471
493
|
this.ringL = new Float32Array(this.ringSize);
|
|
472
494
|
this.ringR = new Float32Array(this.ringSize);
|
|
473
495
|
this.writePos = 0;
|
|
474
496
|
this.readPos = 0;
|
|
475
|
-
this.window = new Float32Array(grain);
|
|
476
|
-
for (let i = 0; i < grain; i++) {
|
|
477
|
-
const t = i / (grain - 1);
|
|
478
|
-
// Hann window for OLA crossfade.
|
|
479
|
-
this.window[i] = 0.5 * (1 - Math.cos(2 * Math.PI * t));
|
|
480
|
-
}
|
|
481
497
|
}
|
|
482
498
|
|
|
483
499
|
process(inputs, outputs, params) {
|
|
@@ -583,16 +599,20 @@ function applyRamp(param, now, to, duration, curve) {
|
|
|
583
599
|
const samples = sampleCurve(from, to, curve, 64);
|
|
584
600
|
param.setValueCurveAtTime(samples, now, seconds);
|
|
585
601
|
}
|
|
602
|
+
function equalPowerCurve(from, to, n = 33) {
|
|
603
|
+
return sampleCurve(from, to, "equal-power", n);
|
|
604
|
+
}
|
|
586
605
|
function sampleCurve(from, to, curve, n) {
|
|
587
606
|
const out = new Float32Array(n);
|
|
607
|
+
const rising = to >= from;
|
|
588
608
|
for (let i = 0; i < n; i++) {
|
|
589
609
|
const t = i / (n - 1);
|
|
590
|
-
const eased = ease(t, curve);
|
|
610
|
+
const eased = ease(t, curve, rising);
|
|
591
611
|
out[i] = from + (to - from) * eased;
|
|
592
612
|
}
|
|
593
613
|
return out;
|
|
594
614
|
}
|
|
595
|
-
function ease(t, curve) {
|
|
615
|
+
function ease(t, curve, rising = true) {
|
|
596
616
|
switch (curve) {
|
|
597
617
|
case "easeIn":
|
|
598
618
|
return t * t;
|
|
@@ -600,8 +620,9 @@ function ease(t, curve) {
|
|
|
600
620
|
return 1 - (1 - t) * (1 - t);
|
|
601
621
|
case "easeInOut":
|
|
602
622
|
return t < 0.5 ? 2 * t * t : 1 - 2 * (1 - t) * (1 - t);
|
|
603
|
-
case "equal-power":
|
|
604
|
-
return Math.sin(t * Math.PI / 2)
|
|
623
|
+
case "equal-power": {
|
|
624
|
+
return rising ? Math.sin(t * Math.PI / 2) : 1 - Math.cos(t * Math.PI / 2);
|
|
625
|
+
}
|
|
605
626
|
default:
|
|
606
627
|
return t;
|
|
607
628
|
}
|
|
@@ -653,13 +674,15 @@ var Send = class {
|
|
|
653
674
|
var Bus = class {
|
|
654
675
|
name;
|
|
655
676
|
input;
|
|
656
|
-
/** Sub-bus
|
|
677
|
+
/** Sub-bus the FX chain splices into: input → fxInput → (fx…) → output. */
|
|
657
678
|
fxInput;
|
|
658
679
|
/** Output node — connect to master.input or another bus.input for sends. */
|
|
659
680
|
output;
|
|
660
681
|
_level;
|
|
661
682
|
_muted;
|
|
662
683
|
_soloed = false;
|
|
684
|
+
/** True while the engine's solo rule is silencing this (non-soloed) bus. */
|
|
685
|
+
_soloVeiled = false;
|
|
663
686
|
ctx;
|
|
664
687
|
_concurrency;
|
|
665
688
|
_voices = /* @__PURE__ */ new Set();
|
|
@@ -694,19 +717,20 @@ var Bus = class {
|
|
|
694
717
|
}
|
|
695
718
|
set level(v) {
|
|
696
719
|
this._level = clamp01(v);
|
|
697
|
-
if (!this._muted) this.rampOutput(this._level, 0.01);
|
|
720
|
+
if (!this._muted && !this._soloVeiled) this.rampOutput(this._level, 0.01);
|
|
698
721
|
}
|
|
699
722
|
get muted() {
|
|
700
723
|
return this._muted;
|
|
701
724
|
}
|
|
702
725
|
set muted(v) {
|
|
703
726
|
this._muted = v;
|
|
704
|
-
this.rampOutput(v ? 0 : this._level, 0.01);
|
|
727
|
+
this.rampOutput(v || this._soloVeiled ? 0 : this._level, 0.01);
|
|
705
728
|
}
|
|
706
729
|
/** Fade the bus output to `target` over `duration` seconds. */
|
|
707
730
|
fadeTo(target, duration, curve = "linear") {
|
|
708
731
|
const to = clamp01(target);
|
|
709
732
|
this._level = to;
|
|
733
|
+
if (this._muted || this._soloVeiled) return wait(duration);
|
|
710
734
|
return this.ramp(to, duration, curve);
|
|
711
735
|
}
|
|
712
736
|
/** Active voice count on this bus. */
|
|
@@ -767,7 +791,7 @@ var Bus = class {
|
|
|
767
791
|
this.rewireFxChain();
|
|
768
792
|
}
|
|
769
793
|
fx() {
|
|
770
|
-
return this._fxChain;
|
|
794
|
+
return [...this._fxChain];
|
|
771
795
|
}
|
|
772
796
|
/**
|
|
773
797
|
* Live amplitude readout on the bus output. Returns `{ rms, peak }` as
|
|
@@ -827,7 +851,7 @@ var Bus = class {
|
|
|
827
851
|
}
|
|
828
852
|
/** Read-only snapshot of every active send originating on this bus. */
|
|
829
853
|
sends() {
|
|
830
|
-
return this._sends;
|
|
854
|
+
return [...this._sends];
|
|
831
855
|
}
|
|
832
856
|
/**
|
|
833
857
|
* Solo this bus. The engine coordinates the global rule: while any bus
|
|
@@ -858,6 +882,7 @@ var Bus = class {
|
|
|
858
882
|
* user-visible mute state without re-rendering.
|
|
859
883
|
*/
|
|
860
884
|
applySoloVeil(soloMute) {
|
|
885
|
+
this._soloVeiled = soloMute;
|
|
861
886
|
const target = this._muted ? 0 : soloMute ? 0 : this._level;
|
|
862
887
|
this.rampOutput(target, 0.01);
|
|
863
888
|
}
|
|
@@ -884,6 +909,10 @@ var Bus = class {
|
|
|
884
909
|
return wait(duration);
|
|
885
910
|
}
|
|
886
911
|
dispose() {
|
|
912
|
+
for (const send of this._sends) send.dispose();
|
|
913
|
+
this._sends = [];
|
|
914
|
+
for (const fx of this._fxChain) fx.dispose();
|
|
915
|
+
this._fxChain = [];
|
|
887
916
|
try {
|
|
888
917
|
this.input.disconnect();
|
|
889
918
|
this.fxInput.disconnect();
|
|
@@ -1013,14 +1042,15 @@ var Parameter = class {
|
|
|
1013
1042
|
const from = opts.from ?? 0;
|
|
1014
1043
|
const to = opts.to ?? 1;
|
|
1015
1044
|
const curve = opts.curve ?? "linear";
|
|
1045
|
+
const rising = to >= from;
|
|
1016
1046
|
return this.subscribe((v) => {
|
|
1017
1047
|
const t = Math.max(0, Math.min(1, v));
|
|
1018
|
-
const eased = ease2(t, curve);
|
|
1048
|
+
const eased = ease2(t, curve, rising);
|
|
1019
1049
|
setter(from + (to - from) * eased);
|
|
1020
1050
|
});
|
|
1021
1051
|
}
|
|
1022
1052
|
};
|
|
1023
|
-
function ease2(t, curve) {
|
|
1053
|
+
function ease2(t, curve, rising = true) {
|
|
1024
1054
|
switch (curve) {
|
|
1025
1055
|
case "easeIn":
|
|
1026
1056
|
return t * t;
|
|
@@ -1029,7 +1059,7 @@ function ease2(t, curve) {
|
|
|
1029
1059
|
case "easeInOut":
|
|
1030
1060
|
return t < 0.5 ? 2 * t * t : 1 - 2 * (1 - t) * (1 - t);
|
|
1031
1061
|
case "equal-power":
|
|
1032
|
-
return Math.sin(t * Math.PI / 2)
|
|
1062
|
+
return rising ? Math.sin(t * Math.PI / 2) : 1 - Math.cos(t * Math.PI / 2);
|
|
1033
1063
|
default:
|
|
1034
1064
|
return t;
|
|
1035
1065
|
}
|
|
@@ -1313,12 +1343,12 @@ var DEFAULTS = {
|
|
|
1313
1343
|
targetRms: 0.1,
|
|
1314
1344
|
peakCeiling: 0.99
|
|
1315
1345
|
};
|
|
1316
|
-
function applyLoudnessNormalization(buffer, flag) {
|
|
1346
|
+
function applyLoudnessNormalization(buffer, flag, ctx) {
|
|
1317
1347
|
if (!flag) return buffer;
|
|
1318
1348
|
const opts = { ...DEFAULTS, ...typeof flag === "object" ? flag : {} };
|
|
1319
1349
|
const gain = computeNormalizationGain(buffer, opts);
|
|
1320
1350
|
if (Math.abs(gain - 1) < 1e-3) return buffer;
|
|
1321
|
-
return scaleBuffer(buffer, gain);
|
|
1351
|
+
return scaleBuffer(buffer, gain, ctx);
|
|
1322
1352
|
}
|
|
1323
1353
|
function computeNormalizationGain(buffer, opts) {
|
|
1324
1354
|
const rms = measureRms(buffer);
|
|
@@ -1350,7 +1380,16 @@ function measurePeak(buffer) {
|
|
|
1350
1380
|
}
|
|
1351
1381
|
return peak;
|
|
1352
1382
|
}
|
|
1353
|
-
function scaleBuffer(src, gain) {
|
|
1383
|
+
function scaleBuffer(src, gain, ctx) {
|
|
1384
|
+
if (ctx) {
|
|
1385
|
+
const out = ctx.createBuffer(src.numberOfChannels, src.length, src.sampleRate);
|
|
1386
|
+
for (let c = 0; c < src.numberOfChannels; c++) {
|
|
1387
|
+
const from = src.getChannelData(c);
|
|
1388
|
+
const to = out.getChannelData(c);
|
|
1389
|
+
for (let i = 0; i < from.length; i++) to[i] = from[i] * gain;
|
|
1390
|
+
}
|
|
1391
|
+
return out;
|
|
1392
|
+
}
|
|
1354
1393
|
for (let c = 0; c < src.numberOfChannels; c++) {
|
|
1355
1394
|
const data = src.getChannelData(c);
|
|
1356
1395
|
for (let i = 0; i < data.length; i++) data[i] = data[i] * gain;
|
|
@@ -1624,14 +1663,14 @@ var MusicVoice = class {
|
|
|
1624
1663
|
const segGain = this.ctx.createGain();
|
|
1625
1664
|
if (fadeIn) {
|
|
1626
1665
|
segGain.gain.setValueAtTime(0, when);
|
|
1627
|
-
segGain.gain.
|
|
1666
|
+
segGain.gain.setValueCurveAtTime(equalPowerCurve(0, 1), when, this.loopCrossfadeSec);
|
|
1628
1667
|
} else {
|
|
1629
1668
|
segGain.gain.setValueAtTime(1, when);
|
|
1630
1669
|
}
|
|
1631
1670
|
if (this.crossfadeViable()) {
|
|
1632
1671
|
const fadeOutAt = when + this.buffers.loop.duration - this.loopCrossfadeSec;
|
|
1633
1672
|
segGain.gain.setValueAtTime(1, fadeOutAt);
|
|
1634
|
-
segGain.gain.
|
|
1673
|
+
segGain.gain.setValueCurveAtTime(equalPowerCurve(1, 0), fadeOutAt, this.loopCrossfadeSec);
|
|
1635
1674
|
}
|
|
1636
1675
|
src.connect(segGain).connect(this.gain);
|
|
1637
1676
|
try {
|
|
@@ -2104,6 +2143,10 @@ var Voice = class _Voice {
|
|
|
2104
2143
|
}
|
|
2105
2144
|
/** Async iterator of lifecycle cues — yields started, optional paused, ended. */
|
|
2106
2145
|
async *cues() {
|
|
2146
|
+
if (this.done) {
|
|
2147
|
+
yield "ended";
|
|
2148
|
+
return;
|
|
2149
|
+
}
|
|
2107
2150
|
const queue = [];
|
|
2108
2151
|
let waiter = null;
|
|
2109
2152
|
const push = (c) => {
|
|
@@ -2125,7 +2168,10 @@ var Voice = class _Voice {
|
|
|
2125
2168
|
if (next2 === "ended") return;
|
|
2126
2169
|
continue;
|
|
2127
2170
|
}
|
|
2128
|
-
if (this.done)
|
|
2171
|
+
if (this.done) {
|
|
2172
|
+
yield "ended";
|
|
2173
|
+
return;
|
|
2174
|
+
}
|
|
2129
2175
|
const next = await new Promise((res) => {
|
|
2130
2176
|
waiter = res;
|
|
2131
2177
|
});
|
|
@@ -2220,13 +2266,13 @@ var Voice = class _Voice {
|
|
|
2220
2266
|
const segGain = this.ctx.createGain();
|
|
2221
2267
|
if (fadeIn) {
|
|
2222
2268
|
segGain.gain.setValueAtTime(0, when);
|
|
2223
|
-
segGain.gain.
|
|
2269
|
+
segGain.gain.setValueCurveAtTime(equalPowerCurve(0, 1), when, this.crossfadeSec);
|
|
2224
2270
|
} else {
|
|
2225
2271
|
segGain.gain.setValueAtTime(1, when);
|
|
2226
2272
|
}
|
|
2227
2273
|
const fadeOutAt = when + this.crossfadeRegionLen - this.crossfadeSec;
|
|
2228
2274
|
segGain.gain.setValueAtTime(1, fadeOutAt);
|
|
2229
|
-
segGain.gain.
|
|
2275
|
+
segGain.gain.setValueCurveAtTime(equalPowerCurve(1, 0), fadeOutAt, this.crossfadeSec);
|
|
2230
2276
|
src.connect(segGain).connect(this.gain);
|
|
2231
2277
|
try {
|
|
2232
2278
|
src.start(when, this.crossfadeRegionStart, this.crossfadeRegionLen);
|
|
@@ -2280,11 +2326,12 @@ var Voice = class _Voice {
|
|
|
2280
2326
|
return offset;
|
|
2281
2327
|
}
|
|
2282
2328
|
};
|
|
2283
|
-
function resolveJittered(v,
|
|
2329
|
+
function resolveJittered(v, fallbackBase = 1) {
|
|
2284
2330
|
if (typeof v === "number") return v;
|
|
2285
|
-
if (!v
|
|
2286
|
-
const
|
|
2287
|
-
|
|
2331
|
+
if (!v) return fallbackBase;
|
|
2332
|
+
const center = v.base ?? fallbackBase;
|
|
2333
|
+
if (v.jitter == null) return center;
|
|
2334
|
+
return center + (Math.random() * 2 - 1) * v.jitter;
|
|
2288
2335
|
}
|
|
2289
2336
|
function clamp013(v) {
|
|
2290
2337
|
if (Number.isNaN(v)) return 0;
|
|
@@ -2567,7 +2614,7 @@ var Spatializer = class {
|
|
|
2567
2614
|
const filter = ctx.createBiquadFilter();
|
|
2568
2615
|
filter.type = "lowpass";
|
|
2569
2616
|
filter.frequency.value = OCCLUSION_OPEN_HZ;
|
|
2570
|
-
filter.Q.value =
|
|
2617
|
+
filter.Q.value = Math.SQRT1_2;
|
|
2571
2618
|
const gain = ctx.createGain();
|
|
2572
2619
|
gain.gain.value = 1;
|
|
2573
2620
|
panner.connect(filter).connect(gain);
|
|
@@ -2907,7 +2954,7 @@ var EngineImpl = class {
|
|
|
2907
2954
|
const decodeOpts = { signal: options.signal };
|
|
2908
2955
|
let buffer = await this.resolveBuffer(name, url, decodeOpts);
|
|
2909
2956
|
if (options.normalize) {
|
|
2910
|
-
buffer = applyLoudnessNormalization(buffer, options.normalize);
|
|
2957
|
+
buffer = applyLoudnessNormalization(buffer, options.normalize, this.host.touch());
|
|
2911
2958
|
}
|
|
2912
2959
|
this.soundUrls.set(name, typeof url === "string" ? [url] : [...url]);
|
|
2913
2960
|
return this.createSound(name, buffer, { bus: options.bus });
|
|
@@ -3013,10 +3060,11 @@ var EngineImpl = class {
|
|
|
3013
3060
|
const loopBuf = await this.resolveBuffer(`__music:${name}:loop`, parts.loop, decodeOpts);
|
|
3014
3061
|
const introBuf = parts.intro ? await this.resolveBuffer(`__music:${name}:intro`, parts.intro, decodeOpts) : void 0;
|
|
3015
3062
|
const outroBuf = parts.outro ? await this.resolveBuffer(`__music:${name}:outro`, parts.outro, decodeOpts) : void 0;
|
|
3063
|
+
const ctx = this.host.touch();
|
|
3016
3064
|
const buffers = {
|
|
3017
|
-
intro: introBuf ? options.normalize ? applyLoudnessNormalization(introBuf, options.normalize) : introBuf : void 0,
|
|
3018
|
-
loop: options.normalize ? applyLoudnessNormalization(loopBuf, options.normalize) : loopBuf,
|
|
3019
|
-
outro: outroBuf ? options.normalize ? applyLoudnessNormalization(outroBuf, options.normalize) : outroBuf : void 0
|
|
3065
|
+
intro: introBuf ? options.normalize ? applyLoudnessNormalization(introBuf, options.normalize, ctx) : introBuf : void 0,
|
|
3066
|
+
loop: options.normalize ? applyLoudnessNormalization(loopBuf, options.normalize, ctx) : loopBuf,
|
|
3067
|
+
outro: outroBuf ? options.normalize ? applyLoudnessNormalization(outroBuf, options.normalize, ctx) : outroBuf : void 0
|
|
3020
3068
|
};
|
|
3021
3069
|
const busName = options.bus ?? this.busOrder[0];
|
|
3022
3070
|
const bus = this.buses.get(busName);
|
|
@@ -3070,7 +3118,9 @@ var EngineImpl = class {
|
|
|
3070
3118
|
volume: 0
|
|
3071
3119
|
});
|
|
3072
3120
|
void newVoice.fade({ to: toVolume, duration, curve });
|
|
3073
|
-
const outgoing = this.activeVoices().filter(
|
|
3121
|
+
const outgoing = this.activeVoices().filter(
|
|
3122
|
+
(v) => v !== newVoice && v.sourceName === from && v.bus === newVoice.bus
|
|
3123
|
+
);
|
|
3074
3124
|
for (const v of outgoing) {
|
|
3075
3125
|
void v.fade({ to: 0, duration, curve }).then(() => v.stop());
|
|
3076
3126
|
}
|
|
@@ -3268,10 +3318,20 @@ function combineSignals(a, b) {
|
|
|
3268
3318
|
if (a.aborted) return a;
|
|
3269
3319
|
if (b.aborted) return b;
|
|
3270
3320
|
const ac = new AbortController();
|
|
3271
|
-
const
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3321
|
+
const cleanup = () => {
|
|
3322
|
+
a.removeEventListener("abort", onAbortA);
|
|
3323
|
+
b.removeEventListener("abort", onAbortB);
|
|
3324
|
+
};
|
|
3325
|
+
const onAbortA = () => {
|
|
3326
|
+
cleanup();
|
|
3327
|
+
ac.abort(a.reason);
|
|
3328
|
+
};
|
|
3329
|
+
const onAbortB = () => {
|
|
3330
|
+
cleanup();
|
|
3331
|
+
ac.abort(b.reason);
|
|
3332
|
+
};
|
|
3333
|
+
a.addEventListener("abort", onAbortA);
|
|
3334
|
+
b.addEventListener("abort", onAbortB);
|
|
3275
3335
|
return ac.signal;
|
|
3276
3336
|
}
|
|
3277
3337
|
function levenshtein(a, b) {
|