Files
stemengine_old/backend/services_root_backup/lrc_service.py
T
2026-03-03 14:52:19 +09:00

848 lines
30 KiB
Python
Executable File

"""
LRC alignment service for creating enhanced ELRC files with word-level timestamps.
Uses the lyrics-sync module for vocal extraction and forced alignment.
"""
import os
import re
import sys
import time
import tempfile
from pathlib import Path
from typing import Optional, List, Tuple
from .. import job_manager as jm
from ..config import JOBS_DIR, LRC_SYNC_DIR
from .metadata_util import (
extract_lyrics_from_file,
lyrics_to_plaintext,
format_elrc,
is_lrc_synced
)
# Add lyrics-sync to path
_LRC_SYNC_PATH = Path(__file__).parent.parent.parent / "lyrics-sync"
if str(_LRC_SYNC_PATH) not in sys.path:
sys.path.insert(0, str(_LRC_SYNC_PATH))
class _LyricsSyncNoExtract:
"""
Drop-in replacement for LyricsSync that skips Demucs vocal extraction.
Use this when the input audio is already a separated vocals stem (e.g. from
a project's Stems folder), so we avoid loading and running htdemucs again.
The alignment pipeline is otherwise identical to the standard LyricsSync.
"""
def __init__(self, lang: str = "en-US", blank_id: int = 0, device: str = None) -> None:
from lsync.phoneme_recognizer import PhonemeRecognizer
from lsync.lyrics_processor import LyricsProcessor
self.phone_rec = PhonemeRecognizer(lang=lang, device=device)
self.lp = LyricsProcessor(lang=lang)
self.blank_id = blank_id
def sync(self, audio_fn: str, text_fn: str, save: bool = True, timing_precision: str = 'word'):
import os
import librosa
from lsync.config import TARGET_SR
from lsync.alignment import Aligner
from lsync.util import read_text, save_lrc, save_words
from lsync.lrc_formatter import LrcFormatter
audio_name = os.path.splitext(os.path.basename(audio_fn))[0]
# Load vocals directly (skip Demucs), resample to TARGET_SR
vocals, sr = librosa.load(audio_fn, sr=None, mono=True)
if sr != TARGET_SR:
vocals = librosa.resample(vocals, orig_sr=sr, target_sr=TARGET_SR)
# Rest of alignment pipeline is unchanged
emission, _, _, frame_duration = self.phone_rec.recognize(vocals)
tokens = self.phone_rec.tokenize(text_fn)
path = Aligner.align(emission, tokens, blank_id=self.blank_id)
processed_lyrics = self.lp.process(text_fn)
# Always compute word-level for the basic LRC output
words = self.lp.get_words_from_path(processed_lyrics, path, frame_duration)
original_lyrics = read_text(text_fn)
lrc = LrcFormatter.words2lrc(words, original_lyrics)
if save:
save_words(words, audio_name)
save_lrc(lrc, audio_name)
# Return syllable tokens for ELRC when requested
if timing_precision == 'syllable':
return_words = self.lp.get_syllables_from_path(processed_lyrics, path, frame_duration)
else:
return_words = words
return return_words, lrc
# ── Lyrics cleaning helpers ───────────────────────────────────────────────────
_SECTION_MARKER_RE = re.compile(r'^\[(?!\d{1,2}:\d{2})[^\]]*\]\s*$')
def clean_lyrics_text(text: str) -> str:
"""Remove section markers (e.g. [Chorus], [Verse 1]) and blank lines from plain lyrics."""
cleaned = []
for line in text.splitlines():
stripped = line.strip()
if not stripped:
continue
if _SECTION_MARKER_RE.match(stripped):
continue
cleaned.append(stripped)
return '\n'.join(cleaned)
_LRC_SECTION_LINE_RE = re.compile(
r'^\[\d{1,2}:\d{2}[.\d]*\]\s*\[(?!\d)[^\]]*\]\s*$'
)
def clean_lrc_content(content: str) -> tuple:
"""Remove section-marker lines from LRC/ELRC content.
A section-marker line looks like ``[01:30.00][Chorus]`` — a timestamp
followed by a non-timestamp bracket label and nothing else.
Returns ``(cleaned_str, lines_removed_count)``.
"""
lines = content.splitlines()
cleaned, removed = [], 0
for line in lines:
if _LRC_SECTION_LINE_RE.match(line):
removed += 1
else:
cleaned.append(line)
return '\n'.join(cleaned), removed
def run_lrc_alignment(
job_id: str,
audio_path: str,
lyrics_source: str = "auto", # 'auto', 'tag', 'file', 'text'
lyrics_content: Optional[str] = None,
lyrics_file_path: Optional[str] = None,
output_filename: Optional[str] = None,
style_options: Optional[dict] = None,
is_vocals: bool = False,
output_dir: Optional[str] = None,
timing_precision: str = 'word', # 'word' | 'syllable'
lrc_power_preset: str = 'standard', # 'eco' | 'standard' | 'performance'
):
"""
Background task: align lyrics with vocals to create ELRC file.
Args:
job_id: Job identifier
audio_path: Path to the audio file (with vocals)
lyrics_source: Where to get lyrics from
- 'auto': Try metadata tags, then external LRC, then fail
- 'tag': Only use metadata tags
- 'file': Use provided lyrics_file_path
- 'text': Use provided lyrics_content
lyrics_content: Raw lyrics text (when lyrics_source='text')
lyrics_file_path: Path to LRC/txt file (when lyrics_source='file')
output_filename: Optional custom filename for output ELRC
is_vocals: When True, the audio_path is already a separated vocals stem —
skip the internal Demucs extraction step for faster processing.
output_dir: Optional custom output directory (defaults to LRC_SYNC_DIR / job_id)
"""
if output_dir:
out_path = Path(output_dir)
else:
out_path = LRC_SYNC_DIR / job_id
out_path.mkdir(parents=True, exist_ok=True)
try:
# Import lyrics-sync here to handle any import issues gracefully
try:
from lsync import LyricsSync
except ImportError as e:
jm.update_job(
job_id,
status=jm.JobStatus.ERROR,
error=f"Lyrics sync module not available: {e}",
progress="Error."
)
return
# Step 1: Get lyrics
jm.update_job(job_id, status=jm.JobStatus.RUNNING, progress="Reading lyrics...")
lyrics_text = ""
lyrics_synced = False
if lyrics_source == "text" and lyrics_content:
lyrics_text = lyrics_content
lyrics_synced = is_lrc_synced(lyrics_content)
elif lyrics_source == "file" and lyrics_file_path:
lyrics_data = extract_lyrics_from_file(lyrics_file_path)
lyrics_text = lyrics_data["plain"]
lyrics_synced = lyrics_data["synced"]
elif lyrics_source in ("auto", "tag"):
lyrics_data = extract_lyrics_from_file(audio_path)
lyrics_text = lyrics_data["plain"]
lyrics_synced = lyrics_data["synced"]
# If no lyrics in tags, try external LRC file
if not lyrics_text and lyrics_source == "auto":
audio_dir = Path(audio_path).parent
audio_stem = Path(audio_path).stem
for ext in ['.lrc', '.LRC', '.txt', '.TXT']:
lrc_file = audio_dir / f"{audio_stem}{ext}"
if lrc_file.exists():
lyrics_data = extract_lyrics_from_file(str(lrc_file))
lyrics_text = lyrics_data["plain"]
lyrics_synced = lyrics_data["synced"]
break
if not lyrics_text:
jm.update_job(
job_id,
status=jm.JobStatus.ERROR,
error="No lyrics found. Please provide lyrics text or ensure audio file has lyrics tags.",
progress="Error."
)
return
# Strip section markers ([Chorus], [Verse 1], etc.) and blank lines
if not lyrics_synced:
lyrics_text = clean_lyrics_text(lyrics_text)
# Choose sync class: skip Demucs when audio is already vocals.
# In eco mode, force CPU for the phoneme recogniser to avoid GPU contention
# that causes display glitches on shared-GPU (WSL2) setups.
_align_device = "cpu" if lrc_power_preset == "eco" else None
SyncClass = _LyricsSyncNoExtract if is_vocals else LyricsSync
# If lyrics are already synced (LRC format), convert to ELRC with word-level timestamps
if lyrics_synced:
jm.update_job(job_id, progress="Lyrics already synced. Enhancing to word-level...")
_enhance_existing_lrc(
job_id, audio_path, lyrics_text, out_path,
output_filename, SyncClass, style_options, timing_precision,
align_device=_align_device,
)
else:
# Run full alignment pipeline
jm.update_job(job_id, progress="Starting lyrics alignment...")
_run_full_alignment(
job_id, audio_path, lyrics_text, out_path,
output_filename, SyncClass, style_options, timing_precision,
align_device=_align_device,
)
except Exception as exc:
jm.update_job(job_id, status=jm.JobStatus.ERROR, error=str(exc), progress="Error.")
def _run_full_alignment(
job_id: str,
audio_path: str,
lyrics_text: str,
output_dir: Path,
output_filename: Optional[str],
LyricsSyncClass,
style_options: Optional[dict] = None,
timing_precision: str = 'word',
align_device: Optional[str] = None,
):
"""Run full alignment using lyrics-sync."""
# Write lyrics to temp file
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False, encoding='utf-8') as f:
f.write(lyrics_text)
lyrics_temp_path = f.name
try:
# Initialize lyrics sync
is_no_extract = LyricsSyncClass is _LyricsSyncNoExtract
device_note = f" [{align_device.upper()}]" if align_device else ""
jm.update_job(job_id, progress=f"Initializing alignment model{device_note}...")
if is_no_extract:
lsync = LyricsSyncClass(lang="en-US", device=align_device)
else:
lsync = LyricsSyncClass(lang="en-US")
if jm.should_cancel(job_id):
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
return
# Run alignment
prec_label = "syllable-level" if timing_precision == 'syllable' else "word-level"
if LyricsSyncClass is _LyricsSyncNoExtract:
prog_msg = f"Aligning lyrics ({prec_label})..."
else:
prog_msg = f"Extracting vocals and aligning lyrics ({prec_label})... (this may take a while)"
jm.update_job(job_id, progress=prog_msg)
words, lrc = lsync.sync(audio_path, lyrics_temp_path, save=False, timing_precision=timing_precision)
if jm.should_cancel(job_id):
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
return
# Convert to ELRC format
elrc_content = _convert_to_elrc(words, lyrics_text, timing_precision)
# Save ELRC file
if not output_filename:
output_filename = f"{Path(audio_path).stem}.elrc"
elif not output_filename.endswith('.elrc'):
output_filename += '.elrc'
output_path = output_dir / output_filename
output_path.write_text(elrc_content, encoding='utf-8')
# Also save plain LRC version
lrc_filename = output_filename.replace('.elrc', '.lrc')
lrc_path = output_dir / lrc_filename
lrc_path.write_text(lrc, encoding='utf-8')
files = [
{"name": output_path.name, "path": str(output_path), "type": "elrc"},
{"name": lrc_path.name, "path": str(lrc_path), "type": "lrc"},
]
# Generate styled HTML preview if style options provided
if style_options:
jm.update_job(job_id, progress="Generating styled preview...")
html_content = _generate_styled_html(elrc_content, style_options, Path(audio_path).stem)
html_filename = output_filename.replace('.elrc', '.html')
html_path = output_dir / html_filename
html_path.write_text(html_content, encoding='utf-8')
files.append({"name": html_path.name, "path": str(html_path), "type": "html"})
jm.update_job(
job_id,
status=jm.JobStatus.DONE,
progress="ELRC alignment complete.",
files=files
)
finally:
# Cleanup temp file
try:
os.unlink(lyrics_temp_path)
except:
pass
def _enhance_existing_lrc(
job_id: str,
audio_path: str,
lrc_content: str,
output_dir: Path,
output_filename: Optional[str],
LyricsSyncClass,
style_options: Optional[dict] = None,
timing_precision: str = 'word',
align_device: Optional[str] = None,
):
"""Enhance existing LRC with word-level timestamps."""
from .metadata_util import parse_lrc
# Parse existing LRC
lines = parse_lrc(lrc_content)
# Get plain lyrics text
plain_lyrics = lyrics_to_plaintext(lrc_content)
# Run alignment
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False, encoding='utf-8') as f:
f.write(plain_lyrics)
lyrics_temp_path = f.name
try:
if LyricsSyncClass is _LyricsSyncNoExtract:
lsync = LyricsSyncClass(lang="en-US", device=align_device)
else:
lsync = LyricsSyncClass(lang="en-US")
prec_label = "syllable-level" if timing_precision == 'syllable' else "word-level"
if LyricsSyncClass is _LyricsSyncNoExtract:
prog_msg = f"Aligning words ({prec_label})..."
else:
prog_msg = f"Extracting vocals and aligning words ({prec_label})..."
jm.update_job(job_id, progress=prog_msg)
words, lrc = lsync.sync(audio_path, lyrics_temp_path, save=False, timing_precision=timing_precision)
if jm.should_cancel(job_id):
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
return
# Convert to ELRC
elrc_content = _convert_to_elrc(words, plain_lyrics, timing_precision)
# Save files
if not output_filename:
output_filename = f"{Path(audio_path).stem}.elrc"
elif not output_filename.endswith('.elrc'):
output_filename += '.elrc'
output_path = output_dir / output_filename
output_path.write_text(elrc_content, encoding='utf-8')
lrc_filename = output_filename.replace('.elrc', '.lrc')
lrc_path = output_dir / lrc_filename
lrc_path.write_text(lrc, encoding='utf-8')
files = [
{"name": output_path.name, "path": str(output_path), "type": "elrc"},
{"name": lrc_path.name, "path": str(lrc_path), "type": "lrc"},
]
# Generate styled HTML preview if style options provided
if style_options:
jm.update_job(job_id, progress="Generating styled preview...")
html_content = _generate_styled_html(elrc_content, style_options, Path(audio_path).stem)
html_filename = output_filename.replace('.elrc', '.html')
html_path = output_dir / html_filename
html_path.write_text(html_content, encoding='utf-8')
files.append({"name": html_path.name, "path": str(html_path), "type": "html"})
jm.update_job(
job_id,
status=jm.JobStatus.DONE,
progress="ELRC enhancement complete.",
files=files
)
finally:
try:
os.unlink(lyrics_temp_path)
except:
pass
def _fmt_ts(seconds: float) -> str:
"""Format a time value as an ELRC inline timestamp <mm:ss.xx>."""
m = int(seconds // 60)
s = int(seconds % 60)
cs = int((seconds % 1) * 100)
return f"<{m:02d}:{s:02d}.{cs:02d}>"
def _line_ts(seconds: float) -> str:
"""Format a time value as an ELRC line timestamp [mm:ss.xx]."""
m = int(seconds // 60)
s = int(seconds % 60)
cs = int((seconds % 1) * 100)
return f"[{m:02d}:{s:02d}.{cs:02d}]"
def _convert_to_elrc(words, original_lyrics: str, timing_precision: str = 'word') -> str:
"""
Convert aligned words/syllables to ELRC format.
Word mode → ``[line_ts]<ts>Hello <ts>world``
Syllable mode → ``[line_ts]<ts>Hel<ts>lo <ts>world``
(no space between intra-word syllables; space between words)
Args:
words: List of Word objects (label, start, end, is_word_end)
original_lyrics: Original lyrics text for line structure
timing_precision: 'word' or 'syllable'
Returns:
ELRC formatted string
"""
original_lines = [line.strip() for line in original_lyrics.split('\n') if line.strip()]
elrc_lines = []
word_idx = 0
for line_text in original_lines:
if word_idx >= len(words):
break
orig_words = line_text.split()
if not orig_words:
continue
if timing_precision == 'syllable':
# Collect all syllables for this line by consuming until is_word_end
# for each original word
line_syls = []
for _ in orig_words:
while word_idx < len(words):
syl = words[word_idx]
line_syls.append(syl)
word_idx += 1
if getattr(syl, 'is_word_end', False):
break
if not line_syls:
continue
line_start = line_syls[0].start
line_content = ""
for i, syl in enumerate(line_syls):
# Space before a syllable only when the previous one ended a word
prefix = " " if i > 0 and getattr(line_syls[i - 1], 'is_word_end', True) else ""
line_content += f"{prefix}{_fmt_ts(syl.start)}{syl.label}"
else:
# Word mode: consume one Word per original word
line_aligned = []
for i in range(len(orig_words)):
if word_idx + i < len(words):
w = words[word_idx + i]
line_aligned.append(w)
if not line_aligned:
continue
line_start = line_aligned[0].start
line_content = " ".join(f"{_fmt_ts(w.start)}{w.label}" for w in line_aligned)
word_idx += len(orig_words)
elrc_lines.append(f"{_line_ts(line_start)}{line_content}")
return '\n'.join(elrc_lines)
def _generate_styled_html(elrc_content: str, style_options: dict, title: str) -> str:
"""
Generate an HTML file with styled lyrics preview.
Args:
elrc_content: ELRC formatted lyrics content
style_options: Dictionary with style configuration
title: Song title for the HTML page
Returns:
HTML content as string
"""
from .metadata_util import parse_elrc
# Extract style options with defaults
font_family = style_options.get('fontFamily', 'system-ui')
font_size = style_options.get('fontSize', 24)
text_color = style_options.get('textColor', '#e2e8f0')
highlight_color = style_options.get('highlightColor', '#3b82f6')
background_color = style_options.get('backgroundColor', '#0f172a')
word_highlight_color = style_options.get('wordHighlightColor', '#60a5fa')
line_spacing = style_options.get('lineSpacing', 1.5)
text_align = style_options.get('textAlign', 'center')
# Parse ELRC content
lines = parse_elrc(elrc_content)
# Build HTML lines
html_lines = []
for line_ts, line_text, words in lines:
if words:
# Build line with word spans for animation
word_spans = []
for word_ts, word in words:
minutes = int(word_ts // 60)
seconds = int(word_ts % 60)
millis = int((word_ts % 1) * 1000)
time_attr = f"{minutes:02d}:{seconds:02d}.{millis:03d}"
word_spans.append(
f'<span class="word" data-time="{time_attr}">{word}</span>'
)
line_html = ' '.join(word_spans)
else:
line_html = line_text
minutes = int(line_ts // 60)
seconds = int(line_ts % 60)
millis = int((line_ts % 1) * 1000)
line_time = f"{minutes:02d}:{seconds:02d}.{millis:03d}"
html_lines.append(
f'<div class="lyric-line" data-time="{line_time}">{line_html}</div>'
)
lyrics_html = '\n'.join(html_lines)
html_template = f'''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title} - Lyrics</title>
<style>
* {{
margin: 0;
padding: 0;
box-sizing: border-box;
}}
body {{
font-family: {font_family};
background-color: {background_color};
color: {text_color};
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 2rem 1rem;
}}
.container {{
max-width: 800px;
width: 100%;
}}
h1 {{
font-size: {font_size * 1.5}px;
margin-bottom: 2rem;
text-align: {text_align};
color: {text_color};
}}
.lyrics-container {{
display: flex;
flex-direction: column;
gap: {font_size * line_spacing * 0.5}px;
}}
.lyric-line {{
font-size: {font_size}px;
line-height: {line_spacing};
text-align: {text_align};
color: {text_color};
opacity: 0.6;
transition: all 0.3s ease;
padding: 0.5rem;
border-radius: 0.5rem;
}}
.lyric-line.active {{
color: {highlight_color};
opacity: 1;
transform: scale(1.02);
}}
.word {{
display: inline-block;
transition: color 0.2s ease;
padding: 0 0.1em;
}}
.word.active {{
color: {word_highlight_color};
text-shadow: 0 0 10px {word_highlight_color}40;
}}
.controls {{
position: fixed;
bottom: 2rem;
left: 50%;
transform: translateX(-50%);
background: rgba(0, 0, 0, 0.8);
padding: 1rem 2rem;
border-radius: 2rem;
display: flex;
gap: 1rem;
align-items: center;
backdrop-filter: blur(10px);
}}
button {{
background: {highlight_color};
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: 0.5rem;
cursor: pointer;
font-size: 14px;
transition: opacity 0.2s;
}}
button:hover {{
opacity: 0.8;
}}
input[type="range"] {{
width: 200px;
}}
.time-display {{
font-family: monospace;
font-size: 14px;
color: {text_color};
}}
</style>
</head>
<body>
<div class="container">
<h1>{title}</h1>
<div class="lyrics-container">
{lyrics_html}
</div>
</div>
<div class="controls">
<button id="playBtn">▶ Play</button>
<input type="range" id="progress" min="0" max="100" value="0">
<span class="time-display" id="timeDisplay">00:00</span>
</div>
<script>
const lines = document.querySelectorAll('.lyric-line');
const words = document.querySelectorAll('.word');
const playBtn = document.getElementById('playBtn');
const progress = document.getElementById('progress');
const timeDisplay = document.getElementById('timeDisplay');
let isPlaying = false;
let currentTime = 0;
// Derive duration from the last timestamp in the ELRC content
const allTimestamps = Array.from(document.querySelectorAll('[data-start]'))
.map(el => parseFloat(el.getAttribute('data-start')))
.filter(t => !isNaN(t));
let duration = allTimestamps.length > 0 ? Math.ceil(Math.max(...allTimestamps) + 10) : 300;
let animationId = null;
// Parse time string to seconds
function parseTime(timeStr) {{
const [mins, secs] = timeStr.split(':');
return parseInt(mins) * 60 + parseFloat(secs);
}}
// Format seconds to time string
function formatTime(seconds) {{
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${{mins.toString().padStart(2, '0')}}:${{secs.toString().padStart(2, '0')}}`;
}}
// Track last-active elements to avoid redundant DOM updates
let _activeLine = null;
let _activeWord = null;
// Update active line/word based on current time; scroll active line into view
function updateActiveElements() {{
// Find the last line whose timestamp <= currentTime
let newLine = null;
lines.forEach(line => {{
if (currentTime >= parseTime(line.dataset.time)) newLine = line;
}});
if (newLine !== _activeLine) {{
if (_activeLine) _activeLine.classList.remove('active');
if (newLine) {{
newLine.classList.add('active');
const ctrl = document.querySelector('.controls');
const ctrlH = ctrl ? ctrl.offsetHeight + 32 : 80;
const rect = newLine.getBoundingClientRect();
const lineMid = rect.top + rect.height / 2;
const availH = window.innerHeight - ctrlH;
const target = window.scrollY + lineMid - availH / 2;
window.scrollTo({{ top: Math.max(0, target), behavior: 'smooth' }});
}}
_activeLine = newLine;
}}
// Find the last word whose timestamp <= currentTime
let newWord = null;
words.forEach(word => {{
if (currentTime >= parseTime(word.dataset.time)) newWord = word;
}});
if (newWord !== _activeWord) {{
if (_activeWord) _activeWord.classList.remove('active');
if (newWord) newWord.classList.add('active');
_activeWord = newWord;
}}
}}
// Animation loop — use performance.now() delta for accurate wall-clock timing
let _lastTs = null;
function animate(ts) {{
if (isPlaying) {{
if (_lastTs !== null) currentTime += (ts - _lastTs) / 1000;
_lastTs = ts;
if (currentTime > duration) {{
currentTime = 0;
isPlaying = false;
playBtn.textContent = '▶ Play';
_lastTs = null;
}}
progress.value = (currentTime / duration) * 100;
timeDisplay.textContent = formatTime(currentTime);
updateActiveElements();
}} else {{
_lastTs = null;
}}
animationId = requestAnimationFrame(animate);
}}
// Play/Pause toggle
playBtn.addEventListener('click', () => {{
isPlaying = !isPlaying;
playBtn.textContent = isPlaying ? '⏸ Pause' : '▶ Play';
}});
// Progress bar interaction
progress.addEventListener('input', (e) => {{
currentTime = (e.target.value / 100) * duration;
timeDisplay.textContent = formatTime(currentTime);
updateActiveElements();
}});
// Start animation loop
animate();
</script>
</body>
</html>'''
return html_template
def extract_vocals_for_alignment(
job_id: str,
audio_path: str,
output_dir: Path
) -> str:
"""
Extract vocals from audio file for alignment purposes.
Returns path to extracted vocal file.
"""
jm.update_job(job_id, progress="Extracting vocals...")
try:
import torch
from demucs.pretrained import get_model
from demucs.apply import apply_model
from demucs.separate import load_track
device = "cuda" if torch.cuda.is_available() else "cpu"
model = get_model(name="htdemucs", repo=None)
model.to(device)
model.eval()
vocals_idx = model.sources.index("vocals")
# Load audio
wav = load_track(audio_path, 2, model.samplerate)
# Separate
ref = wav.mean(0)
wav = (wav - ref.mean()) / ref.std()
with torch.no_grad():
sources = apply_model(
model, wav[None], device=device, shifts=1,
split=True, overlap=0.25, progress=False
)
vocals = sources[0][vocals_idx]
vocals_np = vocals.cpu().numpy()[0, ...]
# Save vocals
import soundfile as sf
vocals_path = output_dir / f"{Path(audio_path).stem}_vocals.wav"
sf.write(vocals_path, vocals_np, model.samplerate)
return str(vocals_path)
except Exception as e:
# If vocal extraction fails, return original audio
jm.update_job(job_id, progress=f"Vocal extraction failed, using original audio: {e}")
return audio_path