The beat parser uses the audio-decode library for robust audio format support, enabling processing of various compressed and uncompressed audio formats.
- WAV - Uncompressed PCM audio, best quality
- MP3 - MPEG Layer 3, most common format
- FLAC - Free Lossless Audio Codec, high quality compression
- OGG - Ogg Vorbis, open source compression
- OPUS - Modern low-latency codec
| Format | Compression | Quality | Processing Speed |
|---|---|---|---|
| WAV | None | Highest | Fastest |
| FLAC | Lossless | High | Fast |
| OGG | Lossy | Good | Medium |
| MP3 | Lossy | Good | Medium |
| OPUS | Lossy | Good | Medium |
import { BeatParser } from '@beat-parser/core';
const parser = new BeatParser();
// Process different formats
const mp3Result = await parser.parseFile('./music.mp3');
const wavResult = await parser.parseFile('./music.wav');
const flacResult = await parser.parseFile('./music.flac');// Decode audio buffer
const audioBuffer = await fs.readFile('./music.mp3');
const result = await parser.parseBuffer(audioBuffer);// Check supported formats
const formats = BeatParser.getSupportedFormats();
console.log('Supported formats:', formats);
// Output: ['wav', 'mp3', 'flac', 'ogg', 'opus']The library automatically detects audio format from:
- File extension
- Magic bytes in header
- MIME type (for web usage)
Audio File → audio-decode → Float32Array → BeatParser → Results
↓ ↓ ↓ ↓
Binary Decode to Normalized Beat Detection
Format PCM Audio Samples Processing
- Input: Any sample rate supported by format
- Processing: Converted to configured sample rate (default 44.1kHz)
- Output: Beat timestamps in milliseconds
- Stereo: Automatically mixed to mono for processing
- Mono: Used directly
- Multi-channel: Mixed to mono with equal weighting
const parser = new BeatParser({
sampleRate: 44100, // Target sample rate
enablePreprocessing: true, // Enable audio preprocessing
enableNormalization: true // Enable amplitude normalization
});const parseOptions = {
filename: 'music.mp3', // Helps with format detection
progressCallback: (progress) => {
console.log(`Decoding: ${progress}%`);
}
};- WAV - Fastest (no decompression needed)
- FLAC - Fast (efficient lossless decoding)
- OGG/OPUS - Medium (standard lossy decoding)
- MP3 - Medium (standard lossy decoding)
- Compressed formats: ~10x less memory during file loading
- Uncompressed formats: Full audio data loaded to memory
- Streaming: Reduces memory usage for all formats
// For best performance with large files
const result = await parser.parseFile('./large-audio.mp3', {
progressCallback: (progress) => {
// Monitor decoding progress
console.log(`Decoded: ${progress}%`);
}
});try {
const result = await parser.parseFile('./audio.mp3');
} catch (error) {
if (error.message.includes('Unsupported audio format')) {
console.error('Format not supported');
} else if (error.message.includes('Failed to decode audio')) {
console.error('File corrupted or invalid');
} else if (error.message.includes('File not found')) {
console.error('Audio file missing');
}
}// Check if format is supported before processing
const supportedFormats = BeatParser.getSupportedFormats();
const fileExtension = path.extname('./music.mp3').slice(1);
if (!supportedFormats.includes(fileExtension)) {
throw new Error(`Format ${fileExtension} not supported`);
}- Sample Rate: 44.1kHz (CD quality)
- Bit Depth: 16-bit minimum, 24-bit preferred
- Format: WAV or FLAC for best accuracy
- High Quality: WAV/FLAC, 44.1kHz+, slower processing
- Balanced: MP3/OGG, 44.1kHz, good speed and quality
- Fast Processing: Lower sample rates, compressed formats
- WAV/FLAC - Highest accuracy (no compression artifacts)
- OGG/OPUS - Good accuracy (modern compression)
- MP3 - Good accuracy (widely compatible)
- Check file format is supported
- Verify file is not corrupted
- Ensure file has audio content (not empty)
- Try different sample rate settings
- Use higher quality source audio
- Avoid highly compressed MP3s (<128kbps)
- Ensure audio has clear rhythmic content
- Adjust confidence threshold settings
- Use streaming processing for files >2 minutes
- Enable Web Worker processing
- Process files in smaller batches
- Always call cleanup() after processing
// Handle file upload and decode
const handleFileUpload = async (file: File) => {
const arrayBuffer = await file.arrayBuffer();
const audioBuffer = new Uint8Array(arrayBuffer);
const parser = new BeatParser();
try {
const result = await parser.parseBuffer(audioBuffer, {
filename: file.name,
progressCallback: (progress) => {
updateProgressBar(progress);
}
});
return result;
} finally {
await parser.cleanup();
}
};import fs from 'fs/promises';
import path from 'path';
const processAudioDirectory = async (dirPath: string) => {
const files = await fs.readdir(dirPath);
const audioFiles = files.filter(file =>
BeatParser.getSupportedFormats().includes(
path.extname(file).slice(1).toLowerCase()
)
);
const parser = new BeatParser();
const results = [];
try {
for (const file of audioFiles) {
const filePath = path.join(dirPath, file);
const result = await parser.parseFile(filePath);
results.push({ file, ...result });
}
return results;
} finally {
await parser.cleanup();
}
};For complete API documentation, see API.md.