Skip to content

EZ Web Audio / createGrainPlayer

Function: createGrainPlayer()

Call Signature

createGrainPlayer(buffer, options?): Promise<GrainPlayer>

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

Create a GrainPlayer for granular synthesis from an audio buffer.

GrainPlayer generates continuous texture/pad sounds by scheduling many overlapping short "grains" of audio from a source buffer. Provides independent control over pitch (via playbackRate per grain) and playback position (which region of the buffer to sample from).

Note: Pitch shifting is implemented via playbackRate on each grain. This changes grain duration proportionally, which the overlap system compensates for. Extreme pitch values (beyond +/-24 semitones) may affect texture quality.

Parameters

buffer

AudioBuffer

The AudioBuffer to use as the grain source

options?

GrainPlayerOptions

Optional GrainPlayer configuration

Returns

Promise<GrainPlayer>

Promise resolving to a GrainPlayer instance

Example

typescript
import { createSound, createGrainPlayer } from 'ez-web-audio'

// Load an audio file and create a grain player from its buffer
const sound = await createSound('pad.mp3')
const grains = await createGrainPlayer(sound.audioBuffer, {
  grainSize: 0.1,
  overlap: 0.05,
  pitch: 0,
  jitter: 0.1
})

grains.play()

// Scrub through the buffer
grains.position = 0.5  // middle of buffer

// Pitch shift up a fifth
grains.pitch = 7

// Add effects
const reverb = createReverb({ decay: 3, wet: 0.5 })
grains.addEffect(reverb)

Call Signature

createGrainPlayer(audioContext, buffer, options?): Promise<GrainPlayer>

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

Create a GrainPlayer for granular synthesis from an audio buffer.

GrainPlayer generates continuous texture/pad sounds by scheduling many overlapping short "grains" of audio from a source buffer. Provides independent control over pitch (via playbackRate per grain) and playback position (which region of the buffer to sample from).

Note: Pitch shifting is implemented via playbackRate on each grain. This changes grain duration proportionally, which the overlap system compensates for. Extreme pitch values (beyond +/-24 semitones) may affect texture quality.

Parameters

audioContext

BaseAudioContext

buffer

AudioBuffer

The AudioBuffer to use as the grain source

options?

GrainPlayerOptions

Optional GrainPlayer configuration

Returns

Promise<GrainPlayer>

Promise resolving to a GrainPlayer instance

Example

typescript
import { createSound, createGrainPlayer } from 'ez-web-audio'

// Load an audio file and create a grain player from its buffer
const sound = await createSound('pad.mp3')
const grains = await createGrainPlayer(sound.audioBuffer, {
  grainSize: 0.1,
  overlap: 0.05,
  pitch: 0,
  jitter: 0.1
})

grains.play()

// Scrub through the buffer
grains.position = 0.5  // middle of buffer

// Pitch shift up a fifth
grains.pitch = 7

// Add effects
const reverb = createReverb({ decay: 3, wet: 0.5 })
grains.addEffect(reverb)