From 3d925097e89801707c4cf525eb9637c588037485 Mon Sep 17 00:00:00 2001 From: Robin Ekman Date: Tue, 1 Feb 2022 20:55:04 +0100 Subject: [PATCH] refactor checking if items belong to same album --- src/playlist.c | 61 +++++++++++++++++++++++--------------------------- src/playlist.h | 3 +++ src/streamer.c | 27 +--------------------- 3 files changed, 32 insertions(+), 59 deletions(-) diff --git a/src/playlist.c b/src/playlist.c index 66bb751470..83fb9f97d3 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -1679,24 +1679,7 @@ plt_insert_item (playlist_t *playlist, playItem_t *after, playItem_t *it) { // shuffle playItem_t *prev = it->prev[PL_MAIN]; - const char *aa = NULL, *prev_aa = NULL; - if (prev) { - aa = pl_find_meta_raw (it, "band"); - if (!aa) { - aa = pl_find_meta_raw (it, "album artist"); - } - if (!aa) { - aa = pl_find_meta_raw (it, "albumartist"); - } - prev_aa = pl_find_meta_raw (prev, "band"); - if (!prev_aa) { - prev_aa = pl_find_meta_raw (prev, "album artist"); - } - if (!prev_aa) { - prev_aa = pl_find_meta_raw (prev, "albumartist"); - } - } - if (streamer_get_shuffle () == DDB_SHUFFLE_ALBUMS && prev && pl_find_meta_raw (prev, "album") == pl_find_meta_raw (it, "album") && ((aa && prev_aa && aa == prev_aa) || pl_find_meta_raw (prev, "artist") == pl_find_meta_raw (it, "artist"))) { + if (streamer_get_shuffle () == DDB_SHUFFLE_ALBUMS && prev && pl_items_from_same_album(prev, it)) { it->shufflerating = prev->shufflerating; } else { @@ -2610,27 +2593,13 @@ plt_reshuffle (playlist_t *playlist, playItem_t **ppmin, playItem_t **ppmax) { playItem_t *pmin = NULL; playItem_t *pmax = NULL; playItem_t *prev = NULL; - const char *alb = NULL; - const char *art = NULL; - const char *aa = NULL; for (playItem_t *it = playlist->head[PL_MAIN]; it; it = it->next[PL_MAIN]) { - const char *new_aa = NULL; - new_aa = pl_find_meta_raw (it, "band"); - if (!new_aa) { - new_aa = pl_find_meta_raw (it, "album artist"); - } - if (!new_aa) { - new_aa = pl_find_meta_raw (it, "albumartist"); - } - if (streamer_get_shuffle () == DDB_SHUFFLE_ALBUMS && prev && alb == pl_find_meta_raw (it, "album") && ((aa && new_aa && aa == new_aa) || art == pl_find_meta_raw (it, "artist"))) { + if (streamer_get_shuffle () == DDB_SHUFFLE_ALBUMS && prev && pl_items_from_same_album(it, prev)) { it->shufflerating = prev->shufflerating; } else { prev = it; it->shufflerating = rand (); - alb = pl_find_meta_raw (it, "album"); - art = pl_find_meta_raw (it, "artist"); - aa = new_aa; } if (!pmin || it->shufflerating < pmin->shufflerating) { pmin = it; @@ -4170,3 +4139,29 @@ pl_set_shufflerating (playItem_t *it, int rating) { it->shufflerating = rating; pl_unlock(); } + +int +pl_items_from_same_album (playItem_t* a, playItem_t* b){ + const char *keys[] = { + "band", + "album artist", + "albumartist", + "artist", + NULL + }; + + const char *a_artist = NULL; + const char *b_artist = NULL; + for (int i = 0; keys[i]; i++) { + if (!a_artist) { + a_artist = pl_find_meta_raw (a, keys[i]); + } + if (!b_artist) { + b_artist = pl_find_meta_raw (b, keys[i]); + } + if (a_artist && b_artist) { + break; + } + } + return pl_find_meta_raw(a, "album") == pl_find_meta_raw (b, "album") && a_artist == b_artist; +} diff --git a/src/playlist.h b/src/playlist.h index f4614553ce..22a8eafb2e 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -563,6 +563,9 @@ pl_get_shufflerating (playItem_t *it); void pl_set_shufflerating (playItem_t *it, int rating); +int +pl_items_from_same_album(playItem_t* a, playItem_t* b); + #ifdef __cplusplus } #endif diff --git a/src/streamer.c b/src/streamer.c index 720bbcca95..8cb93ad9e5 100644 --- a/src/streamer.c +++ b/src/streamer.c @@ -493,32 +493,7 @@ stop_after_album_check (playItem_t *cur, playItem_t *next) { return 1; } - const char *cur_album = pl_find_meta_raw (cur, "album"); - const char *next_album = pl_find_meta_raw (next, "album"); - - const char *keys[] = { - "band", - "album artist", - "albumartist", - "artist", - NULL - }; - - const char *cur_artist = NULL; - const char *next_artist = NULL; - for (int i = 0; keys[i]; i++) { - if (!cur_artist) { - cur_artist = pl_find_meta_raw (cur, keys[i]); - } - if (!next_artist) { - next_artist = pl_find_meta_raw (next, keys[i]); - } - if (cur_artist && next_artist) { - break; - } - } - - if (cur_artist == next_artist && cur_album == next_album) { + if (pl_items_from_same_album(cur, next)) { return 0; }