music-library
A simple music library, acting as a Local DB as JS Object. Contains everything a person would need to store their music data for website playback.
How to use:
Simple demo using euterpe player here
Recommended to make a db.ts file where one instanciates their database, then exports it for use elsewhere.
db.ts
import { DB, Artist, Song, RefTo, Ref, Platforms } from "@euterpe/music-library";
export const db = new DB
db.add([
new Artist({
name: "Jamie xx",
}),
new Artist({
name: "Machinedrum",
}),
new Artist({
name: "Tanerélle",
}),
new Artist({
name: "Mono/Poly",
}),
new Artist({
name: "IMANU",
links: [
[Platforms.Spotify, new URL("https://open.spotify.com/artist/5Y7rFm0tiJTVDzGLMzz0W1?si=DRaZyugTTIqlBHDkMGKVqA&nd=1")]
]
})])
db.add([
new Song({
artists: [new Ref(RefTo.Artists, 2), new Ref(RefTo.Artists, 3), new Ref(RefTo.Artists, 4)],
duration: 252,
name: "Star",
remix_artists: [new Ref(RefTo.Artists, 5)],
url: new URL("http://127.0.0.1:4200/Machinedrum, Tanerelle & Mono Poly - Star (IMANU Remix) final.mp3")
}),
new Song({
artists: [new Ref(RefTo.Artists, db.artists.find((a) => a.name == "Jamie xx")!.id!)],
duration: 331,
name: "Sleep Sound",
url: new URL("http://127.0.0.1:4200/Jamie xx - Sleep Sound.mp3")
}),
])
And then we can easily get any data we want elsewhere, like:
main.ts
import { db } from "./db";
let curr_song_id = 1;
document.querySelector("#previous")?.addEventListener("click", () => {
curr_song_id--
if (curr_song_id < 0) curr_song_id = 2
music_player.try_new_song_async(db.songs[curr_song_id].url.pathname).then((s) => {
change_current_song_text(db)
music_player.play_async().catch((err) => { console.log(err) })
}, (e) => { console.log(e) })
})
document.querySelector("#next")?.addEventListener("click", () => {
curr_song_id++
if (curr_song_id > 2) curr_song_id = 0
music_player.try_new_song_async(db.songs[curr_song_id].url.pathname).then((s) => {
change_current_song_text(db)
music_player.play_async().catch((err) => { console.log(err) })
}, (e) => { console.log(e) })
})
Example on how to produce final titles:
- If the current song has multiple titles, add them with
,
between, then append " - " and song name. - If the song has remix artists, we add a " (", add all artists with ", " between, and even make them link to artists' links if there are some.
- Results with given db:
-
Machinedrum, Tanerélle, Mono/Poly - Star (<a href="{{spotify link}}">IMANU</a> Remix)
- Jamie xx - Sleep Sound
function change_current_song_text(db: DB) {
const curr_song = db.songs[curr_song_id]
let final_text = ""
for (const artist of curr_song.artists) {
const curr_artist = artist.get(db) as Artist
final_text += curr_artist.name + ", "
}
final_text = final_text.slice(0, final_text.length - 2)
final_text += " - " + curr_song.name
if (curr_song.remix_artists.length > 0) {
final_text += " ("
for (const artist of curr_song.remix_artists) {
const curr_artist = artist.get(db) as Artist
if (curr_artist.links && curr_artist.links.length > 0) {
console.log("found a link! " + Platforms[curr_artist.links[0][0]])
const url = curr_artist.links[0][1]
final_text += `<a href=${url}>${curr_artist.name}</a>, `
} else {
final_text += curr_artist.name + ", "
}
}
final_text = final_text.slice(0, final_text.length - 2)
final_text += " Remix)"
}
elem_curr_song!.innerHTML = final_text
}
What data this database stores right now:
class Song {
name: string
artists: Ref[]
url: URL
duration: number
remix_artists: Ref[]
publish_date?: Date
in_collection?: Ref
cover?: URL
bpm?: number
key?: string
fft_data?: number[]
id?: ID
}
class Artist {
name = ""
pfp?: URL
songs: Ref[]
collections: Ref[]
links?: [Platforms, URL][]
id?: ID
}
class Collection {
artists: Ref[]
songs: Ref[]
cover: URL
duration: number
publish_date?: Date
id?: ID
}