Skip to content

EZ Web Audio / createSampler

Function: createSampler() ​

Call Signature ​

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

Defined in: packages/core/src/index.ts:706

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 ​

inputs ​

AudioInput[]

Array of audio file URLs, ArrayBuffers, Blobs, or Files 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'

// From URLs
const gunshot = await createSampler([
  'shot1.mp3', 'shot2.mp3', 'shot3.mp3'
])

// From mixed inputs (URLs, ArrayBuffers, Files)
const snare = await createSampler([snareUrl, snareBuffer, snareFile])

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

Call Signature ​

createSampler(audioContext, inputs, opts?): Promise<Sampler>

Defined in: packages/core/src/index.ts:707

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 ​

audioContext ​

BaseAudioContext

inputs ​

AudioInput[]

Array of audio file URLs, ArrayBuffers, Blobs, or Files 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'

// From URLs
const gunshot = await createSampler([
  'shot1.mp3', 'shot2.mp3', 'shot3.mp3'
])

// From mixed inputs (URLs, ArrayBuffers, Files)
const snare = await createSampler([snareUrl, snareBuffer, snareFile])

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