import pygame
import re

# ------------------------------
# CONFIG
# ------------------------------
OGG_FILE = "Enemies.ogg"       # your audio file
LRC_FILE = "Enemies.lrc.HR"    # word-level LRC

# ------------------------------
# LOAD LRC WORDS
# ------------------------------
def load_lrc_words(lrc_file):
    words = []
    pattern = re.compile(r"\[(\d+):(\d+\.\d+)\|(\d+):(\d+\.\d+)\](.+)")
    with open(lrc_file, "r", encoding="utf-8") as f:
        for line in f:
            match = pattern.match(line.strip())
            if match:
                start_min = int(match.group(1))
                start_sec = float(match.group(2))
                end_min = int(match.group(3))
                end_sec = float(match.group(4))
                start_time = start_min * 60 + start_sec
                end_time = end_min * 60 + end_sec
                word = match.group(5).strip()
                if word:
                    words.append((start_time, end_time, word))
    return words

words = load_lrc_words(LRC_FILE)

# ------------------------------
# INITIALIZE AUDIO
# ------------------------------
pygame.mixer.init()
pygame.mixer.music.load(OGG_FILE)
pygame.mixer.music.play()

# ------------------------------
# PRINT WORDS IN REAL TIME
# ------------------------------
word_index = 0
while word_index < len(words):
    # get current position in seconds
    current_time = pygame.mixer.music.get_pos() / 1000.0
    start, end, word = words[word_index]

    if current_time >= start:
        print(word)
        word_index += 1

    # pygame.time.delay(10)  # small sleep to avoid busy loop

# Wait until audio finishes
while pygame.mixer.music.get_busy():
    pygame.time.delay(100)
