Rockbox Technical Forums

Support and General Use => Plugins/Viewers => Topic started by: rokboxyo on September 14, 2018, 09:35:57 PM

Title: Here is a plugin for randomly changing the playlist with a timer(LUA)
Post by: rokboxyo on September 14, 2018, 09:35:57 PM
I wanted to make a plugin for the feature I requested in this thread (http://forums.rockbox.org/index.php/topic,52447.0.html)
aka automatically change the playlist randomly after X minutes have passed.
Because I have a lot of different songs across lots of different playlists and having to select a playlist myself kinda kills the mood, especially while driving.
So I made this LUA plugin because I can't compile the C code right now.

The advantage of using a LUA script is that it can be easily modified to fit other needs

(ATTENTION : LUA plugins might not work with rockbox 3.14, you'll need to download the latest dev release)
Title: Re: Here is a plugin for randomly changing the playlist with a timer(LUA)
Post by: rokboxyo on September 14, 2018, 09:48:52 PM
How it works:
first you need to make a file named "playlist_randomizer_config.txt" located in the plugin dir (/.rockbox/rocks/apps/).

It should look like this
Code: [Select]
/playlists/jingles.m3u8
25
funk.m3u8
rock.m3u8
new_wave.m3u8
(...)




Then just copy this code to a lua file in the plugin app directory and run it, it's pretty straightforward, I think I have explained everything already

Code: [Select]
-- ROCKBOX META SHUFFLER
-- This script shuffles playlist on a timer
-- version 2.5 (december 7th 2018)
-- works with rockbox dev-release 9450689483-181207 and later
-- creator @rockboxyo
-- This program is free software; you can redistribute it and/or  modify it under the terms of the GNU General Public License  as
-- published by the Free Software Foundation; either version 2  of the License, or (at your option) any later version.

-- This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.


require("actions")   
require("rbcompat")

local pl={} -- array of playlist names
local pl_count=-2 -- total number of playlists
local minutes_of_play=40  --number of minutes for each playlist before switch
local trans_pl  -- name of transition playlist
local prev_pl={} -- keeps track of 2 previous playlists to avoid repeating
local prev_switch=0
local seconds_elapsed=0
local ticks_elapsed=0  -- time since new playlist started
local last_tick=0
local current_tick=0
local action -- input  management

function changePlaylist()
local trans_nr=0  -- number of available transitions
local trans_name
local nr=0
rb.lcd_clear_display()

-- first choose random indice not recently played
repeat
nr=math.random(0,pl_count-1)
until nr~=prev_pl[0] and nr~=prev_pl[1]
prev_pl[prev_switch]=nr
prev_switch=1-prev_switch
local playlist_name=pl[nr]

-- set playlist
rb.audio("stop")
rb.playlist("create","/Playlists/",playlist_name)
rb.playlist("shuffle",rb.current_tick(),0)

--add transition if apply
local file = io.open(trans_pl, "r")
if not file then
rb.splash(3*rb.HZ,"transition FILE NOT FOUND !")
else
for line in file:lines() do
trans_nr =trans_nr +1
end

nr=math.random(1,trans_nr)
file:seek("set")
for i=1,nr do
trans_name=file:read("*l")
end
rb.playlist("insert_track",trans_name,0 ,true,-1)
end

--start play
rb.sleep(2*rb.HZ)  -- added this to void buffering problems (?)
rb.playlist("start",0,0,0)
last_tick=rb.current_tick()
current_tick=last_tick
seconds_elapsed=0
ticks_elapsed=0
rb.lcd_clear_display()
rb.splash(2*rb.HZ, "now playing: "..playlist_name)
end


-- MAIN SCRIPT

math.randomseed(rb.current_tick())
rb.lcd_clear_display()
prev_pl[0]=-1
prev_pl[1]=-1

local file = io.open("/.rockbox/rocks/apps/playlist_randomizer_config.txt", "r")
if not file then
rb.splash(3*rb.HZ,"CONFIG FILE NOT FOUND !")
return nil
else

for line in file:lines() do
pl[pl_count]=line
pl_count=pl_count+1
end
if pl_count<=1 then
rb.splash(4*rb.HZ,"ERROR: No playlist found in file!")
return nil
end

trans_pl=pl[-2]
minutes_of_play=tonumber(pl[-1])

file:close()

--if rockbox is currently playing when plugin is started, then this will not change the playlist
if rb.audio("status")==0 then
changePlaylist()
else
rb.splash(3*rb.HZ,"Playlist randomized in "..minutes_of_play.." minutes...")
end

repeat
current_tick=rb.current_tick()
if current_tick>last_tick then
ticks_elapsed=ticks_elapsed+(current_tick-last_tick)
last_tick=current_tick
if ticks_elapsed>=rb.HZ then
ticks_elapsed=ticks_elapsed-rb.HZ
seconds_elapsed=seconds_elapsed+1
if seconds_elapsed>=(60*minutes_of_play) then
changePlaylist()
end
end

end

action = rb.get_action(rb.contexts.CONTEXT_STD, rb.HZ)
if action==rb.actions.ACTION_STD_OK then
changePlaylist()
end
until action == rb.actions.ACTION_STD_CANCEL or action == rb.actions.ACTION_STD_MENU

--rb.audio_stop()
end




-Use the EXIT or BACK button to stop the plugin and the OK button to force a playlist change immediately
-the plugin doesnt stop the music if something is already playing, so you will keep the current playlist until the delay is elapsed, you can still force a change with the OKAY button
-the music keeps playing when you leave the plugin, so if you want to remain on a particular playlist, you can just exit the plugin.

(version 2.5 - 07/12/2018)
Title: Re: Here is a plugin for randomly changing the playlist with a timer(LUA)
Post by: rokboxyo on September 25, 2018, 05:52:15 PM
This plugin's display is currently pretty crude, but that's because the things exposed to LUA are rather limited at the moment.
I hope to be able to display a nice HUD with  song informations, etc as soon as more options get available in rockbox LUA.
Title: Re: Here is a plugin for randomly changing the playlist with a timer(LUA)
Post by: Bilgus on September 26, 2018, 02:19:42 AM
Most of the backend for rliimage is in lua (last few dev versions of RB)  I'll have an update finished in the next week or so that speeds it up a bit but you can check out the sample programs to see how it works

http://forums.rockbox.org/index.php/topic,52352.0.html

Title: Re: Here is a plugin for randomly changing the playlist with a timer(LUA)
Post by: rokboxyo on October 01, 2018, 12:28:22 PM
Most of the backend for rliimage is in lua (last few dev versions of RB)  I'll have an update finished in the next week or so that speeds it up a bit but you can check out the sample programs to see how it works

http://forums.rockbox.org/index.php/topic,52352.0.html

Thanks for your work on LUA. I think scripting will be a big next step for rockbox, it will fill many user needs such as in my case and reduce the requests for new features. It opens a whole world of possibilities for rockbox without having to mess with the source.

Those graphic routines are great, but my problem right now is that I need to retrieve informations on the song that is currently playing ; title, artist, album art, playtime, elapsed time, etc... I don't think this is possible in LUA at the moment. Or is it?
Title: Re: Here is a plugin for randomly changing the playlist with a timer(LUA)
Post by: Bilgus on October 01, 2018, 06:51:54 PM
Oh looking at it I guess it isn't I'll see about adding it with the global settings and status update
Title: Re: Here is a plugin for randomly changing the playlist with a timer(LUA)
Post by: Bilgus on October 11, 2018, 10:37:21 AM
See here for grabbing track info:
http://forums.rockbox.org/index.php/topic,52509.0.html
Title: Re: Here is a plugin for randomly changing the playlist with a timer(LUA)
Post by: rokboxyo on October 14, 2018, 10:59:27 PM
See here for grabbing track info:
http://forums.rockbox.org/index.php/topic,52509.0.html
Oh thanks, I'll test it when I have a chance 8)
Title: Re: Here is a plugin for randomly changing the playlist with a timer(LUA)
Post by: rokboxyo on December 07, 2018, 04:45:47 PM
I made it compatible with the latest LUA changes, I'll make a fancy interface later