diff options
author | William Wilgus <me.theuser@yahoo.com> | 2019-09-06 19:24:26 -0500 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2019-09-07 03:10:59 +0200 |
commit | 267d04d2bd2a7681c8bbcfbb655612101440b765 (patch) | |
tree | a518b4c10244cc77a5eee636132031c5a542c00a /apps/plugins/lua_scripts | |
parent | a3cbd86a5193e5227344db360f45f114fda10aab (diff) | |
download | rockbox-267d04d2bd2a7681c8bbcfbb655612101440b765.tar.gz rockbox-267d04d2bd2a7681c8bbcfbb655612101440b765.zip |
Lua add metadata and settings reading helper module
Adds example scripts for reading track metadata + dumping albumart
and rockbox settings
settings are now stored as a table of strings rather than a table of tables
as it saves ~15 kb of ram without adding much complexity
Change-Id: I611c312b2a60ab96e595e4710b17aedbd6c0689b
Diffstat (limited to 'apps/plugins/lua_scripts')
-rw-r--r-- | apps/plugins/lua_scripts/dump_rbsettings.lua | 49 | ||||
-rw-r--r-- | apps/plugins/lua_scripts/track_metadata.lua | 90 |
2 files changed, 139 insertions, 0 deletions
diff --git a/apps/plugins/lua_scripts/dump_rbsettings.lua b/apps/plugins/lua_scripts/dump_rbsettings.lua new file mode 100644 index 0000000000..2811d27487 --- /dev/null +++ b/apps/plugins/lua_scripts/dump_rbsettings.lua | |||
@@ -0,0 +1,49 @@ | |||
1 | require("rbsettings") | ||
2 | require("settings") | ||
3 | rb.metadata = nil -- remove track metadata settings | ||
4 | ------------------------------------------------------------------------------- | ||
5 | |||
6 | local function print_setting_table(t_tbl, s_sep) | ||
7 | s_sep = s_sep or "" | ||
8 | local str = "" | ||
9 | local function pfunct(t, sep, s, n) -- recursive print function | ||
10 | local vtype | ||
11 | for k, v in pairs(t) do | ||
12 | vtype = type(v) | ||
13 | if vtype == "table" then | ||
14 | local f = string.format("%s[%s]", n, k) | ||
15 | s = pfunct(v, sep, s, f) | ||
16 | elseif vtype == "boolean" then | ||
17 | v = v and "true" or "false" | ||
18 | s = string.format("%s%s[%s] = %s%s", s, n, k, v, sep) | ||
19 | elseif v then | ||
20 | s = string.format("%s%s[%s] = %s%s", s, n, k, v, sep) | ||
21 | end | ||
22 | end | ||
23 | return s | ||
24 | end | ||
25 | return pfunct(t_tbl, s_sep, str, "") | ||
26 | end | ||
27 | |||
28 | local filename = "/settings.txt" | ||
29 | local file = io.open(filename, "w+") -- overwrite | ||
30 | local t_settings | ||
31 | |||
32 | if not file then | ||
33 | rb.splash(rb.HZ, "Error writing " .. filename) | ||
34 | return | ||
35 | end | ||
36 | |||
37 | t_settings = rb.settings.dump('global_settings', "system") | ||
38 | file:write("global_settings:\n") | ||
39 | file:write(print_setting_table(t_settings, "\n")) | ||
40 | file:write("\n\n") | ||
41 | |||
42 | t_settings = rb.settings.dump('global_status', "system") | ||
43 | file:write("global_status:\n") | ||
44 | file:write(print_setting_table(t_settings, "\n")) | ||
45 | file:write("\n\n") | ||
46 | |||
47 | file:close() | ||
48 | |||
49 | rb.splash(100, "rb settings dumped: " .. filename) | ||
diff --git a/apps/plugins/lua_scripts/track_metadata.lua b/apps/plugins/lua_scripts/track_metadata.lua new file mode 100644 index 0000000000..08c1c865f3 --- /dev/null +++ b/apps/plugins/lua_scripts/track_metadata.lua | |||
@@ -0,0 +1,90 @@ | |||
1 | require("rbsettings") | ||
2 | require("settings") --settings.lua | ||
3 | rb.system = nil -- remove system settings | ||
4 | ------------------------------------------------------------------------------- | ||
5 | local track_data = rb.metadata.mp3_entry | ||
6 | local cur_trk = "audio_current_track" | ||
7 | ------------------------------------------------------------------------------- | ||
8 | local trackname = rb.settings.read(cur_trk, track_data.title) or | ||
9 | rb.settings.read(cur_trk, track_data.path) | ||
10 | if not trackname or trackname == "" then | ||
11 | os.exit(1, "No track loaded") | ||
12 | else | ||
13 | rb.splash(100, trackname) | ||
14 | end | ||
15 | ------------------------------------------------------------------------------- | ||
16 | local function dump_albumart(fileout) | ||
17 | local t_albumart = rb.settings.read(cur_trk, track_data.albumart, "metadata") | ||
18 | local t_aaext = {".bmp",".png", ".jpg"} | ||
19 | local path = rb.settings.read(cur_trk, track_data.path) | ||
20 | if t_albumart.pos > 0 and t_albumart.size > 0 and t_albumart.type > 0 then | ||
21 | |||
22 | if t_aaext[t_albumart.type] then | ||
23 | local filename = "/" .. fileout .. t_aaext[t_albumart.type] | ||
24 | local aa = io.open(filename, "w+") -- overwrite | ||
25 | if not aa then | ||
26 | rb.splash(rb.HZ, "Error writing " .. filename) | ||
27 | return | ||
28 | end | ||
29 | |||
30 | local track = io.open(path, "r") | ||
31 | if not track then | ||
32 | rb.splash(rb.HZ, "Error opening " .. path) | ||
33 | return | ||
34 | end | ||
35 | track:seek("set", t_albumart.pos ) | ||
36 | for i = 0, t_albumart.size, 32 do | ||
37 | aa:write(track:read(32)) | ||
38 | end | ||
39 | rb.splash(rb.HZ, "Saved: " .. filename) | ||
40 | track:close() | ||
41 | aa:close() | ||
42 | else | ||
43 | |||
44 | end | ||
45 | end | ||
46 | end | ||
47 | |||
48 | local function print_setting_table(t_tbl, s_sep) | ||
49 | s_sep = s_sep or "" | ||
50 | local str = "" | ||
51 | local function pfunct(t, sep, s, n) -- recursive print function | ||
52 | local vtype | ||
53 | for k, v in pairs(t) do | ||
54 | vtype = type(v) | ||
55 | if vtype == "table" then | ||
56 | local f = string.format("%s[%s]", n, k) | ||
57 | s = pfunct(v, sep, s, f) | ||
58 | elseif vtype == "boolean" then | ||
59 | v = v and "true" or "false" | ||
60 | s = string.format("%s%s[%s] = %s%s", s, n, k, v, sep) | ||
61 | elseif v then | ||
62 | s = string.format("%s%s[%s] = %s%s", s, n, k, v, sep) | ||
63 | end | ||
64 | end | ||
65 | return s | ||
66 | end | ||
67 | return pfunct(t_tbl, s_sep, str, "") | ||
68 | end | ||
69 | |||
70 | local filename = "/metadata.txt" | ||
71 | local file = io.open(filename, "w+") -- overwrite | ||
72 | local t_settings | ||
73 | |||
74 | if not file then | ||
75 | rb.splash(rb.HZ, "Error writing " .. filename) | ||
76 | return | ||
77 | end | ||
78 | |||
79 | ---[[ | ||
80 | t_settings = rb.settings.dump(cur_trk, "metadata", "mp3_entry") | ||
81 | file:write(trackname .. ":\n") | ||
82 | file:write(print_setting_table(t_settings, "\n")) | ||
83 | file:write("\n\n") | ||
84 | file:close() | ||
85 | |||
86 | rb.splash(100, "metadata dumped: " .. filename) | ||
87 | |||
88 | if rb.settings.read(cur_trk, track_data.has_embedded_albumart) then | ||
89 | dump_albumart("/albumart") | ||
90 | end | ||