open-index/open-npm huggingface dataset:
npm package: @leptonai/audio
Description: The `AudioController` class is responsible for managing audio playback, including decoding, queuing, and playing audio data.
Latest version: 0.0.3

# 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

```sh
npm install @leptonai/audio
```

## Usage

### Importing

```typescript
import { AudioController } from '@leptonai/audio';
```

### Creating an Instance

```typescript
const audioController = new AudioController();
```

### Enqueueing Audio Data

To enqueue base64-encoded audio data for playback:

```typescript
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:

```typescript
audioController.play();
```

### Handling State Changes

You can listen for state changes (e.g., when playback starts or stops) using the `stateChange` event:

```typescript
audioController.on('stateChange', (playing) => {
  console.log('Playback state changed:', playing ? 'Playing' : 'Stopped');
});
```

### Closing the Audio Controller

To close the audio context and free resources:

```typescript
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.