Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lyrix provider #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ Make sure all packages are updated / installed by entering `yarn install` after.

## Lyrics

Currently (experimentally) fetches lyrics using [musixmatch.com](https://musixmatch.com/)'s internal API and uses them to schedule each lyric in-sync with the song.
This feature is currently in development and I will **not** provide support on retrieving an internal API. (See .env.example on how to setup)
~~Currently (experimentally) fetches lyrics using [musixmatch.com](https://musixmatch.com/)'s internal API and uses them to schedule each lyric in-sync with the song.
This feature is currently in development and I will **not** provide support on retrieving an internal API. (See .env.example on how to setup)~~

Using a custom token can still be used but will now also automatically fetch lyrics from [lyrix.vercel.app](https://github.com/BlueCatSoftware/Lyrix). No need to configure anything!

## Screenshots

Expand Down
2 changes: 1 addition & 1 deletion src/app/cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import config from "./config";
import { log } from "./logger";
import { Subtitle } from "./mxm";
import { Subtitle } from "./lyrics/types";
import { storage } from ".";

interface SubtitleCache {
Expand Down
2 changes: 2 additions & 0 deletions src/app/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const setupConfiguration = () => {
MXM_SIGNATURE: z.string().optional(),
MXM_COOKIE: z.string().optional(),

LYRIX_SERVER: z.string().default("https://lyrix.vercel.app"),

OSC_TARGET_ADDRESS: z.string().optional().default("localhost"),
OSC_TARGET_PORT: z.number().optional().default(9000),

Expand Down
6 changes: 5 additions & 1 deletion src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { create as createStorage } from "node-persist";
import SpotifyWebApi from "spotify-web-api-node";
import config from "./config";
import { log } from "./logger";
import { queueLyrics } from "./mxm";
import { queueLyrix } from "./lyrics/lyrix";
import { queueLyrics } from "./lyrics/mxm";

let server: Server | undefined;
const oscClient = new Client(config.OSC_TARGET_ADDRESS, config.OSC_TARGET_PORT);
Expand Down Expand Up @@ -129,6 +130,9 @@ const startListening = async (
) {
currentLyrics.forEach((lyricTimer) => clearTimeout(lyricTimer));
currentLyrics = await queueLyrics(progress_ms, item, oscClient);
} else if (config.LYRIX_SERVER && progress_ms) {
currentLyrics.forEach((lyricTimer) => clearTimeout(lyricTimer));
currentLyrics = await queueLyrix(progress_ms, item, oscClient);
}
currentSong = item.id;
oscClient.send(
Expand Down
79 changes: 79 additions & 0 deletions src/app/lyrics/lyrix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import axios from "axios";
import { Client, Message } from "node-osc";
import { loadSubtitles, saveSubtitles } from "../cache";
import config from "../config";
import { log } from "../logger";
import { LyrixSubtitle, Subtitle } from "./types";

const pullLyrics = async (song: SpotifyApi.TrackObjectFull) => {
const response = await axios.get(
`${config.LYRIX_SERVER}/getLyrics/${song.id}`,
{ validateStatus: () => true }
);

const { status } = response;
if (status != 200) {
log(`Error: Could not retrieve lyrics: ${status}.`);
return undefined;
}

const lines = response.data.lyrics?.lines as LyrixSubtitle[] | undefined;

if (!lines) return undefined;

return lines.map(
(line) =>
({
text: line.words,
time: {
total: +line.startTimeMs / 1000,
},
} as Subtitle)
);
};

const queueLyrix = async (
playbackProgress: number,
song: SpotifyApi.TrackObjectFull,
oscClient: Client
) => {
const queuedLyrics: NodeJS.Timeout[] = [];
const retrievedLyrics = await getLyrics(song);

if (!retrievedLyrics) return [];

retrievedLyrics.forEach((lyric) => {
const lyricTime = lyric.time.total * 1000;
if (lyricTime <= playbackProgress) return [];

queuedLyrics.push(
setTimeout(() => {
log(`>> ${lyric.text}`, false);
oscClient.send(
new Message(
"/chatbox/input",
lyric.text ? `(♪) ${lyric.text}` : "",
true,
false
)
);
}, lyricTime - playbackProgress)
);
});

log(`Queued ${retrievedLyrics.length} lyrics.`);
return queuedLyrics;
};

const getLyrics = async (song: SpotifyApi.TrackObjectFull) => {
let retrievedLyrics = await loadSubtitles(song);

if (!retrievedLyrics) {
log(`No local lyrics found for ${song.name}. Pulling from lyrix...`);
retrievedLyrics = (await pullLyrics(song)) || [];
await saveSubtitles(song, retrievedLyrics);
}
return retrievedLyrics;
};

export { pullLyrics, queueLyrix };
20 changes: 5 additions & 15 deletions src/app/mxm.ts → src/app/lyrics/mxm.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import axios from "axios";
import { Client, Message } from "node-osc";
import { loadSubtitles, saveSubtitles } from "./cache";
import config from "./config";
import { log } from "./logger";

interface Subtitle {
text: string;
time: {
total: number;
minutes: number;
seconds: number;
hundredths: number;
};
}
import { loadSubtitles, saveSubtitles } from "../cache";
import config from "../config";
import { log } from "../logger";
import { Subtitle } from "./types";

const pullLyrics = async (song: SpotifyApi.TrackObjectFull) => {
const response = await axios.get(
Expand Down Expand Up @@ -92,7 +83,6 @@ const queueLyrics = async (
queuedLyrics.push(
setTimeout(() => {
log(`>> ${lyric.text}`, false);
//console.log(`>> ${lyric.text}`);
oscClient.send(
new Message(
"/chatbox/input",
Expand Down Expand Up @@ -120,4 +110,4 @@ const getLyrics = async (song: SpotifyApi.TrackObjectFull) => {
return retrievedLyrics;
};

export { pullLyrics, queueLyrics, getLyrics, Subtitle };
export { pullLyrics, queueLyrics, getLyrics };
14 changes: 14 additions & 0 deletions src/app/lyrics/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface Subtitle {
text: string;
time: {
total: number;
minutes?: number;
seconds?: number;
hundredths?: number;
};
}

export interface LyrixSubtitle {
startTimeMs: string;
words: string;
}