77 lines
2.3 KiB
C++
77 lines
2.3 KiB
C++
#pragma once
|
|
|
|
#include "echoion/core/NetworkClient.hpp"
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
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<Line> 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<NetworkClient> 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<Line>
|
|
parseLrc(const std::string& lrc);
|
|
|
|
std::shared_ptr<NetworkClient> net_;
|
|
|
|
struct Entry {
|
|
Lyrics value;
|
|
std::chrono::steady_clock::time_point at;
|
|
};
|
|
mutable std::mutex mutex_;
|
|
std::unordered_map<std::string, Entry> cache_;
|
|
static constexpr std::chrono::hours kCacheTtl{6};
|
|
};
|
|
|
|
} // namespace Echoion::Core
|