File size: 11,227 Bytes
2baec46 f623d99 4304694 f623d99 4304694 f623d99 4304694 f623d99 4304694 f623d99 4304694 f623d99 4304694 f623d99 4304694 f623d99 4304694 f623d99 2baec46 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | ---
tags:
- ml-intern
---
# π΅ lyric-sync
**Automatic perfect song lyric acquisition and synchronization.**
Produces word-level synchronized lyrics with sub-10ms precision from any audio file.
## Pipeline Architecture
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β lyric-sync Pipeline β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ β
β β Input β β Demucs β β WhisperX β β Output β β
β β Audio βββββΆβ Vocals βββββΆβTranscribeβββββΆβ Synced β β
β β (mix) β βSeparationβ β + Timing β β Lyrics β β
β ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ β
β β β² β² β
β β β β β
β βΌ β β β
β ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ β
β βAcoustID βββββΆβ Fetch β βAlign ASR β β Refine β β
β β Identify β βReference βββββΆβto Lyrics βββββΆβ Onsets/ β β
β β Song β β Lyrics β β(transfer β β Offsets β β
β ββββββββββββ ββββββββββββ β timings) β ββββββββββββ β
β β ββββββββββββ β
β βΌ (fallback) β
β ββββββββββββ β
β βTranscriptβ β
β β Search β β
β ββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
## Steps in Detail
### 1. Song Identification
- **Primary**: Audio fingerprinting via Chromaprint/fpcalc β AcoustID lookup β MusicBrainz metadata
- **Fallback**: Transcribe vocals β search lyrics databases (LRCLIB, Genius) for matching text
### 2. Vocal Stem Separation (Demucs)
- Uses `htdemucs_ft` (best available: ~9.2 dB SDR on MUSDB18-HQ)
- Produces clean vocal track, dramatically improving downstream ASR accuracy
- Per [arxiv:2506.15514](https://arxiv.org/abs/2506.15514): Demucs + Whisper achieves ~20% WER on singing
### 3. Word-Level Transcription (WhisperX)
- **WhisperX** (recommended): Whisper large-v2 transcription + wav2vec2 forced phoneme alignment
- Decoupled approach is robust to timing drift on stretched/sung syllables
- Alternative backends: Whisper (transformers pipeline), Granite Speech 4.1
### 4. Reference Lyrics Acquisition
- **LRCLIB** (free, no auth): Community-maintained LRC database with synced timestamps
- **syncedlyrics** (multi-source): Aggregates Lrclib + NetEase + Musixmatch + Megalobiz
- **Genius** (fallback): Plain text lyrics, requires API key
### 5. Sequence Alignment (ASR β Reference)
- Maps imperfect ASR output onto correct reference lyrics text
- Uses `difflib.SequenceMatcher` (LCS-based global alignment)
- Handles: exact matches (direct transfer), substitutions (interpolation), gaps
- Optional fuzzy pre-pass for phonetic ASR errors ("gonna" β "going to")
- Handles repeated sections (chorus/verse) via sectional alignment
### 6. Timing Refinement (Audio Analysis)
- **Onset detection**: Spectral flux + librosa ODF β snap word starts to actual sound onsets
- **Energy envelope**: RMS decay β find precise word endings
- **Silence gaps**: Detect inter-word pauses β refine boundaries
- **Backtracking**: Snaps to the energy trough preceding each onset (true word start)
- Result: sub-10ms precision (5.8ms frame resolution at 44100Hz, hop=256)
## Installation
```bash
# Core (separation + refinement)
pip install lyric-sync
# With WhisperX transcription (recommended)
pip install lyric-sync[whisperx]
# With song identification
pip install lyric-sync[identify]
# Everything
pip install lyric-sync[all]
# System dependency: chromaprint (for AcoustID fingerprinting)
# Ubuntu/Debian:
sudo apt-get install chromaprint-tools ffmpeg
# macOS:
brew install chromaprint ffmpeg
```
## Usage
### CLI
```bash
# Full automatic (identify + fetch lyrics + sync)
lyric-sync song.mp3 --acoustid-key YOUR_KEY -v
# With known metadata (faster, skips fingerprinting)
lyric-sync song.mp3 --artist "Radiohead" --title "Creep" -o synced.lrc
# JSON output for apps
lyric-sync song.mp3 --artist "Queen" --title "Bohemian Rhapsody" --format json
# ASS karaoke subtitles
lyric-sync song.mp3 --artist "Artist" --title "Song" --format ass -o karaoke.ass
# CPU-only processing (slower but no GPU needed)
lyric-sync song.mp3 --device cpu --artist "Artist" --title "Song"
```
### Python API
```python
from lyric_sync import LyricSyncPipeline
# Initialize
pipeline = LyricSyncPipeline(
acoustid_key="YOUR_ACOUSTID_KEY", # optional
device="cuda", # or "cpu"
)
# Full auto
result = pipeline.sync("song.mp3")
# With known metadata
result = pipeline.sync(
"song.mp3",
artist="Radiohead",
title="Creep",
)
# Access results
print(result.song) # SongIdentification(title=..., artist=...)
print(result.quality_score) # 0.85 (0-1 quality estimate)
# Export
print(result.to_lrc()) # Enhanced LRC with word-level timestamps
print(result.to_json()) # JSON array of {word, start, end, confidence}
print(result.to_srt()) # SRT subtitles
print(result.to_ass()) # ASS karaoke with \k tags
```
### Step-by-Step (Advanced)
```python
from lyric_sync.separate import VocalSeparator
from lyric_sync.transcribe import transcribe_vocals
from lyric_sync.lyrics import fetch_lyrics
from lyric_sync.align import align_words
from lyric_sync.refine import refine_timings
# 1. Separate vocals
separator = VocalSeparator(device="cuda")
vocals_16k, sr = separator.extract_vocals("song.mp3", target_sr=16000)
vocals_full, sr_full = separator.extract_vocals_full_rate("song.mp3")
# 2. Transcribe
transcript = transcribe_vocals(vocals_16k, sr=sr, backend="whisperx")
# 3. Fetch lyrics
lyrics = fetch_lyrics(artist="Radiohead", title="Creep")
# 4. Align
aligned_words, stats = align_words(
asr_words=transcript.words,
ref_words=lyrics.words,
)
# 5. Refine
refined_words = refine_timings(vocals_full, sr_full, aligned_words)
```
## Output Formats
| Format | Description | Use Case |
|--------|-------------|----------|
| `lrc` (enhanced) | `[MM:SS.cc] <MM:SS.cc> word ...` | Music players with word-level sync |
| `lrc_standard` | `[MM:SS.cc] Line of text` | Standard music players |
| `json` | `[{"word": ..., "start": ..., "end": ...}]` | Apps, programmatic use |
| `srt` | Standard SRT subtitles | Video players |
| `ass` | ASS with `\kf` karaoke tags | Karaoke / video editing |
## Configuration
### Environment Variables
| Variable | Description |
|----------|-------------|
| `ACOUSTID_API_KEY` | AcoustID API key (free, register at acoustid.org) |
| `GENIUS_TOKEN` | Genius API token (free, for plain lyrics fallback) |
### Hardware Requirements
| Component | GPU (CUDA) | CPU |
|-----------|-----------|-----|
| Demucs (htdemucs_ft) | ~4-6 GB VRAM | ~8 GB RAM, slower |
| WhisperX (large-v2) | ~5-6 GB VRAM | ~8 GB RAM, much slower |
| **Total** | **~10-12 GB VRAM** | **~16 GB RAM** |
| Processing time (4min song) | ~30-60s | ~5-10 min |
### Transcription Backends
| Backend | Quality (singing) | Speed | Dependencies |
|---------|----------|-------|--------------|
| **WhisperX** β | Best (phoneme alignment) | Fast (batched) | `whisperx` |
| Whisper (pipeline) | Good (attention-based) | Fast | `transformers` |
| Granite Speech | Unknown (speech-trained) | Medium | `transformers` |
## How It Works (Technical)
### Alignment Algorithm
The core challenge: ASR makes errors on singing (WER ~15-25%), but we need timestamps
on the *correct* lyrics. We solve this with sequence alignment:
1. **Normalize** both word sequences (lowercase, strip punctuation, expand contractions)
2. **Fuzzy pre-pass**: Map phonetically similar ASR words to their reference equivalents
3. **SequenceMatcher**: Compute optimal global alignment (LCS-based, O(nΒ²))
4. **Transfer**: For `equal` blocks β direct timestamp copy. For `replace` β linear interpolation
5. **Gap-fill**: Interpolate from surrounding anchors for missed words
### Onset Detection for Refinement
After alignment gives ~20-50ms accuracy, we refine to ~5-10ms using:
1. **Fused ODF**: Spectral flux (catches plosives: p/b/t/k) + librosa onset_strength (catches vowels)
2. **Backtrack**: Each onset is snapped to the preceding energy trough (true attack point)
3. **RMS decay**: Word ends are found where energy drops below threshold
4. **Silence gaps**: Inter-word pauses provide definitive boundary anchors
## References
- **WhisperX**: [arxiv:2303.00747](https://arxiv.org/abs/2303.00747) β Forced phoneme alignment
- **HTDemucs**: [arxiv:2211.08553](https://arxiv.org/abs/2211.08553) β Hybrid Transformer source separation
- **ALT Benchmark**: [arxiv:2506.15514](https://arxiv.org/abs/2506.15514) β Demucs+Whisper for lyrics
- **Granite Speech**: [arxiv:2604.22817](https://arxiv.org/abs/2604.22817) β In-Sync timestamp training
- **LRCLIB**: [lrclib.net](https://lrclib.net) β Community synced lyrics database
- **AcoustID**: [acoustid.org](https://acoustid.org) β Open audio fingerprint database
## License
MIT
<!-- ml-intern-provenance -->
## Generated by ML Intern
This model repository was generated by [ML Intern](https://github.com/huggingface/ml-intern), an agent for machine learning research and development on the Hugging Face Hub.
- Try ML Intern: https://smolagents-ml-intern.hf.space
- Source code: https://github.com/huggingface/ml-intern
|