ts-audio
Version:
`ts-audio` is an agnostic and easy-to-use library to work with the `AudioContext` API and create Playlists.
139 lines (138 loc) • 4.82 kB
TypeScript
/**
* Configuration options for creating an Audio instance.
*/
type AudioProp = {
/** Path or URL to the audio file */
file: string;
/** Initial volume level (0 to 1) */
volume?: number;
/** Time in seconds to start playback */
time?: number;
/** Whether to start playing automatically */
autoPlay?: boolean;
/** Whether to loop the audio */
loop?: boolean;
/** Whether to preload the audio file */
preload?: boolean;
};
/**
* Valid event types that can be emitted by the Audio instance.
*/
type AudioEvent = 'ready' | 'start' | 'state' | 'end';
/**
* Audio player class that provides control over a single audio file.
* Implements the AudioType interface for managing audio playback, volume, and events.
*/
export declare class AudioClass {
/** @private Path or URL to the audio file */
private _file;
/** @private Initial volume level set during construction */
private _initialVolume;
/** @private Initial time in seconds to start playback */
private _initialTime;
/** @private Flag indicating if audio should play automatically */
private _autoPlay;
/** @private Initial loop state set during construction */
private _initialLoop;
/** @private Web Audio API context */
private _audioCtx;
/** @private Internal state management object */
private _states;
/** @private Event emitter for handling audio events */
private _emitter;
/** @private Event handler for managing event subscriptions */
private _eventHandler;
/**
* Creates an instance of Audio player.
*
* @param {AudioProp} config - The audio configuration object
* @param {string} config.file - Path or URL to the audio file
* @param {number} [config.volume=1] - Initial volume level (0 to 1)
* @param {number} [config.time=0] - Time in seconds to start playback
* @param {boolean} [config.autoPlay=false] - Whether to start playing automatically
* @param {boolean} [config.loop=false] - Whether to loop the audio
* @param {boolean} [config.preload=false] - Whether to preload the audio file
*/
constructor({ file, volume, time, autoPlay, loop, preload, }: AudioProp);
/**
* Fetches and decodes the audio buffer for the given source node.
* @private
* @param {AudioBufferSourceNode} source - The audio source node to load buffer into
*/
private curryGetBuffer;
/**
* Starts or resumes audio playback.
* If playback hasn't started, initializes audio source and begins playback.
* If playback was paused, resumes from the current position.
*/
play(): void;
/**
* Pauses audio playback by suspending the audio context.
*/
pause(): void;
/**
* Toggles between play and pause states.
*/
toggle(): void;
/**
* Stops audio playback completely.
* Different from pause as it resets the playback position.
*/
stop(): void;
/**
* Subscribes to audio events.
* @param {AudioEvent} eventType - Type of event to listen for
* @param {Function} callback - Function to call when event occurs
*/
on(eventType: AudioEvent, callback: <T>(param: {
[data: string]: T;
}) => void): void;
/**
* Gets the current volume level.
* @returns {number} Current volume value between 0 and 1
*/
get volume(): number;
/**
* Sets the audio volume level.
* @param {number} newVolume - New volume value between 0 and 1
*/
set volume(newVolume: number);
/**
* Gets the current loop state.
* @returns {boolean} Whether audio is set to loop
*/
get loop(): boolean;
/**
* Sets the loop state.
* @param {boolean} newLoop - Whether audio should loop
*/
set loop(newLoop: boolean);
/**
* Gets the current state of the audio context.
* @returns {AudioContextState} Current state of the audio context
*/
get state(): AudioContextState;
/**
* Gets the current AudioContext instance.
* @returns {AudioContext} The current AudioContext instance
*/
get audioCtx(): AudioContext;
/**
* Gets the total duration of the loaded audio in seconds.
* @returns {number} The duration of the audio if available; otherwise, returns 0.
*/
get duration(): number;
/**
* Gets the current playback position in seconds.
* @returns {number} The current playback position if available; otherwise, returns 0.
*/
get currentTime(): number;
}
/**
* Factory function to create a new Audio instance.
*
* @param {AudioPropType} props - The audio configuration properties
* @returns {AudioType} A new Audio instance
*/
declare const _default: (props: AudioProp) => AudioClass;
export default _default;