Euterpe.js Player
A simple, safe AudioContext web music player.
How to use
Euterpe player is very Rust inspired, meaning there's always a safer function. play()
,play_async()
and try_play_async()
for example. The goal is to provide the developer with knowledge of what happened when they ran the function, so they can decide what to do if it failed or succeeded.
import { MusicPlayerBuilder } from "@euterpe/player";
const audio_el = document.querySelector("#audio")
const music_player_builder = MusicPlayerBuilder(audio_el)
music_player_builder.start()
const panning_node = music_player_builder.add_stereo_panner_node()
panning_node.pan.value = -0.4
const wave_shaper_node = music_player_builder.add_wave_shaper_node()
waves_shaper_node.oversample = '4x'
const music_player = music_player_builder.build()
music_player.try_new_song_async(encodeURI("my_song.ogg"))
.then(() => {
document.querySelector("#play")?.addEventListener("click", () => {
music_player.play_async()
.then(
() => { console.log("Playing!") },
(e) => alert("Failed to play, " + e)
)
})
})
It's quite easy to give user the control in UI
document.querySelector("#play-button")?.addEventListener("click", () => {
music_player.play_async()
.then(() => { console.log("Playing!") }, (e) => alert("Failed to play, " + e))
})
document.querySelector("#mute")?.addEventListener("click", () => {
music_player.mute()
})
document.querySelector("#volume")?.addEventListener("input", (e) => {
music_player.change_volume(e.target?.valueAsNumber)
})
Euterpe Player also provides functions to easily track the status of playback. It does this via Subscription/Publisher pattern which publishes every frame ( Using requestAnimationFrame()
). This allows for always up todate values reflecting on the UI.
music_player.on_duration_formatted((time) => {
document.querySelector("#duration-text").innerHTML = time
document.querySelector("#input-seek-range").max = "" + music_player.get_current_duration()
})
music_player.on_time_tick_formatted((time) => {
document.querySelector("#current-text").innerHTML = time
})
music_player.on_time_tick((time) => {
document.querySelector("#input-seek-range").value = "" + time
})