Files
2026-03-03 14:52:19 +09:00

47 lines
1.9 KiB
Python
Executable File

from .alignment import Aligner
from .lyrics_processor import LyricsProcessor
from .util import read_text, save_lrc, save_words, save_audio
from .config import TARGET_SR
from .lrc_formatter import LrcFormatter
import os
class LyricsSync():
def __init__(self, lang="en-US", blank_id=0) -> None:
# Deferred so that merely importing this module doesn't pull in
# transformers/demucs at module load time (those fail loudly if the
# installed transformers version is broken or incompatible).
from .voice_extractor import VoiceExtractor
from .phoneme_recognizer import PhonemeRecognizer
self.ve = VoiceExtractor()
self.phone_rec = PhonemeRecognizer(lang=lang)
self.lp = LyricsProcessor(lang=lang)
self.blank_id = blank_id
def sync(self, audio_fn: str, text_fn: str, save=True, timing_precision: str = 'word'):
audio_name = os.path.splitext(os.path.basename(audio_fn))[0]
vocals = self.ve.extract_voice(audio_fn)
if save:
save_audio(vocals, audio_name, TARGET_SR)
emission, _, _, frame_duration = self.phone_rec.recognize(vocals)
# Preprocessing & tokenization
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