@ra2web/wavefile
Version:
WAV file processing library from ra2web project
129 lines (94 loc) • 3.17 kB
Markdown
# @ra2web/wavefile
A WAV file processing library extracted from the ra2web project. This library provides comprehensive functionality for reading, writing, and manipulating WAV audio files.
## Features
- Read and write WAV files
- Support for various bit depths (8, 16, 24, 32, 64-bit)
- Support for floating-point audio formats (32f, 64)
- Audio compression formats (IMA-ADPCM, A-Law, μ-Law)
- Sample rate conversion
- Bit depth conversion
- Audio format conversion
- Base64 encoding/decoding
- Data URI support
## Installation
```bash
npm install @ra2web/wavefile
```
## Usage
### Basic Usage
```javascript
import { WaveFile } from '@ra2web/wavefile';
// Create a new WaveFile instance
const wav = new WaveFile();
// Load from buffer
const buffer = /* your audio buffer */;
wav.fromBuffer(buffer);
// Access audio properties
console.log('Sample Rate:', wav.fmt.sampleRate);
console.log('Channels:', wav.fmt.numChannels);
console.log('Bit Depth:', wav.bitDepth);
// Get samples
const samples = wav.getSamples();
// Convert to different formats
wav.toBitDepth('16');
wav.toSampleRate(44100);
// Export as buffer
const outputBuffer = wav.toBuffer();
```
### Format Conversion
```javascript
// Convert to different compression formats
wav.toALaw(); // Convert to A-Law
wav.toMuLaw(); // Convert to μ-Law
wav.toIMAADPCM(); // Convert to IMA-ADPCM
// Convert from compressed formats
wav.fromALaw();
wav.fromMuLaw();
wav.fromIMAADPCM();
```
### Base64 and Data URI Support
```javascript
// Convert to Base64
const base64String = wav.toBase64();
// Load from Base64
wav.fromBase64(base64String);
// Convert to Data URI
const dataUri = wav.toDataURI();
// Load from Data URI
wav.fromDataURI(dataUri);
```
## API Reference
### WaveFile Class
#### Constructor
- `new WaveFile(buffer?)` - Create a new WaveFile instance, optionally from a buffer
#### Methods
##### File I/O
- `fromBuffer(buffer)` - Load WAV data from a buffer
- `toBuffer()` - Export WAV data as a buffer
- `fromBase64(base64String)` - Load WAV data from Base64 string
- `toBase64()` - Export WAV data as Base64 string
- `fromDataURI(dataUri)` - Load WAV data from Data URI
- `toDataURI()` - Export WAV data as Data URI
##### Format Conversion
- `toBitDepth(bitDepth, dithered?)` - Convert bit depth
- `toSampleRate(sampleRate, options?)` - Convert sample rate
- `toRIFF()` - Convert to RIFF format
- `toRIFX()` - Convert to RIFX format
##### Compression
- `toALaw()` - Convert to A-Law compression
- `fromALaw(bitDepth?)` - Convert from A-Law compression
- `toMuLaw()` - Convert to μ-Law compression
- `fromMuLaw(bitDepth?)` - Convert from μ-Law compression
- `toIMAADPCM()` - Convert to IMA-ADPCM compression
- `fromIMAADPCM(bitDepth?)` - Convert from IMA-ADPCM compression
##### Audio Data
- `getSamples(interleaved?)` - Get audio samples
#### Properties
- `fmt` - Format chunk data
- `data` - Audio data chunk
- `bitDepth` - Current bit depth
- `container` - Container format (RIFF/RIFX/RF64)
## License
MIT
## Contributing
This library was extracted from the ra2web project. For issues and contributions, please refer to the original project repository.