Duration Calculation
Calculate precise audio duration using the soundfile library for quality assessment and metadata generation in audio curation pipelines.
Overview
The GetAudioDurationStage extracts precise timing information from audio files using the soundfile library. This information is essential for quality filtering, dataset analysis, and ensuring consistent audio lengths for training.
Key Features
- High Precision: Uses
soundfilefor frame-accurate duration calculation - Format Support: Works with all audio formats supported by
soundfile(WAV, FLAC, OGG, and so on) - Error Handling: Returns -1.0 for corrupted or unreadable files
- Pipeline Integration: Designed for use in NeMo Curator processing pipelines
How It Works
The duration calculation stage reads audio samples and sample rate to determine exact duration:
Duration Calculation Process
- File Reading: Uses
soundfileto read audio samples and sample rate - Frame Counting: Counts total audio frames from the loaded samples
- Duration Calculation: Computes duration as
frames ÷ sample_rate - Error Handling: Sets duration to -1.0 for corrupted files
Configuration
Basic Configuration
Custom Field Names
Usage Examples
Basic Duration Calculation
Pipeline Integration
Batch Processing
Output Format
The stage adds duration information to each audio sample’s metadata:
For corrupted or unreadable files:
Error Handling
The stage handles various error conditions:
File Not Found
Corrupted Audio Files
Filtering Error Files
Integration with Quality Assessment
Duration calculation is typically the first step in quality assessment workflows:
Performance Considerations
Memory Usage
- The stage reads audio samples to compute frames
- Memory usage scales with file duration, channels, and data type
- Reduce batch size when processing large files or large batches of files
- For a custom alternative that avoids loading samples, use
soundfile.infoto getframesandsamplerate
Processing Speed
- Duration calculation is I/O bound and scales with file size
- Network-mounted files can be slower than local storage
- Consider parallel processing for large datasets using Ray
File System Optimization
For better performance with large datasets:
- Use local storage when possible
- Ensure sufficient I/O bandwidth
- Consider file system caching