From 74dd258b621dd9409ef595f62f6bdb5d1ee1aab3 Mon Sep 17 00:00:00 2001 From: Mateusz Faderewski Date: Mon, 14 Aug 2023 22:00:14 +0200 Subject: [PATCH] MP3 player seeking improvement --- src/menu/menu.c | 2 +- src/menu/mp3_player.c | 29 ++++++++++++++++++----------- src/menu/views/music_player.c | 17 +---------------- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/src/menu/menu.c b/src/menu/menu.c index 6b32eaef..fa339bc4 100644 --- a/src/menu/menu.c +++ b/src/menu/menu.c @@ -40,7 +40,7 @@ static void menu_init (boot_params_t *boot_params) { controller_init(); timer_init(); rtc_init(); - audio_init(44100, 3); + audio_init(44100, 2); mixer_init(2); rspq_init(); rdpq_init(); diff --git a/src/menu/mp3_player.c b/src/menu/mp3_player.c index 0788abd7..76e43da4 100644 --- a/src/menu/mp3_player.c +++ b/src/menu/mp3_player.c @@ -6,11 +6,13 @@ #include "utils/utils.h" #define MINIMP3_IMPLEMENTATION +#define MINIMP3_ONLY_MP3 #include #include -#define MIXER_CHANNEL (0) +#define MIXER_CHANNEL (0) +#define SEEK_PREDECODE_FRAMES (5) typedef struct { @@ -19,11 +21,12 @@ typedef struct { FIL fil; FSIZE_t data_start; + int seek_predecode_frames; mp3dec_t dec; mp3dec_frame_info_t info; - uint8_t buffer[16 * 1024]; + uint8_t buffer[MAX_FREE_FORMAT_FRAME_SIZE]; uint8_t *buffer_ptr; size_t buffer_left; @@ -38,6 +41,7 @@ static mp3player_t *p = NULL; static void mp3player_reset_decoder (void) { mp3dec_init(&p->dec); + p->seek_predecode_frames = 0; p->buffer_ptr = p->buffer; p->buffer_left = 0; } @@ -49,10 +53,6 @@ static void mp3player_fill_buffer (void) { return; } - if (p->buffer_left >= (MAX_FREE_FORMAT_FRAME_SIZE * 3)) { - return; - } - if ((p->buffer_ptr != p->buffer) && (p->buffer_left > 0)) { memmove(p->buffer, p->buffer_ptr, p->buffer_left); p->buffer_ptr = p->buffer; @@ -79,6 +79,11 @@ static void mp3player_wave_read (void *ctx, samplebuffer_t *sbuf, int wpos, int mp3dec_decode_frame(&p->dec, p->buffer_ptr, p->buffer_left, buffer, &p->info); + if (p->seek_predecode_frames > 0) { + p->seek_predecode_frames -= 1; + memset(buffer, 0, samples * sizeof(short) * p->info.channels); + } + wlen -= samples; } @@ -229,7 +234,7 @@ bool mp3player_is_playing (void) { } bool mp3player_is_finished (void) { - return p->loaded && f_eof(&p->fil) && p->buffer_left == 0; + return p->loaded && f_eof(&p->fil) && (p->buffer_left == 0); } mp3player_err_t mp3player_play (void) { @@ -265,7 +270,7 @@ mp3player_err_t mp3player_toggle (void) { } void mp3player_mute (bool mute) { - float volume = mute ? 0.f : 1.f; + float volume = mute ? 0.0f : 1.0f; mixer_ch_set_vol(MIXER_CHANNEL, volume, volume); } @@ -295,6 +300,8 @@ mp3player_err_t mp3player_seek (int seconds) { mp3player_reset_decoder(); mp3player_fill_buffer(); + p->seek_predecode_frames = (position == p->data_start) ? 0 : SEEK_PREDECODE_FRAMES; + if (p->io_error) { return MP3PLAYER_ERR_IO; } @@ -304,7 +311,7 @@ mp3player_err_t mp3player_seek (int seconds) { float mp3player_get_duration (void) { if (!p->loaded) { - return 0.f; + return 0.0f; } return p->duration; @@ -312,7 +319,7 @@ float mp3player_get_duration (void) { float mp3player_get_bitrate (void) { if (!p->loaded) { - return 0.f; + return 0.0f; } return p->bitrate; @@ -331,7 +338,7 @@ float mp3player_get_progress (void) { // Good enough but not very accurate for variable bitrate files. if (!p->loaded) { - return 0.f; + return 0.0f; } FSIZE_t data_size = f_size(&p->fil) - p->data_start; diff --git a/src/menu/views/music_player.c b/src/menu/views/music_player.c index dc9a670b..c85ad3c3 100644 --- a/src/menu/views/music_player.c +++ b/src/menu/views/music_player.c @@ -2,11 +2,7 @@ #include "views.h" -#define SEEK_SECONDS 10 -#define SEEK_UNMUTE_TIMEOUT 17 - - -static int unmute_counter; +#define SEEK_SECONDS (5) static char *convert_error_message (mp3player_err_t err) { @@ -39,13 +35,6 @@ static void format_elapsed_duration (char *buffer, float elapsed, float duration static void process (menu_t *menu) { mp3player_err_t err; - if (unmute_counter > 0) { - unmute_counter -= 1; - if (unmute_counter == 0) { - mp3player_mute(false); - } - } - err = mp3player_process(); if (err != MP3PLAYER_OK) { menu_show_error(menu, convert_error_message(err)); @@ -57,8 +46,6 @@ static void process (menu_t *menu) { menu_show_error(menu, convert_error_message(err)); } } else if (menu->actions.go_left || menu->actions.go_right) { - mp3player_mute(true); - unmute_counter = SEEK_UNMUTE_TIMEOUT; int seconds = (menu->actions.go_left ? -SEEK_SECONDS : SEEK_SECONDS); err = mp3player_seek(seconds); if (err != MP3PLAYER_OK) { @@ -129,8 +116,6 @@ static void deinit (void) { void view_music_player_init (menu_t *menu) { mp3player_err_t err; - unmute_counter = 0; - err = mp3player_init(); if (err != MP3PLAYER_OK) { menu_show_error(menu, convert_error_message(err));