Files
Hatsune-Miku-s-Shoshitsu-in…/modules/lyricResolve.h
T
NanamiAdmin 00b7c0b324 feat: add lyricResolve for resole .lrc file
feat: add backgroundDisplay for play ascii video in terminal
	feat: add musicPlayer for play music in terminal
2026-08-30 11:35:47 +08:00

55 lines
1.8 KiB
C++

// lyricResolve.h
//
// Parse .lrc lyric files and play them back on a timeline in the terminal.
//
// playLyrics()/playLyricsFile() anchor their timeline at the moment they are
// called (t = 0) and print each lyric line at its timestamp. The module is
// self-contained and multibyte-safe: Japanese/Chinese UTF-8 lines are printed
// whole and only ever truncated on a character boundary.
#pragma once
#include <atomic>
#include <cstdint>
#include <string>
#include <vector>
namespace lr {
struct LyricLine {
int64_t timeMs = 0; // from the start of the track
std::string text; // empty => clear the display
};
struct LyricInfo {
std::vector<LyricLine> lines; // sorted ascending by timeMs (stable)
std::string title, artist, album, by;
int offsetMs = 0;
bool valid = false; // false if nothing was parsed
};
// Parse LRC text: "[mm:ss.xx]text", multiple timestamps per line, metadata
// tags ([ti:]/[ar:]/[al:]/[by:]/[offset:]) and timestamp-only lines (empty
// text, which clears the display). The [offset:] value is applied to all lines.
LyricInfo parseLrc(const std::string& text);
// Read a file and parse it. Returns a LyricInfo with valid == false on error.
LyricInfo parseLrcFile(const std::string& path);
struct LyricDisplayOptions {
int row = 0; // 1-based terminal row; 0 = auto (last terminal row)
bool show = true; // false => run the timeline silently (no terminal writes)
};
// Play the lyrics on a timeline starting at t = 0. Blocks until every line has
// been shown (or `stop` is set). Returns 0.
int playLyrics(const LyricInfo& lyric,
const LyricDisplayOptions& opts = {},
std::atomic<bool>* stop = nullptr);
int playLyricsFile(const std::string& path,
const LyricDisplayOptions& opts = {},
std::atomic<bool>* stop = nullptr);
} // namespace lr