From 7482c8a3508e68ccd705d12e2291d402bb011351 Mon Sep 17 00:00:00 2001 From: YP3 Audiobook Date: Wed, 25 Nov 2026 20:11:27 +0000 Subject: [PATCH 1/3] yp3: audiobook resume (save on pause/periodically, per-file bookmarks, defaults) --- apps/bookmark.c | 87 +++++++++++++++++++++++++++++++++--- apps/bookmark.h | 3 ++ apps/filetree.c | 9 ++++ apps/playback.c | 30 +++++++++++++ apps/settings.c | 12 +++++ apps/settings.h | 3 ++ apps/settings_list.c | 38 +++++++++++++--- firmware/export/config/yp3.h | 7 +++ 8 files changed, 177 insertions(+), 12 deletions(-) diff --git a/apps/bookmark.c b/apps/bookmark.c index 4bb97a15..a6790c7f 100644 --- a/apps/bookmark.c +++ b/apps/bookmark.c @@ -144,7 +144,8 @@ static long long_token(const char **s) /*-------------------------------------------------------------------------*/ static bool bookmark_get_playlist_and_track_hash(const char *bookmark, uint32_t *pl_hash, - uint32_t *track_hash) + uint32_t *track_hash, + bool per_track) { *pl_hash = 0; *track_hash = 0; @@ -163,7 +164,7 @@ static bool bookmark_get_playlist_and_track_hash(const char *bookmark, track = pl_end + 1; get_hash(pl_start, pl_hash, pl_len); - if (global_settings.usemrb == BOOKMARK_ONE_PER_TRACK) + if (per_track) { get_hash(track, track_hash, strlen(track)); } @@ -322,12 +323,26 @@ static bool add_bookmark(const char* bookmark_file_name, || (global_settings.usemrb == BOOKMARK_ONE_PER_TRACK))) { - if (bookmark_get_playlist_and_track_hash(bookmark, &pl_hash, &pl_track_hash)) + bool per_track = (global_settings.usemrb == BOOKMARK_ONE_PER_TRACK); + if (bookmark_get_playlist_and_track_hash(bookmark, &pl_hash, + &pl_track_hash, per_track)) { comp_playlist = true; - comp_track = (global_settings.usemrb == BOOKMARK_ONE_PER_TRACK); + comp_track = per_track; } } +#ifdef HAVE_AUDIOBOOK_RESUME + /* A folder's bookmark file keeps one bookmark per file: the newest + place in each book. Upstream adds one on every stop and keeps them + all, and autoload took the newest whichever file was picked. */ + else if (!most_recent && + bookmark_get_playlist_and_track_hash(bookmark, &pl_hash, + &pl_track_hash, true)) + { + comp_playlist = true; + comp_track = true; + } +#endif logf("adding bookmark to %s [%s]", fnamebuf, bookmark); /* Writing the new bookmark to the begining of the temp file */ @@ -357,7 +372,8 @@ static bool add_bookmark(const char* bookmark_file_name, if (comp_playlist) { if (bookmark_get_playlist_and_track_hash(global_temp_buffer, - &bm_pl_hash, &bm_pl_track_hash)) + &bm_pl_hash, &bm_pl_track_hash, + comp_track)) { equal = (pl_hash == bm_pl_hash); if (equal && comp_track) @@ -1333,6 +1349,67 @@ int bookmark_autoload(const char* file) } } +#ifdef HAVE_AUDIOBOOK_RESUME +/* The track a bookmark line names, without its folder. */ +static const char *bookmark_track_name(const char *bookmark) +{ + const char *track = strrchr(bookmark, ';'); + const char *slash; + + if (!track) + return NULL; + track++; + slash = strrchr(track, '/'); + return slash ? slash + 1 : track; +} + +/* ----------------------------------------------------------------------- */ +/* Autoload for a file picked in the browser: resume that file at its own */ +/* bookmark in its folder's bookmark file, so each book in a folder keeps */ +/* its place. A file with no bookmark there plays from its start. */ +/* Autoload "ask" keeps the upstream list. */ +/* ----------------------------------------------------------------------- */ +int bookmark_autoload_track(const char* dir, const char* track_path) +{ + static char line[MAX_BOOKMARK_SIZE]; + char bm_filename[MAX_PATH]; + const char *want; + bool found = false; + int fd; + + if (global_settings.autoloadbookmark != BOOKMARK_YES) + return bookmark_autoload(dir); + + if (!generate_bookmark_file_name(bm_filename, sizeof(bm_filename), dir, -1)) + return BOOKMARK_DONT_RESUME; + + fd = open(bm_filename, O_RDONLY); + if (fd < 0) + return BOOKMARK_DONT_RESUME; + + want = strrchr(track_path, '/'); + want = want ? want + 1 : track_path; + + /* newest first */ + while (read_line(fd, line, sizeof(line)) > 0) + { + const char *track = bookmark_track_name(line); + if (track && !strcmp(track, want) && + parse_bookmark(NULL, 0, line, NULL, false)) + { + found = true; + break; + } + } + close(fd); + + if (!found) + return BOOKMARK_DONT_RESUME; + logf("autoload %s from %s", want, bm_filename); + return play_bookmark(line) ? BOOKMARK_DO_RESUME : BOOKMARK_DONT_RESUME; +} +#endif /* HAVE_AUDIOBOOK_RESUME */ + /* ----------------------------------------------------------------------- */ /* This function loads the bookmark information into the resume memory. */ /* This is an interface function. */ diff --git a/apps/bookmark.h b/apps/bookmark.h index 192e577c..392488d7 100644 --- a/apps/bookmark.h +++ b/apps/bookmark.h @@ -40,6 +40,9 @@ bool bookmark_autobookmark(bool prompt_ok); bool bookmark_create_menu(void); bool bookmark_mrb_load(void); int bookmark_autoload(const char* file); +#ifdef HAVE_AUDIOBOOK_RESUME +int bookmark_autoload_track(const char* dir, const char* track_path); +#endif bool bookmark_load(const char* file, bool autoload); bool bookmark_exists(void); bool bookmark_is_bookmarkable_state(void); diff --git a/apps/filetree.c b/apps/filetree.c index efbdbf72..e11036df 100644 --- a/apps/filetree.c +++ b/apps/filetree.c @@ -610,7 +610,16 @@ int ft_enter(struct tree_context* c) case FILE_ATTR_AUDIO: { +#ifdef HAVE_AUDIOBOOK_RESUME + /* Picking a file replaces what is playing: keep that + book's place first (upstream only bookmarks on stop), + then resume the picked file at its own bookmark. */ + if (audio_status() && !global_settings.party_mode) + bookmark_autobookmark(false); + int res = bookmark_autoload_track(c->currdir, buf); +#else int res = bookmark_autoload(c->currdir); +#endif if (res == BOOKMARK_CANCEL || res == BOOKMARK_DO_RESUME) break; diff --git a/apps/playback.c b/apps/playback.c index 5e09ae3e..bc5f91c4 100644 --- a/apps/playback.c +++ b/apps/playback.c @@ -1807,6 +1807,25 @@ static void audio_playlist_track_change(void) playlist_update_resume_info(id3); } +#ifdef HAVE_AUDIOBOOK_RESUME +static long audiobook_next_save_tick; + +/* Store the playing position as the resume point and have it written out + soon. Upstream refreshes it only at track changes and stop, so in a + long single-file audiobook a crash or dead battery went back to the + start of the file. */ +static void audiobook_save_position(void) +{ + struct mp3entry *id3 = valid_mp3entry(id3_get(PLAYING_ID3)); + + audiobook_next_save_tick = current_tick + AUDIOBOOK_RESUME_SAVE_TICKS; + if (!id3 || !id3->elapsed) + return; + playlist_update_resume_info(id3); + status_save_now(); +} +#endif /* HAVE_AUDIOBOOK_RESUME */ + /* Change the data for the next track and send the event */ static void audio_update_and_announce_next_track(const struct mp3entry *id3_next) { @@ -3694,6 +3713,11 @@ static void audio_on_pause(bool pause) play_status = pause ? PLAY_PAUSED : PLAY_PLAYING; iap_on_play_status(play_status); +#ifdef HAVE_AUDIOBOOK_RESUME + if (pause && !ff_rw_mode) + audiobook_save_position(); +#endif + if (!pause && codec_skip_pending) { /* Actually do the skip that is due - resets the status flag */ @@ -4202,6 +4226,12 @@ void audio_playback_handler(struct queue_event *ev) break; } /* end switch */ +#ifdef HAVE_AUDIOBOOK_RESUME + if (play_status == PLAY_PLAYING && !ff_rw_mode && + TIME_AFTER(current_tick, audiobook_next_save_tick)) + audiobook_save_position(); +#endif + switch (filling) { /* Active states */ diff --git a/apps/settings.c b/apps/settings.c index ef77a799..62620cc8 100644 --- a/apps/settings.c +++ b/apps/settings.c @@ -748,6 +748,18 @@ void status_save(bool force) register_storage_idle_func(flush_global_status_callback); } +#ifdef HAVE_AUDIOBOOK_RESUME +/* Write the resume point at the next storage idle, skipping the 15 minute + * throttle: playback calls this on pause and every + * AUDIOBOOK_RESUME_SAVE_TICKS while playing, so a crash, RESET or a dead + * battery loses at most that much of a long single-file audiobook. */ +void status_save_now(void) +{ + next_status_update_tick = current_tick - 1; + register_storage_idle_func(flush_global_status_callback); +} +#endif + int settings_save(void) { logf("%s", __func__); diff --git a/apps/settings.h b/apps/settings.h index a67981dc..9a6b67ef 100644 --- a/apps/settings.h +++ b/apps/settings.h @@ -262,6 +262,9 @@ void settings_load(void) INIT_ATTR; bool settings_load_config(const char* file, bool apply); void status_save(bool force); +#ifdef HAVE_AUDIOBOOK_RESUME +void status_save_now(void); +#endif int settings_save(void); /* defines for the options paramater */ diff --git a/apps/settings_list.c b/apps/settings_list.c index e0eda5e7..4f5e2f58 100644 --- a/apps/settings_list.c +++ b/apps/settings_list.c @@ -324,6 +324,22 @@ static const char off[] = "off"; static const char off_on[] = "off,on"; static const char off_on_ask[] = "off,on,ask"; static const char off_number_spell[] = "off,number,spell"; +/* Defaults for an audiobook player (HAVE_AUDIOBOOK_RESUME): bookmark every + * book on stop, power-off and switching books, resume a picked file at its + * bookmark, start in the book that was playing, rewind a little on resume, + * and step 30 s on Left/Right. */ +#ifdef HAVE_AUDIOBOOK_RESUME +#define AUDIOBOOK_DEFAULT(audiobook, upstream) (audiobook) +#else +#define AUDIOBOOK_DEFAULT(audiobook, upstream) (upstream) +#endif +/* "start in screen": previous, root, files, [db,] wps... */ +#ifdef HAVE_TAGCACHE +#define AUDIOBOOK_START_WPS 4 +#else +#define AUDIOBOOK_START_WPS 3 +#endif + static const int timeout_sec_common[] = {-1,0,1,2,3,4,5,6,7,8,9,10,15,20,25,30, 45,60,90,120,180,240,300,600,900,1200, 1500,1800,2700,3600,4500,5400,6300,7200}; @@ -1706,7 +1722,8 @@ const struct settings_list settings[] = { ID2P(LANG_OFF), ID2P(LANG_ON), ID2P(LANG_ASK)), /* bookmarks */ CHOICE_SETTING(0, autocreatebookmark, LANG_BOOKMARK_SETTINGS_AUTOCREATE, - BOOKMARK_NO, "autocreate bookmarks", + AUDIOBOOK_DEFAULT(BOOKMARK_YES, BOOKMARK_NO), + "autocreate bookmarks", "off,on,ask,recent only - on,recent only - ask", NULL, 5, ID2P(LANG_SET_BOOL_NO), ID2P(LANG_SET_BOOL_YES), ID2P(LANG_ASK), ID2P(LANG_BOOKMARK_SETTINGS_RECENT_ONLY_YES), @@ -1714,11 +1731,13 @@ const struct settings_list settings[] = { OFFON_SETTING(0, autoupdatebookmark, LANG_BOOKMARK_SETTINGS_AUTOUPDATE, false, "autoupdate bookmarks", NULL), CHOICE_SETTING(0, autoloadbookmark, LANG_BOOKMARK_SETTINGS_AUTOLOAD, - BOOKMARK_NO, "autoload bookmarks", off_on_ask, NULL, 3, + AUDIOBOOK_DEFAULT(BOOKMARK_YES, BOOKMARK_NO), + "autoload bookmarks", off_on_ask, NULL, 3, ID2P(LANG_SET_BOOL_NO), ID2P(LANG_SET_BOOL_YES), ID2P(LANG_ASK)), CHOICE_SETTING(0, usemrb, LANG_BOOKMARK_SETTINGS_MAINTAIN_RECENT_BOOKMARKS, - BOOKMARK_NO, "use most-recent-bookmarks", + AUDIOBOOK_DEFAULT(BOOKMARK_ONE_PER_TRACK, BOOKMARK_NO), + "use most-recent-bookmarks", "off,on,unique only,one per track", NULL, 4, ID2P(LANG_SET_BOOL_NO), ID2P(LANG_SET_BOOL_YES), ID2P(LANG_BOOKMARK_SETTINGS_ONE_PER_PLAYLIST), @@ -2226,7 +2245,8 @@ const struct settings_list settings[] = { OFFON_SETTING(0, bt_autoconnect, LANG_BLUETOOTH_AUTOCONNECT, true, "bluetooth auto-connect", NULL), #endif - INT_SETTING(F_TIME_SETTING, pause_rewind, LANG_PAUSE_REWIND, 0, + INT_SETTING(F_TIME_SETTING, pause_rewind, LANG_PAUSE_REWIND, + AUDIOBOOK_DEFAULT(2, 0), "rewind duration on pause", UNIT_SEC, 0, 15, 1, formatter_time_unit_0_is_off, getlang_time_unit_0_is_off, NULL), #if CONFIG_TUNER @@ -2287,12 +2307,15 @@ const struct settings_list settings[] = { NULL), #endif TABLE_SETTING_LIST(F_TIME_SETTING | F_ALLOW_ARBITRARY_VALS, skip_length, - LANG_SKIP_LENGTH, 0, "skip length", + LANG_SKIP_LENGTH, AUDIOBOOK_DEFAULT(30, 0), "skip length", "outro,track", UNIT_SEC, formatter_time_unit_0_is_skip_track, getlang_time_unit_0_is_skip_track, NULL, 25, timeout_sec_common), - CHOICE_SETTING(F_CB_ON_SELECT_ONLY, start_in_screen, LANG_START_SCREEN, 1, + CHOICE_SETTING(F_CB_ON_SELECT_ONLY, start_in_screen, LANG_START_SCREEN, + /* audiobook: "Resume Playback" (main menu without + anything to resume) */ + AUDIOBOOK_DEFAULT(AUDIOBOOK_START_WPS, 1), "start in screen", "previous,root,files," #ifdef HAVE_TAGCACHE #define START_DB_COUNT 1 @@ -2561,7 +2584,8 @@ const struct settings_list settings[] = { #endif /* HAVE_HOTKEY */ - INT_SETTING(F_TIME_SETTING, resume_rewind, LANG_RESUME_REWIND, 0, + INT_SETTING(F_TIME_SETTING, resume_rewind, LANG_RESUME_REWIND, + AUDIOBOOK_DEFAULT(5, 0), "resume rewind", UNIT_SEC, 0, 60, 5, formatter_time_unit_0_is_off, getlang_time_unit_0_is_off, NULL), CUSTOM_SETTING(0, root_menu_customized, diff --git a/firmware/export/config/yp3.h b/firmware/export/config/yp3.h index d43aa9ed..07614b6b 100644 --- a/firmware/export/config/yp3.h +++ b/firmware/export/config/yp3.h @@ -250,6 +250,13 @@ * a whole 40 KiB frame of heap, and cabbiev2's two left playback without * its buffer (OOM panic). Skins keep their colours, fonts and icons. */ #define HAVE_NO_BACKDROP_FILES +/* Audiobook player: the resume point is written on pause and every + * AUDIOBOOK_RESUME_SAVE_TICKS while playing (not just at track changes and + * stop), each book in a folder keeps its own bookmark, switching books + * bookmarks the one playing, and the defaults bookmark and resume + * (apps/playback.c, bookmark.c, filetree.c, settings_list.c). */ +#define HAVE_AUDIOBOOK_RESUME +#define AUDIOBOOK_RESUME_SAVE_TICKS (60 * HZ) /* No surround: its 31 KB delay buffer comes out of the playback buffer, * which then stayed shrunk (stutter until a reset) */ #define HAVE_NO_SURROUND -- 2.56.1