#pragma once #include "echoion/core/NetworkClient.hpp" #include #include #include #include #include #include #include namespace Echoion::Core { // Fetches song lyrics from lrclib.net (free, no auth) for the currently // playing track. Returns both plain lyrics and, when available, LRC // time-synced lines so the web UI can highlight the active line against the // playback clock. Results are cached in-process for a while since lyrics // never change and the same track is asked about repeatedly (chat command, // web panel, reconnects). // // Reachable by chat commands via the service registry and directly by the // RPC handlers. All network calls are synchronous; callers already run off // the audio thread (command thread pool / RPC worker). class LyricsService { public: struct Line { std::int64_t ms = 0; // offset from track start std::string text; }; struct Lyrics { bool found = false; std::string plain; // full plain-text lyrics std::vector synced; // empty when only plain exists std::string source; // "lrclib" when resolved std::string title; // as reported by the provider std::string artist; }; explicit LyricsService(std::shared_ptr net); // Resolve lyrics for a track. `durationSec` == 0 means unknown and // skips the exact-duration lookup (search fallback still runs). [[nodiscard]] Lyrics fetch(const std::string& title, const std::string& artist, const std::string& album, long long durationSec); private: [[nodiscard]] Lyrics fetchUncached(const std::string& title, const std::string& artist, const std::string& album, long long durationSec) const; // Parse an LRC-format string ("[mm:ss.xx] text") into sorted, // timestamped lines, ignoring metadata tags ([ar:], [ti:], …). [[nodiscard]] static std::vector parseLrc(const std::string& lrc); std::shared_ptr net_; struct Entry { Lyrics value; std::chrono::steady_clock::time_point at; }; mutable std::mutex mutex_; std::unordered_map cache_; static constexpr std::chrono::hours kCacheTtl{6}; }; } // namespace Echoion::Core