avr-resampler
Version:
Audio Resampler for Agent Voice Response (AVR) STS or TTS service
116 lines (76 loc) • 4.3 kB
Markdown
# Agent Voice Response - Resampler
[](https://discord.gg/DFTU69Hg74)
[](https://github.com/agentvoiceresponse/avr-resampler)
[](https://www.npmjs.com/package/avr-resampler)
[](https://www.npmjs.com/package/avr-resampler)
[](https://ko-fi.com/agentvoiceresponse)
A TypeScript npm package for audio resampling between different sample rates, specifically designed for AVR (Agent Voice Response) STS or TTS service integration.
## Features
- **Configurable Provider Sample Rate**: Set the provider sample rate via constructor parameter
- **Fixed Client Sample Rate**: Client sample rate is fixed at 8000 Hz for compatibility (Asterisk AudioSocket Module)
- **Bidirectional Resampling**: Handle both upsampling (client → provider) and downsampling (provider → client)
- **Buffered Processing**: Accumulates audio chunks for optimal processing
- **TypeScript Support**: Full TypeScript support with type definitions
## Installation
```bash
npm install avr-resampler
```
## Usage
### Basic Usage
```typescript
import { AudioResampler } from 'avr-resampler';
const customResampler = new AudioResampler(48000); // 48 kHz provider rate
await customResampler.initialize();
const result = customResampler.downsample(audioBuffer);
```
### Constructor Parameters
- `providerSampleRate` (number, optional): The sample rate of the audio provider in Hz. Defaults to 48000 Hz.
### Methods
#### `downsample(data: Buffer): Buffer`
Processes incoming audio chunks from the provider and converts them to client format (8000 Hz).
- **Input**: Raw audio data from provider
- **Output**: Converted audio data if not enough data accumulated
- **Usage**: Call repeatedly with incoming audio chunks
#### `upsample(data: Buffer): Buffer`
Processes incoming audio chunks from the client (8000 Hz) and converts them to provider format.
- **Input**: Raw audio data from client
- **Output**: Converted audio data for provider
- **Usage**: Call with client audio chunks
#### `destroy(): void`
Cleans up resources and resets the resampler state.
#### `initialize(): Promise<void>`
Initializes the audio resamplers. Called automatically when needed.
## Example
```typescript
import { AudioResampler } from 'avr-resampler';
// Create resampler for 48 kHz provider
const resampler = new AudioResampler(48000);
await resampler.initialize();
// Process provider audio → client audio
const providerAudio = Buffer.from([/* your audio data */]);
const clientAudio = resampler.downsample(providerAudio);
if (clientAudio) {
// Send to client
sendToClient(clientAudio);
}
// Process client audio → provider audio
const clientInput = Buffer.from([/* client audio data */]);
const providerInput = await resampler.upsample(clientInput);
// Send to provider
sendToProvider(providerInput);
// Clean up when done
resampler.destroy();
```
## Dependencies
- `@alexanderolsen/libsamplerate-js`: Audio resampling library
## Support & Community
* **GitHub:** [https://github.com/agentvoiceresponse](https://github.com/agentvoiceresponse) - Report issues, contribute code.
* **Discord:** [https://discord.gg/DFTU69Hg74](https://discord.gg/DFTU69Hg74) - Join the community discussion.
* **Docker Hub:** [https://hub.docker.com/u/agentvoiceresponse](https://hub.docker.com/u/agentvoiceresponse) - Find Docker images.
* **NPM:** [https://www.npmjs.com/~agentvoiceresponse](https://www.npmjs.com/~agentvoiceresponse) - Browse our packages.
* **Wiki:** [https://wiki.agentvoiceresponse.com/en/home](https://wiki.agentvoiceresponse.com/en/home) - Project documentation and guides.
## Support AVR
AVR is free and open-source. If you find it valuable, consider supporting its development:
<a href="https://ko-fi.com/agentvoiceresponse" target="_blank"><img src="https://ko-fi.com/img/githubbutton_sm.svg" alt="Support us on Ko-fi"></a>
## License
MIT License - see the [LICENSE](LICENSE.md) file for details.