Rockbox Technical Forums

Rockbox Development => Starting Development and Compiling => Topic started by: rokboxyo on August 30, 2018, 12:32:16 PM

Title: trying to start a playlist from LUA
Post by: rokboxyo on August 30, 2018, 12:32:16 PM
Hi guys and thank you to whoever is coding/maintaining Rockbox, I've been using it almost daily for years.

Now I need to code a script for my daily planning needs but I stumble on some problems. I chose to use LUA because I don't want to bother with a virtual machine or the main source code, and also for some reason the rock/rockbox password stopped working on the VM I downloaded.

so to make my hand using LUA I adapted this code from the LUA sample in the docs

Code: [Select]
function file_put_contents(filename, mode, contents)
  local file = io.open(filename, mode)
  if not file then
  return false
  end
 
  local ret = file:write(contents) == true
  file:close()
  return ret
end

rb.lcd_clear_display()
file_put_contents("/temp.m3u8","w+","/test.mp3\n")
file_put_contents("/temp.m3u8","a","/test2.mp3\n")
rb.playlist_create("/","temp.m3u8")
rb.sleep(2 * rb.HZ)
rb.playlist_shuffle(rb.current_tick(),-1)
rb.sleep(2 * rb.HZ)
rb.playlist_start(0,0,0)
return 1

I've found out that the playlist.shuffle function doesn't seem to work.
Also the playlist starts playing but the machine gets bricked(freezes) after 28 seconds of music every time.




So, I tried with a normal playlist from the playlist dir:
Code: [Select]
rb.lcd_clear_display()
rb.playlist_create("/Playlists/","MyPlaylist.m3u8")
rb.sleep(2 * rb.HZ)
rb.playlist_shuffle(rb.current_tick(),-1)
rb.sleep(2 * rb.HZ)
rb.playlist_start(0,0,0)
return 1

The playlist is correctly set as myPlaylist, but once again, no shuffle happens, and this time the machine bricks instantly after start.
I suppose there might be some management to do around the playlists, but I don't know what it is, I didn't find examples in the C code.

I have also noticed that I have to restart the device, every time I unplug it from USB or when I've tested the plugin once (can't run the plugin twice without bricking the machine).

So what am I doing wrong?

I'm using a SANSA CLIP ZIP

thanks in advance
Title: Re: trying to start a playlist from LUA
Post by: Bilgus on August 30, 2018, 02:21:51 PM
Currently lua only accesses the current playlist so that might explain your issue if the
playlist wasn't already loaded.
I'll see about fixing that..

playlist_set_current is not exposed to plugins instead preload a playlist and use rb.playlist_remove_all_tracks()
as a workaround if you wish to fill the playlist with your own tracks

As for bricking when running plugin after usb unplug that is probably a buffer freeing issue I'll look into that when I get a chance
Title: Re: trying to start a playlist from LUA
Post by: rokboxyo on August 30, 2018, 10:22:34 PM
Currently lua only accesses the current playlist so that might explain your issue if the
playlist wasn't already loaded.
I'll see about fixing that..

playlist_set_current is not exposed to plugins instead preload a playlist and use rb.playlist_remove_all_tracks()
as a workaround if you wish to fill the playlist with your own tracks

As for bricking when running plugin after usb unplug that is probably a buffer freeing issue I'll look into that when I get a chance

thanks for the quick answer, I'll look into your suggestion.
Is there the same problem for C plugins?

As for the buffers, I remember changing the setting, I'll check that
Title: Re: trying to start a playlist from LUA
Post by: Bilgus on September 01, 2018, 07:40:19 AM
Yeah we don't have it exposed to plugins either which lua is one of
Title: Re: trying to start a playlist from LUA
Post by: rokboxyo on September 05, 2018, 10:57:01 PM
After further testings, it appears that it's simply LUA that is the problem, none of the LUA examples can work while a playlist is playing, the result is always a bricking of the device.  That probably explains why I can't start a playlist from LUA.
All the C plugins OTOH can run fine while the music is playing.

So either I'll code a C plugin or I'll write a LUA script to populate a playlist file and save that file on disk, that I'll manually start afterward.
It might be an acceptable solution for my purpose.
Title: Re: trying to start a playlist from LUA
Post by: Bilgus on September 06, 2018, 01:06:23 AM
what C plugins work that modify/load a playlist and what is the lua script you are trying to use one of the above?
Title: Re: trying to start a playlist from LUA
Post by: Bilgus on September 06, 2018, 01:36:33 AM
What happens if you stop the audio first and actually add tracks?

Code: [Select]
rb.lcd_clear_display()

rb.audio_stop()

rb.playlist_create("/Playlists/","MyPlaylist2.m3u8")

rb.playlist_insert_directory("/yourmusicdir/")

rb.playlist_shuffle(rb.current_tick(),-1)

rb.playlist_start(0,0,0)
Title: Re: trying to start a playlist from LUA
Post by: Bilgus on September 06, 2018, 02:22:37 AM
Just a note rb.playlist_create is going to make a new dynamic playlist even if you named it as an existing playlist
you still need to fill it with tracks as it is empty also note the dynamic playlist won't be saved.

However you could manually parse an existing named playlist outside of the rb.playlist functions
and you should be able to save it manually as well. It appears this is what you were trying to do with the first script
BUT you wouldn't be able to start it by name through the script instead you would have to parse the new file
and add it to a dynamic playlist

Ill show you a little pseudo code to exemplify
Code: [Select]
local playlistdir = "/Playlists/"
local playlist    = "MyPlaylist.m3u8"
local newplaylist = "MyPlaylist2.m3u8"
local songs = {}
local file = io.open(playlistdir .. playlist)
if file then
    for line in file:lines() do
        songs[#songs+1] = line
    end
    file:close()
else
    rb.splash(rb.HZ, "Invalid Playlist")
    os.exit()
end

--code to save playlist
file = io.open(playlistdir .. newplaylist, "w+")
if file then
    --just reverses the order you need to come up with your own shuffle routine
    for line = #songs, 1, -1 do
        file:write(songs[line] .. "\n")
    end
    file:close()
else
    rb.splash(rb.HZ, "Invalid New Playlist")
    os.exit()
end


rb.lcd_clear_display()

rb.audio_stop()

rb.playlist_create(playlistdir, newplaylist)

for line = #songs, 1, -1 do
    rb.playlist_insert_track(songs[line])
end

rb.playlist_start(0,0,0)

So here we parse an existing playlist, reverse the song order,
save it to a new playlist and then using the same logic make a dynamic playlist
so we can play it immediately

Next time we can just start the new playlist through rockbox so we don't have to use the script again
or if you just want to shuffle each time you don't even have to save the new playlist
Title: Re: trying to start a playlist from LUA
Post by: rokboxyo on September 06, 2018, 12:00:03 PM
what C plugins work that modify/load a playlist and what is the lua script you are trying to use one of the above?

There are no C plugins that load or play music to my knowledge, so I don't actually know if it would work, I'd have to make some tests, but it will take me some time to download and set up the vm. That said there is a C plugin that displays a HUD with info on the music that's playing so I know it can at least get this far.
There are not a lot of LUA scripts available in the official rockbox distribution, only boomshine, the menu example taken from the middle of this page (https://www.rockbox.org/wiki/PluginLua) and a couple small lua scripts I did for training.
What I can say is that all these lua scripts work perfectly when no music is playing, but if rockbox is playing music they always brick instantly, even with very simple "hello world" scripts. They also brick if I try to start a playlist from within the scripts, which seems logical.

That said, while playing with LUA, I think I have noticed some instances of scripts that could actually result in the loading and even shuffling of a playlist but I don't remember how I had scripted them. I discarded them because they bricked when I tried playing the pl.

I'll take a look at the other code snippets you posted and report later.
Thanks for your help


As for my own needs, I know that a lua scripting can create and save a playlist on the disk, and the more I think about it, the more I think it would be a good solution to what I need to do.
Title: Re: trying to start a playlist from LUA
Post by: rokboxyo on September 06, 2018, 12:35:35 PM
What happens if you stop the audio first and actually add tracks?

Code: [Select]
rb.lcd_clear_display()

rb.audio_stop()

rb.playlist_create("/Playlists/","MyPlaylist2.m3u8")

rb.playlist_insert_directory("/yourmusicdir/")

rb.playlist_shuffle(rb.current_tick(),-1)

rb.playlist_start(0,0,0)

I tried running this one, it bricks.
If I remove the "playlist_start" line and run it, it creates the playlist but the shuffling isn't done, all the songs are in original order.
After this, if I try to run the playlist manually it bricks.
if I restart the device after running the script, it will play the playlist normally, if I don't restart after running any lua script it bricks the device when I try to play. So it looks like there might be a buffer problem with lua scripts or sthg like that.
Title: Re: trying to start a playlist from LUA
Post by: Bilgus on September 06, 2018, 12:48:44 PM
Are you using 3.14 or the latest dev version?

I have tested all of these on a clipzip with the latest
dev version and all worked so I'm unsure what the issue could be..

If you are already using the DEV version
If you have a SD card in the device could you try without the SD card?

Also try removing rb.lcd_clear_display() or place rb.lcd_scroll_stop() just before it
as I discovered that it can lead to hangs on some devices
Title: Re: trying to start a playlist from LUA
Post by: rokboxyo on September 06, 2018, 01:32:53 PM
I'm using 3.14.
I'll try the dev version and I'll report
Title: Re: trying to start a playlist from LUA
Post by: rokboxyo on September 07, 2018, 06:22:44 PM
What happens if you stop the audio first and actually add tracks?

Code: [Select]
rb.lcd_clear_display()

rb.audio_stop()

rb.playlist_create("/Playlists/","MyPlaylist2.m3u8")

rb.playlist_insert_directory("/yourmusicdir/")

rb.playlist_shuffle(rb.current_tick(),-1)

rb.playlist_start(0,0,0)

All right I tried with the latest dev release (sept 7th):
1-all lua scripts now work and they don't brick the machine if you start them when music is playing (like they do with 3.14)
2-boomshine takes a performance hit when music is playing, but this was to be expected I suppose
3-the example above now works perfectly, except...
4-rb.playlist_shuffle(rb.current_tick(),-1) doesn't shuffle the playlist, it does work if I replace the -1 by 0


great job, now I have to think of the best way to achieve my goals
thanks for the help