diff --git a/design.txt b/design.txt index 52763a7f..2e3e750a 100644 --- a/design.txt +++ b/design.txt @@ -736,22 +736,33 @@ Playqueue: (lib/playqueue.cpp) - Sort order: enum sort_t { - SORT_ARTIST = 1, - SORT_ALBUM = 2, - SORT_COUNT = 3, - SORT_GENRE = 4, - SORT_LENGTH = 5, - SORT_PLAYED = 6, - SORT_TITLE = 7, - SORT_TRACK = 8, - SORT_YEAR = 9, + SORT_ARTIST_ASC = 1, + SORT_ARTIST_DESC = 2, + SORT_ALBUM_ASC = 3, + SORT_ALBUM_DESC = 4, + SORT_COUNT_ASC = 5, + SORT_COUNT_DESC = 6, + SORT_GENRE_ASC = 7, + SORT_GENRE_DESC = 8, + SORT_LENGTH_ASC = 9, + SORT_LENGTH_DESC = 10, + SORT_PLAYED_ASC = 11, + SORT_PLAYED_DESC = 12, + SORT_TITLE_ASC = 13, + SORT_TITLE_DESC = 14, + SORT_TRACK_ASC = 15, + SORT_TRACK_DESC = 16, + SORT_YEAR_ASC = 17, + SORT_YEAR_DESC = 18, }; - Playqueue: class Playqueue { private: vector tracks; - list sort_order; + list sort_order; /* default = { SORT_ARTIST_ASC, + SORT_YEAR_ASC, + SORT_TRACK_ASC }; unsigned int cur; unsigned int flags; unsigned int length; diff --git a/design/playqueue.txt b/design/playqueue.txt index 5b45fc7a..daab7fdd 100644 --- a/design/playqueue.txt +++ b/design/playqueue.txt @@ -19,22 +19,33 @@ Playqueue: (lib/playqueue.cpp) - Sort order: enum sort_t { - SORT_ARTIST = 1, - SORT_ALBUM = 2, - SORT_COUNT = 3, - SORT_GENRE = 4, - SORT_LENGTH = 5, - SORT_PLAYED = 6, - SORT_TITLE = 7, - SORT_TRACK = 8, - SORT_YEAR = 9, + SORT_ARTIST_ASC = 1, + SORT_ARTIST_DESC = 2, + SORT_ALBUM_ASC = 3, + SORT_ALBUM_DESC = 4, + SORT_COUNT_ASC = 5, + SORT_COUNT_DESC = 6, + SORT_GENRE_ASC = 7, + SORT_GENRE_DESC = 8, + SORT_LENGTH_ASC = 9, + SORT_LENGTH_DESC = 10, + SORT_PLAYED_ASC = 11, + SORT_PLAYED_DESC = 12, + SORT_TITLE_ASC = 13, + SORT_TITLE_DESC = 14, + SORT_TRACK_ASC = 15, + SORT_TRACK_DESC = 16, + SORT_YEAR_ASC = 17, + SORT_YEAR_DESC = 18, }; - Playqueue: class Playqueue { private: vector tracks; - list sort_order; + list sort_order; /* default = { SORT_ARTIST_ASC, + SORT_YEAR_ASC, + SORT_TRACK_ASC }; unsigned int cur; unsigned int flags; unsigned int length; diff --git a/include/playqueue.h b/include/playqueue.h index ad4843b1..d2e3b97c 100644 --- a/include/playqueue.h +++ b/include/playqueue.h @@ -5,6 +5,7 @@ #define OCARINA_PLAYQUEUE_H #include +#include #include enum playqueue_flags { @@ -18,6 +19,7 @@ private: std :: vector tracks; unsigned int flags; unsigned int cur; + unsigned int length; public: Playqueue(); @@ -29,8 +31,10 @@ public: void set_flag(playqueue_flags); void unset_flag(playqueue_flags); const unsigned int get_flags(); + unsigned int get_length(); unsigned int add(unsigned int); + unsigned int add_front(unsigned int); void del(unsigned int); unsigned int size(); diff --git a/lib/Sconscript b/lib/Sconscript index 270bc0b2..c3298b3a 100644 --- a/lib/Sconscript +++ b/lib/Sconscript @@ -23,7 +23,7 @@ modules = { "IDLE" : Module("idle.cpp"), "LIBRARY" : Module("library.cpp", package = "taglib", depends = [ "DATABASE", "FILTER", "IDLE" ]), "PLAYLIST" : Module("playlist.cpp", depends = [ "DATABASE" ]), - "PLAYQUEUE" : Module("playqueue.cpp", depends = [ "FILE" ]), + "PLAYQUEUE" : Module("playqueue.cpp", depends = [ "LIBRARY" ]), ########################### ########################### diff --git a/lib/playqueue.cpp b/lib/playqueue.cpp index b43cddc2..f6996b19 100644 --- a/lib/playqueue.cpp +++ b/lib/playqueue.cpp @@ -1,16 +1,17 @@ /* * Copyright 2013 (c) Anna Schumaker. */ +#include #include #include Playqueue :: Playqueue() - : flags(0), cur(-1) + : flags(0), cur(-1), length(0) { } Playqueue :: Playqueue(playqueue_flags f) - : flags(f), cur(-1) + : flags(f), cur(-1), length(0) { } @@ -49,15 +50,39 @@ const unsigned int Playqueue :: get_flags() return flags; } +unsigned int Playqueue :: get_length() +{ + return length; +} + unsigned int Playqueue :: add(unsigned int track_id) { + library :: Song song; tracks.push_back(track_id); + + library :: lookup(track_id, &song); + length += song.track->length; return tracks.size() - 1; } +unsigned int Playqueue :: add_front(unsigned int track_id) +{ + library :: Song song; + tracks.insert(tracks.begin(), track_id); + + library :: lookup(track_id, &song); + length += song.track->length; + return 0; +} + void Playqueue :: del(unsigned int plist_id) { + library :: Song song; + unsigned int track_id = tracks[plist_id]; + tracks.erase(tracks.begin() + plist_id); + library :: lookup(track_id, &song); + length -= song.track->length; } unsigned int Playqueue :: size() @@ -81,7 +106,7 @@ unsigned int Playqueue :: next() res = tracks[cur]; if (!(flags & PQ_REPEAT)) { - tracks.erase(tracks.begin() + cur); + del(cur); cur--; } return res; diff --git a/tests/playqueue/playqueue.cpp b/tests/playqueue/playqueue.cpp index 614741aa..cb839e1b 100644 --- a/tests/playqueue/playqueue.cpp +++ b/tests/playqueue/playqueue.cpp @@ -1,6 +1,7 @@ /* * Copyright 2013 (c) Anna Schumaker. */ +#include #include #include @@ -32,6 +33,23 @@ void test_add_tracks(const std :: string & test, Playqueue &pqueue, print("FAILED\n"); } +void test_add_tracks_front(const std :: string &test, Playqueue &pqueue, + unsigned int n) +{ + bool passed = true; + + print("Test %s: ", test.c_str()); + for (unsigned int i = 0; i < n; i++) { + if (pqueue.add_front(i) != 0) + passed = false; + } + + if (passed == true) + print("SUCCESS\n"); + else + print("FAILED\n"); +} + void test_rm_tracks(const std :: string & test, Playqueue & pqueue, unsigned int n) { unsigned int size = pqueue.size(); @@ -48,6 +66,12 @@ void test_rm_tracks(const std :: string & test, Playqueue & pqueue, unsigned int print("FAILED\n"); } +void test_pqueue_status(const std :: string &test, Playqueue &pqueue) +{ + print("Test %s: size: %u, length: %u\n", test.c_str(), + pqueue.size(), pqueue.get_length()); +} + /* Test flag setting / unsetting / getting */ void test_0() { @@ -70,8 +94,11 @@ void test_1() { Playqueue pqueue(PQ_ENABLED); test_add_tracks("1a", pqueue, 10, 49); - print("Test 1b: Plist size: %u\n", pqueue.size()); + test_pqueue_status("1b", pqueue); test_rm_tracks("1c", pqueue, 10); + test_pqueue_status("1d", pqueue); + test_add_tracks_front("1e", pqueue, 5); + test_pqueue_status("1f", pqueue); print("\n"); } @@ -111,9 +138,10 @@ void test_3() { Playqueue pqueue(PQ_ENABLED); pqueue.set_flag(PQ_REPEAT); - test_add_tracks("3", pqueue, 0, 15); + test_add_tracks("3a", pqueue, 0, 15); for (unsigned int i = 0; i < 20; i++) print("Selecting id: %u\n", pqueue.next()); + test_pqueue_status("3b", pqueue); print("\n"); } @@ -121,9 +149,10 @@ void test_3() void test_4() { Playqueue pqueue(PQ_ENABLED); - test_add_tracks("4", pqueue, 0, 15); + test_add_tracks("4a", pqueue, 0, 15); while (pqueue.size() > 0) print("Selecting id: %u\n", pqueue.next()); + test_pqueue_status("4b", pqueue); print("\n"); } @@ -133,9 +162,10 @@ void test_5() Playqueue pqueue(PQ_ENABLED); pqueue.set_flag(PQ_RANDOM); pqueue.set_flag(PQ_REPEAT); - test_add_tracks("5", pqueue, 0, 15); + test_add_tracks("5a", pqueue, 0, 15); for (unsigned int i = 0; i < 30; i++) print("Selecting id: %u\n", pqueue.next()); + test_pqueue_status("5b", pqueue); print("\n"); } @@ -144,14 +174,19 @@ void test_6() { Playqueue pqueue(PQ_ENABLED); pqueue.set_flag(PQ_RANDOM); - test_add_tracks("6", pqueue, 0, 15); + test_add_tracks("6a", pqueue, 0, 15); while (pqueue.size() > 0) print("Selecting id: %u\n", pqueue.next()); + test_pqueue_status("6b", pqueue); } int main(int argc, char **argv) { srand(42); + library :: init(); + library :: reset(); + library :: add_path("/tmp/library/0"); + while (idle :: run_task()); test_0(); test_1();