@schmooky/zvuk 1.9.0 → 1.10.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/dist/index.d.ts CHANGED
@@ -297,11 +297,48 @@ interface StopOptions {
297
297
  interface VoiceJitter {
298
298
  jitter?: number;
299
299
  }
300
+ /**
301
+ * Distance-attenuation curve. Mirrors `PannerNode.distanceModel`:
302
+ * - `'inverse'` (default) — natural-sounding rolloff, matches outdoor sound.
303
+ * - `'linear'` — straight-line attenuation between `refDistance` and `maxDistance`.
304
+ * - `'exponential'` — steeper than inverse; useful for tight environments.
305
+ */
306
+ type DistanceModel = 'linear' | 'inverse' | 'exponential';
300
307
  interface SpatialOptions {
301
308
  /** [-1, 1] stereo pan (2D). Mutually exclusive with `position`. */
302
309
  pan?: number;
303
310
  /** [x, y, z] world-space position (3D). Mutually exclusive with `pan`. */
304
311
  position?: [number, number, number];
312
+ /**
313
+ * Distance below which the source is at full volume. Default 1.
314
+ * 3D only; ignored when `pan` is used.
315
+ */
316
+ refDistance?: number;
317
+ /**
318
+ * Distance beyond which `'linear'` attenuation reaches zero. The
319
+ * `'inverse'` and `'exponential'` models keep falling off but use this
320
+ * as the curve's anchor. Default 10000.
321
+ * 3D only; ignored when `pan` is used.
322
+ */
323
+ maxDistance?: number;
324
+ /**
325
+ * How aggressively distance attenuates the sound. 1 is the natural
326
+ * rolloff for the chosen `distanceModel`; higher values produce a
327
+ * more dramatic falloff. Default 1.
328
+ * 3D only; ignored when `pan` is used.
329
+ */
330
+ rolloffFactor?: number;
331
+ /** Distance-attenuation curve. Default `'inverse'`. 3D only. */
332
+ distanceModel?: DistanceModel;
333
+ /**
334
+ * Occlusion amount (0..1). Drives an internal low-pass filter that
335
+ * sweeps cutoff from 22050 Hz (transparent) down to ~500 Hz (heavily
336
+ * muffled), plus a small gain dip — the standard "behind a wall"
337
+ * effect. Independent of distance attenuation; combine via a
338
+ * `Parameter` if you want one knob to drive both.
339
+ * 3D only; ignored when `pan` is used.
340
+ */
341
+ occlusion?: number;
305
342
  }
306
343
  interface PlayOptions {
307
344
  /** Initial volume (0..1). Default 1. */
@@ -447,18 +484,43 @@ interface PreloadOptions {
447
484
  }
448
485
 
449
486
  /**
450
- * PannerNode wrapper. Inserted between a Voice and its Bus when a play call
451
- * passes a `spatializer` option. 2D pan uses StereoPannerNode (cheap), 3D
452
- * uses PannerNode in HRTF mode (one node per voice — fine for the dozens
453
- * of simultaneous voices a typical game holds, expensive past hundreds).
487
+ * PannerNode + occlusion biquad wrapper. Inserted between a Voice and
488
+ * its Bus when a play call passes a `spatializer` option.
489
+ *
490
+ * - 2D pan uses `StereoPannerNode` (cheap, just left/right shift).
491
+ * - 3D uses `PannerNode` in HRTF mode (one node per voice — fine for
492
+ * the dozens of simultaneous voices a typical game holds, expensive
493
+ * past hundreds), followed by a biquad lowpass for occlusion plus a
494
+ * gain stage so occlusion can also drop level slightly.
495
+ *
496
+ * 3D config (`refDistance`, `maxDistance`, `rolloffFactor`,
497
+ * `distanceModel`) is configurable per voice via `SpatialOptions` and
498
+ * tunable live via the `set*` methods. Occlusion is a single 0..1 knob
499
+ * that drives the lowpass cutoff plus a small gain dip.
454
500
  */
455
501
  declare class Spatializer {
456
- private node;
502
+ private ctx;
457
503
  private kind;
504
+ private input;
505
+ private output;
506
+ private stereoPan;
507
+ private panner;
508
+ private occlusionFilter;
509
+ private occlusionGain;
458
510
  constructor(ctx: AudioContext, opts: SpatialOptions);
459
511
  setPan(pan: number): void;
460
512
  setPosition(x: number, y: number, z: number): void;
461
- /** Connects internal node into `dest` and returns the input the Voice should use. */
513
+ setRefDistance(d: number): void;
514
+ setMaxDistance(d: number): void;
515
+ setRolloffFactor(f: number): void;
516
+ setDistanceModel(m: DistanceModel): void;
517
+ /**
518
+ * Set occlusion amount (0..1). Drives the internal lowpass cutoff
519
+ * and a small gain dip — independent of distance attenuation.
520
+ * No-op on 2D spatializers.
521
+ */
522
+ setOcclusion(amount: number): void;
523
+ /** Connects the spatializer's output into `dest` and returns the input the Voice should use. */
462
524
  connectInto(dest: AudioNode): AudioNode;
463
525
  dispose(): void;
464
526
  private setPanner3D;
@@ -1439,4 +1501,4 @@ type NormalizeFlag = boolean | LoudnessOptions;
1439
1501
  declare function applyLoudnessNormalization(buffer: AudioBuffer, flag: NormalizeFlag): AudioBuffer;
1440
1502
  declare function computeNormalizationGain(buffer: AudioBuffer, opts: Required<LoudnessOptions>): number;
1441
1503
 
1442
- 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, 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, Voice, type VoiceDefaults, type VoiceJitter, ZvukError, applyLoudnessNormalization, canPlay, computeNormalizationGain, createEngine, createStretchWorkletNode, ensureStretchWorklet, mimeForUrl, pickSource, pickSourceOrder };
1504
+ 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, Voice, type VoiceDefaults, type VoiceJitter, ZvukError, applyLoudnessNormalization, canPlay, computeNormalizationGain, createEngine, createStretchWorkletNode, ensureStretchWorklet, mimeForUrl, pickSource, pickSourceOrder };
package/dist/index.js CHANGED
@@ -2471,50 +2471,113 @@ function clamp014(v) {
2471
2471
  }
2472
2472
 
2473
2473
  // src/spatial/spatializer.ts
2474
+ var DEFAULT_REF_DISTANCE = 1;
2475
+ var DEFAULT_MAX_DISTANCE = 1e4;
2476
+ var DEFAULT_ROLLOFF = 1;
2477
+ var DEFAULT_DISTANCE_MODEL = "inverse";
2478
+ var OCCLUSION_OPEN_HZ = 22050;
2479
+ var OCCLUSION_MUFFLED_HZ = 500;
2474
2480
  var Spatializer = class {
2475
- node;
2481
+ ctx;
2476
2482
  kind;
2483
+ input;
2484
+ // what the Voice connects into
2485
+ output;
2486
+ // what we connect to the Bus
2487
+ // 2D-only.
2488
+ stereoPan = null;
2489
+ // 3D-only.
2490
+ panner = null;
2491
+ occlusionFilter = null;
2492
+ occlusionGain = null;
2477
2493
  constructor(ctx, opts) {
2494
+ this.ctx = ctx;
2478
2495
  if (opts.position) {
2479
2496
  const panner = ctx.createPanner();
2480
2497
  panner.panningModel = "HRTF";
2481
- panner.distanceModel = "inverse";
2482
- panner.refDistance = 1;
2483
- panner.maxDistance = 1e3;
2484
- panner.rolloffFactor = 1;
2498
+ panner.distanceModel = opts.distanceModel ?? DEFAULT_DISTANCE_MODEL;
2499
+ panner.refDistance = opts.refDistance ?? DEFAULT_REF_DISTANCE;
2500
+ panner.maxDistance = opts.maxDistance ?? DEFAULT_MAX_DISTANCE;
2501
+ panner.rolloffFactor = opts.rolloffFactor ?? DEFAULT_ROLLOFF;
2485
2502
  const [x, y, z] = opts.position;
2486
2503
  this.setPanner3D(panner, x, y, z);
2487
- this.node = panner;
2504
+ const filter = ctx.createBiquadFilter();
2505
+ filter.type = "lowpass";
2506
+ filter.frequency.value = OCCLUSION_OPEN_HZ;
2507
+ filter.Q.value = 0.707;
2508
+ const gain = ctx.createGain();
2509
+ gain.gain.value = 1;
2510
+ panner.connect(filter).connect(gain);
2511
+ this.panner = panner;
2512
+ this.occlusionFilter = filter;
2513
+ this.occlusionGain = gain;
2514
+ this.input = panner;
2515
+ this.output = gain;
2488
2516
  this.kind = "3d";
2517
+ if (opts.occlusion != null) this.setOcclusion(opts.occlusion);
2489
2518
  } else {
2490
2519
  const sp = ctx.createStereoPanner();
2491
2520
  sp.pan.value = clampPan(opts.pan ?? 0);
2492
- this.node = sp;
2521
+ this.stereoPan = sp;
2522
+ this.input = sp;
2523
+ this.output = sp;
2493
2524
  this.kind = "2d";
2494
2525
  }
2495
2526
  }
2496
2527
  setPan(pan) {
2497
- if (this.kind !== "2d") return;
2498
- this.node.pan.value = clampPan(pan);
2528
+ if (this.kind !== "2d" || !this.stereoPan) return;
2529
+ this.stereoPan.pan.value = clampPan(pan);
2499
2530
  }
2500
2531
  setPosition(x, y, z) {
2501
- if (this.kind !== "3d") return;
2502
- this.setPanner3D(this.node, x, y, z);
2532
+ if (this.kind !== "3d" || !this.panner) return;
2533
+ this.setPanner3D(this.panner, x, y, z);
2534
+ }
2535
+ setRefDistance(d) {
2536
+ if (!this.panner) return;
2537
+ this.panner.refDistance = Math.max(0, d);
2538
+ }
2539
+ setMaxDistance(d) {
2540
+ if (!this.panner) return;
2541
+ this.panner.maxDistance = Math.max(0, d);
2542
+ }
2543
+ setRolloffFactor(f) {
2544
+ if (!this.panner) return;
2545
+ this.panner.rolloffFactor = Math.max(0, f);
2546
+ }
2547
+ setDistanceModel(m) {
2548
+ if (!this.panner) return;
2549
+ this.panner.distanceModel = m;
2503
2550
  }
2504
- /** Connects internal node into `dest` and returns the input the Voice should use. */
2551
+ /**
2552
+ * Set occlusion amount (0..1). Drives the internal lowpass cutoff
2553
+ * and a small gain dip — independent of distance attenuation.
2554
+ * No-op on 2D spatializers.
2555
+ */
2556
+ setOcclusion(amount) {
2557
+ if (!this.occlusionFilter || !this.occlusionGain) return;
2558
+ const a = clamp015(amount);
2559
+ const cutoff = OCCLUSION_OPEN_HZ * (OCCLUSION_MUFFLED_HZ / OCCLUSION_OPEN_HZ) ** a;
2560
+ const now = this.ctx.currentTime;
2561
+ this.occlusionFilter.frequency.setValueAtTime(cutoff, now);
2562
+ const gainLin = 10 ** (-6 * a / 20);
2563
+ this.occlusionGain.gain.setValueAtTime(gainLin, now);
2564
+ }
2565
+ /** Connects the spatializer's output into `dest` and returns the input the Voice should use. */
2505
2566
  connectInto(dest) {
2506
- this.node.connect(dest);
2507
- return this.node;
2567
+ this.output.connect(dest);
2568
+ return this.input;
2508
2569
  }
2509
2570
  dispose() {
2510
2571
  try {
2511
- this.node.disconnect();
2572
+ this.input.disconnect();
2573
+ this.occlusionFilter?.disconnect();
2574
+ this.occlusionGain?.disconnect();
2512
2575
  } catch {
2513
2576
  }
2514
2577
  }
2515
2578
  setPanner3D(p, x, y, z) {
2516
2579
  if (typeof p.positionX?.setValueAtTime === "function") {
2517
- const now = p.context.currentTime;
2580
+ const now = this.ctx.currentTime;
2518
2581
  p.positionX.setValueAtTime(x, now);
2519
2582
  p.positionY.setValueAtTime(y, now);
2520
2583
  p.positionZ.setValueAtTime(z, now);
@@ -2527,6 +2590,10 @@ var Spatializer = class {
2527
2590
  function clampPan(v) {
2528
2591
  return Math.max(-1, Math.min(1, v));
2529
2592
  }
2593
+ function clamp015(v) {
2594
+ if (Number.isNaN(v)) return 0;
2595
+ return Math.max(0, Math.min(1, v));
2596
+ }
2530
2597
 
2531
2598
  // src/mixer/master.ts
2532
2599
  var Master = class {