Display next tracks metadata - ai-radio #4215
-
I am using the ai-radio template from @gAlleb and @toots. I attempted to add the following code, but instead of displaying the upcoming track's metadata, it is showing the variables. I also tried using
next_song,
am trying hard to learn Liquidsoap, and I’m new to this field. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
Hi! The logic is: You need to set the variable first and then populate it with data. When using Liquidsoap with one playlist it's pretty straightforward. Say you have 1 playlist: What you need to do is to add a # We set up an empty ref
next_song = ref("")
# Next we set up check function that will check playlist for the next song and assign a string with data to `next_song` ref.
def check(r)
if request.resolve(r) then
m = request.metadata(r)
next_song := "Next: #{metadata.artist(m)} - #{metadata.title(m)}"
true
else
false
end
end
# Within our playlist operator we then use the above function. So it will check the next track and log it to the ref.
radio = playlist(check_next=check,"./music/music.m3u")
# other stuff ...
# Then we use that ref to show it on screen
## Display for the next track's metadata
background =
video.add_text(
color=0xFCB900,
font=font,
size=rem(1.5),
x=px(234),
y=px(4800),
{next_song()},
background
) That was for def check(r)
m = request.metadata(r)
next := "Next: #{metadata.artist(m)} - #{metadata.title(m)}"
true
end |
Beta Was this translation helpful? Give feedback.
-
Thank you @gAlleb. I want to add more playlists current audio is set up as follows: audio_dir = "/home/ubuntu/ai-radio-main/audio1"
## Audio setup
# Declare the next_song variable to hold the next song's metadata
next_song = ref("")
def check_next(r) =
m = request.metadata(r)
next_song := "#{metadata.title(m)} - #{metadata.artist(m)}"
true
end
radio = playlist(audio_dir, reload_mode="watch", check_next=check_next)
radio = mksafe(radio)
radio = fallback(track_sensitive=true, [radio])
current_title = ref("")
current_artist = ref("")
def update_current_song(m) =
current_title := m["title"]
current_artist := m["artist"]
end
radio.on_metadata(update_current_song)
def position() =
source.elapsed(radio) / source.duration(radio)
end
def remaining() =
time = source.remaining(radio)
seconds = string.of_int(digits=2, int(time mod 60.))
minutes = string.of_int(digits=2, int(time / 60.))
"#{minutes}:#{seconds}"
end Now, I would like to add more playlists and assign custom weights to them. Here's what I tried, but I am confused how to add to the above script. ## Audio setup
# Declare the next_song variable to hold the next song's metadata
next_song = ref("")
def check_next(r) =
m = request.metadata(r)
next_song := "#{metadata.title(m)} - #{metadata.artist(m)}"
true
end
# Define each playlist dynamically by combining the base directory and playlist folder name
playlist1 = playlist(id="playlist1", mode="randomize", reload_mode="watch", "/home/ubuntu/ai-radio-main/audio1/playlist1")
playlist2 = playlist(id="playlist2", mode="randomize", reload_mode="watch", "/home/ubuntu/ai-radio-main/audio1/playlist2")
playlist3 = playlist(id="playlist3", mode="randomize", reload_mode="watch", "/home/ubuntu/ai-radio-main/audio1/playlist3")
playlist4 = playlist(id="playlist4", mode="randomize", reload_mode="watch", "/home/ubuntu/ai-radio-main/audio1/playlist4")
playlist5 = playlist(id="playlist5", mode="randomize", reload_mode="watch", "/home/ubuntu/ai-radio-main/audio1/playlist5")
# Standard Playlists with weighted selection
radio = random(id="standard_playlists", weights=[5, 3, 3, 3, 3], [playlist1, playlist2, playlist3, playlist4, playlist5])
radio = mksafe(radio)
radio = fallback(track_sensitive=true, [radio])
current_title = ref("")
current_artist = ref("")
def update_current_song(m) =
current_title := m["title"]
current_artist := m["artist"]
end
radio.on_metadata(update_current_song)
def position() =
source.elapsed(radio) / source.duration(radio)
end
def remaining() =
time = source.remaining(radio)
seconds = string.of_int(digits=2, int(time mod 60.))
minutes = string.of_int(digits=2, int(time / 60.))
"#{minutes}:#{seconds}"
end Can you please review it? after adding more playlists it was not playing any audio. Thanks 👍. |
Beta Was this translation helpful? Give feedback.
-
@gAlleb, how can I get next_song = ref("")
# Next we set up check function that will check playlist for the next song and assign a string with data to `next_song` ref.
def check_next(r)
if request.resolve(r) then
m = request.metadata(r)
next_song := "#{metadata.title(m)} - #{metadata.artist(m)}"
true
else
false
end
end
radio = playlist(audio_dir, check_next=check_next)
radio = mksafe(radio)
current_title = ref("")
current_albumartist = ref("")
def update_current_song(m) =
current_title := m["title"]
current_artist := m["albumartist"]
end
radio.on_metadata(update_current_song) Thanks. |
Beta Was this translation helpful? Give feedback.
-
Hi @gAlleb, when /play endpoint is called to queue tracks, the check_next (next_song) function was not updating the data which is queued it shows only system queue check_next. ## Audio setup
# Declare the next_song variable to hold the next song's metadata
next_song = ref("")
# Next we set up check function that will check playlist for the next song and assign a string with data to `next_song` ref.
def check_next(r)
if request.resolve(r) then
m = request.metadata(r)
title = m["title"]
album = m["album"]
year = m["year"]
next_song := "#{title} - #{album} (#{year}) "
true
else
false
end
end
radio = playlist(audio_dir, mode="randomize", reload_mode="watch", check_next=check_next)
radio = mksafe(radio)
##Harbor Functions
%include "harbor_functions.liq"
current_title = ref("")
#current_artist = ref("")
current_albumartist = ref("")
def update_current_song(m) =
current_title := m["title"]
# current_artist := m["artist"]
current_albumartist := m["albumartist"]
end
radio.on_metadata(update_current_song)
def position() =
source.elapsed(radio) / source.duration(radio)
end
def remaining() =
time = source.remaining(radio)
seconds = string.of_int(digits=2, int(time mod 60.))
minutes = string.of_int(digits=2, int(time / 60.))
"#{minutes}:#{seconds}"
end Harbor Function # Queue setup for normal playback
queue = request.queue(id="main_queue")
radio = fallback(track_sensitive=false, [queue, radio])
number_queue1 = ref(0)
# Endpoint to play a specific file
def play(request) =
fname = request.query["file"]
log.important("Requested file #{fname}.")
if file.exists(fname) then
log.important("File Request Accepted.")
queue.push.uri("#{fname}")
number_queue1 := number_queue1() + 1
q = queue.queue()
http.response(
content_type="application/json; charset=UTF-8",
status_code=200,
data=json.stringify({
status = "success",
message = "Pushed: #{fname}.",
current_position_in_queue = number_queue1(),
remaining_tracks_in_queue = list.length(q)
})
)
else
log.important("File Request Rejected: File Not Found.")
http.response(
content_type="application/json; charset=UTF-8",
status_code=404,
data=json.stringify({
status = "error",
message = "Invalid file: #{fname}"
})
)
end
end
harbor.http.register.simple("/play", play, port=8007, method="GET") |
Beta Was this translation helpful? Give feedback.
Hi! The logic is:
You need to set the variable first and then populate it with data. When using Liquidsoap with one playlist it's pretty straightforward. Say you have 1 playlist:
radio = playlist("./music.m3u")
What you need to do is to add a
check_next
function to it so it will fetch and log your next song somewhere. In our case it will assign next song value to a var: