ez-web-audio
Version:
Making the Web Audio API super EZ since 2024.
103 lines • 2.87 kB
TypeScript
import { Connectable } from './interfaces/connectable';
import { Playable } from './interfaces/playable';
export interface SamplerOptions {
name?: string;
}
/**
* Round-robin playback of multiple sounds.
*
* Sampler holds multiple Sound instances and automatically alternates between them
* on each play() call. This creates realistic variation when playing repeated samples
* (e.g., multiple recordings of the same drum hit).
*
* @example
* ```typescript
* import { createSampler } from 'ez-web-audio'
*
* // Load multiple kick drum samples for variation
* const kick = await createSampler([
* 'kick-1.mp3',
* 'kick-2.mp3',
* 'kick-3.mp3'
* ])
*
* // Each play() uses the next sample in rotation
* kick.play() // plays kick-1
* kick.play() // plays kick-2
* kick.play() // plays kick-3
* kick.play() // back to kick-1
* ```
*/
export declare class Sampler {
constructor(sounds: (Playable & Connectable)[], opts?: SamplerOptions);
/**
* Optional name to aid in identification.
*/
name: string;
/**
* Gain level applied to each sample when played.
* @default 1
*/
gain: number;
/**
* Stereo pan position applied to each sample (-1 = left, 0 = center, 1 = right).
* @default 0
*/
pan: number;
/**
* Iterator over the sounds Set for round-robin cycling.
* @internal
*/
private soundIterator;
/**
* Collection of sounds that are cycled through on each play.
* @internal
*/
protected sounds: Set<Playable & Connectable>;
/**
* Play the next sound in the rotation immediately.
*
* @example
* ```typescript
* sampler.play() // plays sound 1
* sampler.play() // plays sound 2
* sampler.play() // plays sound 3 (then wraps to 1)
* ```
*/
play(): void;
/**
* Play the next sound in the rotation after a delay.
*
* @param seconds - Number of seconds from now to play the sound
*
* @example
* ```typescript
* sampler.playIn(0.5) // plays next sound in 0.5 seconds
* ```
*/
playIn(seconds: number): void;
/**
* Play the next sound at a specific AudioContext time.
*
* @param time - The AudioContext.currentTime value when to play
*
* @example
* ```typescript
* const startTime = audioContext.currentTime + 1
* sampler.playAt(startTime) // plays next sound at exactly startTime
* ```
*/
playAt(time: number): void;
/**
* Get the next sound from the round-robin rotation.
* When the iterator reaches the end, it automatically restarts.
* @internal
*/
private getNextSound;
/**
* Apply the sampler's gain and pan settings to a sound before playing.
* @internal
*/
private setGainAndPan;
}
//# sourceMappingURL=sampler.d.ts.map