Grand Diomande Research · Full HTML Reader

Beat Tracking Cache Optimization

The `build_phrase_database_incremental.py` script includes a **beat tracking cache** that significantly speeds up reprocessing of audio files.

Embodied Trajectory Systems research note experiment writeup candidate score 24 .md

Full Public Reader

Beat Tracking Cache Optimization

Overview

The `build_phrase_database_incremental.py` script includes a beat tracking cache that significantly speeds up reprocessing of audio files.

What's Cached

Beat tracking is the most computationally expensive step in the phrase database building pipeline (~270s per file). The cache stores:
- Beat times
- Downbeat locations
- BPM information

How It Works

.beat_cache/
    ├── track_name_1_1699123456.beats.pkl  # filename + modification time
    ├── track_name_2_1699123457.beats.pkl
    └── track_name_3_1699123458.beats.pkl

### Cache Key Format
`{filename}_{modification_time}.beats.pkl`

  • The cache automatically invalidates when a file is modified
  • Gracefully handles corrupted cache files by recomputing
  • Cache persists across script runs

Performance Impact

StageWithout CacheWith CacheSpeedup
Beat Tracking~270s<0.5s~500x faster
Total per file~280-310s~10-20s~15-30x faster
10 files~50 min~2-4 min~15-25x faster
200 files~16-17 hours~30-60 min~15-30x faster

Usage

Default (Cache Enabled)

bash
cd diffusion

python scripts/build_phrase_database_incremental.py \
    --input_dir [home-path] \
    --output_dir data_output/phrase_db \
    --batch_size 10 \
    --resume

Disable Cache

bash
python scripts/build_phrase_database_incremental.py \
    --input_dir [home-path] \
    --output_dir data_output/phrase_db \
    --batch_size 10 \
    --no-cache \
    --resume

Clear Cache

bash
# Remove the cache directory
rm -rf diffusion/.beat_cache/

When to Disable Cache

Use `--no-cache` when:
- Beat tracking algorithm has changed
- You suspect cached results are incorrect
- Debugging beat tracking issues
- First-time processing (no cache exists yet anyway)

Cache Location

  • Default: `diffusion/.beat_cache/`
  • Automatically created if it doesn't exist
  • Safe to delete (will be recreated as needed)

Implementation Details

Function: `track_beats_with_cache()`

python
def track_beats_with_cache(audio_file: Path, audio: np.ndarray, sr: int,
                           beat_tracker, use_cache: bool = True):
    """
    Track beats with caching support.

    Cache key includes file modification time for automatic invalidation.
    Returns: (beats, downbeats, bpm, from_cache)
    """

### Cache File Format
- Format: Python pickle (`.pkl`)
- Contents: Dictionary with keys:
- `beats`: Array of beat times
- `downbeats`: Array of downbeat locations
- `bpm`: Detected BPM
- `timestamp`: Cache creation time

Best Practices

1. Keep cache enabled (default) for normal usage
2. Clear cache if you update the beat tracking algorithm
3. Monitor cache size - can grow large with many files
4. Cache directory is safe to include in `.gitignore`

Summary

Beat Tracking Cache: 500x speedup for cached files
Automatic invalidation: Updates when files change
Persistent: Survives script restarts
Graceful fallback: Recomputes if cache is corrupted
Easy to disable: Use `--no-cache` flag

The cache provides substantial speedup (15-30x overall) on subsequent runs with minimal complexity.

Promotion Decision

Attach run IDs, datasets, metrics, and reproduction commands.

Source Anchor

Comp-Core/core/ml/cc-ml/diffusion/scripts/BEAT_CACHE_OPTIMIZATION.md

Detected Structure

Method · Evaluation · Code Anchors · Architecture