Skip to content

EZ Web Audio / createSampler

Function: createSampler()

createSampler(urls, opts?): Promise<Sampler>

Defined in: index.ts:317

Create a Sampler for round-robin playback of multiple sounds.

Sampler holds multiple Sound instances and cycles through them on each play, providing natural variation and preventing the "machine gun" effect of identical sounds played rapidly.

Parameters

urls

string[]

Array of audio file URLs to load as sound sources

opts?

SamplerOptions

Optional Sampler configuration

Returns

Promise<Sampler>

Promise resolving to a Sampler instance

Throws

If any audio file cannot be loaded or decoded

Example

typescript
import { createSampler } from 'ez-web-audio'

// Create a sampler with multiple gunshot variations
const gunshot = await createSampler([
  'shot1.mp3', 'shot2.mp3', 'shot3.mp3'
])

// Each play uses the next sound in rotation
gunshot.play() // shot1
gunshot.play() // shot2
gunshot.play() // shot3
gunshot.play() // shot1 (wraps around)