libsaria: Pick a random song from the playlist

I do this if PL_RANDOM is set before checking PL_SEQUENTIAL.  I still
need to respond to the case that both flags are set...

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-02-22 10:10:17 -05:00
parent d075773682
commit d902de95cc
3 changed files with 22 additions and 38 deletions

View File

@ -31,6 +31,7 @@ namespace libsaria
void incr_iter(list<Track *>::iterator &);
void sequential_next();
void random_next();
public:
Playlist(unsigned int);

View File

@ -8,46 +8,9 @@
#include <stdlib.h>
/*
* I initialize the iterator to the end of the list so
* the code that handling looping will reset the iterator
* to the beginning of the list
*/
static list<libsaria::Track *>::iterator current = track_list.end();
static inline void incr_iter()
{
current++;
if (current == track_list.end())
current = track_list.begin();
}
static void find_id(sid_t songid)
{
for (current = track_list.begin(); current != track_list.end(); current++) {
if ((*current)->get_songid() == songid)
return;
}
}
static void random_next()
{
if (libsaria::index::is_filtered())
find_id( libsaria::index::rand_id() );
else {
unsigned int n = rand() % track_list.size();
for (unsigned int i = 0; i < n; i++)
incr_iter();
}
}
static string pick_next()
{
libsaria::Track *cur;
if (libsaria::prefs::get_bool("random") == true) {
random_next();
return (*current)->get_filepath();
}
cur = lib_playlist.next();
return cur->get_filepath();
}

View File

@ -2,6 +2,8 @@
#include <libsaria/playlist.h>
#include <libsaria/index.h>
#include <stdlib.h>
namespace libsaria
{
@ -33,11 +35,29 @@ namespace libsaria
cur = it;
}
void Playlist::random_next()
{
unsigned int n = rand() % apparent_size();
for (unsigned int i = 0; i < n; i++) {
incr_iter(cur);
if (flags & PL_FILTER) {
sid_t songid = (*cur)->get_songid();
while (!index::is_visible(songid)) {
incr_iter(cur);
songid = (*cur)->get_songid();
}
}
}
}
Track *Playlist::next()
{
Track *track;
if (flags & PL_SEQUENTIAL)
if (flags & PL_RANDOM)
random_next();
else if (flags & PL_SEQUENTIAL)
sequential_next();
track = (*cur);