ts-audio
Version:
`ts-audio` is an agnostic and easy-to-use library to work with the `AudioContext` API and create Playlists.
115 lines (114 loc) • 3.89 kB
TypeScript
/**
* @fileoverview Audio playlist manager that handles playback of multiple audio files
* with features like shuffle, loop, and weighted random selection.
*/
/**
* Configuration options for initializing an AudioPlaylist instance.
*/
type AudioPlaylistConfig = {
files: string[] | {
[key: string]: number;
};
volume?: number;
loop?: boolean;
shuffle?: boolean;
preload?: boolean;
preloadLimit?: number;
};
/**
* Events that can be emitted by the AudioPlaylist.
*/
type AudioPlaylistEvent = 'start' | 'end';
/**
* Manages audio playlist functionality including playback control, file management,
* and event handling.
*/
declare class AudioPlaylist {
/** Event emitter for handling playlist events */
private emmiter;
/** Global state object containing audio playback states */
private states;
/** Array of audio file paths to be played */
private copiedFiles;
/** Flag indicating if playlist should loop */
private shouldLoop;
/** Curried function for playing audio files */
private curryPlayAudio;
/**
* Creates an instance of AudioPlaylist.
* @param {Object} config - The playlist configuration
* @param {(string[] | Record<string, number>)} config.files - Array of audio files or weighted object
* @param {number} [config.volume=1] - Initial volume level (0-1)
* @param {boolean} [config.loop=false] - Whether to loop the playlist
* @param {boolean} [config.shuffle=false] - Whether to shuffle the playlist
* @param {boolean} [config.preload=false] - Whether to preload audio files
* @param {number} [config.preloadLimit=3] - Number of files to preload
*/
constructor({ files, volume, loop, shuffle, preload, preloadLimit, }: AudioPlaylistConfig);
/**
* Starts or resumes audio playback.
* If no audio is playing or playback was stopped, starts from current position.
*/
play(): void;
/**
* Toggles between play and pause states.
*/
toggle(): void;
/**
* Pauses the current audio playback.
*/
pause(): void;
/**
* Stops the current audio playback and resets the player.
*/
stop(): void;
/**
* Plays the next audio file in the playlist sequence.
* Handles wrapping back to the beginning when reaching the end of the playlist.
*/
next(): void;
/**
* Plays the previous audio file in the playlist sequence.
* Handles wrapping to the end of the playlist when at the beginning.
*/
prev(): void;
/**
* Registers an event listener for playlist events.
* @param {AudioPlaylistEvent} eventType - Type of event to listen for
* @param {Function} callback - Callback function to execute when event occurs
*/
on(eventType: AudioPlaylistEvent, callback: (param: {
[data: string]: unknown;
}) => void): void;
/**
* Gets the current volume level.
* @returns {number} Current volume level (0-1)
*/
get volume(): number;
/**
* Sets the volume level.
* @param {number} newVolume - New volume level (0-1)
*/
set volume(newVolume: number);
/**
* Gets the current loop state.
* @returns {boolean} Whether playlist is set to loop
*/
get loop(): boolean;
/**
* Sets the loop state.
* @param {boolean} newLoop - New loop state
*/
set loop(newLoop: boolean);
/**
* Gets the current AudioContext instance.
* @returns {(AudioContext | undefined)} Current AudioContext or undefined if not initialized
*/
get audioCtx(): AudioContext | undefined;
}
/**
* Factory function to create a new AudioPlaylist instance.
* @param {AudioPlaylistConfig} params - Configuration parameters for the playlist
*/
declare const _default: (params: AudioPlaylistConfig) => AudioPlaylist;
export default _default;