Rockbox.org home
Downloads
Release release
Dev builds dev builds
Extras extras
themes themes
Documentation
Manual manual
Wiki wiki
Device Status device status
Support
Forums forums
Mailing lists mailing lists
IRC IRC
Development
Bugs bugs
Patches patches
Dev Guide dev guide
Search



Donate

Rockbox Technical Forums


Login with username, password and session length
Home Help Search Staff List Login Register
News:

Welcome to the Rockbox Technical Forums!

+  Rockbox Technical Forums
|-+  Rockbox Development
| |-+  Feature Ideas
| | |-+  more advanced sleep timer
« previous next »
  • Print
Pages: [1] 2

Author Topic: more advanced sleep timer  (Read 9632 times)

Offline tdb

  • Member
  • *
  • Posts: 41
  • One tequila, two tequila, three tequila, floor.
more advanced sleep timer
« on: December 27, 2016, 07:11:21 AM »
Hi

I have been using rockbox for many years almost daily and am very pleased with it. Use it mainly for listening to podcasts and audiobooks and appreciate the tons of features and settings (especially the bookmarking features).

I also use the sleeptimer function often as an aid to go to sleep with a podcast or audiobook. I usually need 15 minutes to go to sleep and that works fine most of the times, but it can happen that a podcast or audiobook wakes me up within that time frame due to some louder recorded sound/voice or sudden change of voice / speaker etc.
For that reason there are only a few podcasts I have chosen to 'talk me into sleep' and keep the sound level low but even then, it sometimes happens I wake up again before sleeptimer shuts down the device. 

A solution for this 'problem' might be to add a fade out feature for sleeptimer that takes a couple of minutes (preferably something that can be set by the user)
In practice that could mean I would change the length of the sleeptimer from 15 minutes to 20 or 25 minutes and starts very gradually to fade out after 10 minutes and it takes 10-15 minutes to reach volume 0 and shuts down.

That would make the sleeptimer an even more useful tool as a sleeping aid. I am pretty sure lots of folks are already using it as a sleeping aid, but a feature like this takes it to another level imo.
Maybe there is already something like this possible but I couldn't find it. The fade on stop option is too short and not specific for the sleep timer so that doesn't really help. 


 





« Last Edit: December 27, 2016, 07:14:04 AM by tdb »
Logged
Clip V1 4Gb, Clip V2 8Gb, C240 v2, E260 4Gb Rockboxed

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 537
Re: more advanced sleep timer
« Reply #1 on: December 28, 2016, 01:59:32 AM »
Here is a lua script that should do what you want, feel free to tweak it to your hearts desire

set your sleep timer and start your songs
copy into a file called Slow_fade.lua or something like that and run it from the file browser
choose a timeout and it will reduce the volume over the span you selected
when it hits the minimum volume playback will stop

Code: [Select]
--Bilgus 12-2016
require "actions"
require "buttons"
TIMEOUT = 0
SOUND_VOLUME = 0
function say_msg(message, timeout)
    rb.splash(1, message)
    rb.sleep(timeout * rb.HZ)
end

function say_value(value,message,timeout)
  local message = string.format(message .. "%d", value)
  say_msg(message, timeout)
end

function cfg_num_setting(str_name)
local file = io.open(rb.ROCKBOX_DIR .. "/config.cfg", "r")
    if not file then
        return nil
    end
local value = nil
local contents = file:read("*all")

    i, j = string.find(contents, str_name .. ":")
    if j ~= nil then
        file:seek ("set", rb.atoi(j))
        value = file:read ("*num")
    end
    file:close() -- GC takes care of this if you would've forgotten it
    return value
end

function cfg_str_setting(str_name)
local file = io.open(rb.ROCKBOX_DIR .. "/config.cfg", "r")
    if not file then
        return nil
    end
local str = nil
local contents = file:read("*all")

    i, j = string.find(contents, str_name .. ":")
    if j ~= nil then
        file:seek ("set", rb.atoi(j))
        str = file:read ("*line")
        str = string.gsub(str, "%s", "")
    else
        str ="!"
    end
    file:close() -- GC takes care of this if you would've forgotten it
    return str
end

function ShowMainMenu() -- we invoke this function every time we want to display the main menu of the script
local s = 0
local mult = 1
local unit = " Minutes"


    while s == 0 or s == 5 do -- don't exit of program until user selects Exit
        if mult < 1 then
            mult = 1
            s = 0
        end
        mainmenu = {"More", mult * 1 .. unit, mult * 5 .. unit, mult * 10 .. unit, mult * 15 .. unit, "Less", "Exit"} -- define the items of the menu
        s = rb.do_menu("Reduce volume over", mainmenu, s, false) -- actually tell Rockbox to draw the menu

        -- In the line above: "Test" is the title of the menu, mainmenu is an array with the items
        -- of the menu, nil is a null value that needs to be there, and the last parameter is
        -- whether the theme should be drawn on the menu or not.
        -- the variable s will hold the index of the selected item on the menu.
        -- the index is zero based. This means that the first item is 0, the second one is 1, etc.
        if     s == 0 then mult = mult + 1
        elseif s == 1 then TIMEOUT = mult
        elseif s == 2 then TIMEOUT = mult * 5
        elseif s == 3 then TIMEOUT = mult * 10
        elseif s == 4 then TIMEOUT = mult * 15
        elseif s == 5 then mult = mult - 1 -- User selected to exit
        elseif s == 6 then os.exit() -- User selected to exit
        elseif s == -2 then os.exit() -- -2 index is returned from do_menu() when user presses the key to exit the menu (on iPods, it's the left key).
                                      -- In this case, user probably wants to exit (or go back to last menu).
        else rb.splash(2 * rb.HZ, "Error! Selected index: " .. s) -- something strange happened. The program shows this message when
                                                                  -- the selected item is not on the index from 0 to 3 (in this case), and displays
                                                                  -- the selected index. Having this type of error handling is not
                                                                  -- required, but it might be nice to have Especially while you're still
                                                                  -- developing the plugin.
        end
    end
end





ShowMainMenu()
rb.lcd_clear_display()
rb.lcd_update()

--if rb.strcasecmp(cfg_str_setting("keypress restarts sleeptimer"),"on") == 0 then
--say_msg("keypress restarts timer", 1)
--end

local volume = cfg_num_setting("volume")
    if volume == nil then
        volume = rb.sound_default(SOUND_VOLUME)
    end


local vol_min = rb.sound_min(SOUND_VOLUME)
local volsteps = -(vol_min - volume)
local seconds = (TIMEOUT * 60) / volsteps
local sec_left = (TIMEOUT * 60)
local hb = 0
local action = rb.get_action(rb.contexts.CONTEXT_STD, 0)
    if rb.pcm_is_playing() then
        while ((volume > vol_min) and (action ~= rb.actions.ACTION_STD_CANCEL)) do
            rb.lcd_clear_display()
            say_value(volume,sec_left .. " Sec, Volume: ", 1)
            local i = seconds * 2
            while ((i > 0) and (action ~= rb.actions.ACTION_STD_CANCEL)) do
                i = i - 1
                rb.lcd_drawline(hb, 1, hb, 1)
                rb.lcd_update()
                if hb >= rb.LCD_WIDTH then
                    hb = 0
                    rb.lcd_clear_display()
                    say_value(volume,sec_left .. " Sec, Volume: ", 1)
                end
                hb = hb + 1
                rb.sleep(rb.HZ / 2)
                action = rb.get_action(rb.contexts.CONTEXT_STD, 0)
                rb.yield()
            end
            volume = volume - 1
            rb.sound_set(SOUND_VOLUME, volume);
            sec_left = sec_left - seconds

        end
        rb.audio_stop()
        rb.lcd_clear_display()
        rb.lcd_update()

        say_msg("Playback Stopped", 5)
        os.exit()

    else
        rb.lcd_clear_display()
        rb.lcd_update()

        say_msg("Nothing is playing", 2)
        os.exit()
    end
* Slow_fade.lua.txt (5.32 kB - downloaded 190 times.)
« Last Edit: December 28, 2016, 02:10:54 AM by Bilgus »
Logged

Offline tdb

  • Member
  • *
  • Posts: 41
  • One tequila, two tequila, three tequila, floor.
Re: more advanced sleep timer
« Reply #2 on: December 28, 2016, 05:54:32 AM »
Awesome, thanks a lot!

Searching the internet yesterday I found a similar solution someone made for itunes with applescript http://www.jonathanlaliberte.com/2010/03/15/itunes-sleep-timer-with-fade-out-applescript/

Tried your script and added the lua script to my Sleeptimer shortcuts. Works like a charm.

[shortcut]
name: Start Sleep Timer
type: time
data: sleep 25

[shortcut]
name: Stop Sleep Timer
type: time
data: sleep 0

[shortcut]
type: browse
data: /Slow_fade.lua
name: Slow Fade

Will experiment with the script and probably try to optimize the 'fade out curve' a bit to make it work best for me. I think I understand the most important parts of the script so it is very nice to be able to adjust it myself :)
Logged
Clip V1 4Gb, Clip V2 8Gb, C240 v2, E260 4Gb Rockboxed

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 537
Re: more advanced sleep timer
« Reply #3 on: December 28, 2016, 08:00:32 AM »
No Problem, Once you get something awesome post it here for the next guy  :D
Logged

Offline tdb

  • Member
  • *
  • Posts: 41
  • One tequila, two tequila, three tequila, floor.
Re: more advanced sleep timer
« Reply #4 on: December 28, 2016, 05:37:17 PM »
Hi Bilgus - I have tried the scripts on 3 of my Sansa Clip+ players and work fine but my old Sansa Clip hangs immediately after I start the script.
Has the first Clip some limitations the Clip+ doesn't have? Unfortunately it is the only Clip I have, so I am not able to check on another Clip. Not a big problem. I will also test the script on my e200's and C200 later (I got a bunch of Sansa players :) )

Updated them all to the latest dev build (b772782).

BTW - I am not planning on doing anything fancy with your script. Tested it today a couple of  times and I think the linear volume decrease is just working as gradually as I was hoping for. Maybe only change the default setting from 1 - 5 - 10 - 15 minutes to 10 - 15 - 20. 
Logged
Clip V1 4Gb, Clip V2 8Gb, C240 v2, E260 4Gb Rockboxed

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 537
Re: more advanced sleep timer
« Reply #5 on: December 28, 2016, 10:12:52 PM »
I'll have to look into it might be that there isn't enough buffer free on the clip, also you can add a save file and then have your last setting as the first one that comes up
Logged

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 537
Re: more advanced sleep timer
« Reply #6 on: December 29, 2016, 03:44:34 PM »
I don't even see the lua viewer on the clip you must have a V1 clip I see in the
defines that one only has a plugin buffer of 10000 where as most of the rest have one at 80000 or >

I'll make a real live fade to sleep plugin in the next few days
Logged

Offline tdb

  • Member
  • *
  • Posts: 41
  • One tequila, two tequila, three tequila, floor.
Re: more advanced sleep timer
« Reply #7 on: December 30, 2016, 06:08:24 PM »
Quote from: Bilgus on December 29, 2016, 03:44:34 PM
I don't even see the lua viewer on the clip you must have a V1 clip I see in the
defines that one only has a plugin buffer of 10000 where as most of the rest have one at 80000 or >

I'll make a real live fade to sleep plugin in the next few days

The Clip is a v2 so the buffer for that one might be too low as well?

Anyway, having the feature as a plugin would indeed be nice and might encourage more people to try it out. 
Lua script is working fine though, was sleeping like a baby last night ;) 
Logged
Clip V1 4Gb, Clip V2 8Gb, C240 v2, E260 4Gb Rockboxed

Offline tdb

  • Member
  • *
  • Posts: 41
  • One tequila, two tequila, three tequila, floor.
Re: more advanced sleep timer
« Reply #8 on: January 03, 2017, 03:50:23 PM »
Tested the Lua script on my Pandora, no issues. Not working on my C200v2 though. Message 'can't open....'. I guess Lua isn't available for the C200 as well.
Will try it out on my E200 later.
Logged
Clip V1 4Gb, Clip V2 8Gb, C240 v2, E260 4Gb Rockboxed

Offline tdb

  • Member
  • *
  • Posts: 41
  • One tequila, two tequila, three tequila, floor.
Re: more advanced sleep timer
« Reply #9 on: January 05, 2017, 03:33:07 PM »
Lua script works fine on my E200 as well.

The script seems to use the volume to start the fade timer with from the config.cfg file - is it possible with Lua to 'read' the actual playing volume?   
Logged
Clip V1 4Gb, Clip V2 8Gb, C240 v2, E260 4Gb Rockboxed

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 537
Re: more advanced sleep timer
« Reply #10 on: January 05, 2017, 06:12:54 PM »
not that I have figured out as of yet -- Not saying there isn't just saying out of 5 or 6 ways I thought might work the cfg file worked the others not so much.
Logged

Offline tdb

  • Member
  • *
  • Posts: 41
  • One tequila, two tequila, three tequila, floor.
Re: more advanced sleep timer
« Reply #11 on: January 07, 2017, 04:47:12 AM »
OK - np
I am going to change it to a fixed volume level then that is always comfortable to start with.

Logged
Clip V1 4Gb, Clip V2 8Gb, C240 v2, E260 4Gb Rockboxed

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 537
Re: more advanced sleep timer
« Reply #12 on: February 16, 2017, 05:45:33 PM »
I've added the requisite functions to rb plugins with a commit on gerrit it will allow me to pull in the current volume level and the sleep timer seconds I'll post an updated script if and when it gets committed
Logged

Offline tdb

  • Member
  • *
  • Posts: 41
  • One tequila, two tequila, three tequila, floor.
Re: more advanced sleep timer
« Reply #13 on: February 17, 2017, 04:13:37 PM »
Nice - that will improve an already very helpful script  :)

I have been using the script almost daily for more than a month now and it is working brilliantly. After falling asleep I didn't wake up during duration of the sleeptimer once. So it is doing in practice exactly what I was hoping for.

Would this be possible to script after the update: being able to reset the fade out- and sleep-timer while the script is running with a keypress (or two to prevent a key being accidentally pressed)? Especially useful when you notice you have problems getting to sleep and the previously set sleeptimer/fade out duration is not long enough. 
Logged
Clip V1 4Gb, Clip V2 8Gb, C240 v2, E260 4Gb Rockboxed

Offline tdb

  • Member
  • *
  • Posts: 41
  • One tequila, two tequila, three tequila, floor.
Re: more advanced sleep timer
« Reply #14 on: December 24, 2017, 07:57:20 AM »
Quote from: Bilgus on February 16, 2017, 05:45:33 PM
I've added the requisite functions to rb plugins with a commit on gerrit it will allow me to pull in the current volume level and the sleep timer seconds I'll post an updated script if and when it gets committed

Sorry for grave digging this thread.   
Noticed there are indeed some commits on gerrit, but they don't seem to be implemented yet. Still a chance we will see the feature in the dev builds?

Logged
Clip V1 4Gb, Clip V2 8Gb, C240 v2, E260 4Gb Rockboxed

  • Print
Pages: [1] 2
« previous next »
+  Rockbox Technical Forums
|-+  Rockbox Development
| |-+  Feature Ideas
| | |-+  more advanced sleep timer
 

  • SMF 2.0.17 | SMF © 2019, Simple Machines
  • Rockbox Privacy Policy
  • XHTML
  • RSS
  • WAP2

Page created in 0.103 seconds with 21 queries.