@leptonai/audio
v0.0.3
Published
The `AudioController` class is responsible for managing audio playback, including decoding, queuing, and playing audio data.
Downloads
2
Readme
Audio Controller
The AudioController
class is responsible for managing audio playback, including decoding, queuing, and playing audio data.
Features
- Decode base64-encoded MPEG audio data
- Queue audio buffers for sequential playback
- Manage playback state
- Emit events on state changes
Installation
npm install @leptonai/audio
Usage
Importing
import { AudioController } from '@leptonai/audio';
Creating an Instance
const audioController = new AudioController();
Enqueueing Audio Data
To enqueue base64-encoded audio data for playback:
const base64Data = '...'; // Your base64-encoded audio data
audioController.enqueue(base64Data).then((audioBuffer) => {
console.log('Audio buffer enqueued:', audioBuffer);
});
Playing Audio
To start playback of the queued audio data:
audioController.play();
Handling State Changes
You can listen for state changes (e.g., when playback starts or stops) using the stateChange
event:
audioController.on('stateChange', (playing) => {
console.log('Playback state changed:', playing ? 'Playing' : 'Stopped');
});
Closing the Audio Controller
To close the audio context and free resources:
audioController.close().then(() => {
console.log('Audio controller closed');
});
Methods
enqueue(base64Data: string): Promise<AudioBuffer>
Decodes the base64-encoded audio data and enqueues it for playback.
base64Data
: The base64-encoded audio data.- Returns a promise that resolves to the decoded
AudioBuffer
.
play(): void
Starts playback of the queued audio data. If already playing or the queue is empty, it does nothing.
close(): Promise<void>
Closes the audio context and frees resources. Returns a promise that resolves when the operation is complete.
Events
stateChange: [boolean]
Emitted when the playback state changes.
playing
: A boolean indicating whether audio is currently playing.
License
This project is licensed under the MIT License.