diff options
Diffstat (limited to 'apps/plugins')
-rw-r--r-- | apps/plugins/lua/rocklib.c | 11 | ||||
-rw-r--r-- | apps/plugins/lua_scripts/random_playlist.lua | 72 |
2 files changed, 67 insertions, 16 deletions
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index 2672d446fc..6131a479db 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c | |||
@@ -320,11 +320,12 @@ RB_WRAP(playlist) | |||
320 | enum e_playlist {PLAYL_AMOUNT = 0, PLAYL_ADD, PLAYL_CREATE, | 320 | enum e_playlist {PLAYL_AMOUNT = 0, PLAYL_ADD, PLAYL_CREATE, |
321 | PLAYL_START, PLAYL_RESUMETRACK, PLAYL_RESUME, | 321 | PLAYL_START, PLAYL_RESUMETRACK, PLAYL_RESUME, |
322 | PLAYL_SHUFFLE, PLAYL_SYNC, PLAYL_REMOVEALLTRACKS, | 322 | PLAYL_SHUFFLE, PLAYL_SYNC, PLAYL_REMOVEALLTRACKS, |
323 | PLAYL_INSERTTRACK, PLAYL_INSERTDIRECTORY, PLAYL_ECOUNT}; | 323 | PLAYL_INSERTTRACK, PLAYL_INSERTDIRECTORY, PLAYL_INSERTPLAYL, |
324 | PLAYL_ECOUNT}; | ||
324 | 325 | ||
325 | const char *playlist_option[] = {"amount", "add", "create", "start", "resume_track", | 326 | const char *playlist_option[] = {"amount", "add", "create", "start", "resume_track", |
326 | "resume", "shuffle", "sync", "remove_all_tracks", | 327 | "resume", "shuffle", "sync", "remove_all_tracks", |
327 | "insert_track", "insert_directory", NULL}; | 328 | "insert_track", "insert_directory", "insert_playlist", NULL}; |
328 | 329 | ||
329 | const char *filename, *dir; | 330 | const char *filename, *dir; |
330 | int result = 0; | 331 | int result = 0; |
@@ -388,6 +389,12 @@ RB_WRAP(playlist) | |||
388 | recurse = lua_toboolean(L, 5); /* default to false */ | 389 | recurse = lua_toboolean(L, 5); /* default to false */ |
389 | result = rb->playlist_insert_directory(NULL, dir, pos, queue, recurse); | 390 | result = rb->playlist_insert_directory(NULL, dir, pos, queue, recurse); |
390 | break; | 391 | break; |
392 | case PLAYL_INSERTPLAYL: | ||
393 | filename = luaL_checkstring(L, 2); /* only required parameter */ | ||
394 | pos = luaL_optint(L, 3, 0); | ||
395 | queue = lua_toboolean(L, 4); /* default to false */ | ||
396 | result = rb->playlist_insert_playlist(NULL, filename, pos, queue); | ||
397 | break; | ||
391 | } | 398 | } |
392 | 399 | ||
393 | yield(); | 400 | yield(); |
diff --git a/apps/plugins/lua_scripts/random_playlist.lua b/apps/plugins/lua_scripts/random_playlist.lua index c87b005a49..9d300025ac 100644 --- a/apps/plugins/lua_scripts/random_playlist.lua +++ b/apps/plugins/lua_scripts/random_playlist.lua | |||
@@ -29,6 +29,9 @@ local playlistpath = "/Playlists" | |||
29 | local max_tracks = 500; -- size of playlist to create | 29 | local max_tracks = 500; -- size of playlist to create |
30 | local min_repeat = 500; -- this many songs before a repeat | 30 | local min_repeat = 500; -- this many songs before a repeat |
31 | local play_on_success = true; | 31 | local play_on_success = true; |
32 | --program vars | ||
33 | local playlist_handle | ||
34 | local t_playlistbuf -- table for playlist write buffer | ||
32 | 35 | ||
33 | -- Random integer function | 36 | -- Random integer function |
34 | local random = math.random; -- ref random(min, max) | 37 | local random = math.random; -- ref random(min, max) |
@@ -48,7 +51,6 @@ rb.contexts = nil | |||
48 | local sINITDATABASE = "Initialize Database" | 51 | local sINITDATABASE = "Initialize Database" |
49 | local sHEADERTEXT = "Random Playlist" | 52 | local sHEADERTEXT = "Random Playlist" |
50 | local sPLAYLISTERROR = "Playlist Error!" | 53 | local sPLAYLISTERROR = "Playlist Error!" |
51 | local sREMOVEPLAYLIST = "Removing Dynamic Playlist" | ||
52 | local sSEARCHINGFILES = "Searching for Files.." | 54 | local sSEARCHINGFILES = "Searching for Files.." |
53 | local sERROROPENFMT = "Error Opening %s" | 55 | local sERROROPENFMT = "Error Opening %s" |
54 | local sINVALIDDBFMT = "Invalid Database %s" | 56 | local sINVALIDDBFMT = "Invalid Database %s" |
@@ -174,13 +176,55 @@ local function _setup_random_playlist(tag_entries, play, min_repeat, trackcount) | |||
174 | return play, min_repeat, trackcount; | 176 | return play, min_repeat, trackcount; |
175 | end | 177 | end |
176 | 178 | ||
179 | --deletes existing file and creates a new playlist | ||
180 | local function playlist_create(filename) | ||
181 | local filehandle = io.open(filename, "w+") --overwrite | ||
182 | if not filehandle then | ||
183 | rb.splash(rb.HZ, "Error opening " .. filename) | ||
184 | return false | ||
185 | end | ||
186 | t_playlistbuf = {} | ||
187 | filehandle:write("\239\187\191") -- Write BOM --"\xEF\xBB\xBF" | ||
188 | playlist_handle = filehandle | ||
189 | return true | ||
190 | --os.remove( playlistpath .. "/" .. playlist) | ||
191 | --rb.playlist("remove_all_tracks") | ||
192 | --rb.playlist("create", playlistpath .. "/", playlist) | ||
193 | end | ||
194 | |||
195 | -- writes track path to a buffer must be flushed | ||
196 | local function playlist_write(trackpath) | ||
197 | t_playlistbuf[#t_playlistbuf + 1] = trackpath | ||
198 | t_playlistbuf[#t_playlistbuf + 1] = "\n" | ||
199 | --[[if rb.playlist("insert_track", str) < 0 then | ||
200 | rb.splash(rb.HZ, sPLAYLISTERROR) | ||
201 | break; -- ERROR, PLAYLIST FULL? | ||
202 | end]] | ||
203 | end | ||
204 | |||
205 | -- flushes playlist buffer to file | ||
206 | local function playlist_flush() | ||
207 | playlist_handle:write(table.concat(t_playlistbuf)) | ||
208 | t_playlistbuf = {} | ||
209 | --[[if rb.playlist("insert_track", str) < 0 then | ||
210 | rb.splash(rb.HZ, sPLAYLISTERROR) | ||
211 | break; -- ERROR, PLAYLIST FULL? | ||
212 | end]] | ||
213 | end | ||
214 | |||
215 | -- closes playlist file descriptor | ||
216 | local function playlist_finalize() | ||
217 | playlist_handle:close() | ||
218 | end | ||
219 | |||
177 | --[[ Given the filenameDB file [database] | 220 | --[[ Given the filenameDB file [database] |
178 | creates a random dynamic playlist with a default savename of [playlist] | 221 | creates a random dynamic playlist with a default savename of [playlist] |
179 | containing [trackcount] tracks, played on completion if [play] is true]] | 222 | containing [trackcount] tracks, played on completion if [play] is true]] |
180 | function create_random_playlist(database, playlist, trackcount, play) | 223 | local function create_random_playlist(database, playlist, trackcount, play) |
181 | if not database or not playlist or not trackcount then return end | 224 | if not database or not playlist or not trackcount then return end |
182 | if not play then play = false end | 225 | if not play then play = false end |
183 | 226 | ||
227 | local playlist_handle | ||
184 | local file = io.open('/' .. database or "", "r") --read | 228 | local file = io.open('/' .. database or "", "r") --read |
185 | if not file then rb.splash(100, string.format(sERROROPENFMT, database)) return end | 229 | if not file then rb.splash(100, string.format(sERROROPENFMT, database)) return end |
186 | 230 | ||
@@ -214,6 +258,7 @@ function create_random_playlist(database, playlist, trackcount, play) | |||
214 | end | 258 | end |
215 | 259 | ||
216 | local tag_entries = bytesLE_n(tagcache_entries) | 260 | local tag_entries = bytesLE_n(tagcache_entries) |
261 | if tag_entries > 50000 then play = false end | ||
217 | 262 | ||
218 | play, min_repeat, trackcount = _setup_random_playlist( | 263 | play, min_repeat, trackcount = _setup_random_playlist( |
219 | tag_entries, play, min_repeat, trackcount); | 264 | tag_entries, play, min_repeat, trackcount); |
@@ -250,6 +295,7 @@ function create_random_playlist(database, playlist, trackcount, play) | |||
250 | if y >= max_h then | 295 | if y >= max_h then |
251 | do_progress_header() | 296 | do_progress_header() |
252 | rb.lcd_clear_display() | 297 | rb.lcd_clear_display() |
298 | playlist_flush(playlist_handle) | ||
253 | rb.yield() | 299 | rb.yield() |
254 | y = h | 300 | y = h |
255 | end | 301 | end |
@@ -319,14 +365,11 @@ function create_random_playlist(database, playlist, trackcount, play) | |||
319 | tracks = tracks + 1 | 365 | tracks = tracks + 1 |
320 | show_progress() | 366 | show_progress() |
321 | push_lru(idxp) -- add to repeat list | 367 | push_lru(idxp) -- add to repeat list |
322 | if rb.playlist("insert_track", str) < 0 then | 368 | playlist_write(str) |
323 | rb.splash(rb.HZ, sPLAYLISTERROR) | ||
324 | break; -- ERROR, PLAYLIST FULL? | ||
325 | end | ||
326 | |||
327 | end | 369 | end |
328 | 370 | ||
329 | if tracks >= trackcount then | 371 | if tracks >= trackcount then |
372 | playlist_flush() | ||
330 | do_progress_header() | 373 | do_progress_header() |
331 | break | 374 | break |
332 | end | 375 | end |
@@ -400,11 +443,7 @@ function create_random_playlist(database, playlist, trackcount, play) | |||
400 | anchor_index = nil | 443 | anchor_index = nil |
401 | end | 444 | end |
402 | 445 | ||
403 | rb.splash(10, sREMOVEPLAYLIST) | 446 | if not playlist_create(playlistpath .. "/" .. playlist) then return end |
404 | rb.audio("stop") | ||
405 | os.remove( playlistpath .. "/" .. playlist) | ||
406 | --rb.playlist("remove_all_tracks") | ||
407 | rb.playlist("create", playlistpath .. "/", playlist) | ||
408 | --[[ --profiling | 447 | --[[ --profiling |
409 | local starttime = rb.current_tick(); | 448 | local starttime = rb.current_tick(); |
410 | get_tracks_random() | 449 | get_tracks_random() |
@@ -414,15 +453,20 @@ function create_random_playlist(database, playlist, trackcount, play) | |||
414 | if (false) then | 453 | if (false) then |
415 | --]] | 454 | --]] |
416 | get_tracks_random() | 455 | get_tracks_random() |
456 | playlist_finalize(playlist_handle) | ||
417 | end | 457 | end |
418 | 458 | ||
419 | file:close() | 459 | file:close() |
420 | collectgarbage("collect") | 460 | collectgarbage("collect") |
421 | if trackcount and rb.playlist("amount") >= trackcount and play == true then | 461 | if trackcount and play == true then |
462 | rb.audio("stop") | ||
463 | rb.yield() | ||
464 | rb.playlist("create", playlistpath .. "/", "dynamic_playlist.m3u8") | ||
465 | rb.playlist("insert_playlist", playlistpath .. "/" .. playlist) | ||
422 | rb.playlist("start", 0, 0, 0) | 466 | rb.playlist("start", 0, 0, 0) |
423 | end | 467 | end |
424 | 468 | ||
425 | end -- create_playlist | 469 | end -- playlist_create |
426 | 470 | ||
427 | local function main() | 471 | local function main() |
428 | if not rb.file_exists(rb.ROCKBOX_DIR .. "/database_4.tcd") then | 472 | if not rb.file_exists(rb.ROCKBOX_DIR .. "/database_4.tcd") then |