Skip to content

Commit

Permalink
refactor checking if items belong to same album
Browse files Browse the repository at this point in the history
  • Loading branch information
rsekman committed Oct 9, 2023
1 parent bf751b6 commit 3d92509
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 59 deletions.
61 changes: 28 additions & 33 deletions src/playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions src/playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 1 addition & 26 deletions src/streamer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 3d92509

Please sign in to comment.