playqueue: Implement length tracking

I use this to know the running time of the playqueue, which will be
displayed on the GUI.

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2014-01-02 21:58:18 -05:00 committed by Anna Schumaker
parent 82a57ec2b5
commit e889ca538f
6 changed files with 115 additions and 29 deletions

View File

@ -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<track_id> tracks;
list<sort_t> sort_order;
list<sort_t> sort_order; /* default = { SORT_ARTIST_ASC,
SORT_YEAR_ASC,
SORT_TRACK_ASC };
unsigned int cur;
unsigned int flags;
unsigned int length;

View File

@ -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<track_id> tracks;
list<sort_t> sort_order;
list<sort_t> sort_order; /* default = { SORT_ARTIST_ASC,
SORT_YEAR_ASC,
SORT_TRACK_ASC };
unsigned int cur;
unsigned int flags;
unsigned int length;

View File

@ -5,6 +5,7 @@
#define OCARINA_PLAYQUEUE_H
#include <file.h>
#include <library.h>
#include <vector>
enum playqueue_flags {
@ -18,6 +19,7 @@ private:
std :: vector <unsigned int> 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();

View File

@ -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" ]),
###########################
###########################

View File

@ -1,16 +1,17 @@
/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <library.h>
#include <playqueue.h>
#include <stdlib.h>
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;

View File

@ -1,6 +1,7 @@
/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <idle.h>
#include <playqueue.h>
#include <print.h>
@ -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();