UNPKG

audio2d

Version:

Easy to use API to add the power of web audio to your game.

108 lines (107 loc) 3.18 kB
import { Nodes } from './Nodes'; import { AudioClip } from './audio_clip'; import { AudioClipOptions } from './interfaces/AudioClipOptions'; /** * Audio2D is a web audio helper for adding sound/music to your JavaScript games. */ export declare class Audio2D { /** * A reference to the audio context. * * @private * * @property {AudioContext} */ private _ctx; /** * The object that contains all of the audio clips created. * * @private * * @property {Array<AudioClip>} */ private _clips; /** * A reference to the nodes module. * * @private * * @property {Nodes} */ private _nodes; /** * Returns the created audio clips. * * @returns {Array<AudioClip>} */ get clips(): Array<AudioClip>; /** * Returns the nodes module to use for creating nodes and adding them to clips. * * @returns {Nodes} */ get nodes(): Nodes; /** * Adds audio to the media library. * * @param {string} name The name of this audio clip used to reference it. * @param {AudioBuffer} buffer A reference to the audio buffer for this audio. * @param {Object} [options] * @param {Array<Marker>} [options.markers] A breakdown of the audio into individual parts that can be used independently. * @param {string} [options.trigger] An id or classname of a dom element that when clicked will trigger the clip to play. * * @example * * // Adding an audio clip with no markers. * const levelUp = a2d.addAudio('level-up', levelUpBuffer); * * // Adding an audio clip with markers. * const sfxMarkers = [ * { name: 'walk', start: 1500, duration: 1000 }, * { name: 'fall': start: 2500, duration: 1500 }, * ]; * const sfx = a2d.addAudio('sfx', sfxBuffer, { markers: sxfMarkers }); */ addAudio(name: string, audio: AudioBuffer, options?: AudioClipOptions): AudioClip; /** * Gets an audio clip from the media library. * * @param {string} name The name of the audio clip to get. * * @returns {AudioClip|undefined} Returns the audio clip if its found or undefined otherwise. * * @example * * a2d.addAudio('track1', buffer); * * const clip = a2d.getAudio('track1'); */ getAudio(name: string): (AudioClip | undefined); /** * Removes an audio clip from the media library. * * @param {string} name The name of the audio clip to remove. * * @returns {Audio2D} Returns this for chaining. * * @example * * a2d.addAudio('track1', buffer); * * a2d.removeAudio('track1'); */ removeAudio(name: string): Audio2D; /** * Removes all audio clips from the media library. * * @returns {Audio2D} Returns this for chaining. * * @example * * a2d.addAudio('track1', buffer1); * a2d.addAudio('track2', buffer2); * * a2d.removeAllAudio(); */ removeAllAudio(): Audio2D; }