@weasel-js/audio 1.15.1 → 1.16.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 +17 -8
- package/dist/index.js +11 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -228,11 +228,15 @@ interface SoundCache {
|
|
|
228
228
|
* between concurrent loads of it; a failed load is not cached. */
|
|
229
229
|
declare function createSoundCache(ctx: AudioContext, fetchFn?: typeof fetch): SoundCache;
|
|
230
230
|
|
|
231
|
-
/** A
|
|
232
|
-
|
|
231
|
+
/** A world position or direction, in the consumer's own units. `z` is absent
|
|
232
|
+
* in a 2D scene and read as 0. */
|
|
233
|
+
interface SpatialPoint {
|
|
233
234
|
x: number;
|
|
234
235
|
y: number;
|
|
236
|
+
z?: number;
|
|
235
237
|
}
|
|
238
|
+
/** @deprecated Use `SpatialPoint`, which also takes an optional `z`. */
|
|
239
|
+
type Vec2 = SpatialPoint;
|
|
236
240
|
/** Distance and pan model for `spatialize`, in the same units as the
|
|
237
241
|
* positions. */
|
|
238
242
|
interface SpatialOptions {
|
|
@@ -246,15 +250,20 @@ interface SpatialOptions {
|
|
|
246
250
|
rolloff?: 'inverse' | 'linear';
|
|
247
251
|
/** How sharply gain falls. Default 1. */
|
|
248
252
|
rolloffFactor?: number;
|
|
249
|
-
/**
|
|
253
|
+
/** Offset along `right` mapping to full left/right. Default 500. */
|
|
250
254
|
panWidth?: number;
|
|
255
|
+
/** The listener's right, as a direction; its length is ignored. Default
|
|
256
|
+
* `{ x: 1, y: 0, z: 0 }`. Pan is the source's offset along it. */
|
|
257
|
+
right?: SpatialPoint;
|
|
251
258
|
}
|
|
252
259
|
/**
|
|
253
260
|
* Map a source position to a gain and a stereo pan, relative to the listener.
|
|
261
|
+
* Gain falls with the full 3D distance; pan is the offset along the listener's
|
|
262
|
+
* right, over `panWidth`.
|
|
254
263
|
*
|
|
255
264
|
* Pure — no Web Audio, no state. This is the whole spatial model.
|
|
256
265
|
*/
|
|
257
|
-
declare function spatialize(source:
|
|
266
|
+
declare function spatialize(source: SpatialPoint, listener: SpatialPoint, opts?: SpatialOptions): {
|
|
258
267
|
gain: number;
|
|
259
268
|
pan: number;
|
|
260
269
|
};
|
|
@@ -362,7 +371,7 @@ interface PlayOptions {
|
|
|
362
371
|
/** Explicit stereo pan, -1..1. Ignored when `position` is given. */
|
|
363
372
|
pan?: number;
|
|
364
373
|
/** World position; spatialized against the engine listener. */
|
|
365
|
-
position?:
|
|
374
|
+
position?: SpatialPoint;
|
|
366
375
|
/** Engine time in ms (see `engine.now()`). Default: as soon as possible. */
|
|
367
376
|
when?: number;
|
|
368
377
|
/** `stopKey(key)` stops every voice sharing this key. */
|
|
@@ -402,7 +411,7 @@ interface VoiceHandle {
|
|
|
402
411
|
setDetune(cents: number): void;
|
|
403
412
|
/** Explicit stereo pan. Stops the voice tracking a `position`. */
|
|
404
413
|
setPan(value: number): void;
|
|
405
|
-
setPosition(p:
|
|
414
|
+
setPosition(p: SpatialPoint): void;
|
|
406
415
|
isPlaying(): boolean;
|
|
407
416
|
}
|
|
408
417
|
/** One sine partial of an inharmonic voice. */
|
|
@@ -606,7 +615,7 @@ interface AudioEngine {
|
|
|
606
615
|
analyser(busName?: string, opts?: AnalyserTapOptions): AnalyserTap;
|
|
607
616
|
/** A graph feeding `bus`'s input (default: the first bus). Disposed with the engine. */
|
|
608
617
|
graph(bus?: string): AudioGraph;
|
|
609
|
-
setListener(p:
|
|
618
|
+
setListener(p: SpatialPoint, opts?: SpatialOptions): void;
|
|
610
619
|
dispose(): void;
|
|
611
620
|
}
|
|
612
621
|
/**
|
|
@@ -1166,4 +1175,4 @@ declare function reverbNode(ctx: BaseAudioContext, opts?: ReverbNodeOptions): Gr
|
|
|
1166
1175
|
/** A graph node over a compressor insert: port `in`; params `threshold`, `knee`, `ratio`, `attack` (ms), `release` (ms), `makeup`. */
|
|
1167
1176
|
declare function compressorNode(ctx: BaseAudioContext, opts?: CompressorEffectOptions): GraphNodeDef;
|
|
1168
1177
|
|
|
1169
|
-
export { type Acquisition, type AnalyserTap, type AnalyserTapOptions, type AudioEngine, type AudioEngineOptions, type AudioGraph, type AudioGraphOptions, type BiquadCoefficients, type BusGraph, type BusGraphOptions, type BusHandle, type CompressorEffect, type CompressorEffectOptions, type DelayEffect, type DelayEffectOptions, type Envelope, type EnvelopePoint, type FileSourceNodeOptions, type FilterEffect, type FilterEffectOptions, type Glide, type GraphNodeDef, type GraphPatch, type GraphTarget, type ImpulseResponseOptions, type Inharmonic, type InsertAddOptions, type InsertChain, type InsertChainOptions, type InsertEffect, type InsertSlot, type KernelNoise, type KernelNote, type NoiseColor, type NoiseNodeOptions, type NoiseOptions, type NoisePatch, type NoteOptions, type OscillatorNodeOptions, type OscillatorWave, type PatternDetune, type PatternEvent, type PatternHit, type PatternLoadOptions, type PatternNoise, type PatternNote, type PatternPlayer, type PatternPlayerOptions, type Pitch, type PlayOptions, type ResolvedEnvelope, type ReverbEffect, type ReverbEffectOptions, type ReverbNodeOptions, SCORE_BAR_STEPS, type Scheduler, type SchedulerOptions, type Score, type ScoreBarOptions, type ScoreCue, type ScoreCueName, type ScoreCueReading, type ScoreMode, type ScoreMood, type ScoreOptions, type ScoreParams, type ScoreState, type SoundCache, type SoundHandle, type SpatialOptions, type StealPolicy, type StreamOptions, type SynthPartial, type SynthPatch, type TickTimer, type Vec2, type VoiceDropReason, type VoiceFilter, type VoiceHandle, type VoiceKernel, type VoicePool, type VoicePoolOptions, type VoiceRecord, type Waveform, type WetDryMix, biquadCoefficients, biquadResponseDb, compressorNode, createAnalyserTap, createAudioEngine, createAudioGraph, createBusGraph, createCompressorEffect, createDelayEffect, createFileSourceNode, createFilterEffect, createGainNode, createMixNode, createNoiseKernel, createNoiseNode, createNoteKernel, createOscillatorNode, createPanNode, createPatternPlayer, createReverbEffect, createScheduler, createScore, createSoundCache, createTickTimer, createVoicePool, delayNode, envelopeLevel, envelopePoints, filterNode, impulseResponse, insertNode, midiToFrequency, noiseSamples, noteToMidi, partialPresets, patternLength, patternVoicePeaks, readScoreCue, renderNoise, renderNote, resolveEnvelope, reverbNode, scoreBar, scoreChord, scoreCues, scoreModes, scoreParams, spatialize, toFrequency };
|
|
1178
|
+
export { type Acquisition, type AnalyserTap, type AnalyserTapOptions, type AudioEngine, type AudioEngineOptions, type AudioGraph, type AudioGraphOptions, type BiquadCoefficients, type BusGraph, type BusGraphOptions, type BusHandle, type CompressorEffect, type CompressorEffectOptions, type DelayEffect, type DelayEffectOptions, type Envelope, type EnvelopePoint, type FileSourceNodeOptions, type FilterEffect, type FilterEffectOptions, type Glide, type GraphNodeDef, type GraphPatch, type GraphTarget, type ImpulseResponseOptions, type Inharmonic, type InsertAddOptions, type InsertChain, type InsertChainOptions, type InsertEffect, type InsertSlot, type KernelNoise, type KernelNote, type NoiseColor, type NoiseNodeOptions, type NoiseOptions, type NoisePatch, type NoteOptions, type OscillatorNodeOptions, type OscillatorWave, type PatternDetune, type PatternEvent, type PatternHit, type PatternLoadOptions, type PatternNoise, type PatternNote, type PatternPlayer, type PatternPlayerOptions, type Pitch, type PlayOptions, type ResolvedEnvelope, type ReverbEffect, type ReverbEffectOptions, type ReverbNodeOptions, SCORE_BAR_STEPS, type Scheduler, type SchedulerOptions, type Score, type ScoreBarOptions, type ScoreCue, type ScoreCueName, type ScoreCueReading, type ScoreMode, type ScoreMood, type ScoreOptions, type ScoreParams, type ScoreState, type SoundCache, type SoundHandle, type SpatialOptions, type SpatialPoint, type StealPolicy, type StreamOptions, type SynthPartial, type SynthPatch, type TickTimer, type Vec2, type VoiceDropReason, type VoiceFilter, type VoiceHandle, type VoiceKernel, type VoicePool, type VoicePoolOptions, type VoiceRecord, type Waveform, type WetDryMix, biquadCoefficients, biquadResponseDb, compressorNode, createAnalyserTap, createAudioEngine, createAudioGraph, createBusGraph, createCompressorEffect, createDelayEffect, createFileSourceNode, createFilterEffect, createGainNode, createMixNode, createNoiseKernel, createNoiseNode, createNoteKernel, createOscillatorNode, createPanNode, createPatternPlayer, createReverbEffect, createScheduler, createScore, createSoundCache, createTickTimer, createVoicePool, delayNode, envelopeLevel, envelopePoints, filterNode, impulseResponse, insertNode, midiToFrequency, noiseSamples, noteToMidi, partialPresets, patternLength, patternVoicePeaks, readScoreCue, renderNoise, renderNote, resolveEnvelope, reverbNode, scoreBar, scoreChord, scoreCues, scoreModes, scoreParams, spatialize, toFrequency };
|
package/dist/index.js
CHANGED
|
@@ -1883,7 +1883,8 @@ function spatialize(source, listener, opts = {}) {
|
|
|
1883
1883
|
const panWidth = opts.panWidth ?? 500;
|
|
1884
1884
|
const dx = source.x - listener.x;
|
|
1885
1885
|
const dy = source.y - listener.y;
|
|
1886
|
-
const
|
|
1886
|
+
const dz = (source.z ?? 0) - (listener.z ?? 0);
|
|
1887
|
+
const distance = dz === 0 ? Math.hypot(dx, dy) : Math.hypot(dx, dy, dz);
|
|
1887
1888
|
let gain;
|
|
1888
1889
|
if (distance <= refDistance) {
|
|
1889
1890
|
gain = 1;
|
|
@@ -1894,9 +1895,17 @@ function spatialize(source, listener, opts = {}) {
|
|
|
1894
1895
|
gain = refDistance / (refDistance + rolloffFactor * (distance - refDistance));
|
|
1895
1896
|
}
|
|
1896
1897
|
gain = Math.min(1, Math.max(0, gain));
|
|
1897
|
-
const
|
|
1898
|
+
const lateral = lateralOffset(dx, dy, dz, opts.right);
|
|
1899
|
+
const pan = panWidth <= 0 ? 0 : Math.min(1, Math.max(-1, lateral / panWidth));
|
|
1898
1900
|
return { gain, pan };
|
|
1899
1901
|
}
|
|
1902
|
+
function lateralOffset(dx, dy, dz, right) {
|
|
1903
|
+
if (!right) return dx;
|
|
1904
|
+
const rz = right.z ?? 0;
|
|
1905
|
+
const len = Math.hypot(right.x, right.y, rz);
|
|
1906
|
+
if (len === 0) return 0;
|
|
1907
|
+
return (dx * right.x + dy * right.y + dz * rz) / len;
|
|
1908
|
+
}
|
|
1900
1909
|
|
|
1901
1910
|
// src/voices.ts
|
|
1902
1911
|
var dropped = (id) => ({
|