Last active
March 24, 2021 04:58
-
-
Save bahusoid/480b40adb297f01dd2e0a7503246a4f8 to your computer and use it in GitHub Desktop.
Script for Rockbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local print = require("print") | |
require("buttons") | |
require("rbsettings") | |
require("settings") | |
local goToMs = 0 | |
-- goToMode: 0 - seconds; 1 - minutes; 2 -hours | |
local goToMode=0 | |
local maxGoToMode = 2 | |
local track_length = 0 | |
local track_name = "" | |
local act = rb.buttons | |
local quit = false | |
local maxGoToMs = 0 | |
local function setTrackLength() | |
elapsed = rb.audio("elapsed") or 0 | |
local totalTimeMs = (rb.audio("length") or 0) | |
if(track_length == totalTimeMs) then return end | |
track_length = totalTimeMs | |
local seconds = totalTimeMs/1000 | |
local minutes = seconds/60 | |
local hours = minutes/60 | |
if(seconds < 60) then | |
goToMode = 0 | |
maxGoToMs = (seconds +1)*1000 | |
elseif (minutes < 60) then | |
goToMode = 1 | |
maxGoToMs = (minutes +1)*60*1000 | |
else | |
goToMode = 2 | |
maxGoToMs = (hours +1)*60*60*1000 | |
end | |
maxGoToMode = goToMode | |
end | |
local function setTrackName() | |
if(track_length > 0) then | |
local metadata = rb.settings.read | |
local cur_trk = "audio_current_track" | |
local track_data = {} | |
-- grab only settings we are interested in | |
track_data.title = rb.metadata.mp3_entry.title | |
track_data.path = rb.metadata.mp3_entry.path | |
track_name = metadata(cur_trk, track_data.title) or metadata(cur_trk, track_data.path) | |
else | |
track_name = "" | |
end | |
return track_name | |
end | |
local function nextGoToMode(val) | |
local newVal = goToMode + val | |
if(newVal<0) then | |
goToMode = maxGoToMode | |
elseif newVal > maxGoToMode then | |
goToMode = 0 | |
else | |
goToMode = newVal | |
end | |
end | |
local function time_formatted(time_ms, inputMode) | |
local indicator = "* " | |
local seconds, minutes, hours | |
-- seconds = math.floor(time_ms/1000) | |
-- minutes = math.floor(seconds/60) | |
-- hours = math.floor(minutes/60) | |
seconds = time_ms/1000 | |
minutes = seconds/60 | |
hours = minutes/60 | |
if(seconds >= 60) then | |
seconds = seconds%60 | |
end | |
if(minutes >= 60) then | |
minutes = minutes%60 | |
end | |
local t = { } | |
if(hours > 0 or inputMode == 2) then | |
if inputMode == 2 then | |
t[#t+1] = indicator | |
end | |
t[#t+1] = hours | |
t[#t+1] = "h " | |
end | |
if(minutes > 0 or hours > 0 or (inputMode or 0) > 0) then | |
if inputMode == 1 then | |
t[#t+1] = indicator | |
end | |
t[#t+1] = minutes | |
t[#t+1] = "m " | |
end | |
if inputMode == 0 then | |
t[#t+1] = indicator | |
end | |
t[#t+1] = seconds | |
t[#t+1] = "s" | |
return table.concat(t) | |
end | |
local function audio_seek(time_ms) | |
if(time_ms < track_length) then | |
rb.audio("stop") | |
rb.audio("play", time_ms, 0) | |
end | |
end | |
local function power(value,n) | |
if(n == 0) then return 1 end | |
local result = value | |
for v = 1,n-1,1 do result = result*value end | |
return result | |
end | |
local function getAddMs(count, mode) | |
return count*1000*power(60, mode) | |
end | |
local function setGoToTime(count) | |
-- local addMs = count*1000*power(60, goToMode) | |
local addMs = getAddMs(count, goToMode) | |
local result = goToMs + addMs | |
if(result < 0) then | |
goToMs = 0 | |
elseif(addMs < 0 or result < maxGoToMs) then | |
goToMs = result | |
else | |
goToMs = result % getAddMs(1, maxGoToMode) + (maxGoToMs - getAddMs(1, maxGoToMode)) | |
end | |
end | |
local function action_event(action) | |
if (action) == 0 then return end | |
if action == act.BUTTON_POWER or action == act.BUTTON_MAIN or action == act.BUTTON_HOME then | |
quit = true | |
elseif (bit.band(action,act.BUTTON_REL) == act.BUTTON_REL) then | |
-- do nothing on button release | |
elseif action == act.BUTTON_SELECT then | |
audio_seek(goToMs) | |
elseif action == act.BUTTON_UP then | |
setGoToTime(1) | |
elseif action == act.BUTTON_DOWN then | |
setGoToTime(-1) | |
elseif action == bit.bor(act.BUTTON_UP, act.BUTTON_REPEAT) | |
or bit.band(action, act.BUTTON_VOLUP or act.BUTTON_VOL_UP) == (act.BUTTON_VOLUP or act.BUTTON_VOL_UP) | |
then | |
setGoToTime(10) | |
elseif action == bit.bor(act.BUTTON_DOWN, act.BUTTON_REPEAT) | |
or bit.band(action, act.BUTTON_VOLDOWN or act.BUTTON_VOL_DOWN) == (act.BUTTON_VOLDOWN or act.BUTTON_VOL_DOWN) | |
then | |
setGoToTime(-10) | |
elseif bit.band(action, act.BUTTON_LEFT) == act.BUTTON_LEFT then | |
nextGoToMode(1) | |
elseif bit.band(action, act.BUTTON_RIGHT) == act.BUTTON_RIGHT then | |
nextGoToMode(-1) | |
elseif (action ~= 0) then | |
--rb.splash(0, action) | |
end | |
end | |
local function main() | |
setTrackLength() | |
setTrackName() | |
rb.lcd_clear_display() | |
do -- configure print function | |
local t_print = print.opt.get(true) | |
t_print.autoupdate = false | |
t_print.justify = "center" | |
t_print.col = 2 | |
end | |
local i = 5; | |
while (not quit and track_length > 0)do | |
local but = rb.button_get(false) | |
local timeBased = i == 5; | |
if(but ~= 0 or timeBased) then | |
action_event(but) | |
print.opt.line(1) | |
print.f() -- clear the line | |
print.f(track_name) | |
print.f() | |
print.f("%s / %s", time_formatted(elapsed), time_formatted(track_length)) | |
print.f() | |
print.f("Go To: %s", time_formatted(goToMs, goToMode)) | |
print.f() | |
rb.lcd_update() | |
end | |
rb.sleep(rb.HZ / 10) | |
if timeBased then | |
setTrackLength() | |
setTrackName() | |
i = 0 | |
end | |
i = i + 1 | |
end | |
rb.splash(100, "exiting") | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment