EZ Web Audio / AudioSprite
Class: AudioSprite
Defined in: sprite.ts:60
AudioSprite enables playing segments of a single audio file by name.
Use sprites to bundle multiple short sounds into one file, reducing HTTP requests. Compatible with the audiosprite JSON format.
Example
import { createSprite } from 'ez-web-audio'
const sprite = await createSprite('sounds.mp3', {
spritemap: {
laser: { start: 0, end: 0.3 },
explosion: { start: 1.0, end: 2.5 },
powerup: { start: 3.0, end: 3.5 }
}
})
// Play specific sounds by name
sprite.play('laser')
sprite.play('explosion', { gain: 0.7 })
// Check available sprites
console.log(sprite.names) // ['laser', 'explosion', 'powerup']Constructors
Constructor
new AudioSprite(
audioContext,audioBuffer,manifest):AudioSprite
Defined in: sprite.ts:61
Parameters
audioContext
AudioContext
audioBuffer
AudioBuffer
manifest
Returns
AudioSprite
Accessors
names
Get Signature
get names():
string[]
Defined in: sprite.ts:75
List of available sprite names defined in the manifest.
Example
sprite.names.forEach(name => console.log(name))Returns
string[]
Methods
getDuration()
getDuration(
name):number
Defined in: sprite.ts:109
Get the duration of a sprite in seconds.
Parameters
name
string
The sprite name
Returns
number
Duration in seconds
Throws
Error if sprite name not found
Example
const duration = sprite.getDuration('explosion')
console.log(`Explosion lasts ${duration} seconds`)has()
has(
name):boolean
Defined in: sprite.ts:92
Check if a sprite with the given name exists.
Parameters
name
string
The sprite name to check
Returns
boolean
true if the sprite exists
Example
if (sprite.has('laser')) {
sprite.play('laser')
}play()
play(
name,options):void
Defined in: sprite.ts:141
Play a sprite by name.
Each call creates a new AudioBufferSourceNode, allowing concurrent playback of the same sprite. Use options to control gain and pan.
Parameters
name
string
The sprite name to play
options
SpritePlayOptions = {}
Optional gain (0-1) and pan (-1 to 1) settings
Returns
void
Throws
Error if sprite name not found
Example
// Simple playback
sprite.play('laser')
// With options
sprite.play('explosion', { gain: 0.5, pan: -0.5 })
// Rapid fire (each creates new source)
sprite.play('laser')
sprite.play('laser')
sprite.play('laser')