ez-web-audio
Version:
Making the Web Audio API super EZ since 2024.
125 lines • 3.45 kB
TypeScript
/**
* Definition of a single sprite within the audio file.
*/
export interface SpriteDefinition {
/** Start time in seconds */
start: number;
/** End time in seconds */
end: number;
/** Whether to loop (default: false) */
loop?: boolean;
}
/**
* Manifest describing all sprites in an audio file.
* Compatible with audiosprite JSON format.
*/
export interface SpriteManifest {
/** Optional: alternative audio formats */
resources?: string[];
/** Map of sprite names to their definitions */
spritemap: Record<string, SpriteDefinition>;
}
/**
* Options for playing a specific sprite.
*/
export interface SpritePlayOptions {
/** Gain level 0-1 (default: 1) */
gain?: number;
/** Pan position -1 to 1 (default: 0) */
pan?: number;
}
/**
* 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
* ```typescript
* 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']
* ```
*/
export declare class AudioSprite {
private audioContext;
private audioBuffer;
private manifest;
constructor(audioContext: AudioContext, audioBuffer: AudioBuffer, manifest: SpriteManifest);
/**
* List of available sprite names defined in the manifest.
*
* @example
* ```typescript
* sprite.names.forEach(name => console.log(name))
* ```
*/
get names(): string[];
/**
* Check if a sprite with the given name exists.
*
* @param name - The sprite name to check
* @returns true if the sprite exists
*
* @example
* ```typescript
* if (sprite.has('laser')) {
* sprite.play('laser')
* }
* ```
*/
has(name: string): boolean;
/**
* Get the duration of a sprite in seconds.
*
* @param name - The sprite name
* @returns Duration in seconds
* @throws Error if sprite name not found
*
* @example
* ```typescript
* const duration = sprite.getDuration('explosion')
* console.log(`Explosion lasts ${duration} seconds`)
* ```
*/
getDuration(name: string): number;
/**
* 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.
*
* @param name - The sprite name to play
* @param options - Optional gain (0-1) and pan (-1 to 1) settings
* @throws Error if sprite name not found
*
* @example
* ```typescript
* // 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')
* ```
*/
play(name: string, options?: SpritePlayOptions): void;
}
//# sourceMappingURL=sprite.d.ts.map