libsaria: Randomly pick next song from library

I'll eventually allow more playlists to be stacked, but for now picking
from the library is easy.  I may eventually add in a way to pick
sequentially from a set.

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-03-24 08:41:30 -04:00
parent 07c365fcc0
commit ac3964de01
8 changed files with 57 additions and 16 deletions

View File

@ -27,6 +27,8 @@ namespace libsaria
class Playlist {
private:
unsigned int flags;
list<Track *>::iterator cur;
void incr_iter();
protected:
string name;
@ -34,17 +36,15 @@ namespace libsaria
PlaylistRenderer *renderer;
/*bool del_renderer;
unsigned int flags;
string filename;
list<Track *> plist;
list<Track *>::iterator cur;
PlaylistRenderer *renderer;
string filename;*/
void init_common(string, unsigned int);
/*void init_common(string, unsigned int);
void incr_iter(list<Track *>::iterator &);
bool pick_next(list<Track *>::iterator &);
void sequential_next();
void random_next();*/
Track *pick_random();
void add_sorted(list<Track *> &);
public:
@ -53,6 +53,7 @@ namespace libsaria
~Playlist();
void set_renderer(PlaylistRenderer *);
virtual Track *next() = 0;
string &get_name();
unsigned int get_size();
@ -86,6 +87,7 @@ namespace libsaria
Set(string, unsigned int);
~Set();
Track *next();
void add_tracks(list<Track *> &);
};

View File

@ -13,6 +13,7 @@ namespace libsaria
{
void push(Playlist *);
void next();
};
@ -20,7 +21,6 @@ namespace libsaria
//void add_to_playlist(list<sid_t> &);
Playlist *stack_top();
void stack_top_pop();
string next_file();
void stack_init();
}; /* Namespace: libsaria */

View File

@ -4,6 +4,7 @@
#include <libsaria/print.h>
#include <libsaria/library.h>
#include <libsaria/callback.h>
#include <libsaria/stack.h>
#include "audio.h"
GstElement *player = NULL;
@ -38,7 +39,7 @@ static gboolean on_message(GstBus *bus, GstMessage *message, gpointer data)
case GST_MESSAGE_ERROR:
parse_error(message);
case GST_MESSAGE_EOS:
/*libsaria::next();*/
libsaria::stack::next();
default:
break;
}

View File

@ -14,17 +14,17 @@ static inline bool choose_randomly(unsigned int flags)
return true; //libsaria::prefs::get_bool("random");
return (flags & PL_RANDOM) == PL_RANDOM;
}
*/
namespace libsaria
{
void Playlist::incr_iter(list<Track *>::iterator &it)
void Playlist::incr_iter()
{
it++;
if (it == plist.end())
it = plist.begin();
cur++;
if (cur == plist.end())
cur = plist.begin();
}
/*
bool Playlist::pick_next(list<Track *>::iterator &it)
{
incr_iter(it);
@ -72,9 +72,23 @@ namespace libsaria
}*/
/* }
}
}*/
Track *Playlist::pick_random()
{
unsigned int n, size = get_size();
if (size == 0)
return NULL;
n = rand() % ((size * 3) / 4);
for (unsigned int i = 0; i < n; i++)
incr_iter();
return (*cur);
}
Track *Playlist::next()
/*Track *Playlist::next()
{
int index;
Track *track;
@ -99,6 +113,6 @@ namespace libsaria
}
return track;
}
}*/
};*/ /* Namespace: libsaria */
}; /* Namespace: libsaria */

View File

@ -45,6 +45,7 @@ namespace libsaria
name = n;
flags = f;
renderer = NULL;
cur = plist.begin();
}
Playlist::~Playlist()

View File

@ -17,4 +17,9 @@ namespace libsaria
add_sorted(tracks);
}
Track *Set::next()
{
return pick_random();
}
}; /* Namespace: libsaria */

View File

@ -2,6 +2,7 @@
#include <libsaria/playlist.h>
#include <libsaria/callback.h>
#include <libsaria/stack.h>
#include <libsaria/track.h>
#include <list>
#include <sstream>
@ -22,6 +23,14 @@ namespace libsaria
playlist_stack.push_front(plist);
}
void stack::next()
{
Playlist *plist = playlist_stack.front();
Track *track = plist->next();
if (track)
track->play_now();
}
Playlist *new_playlist(string file, unsigned int flags)
{
/*Playlist *plist = new Playlist(file, flags);

View File

@ -4,6 +4,7 @@
#include <ocarina/body.h>
#include <libsaria/audio.h>
#include <libsaria/stack.h>
static GtkWidget *notebook;
static GtkWidget *idle_progress;
@ -13,6 +14,7 @@ static GtkWidget *dur_label;
static GtkWidget *play_button;
static GtkWidget *pause_button;
static GtkWidget *stop_button;
static GtkWidget *next_button;
static GstState last_state;
static void add_page(string text, GtkWidget *page)
@ -64,6 +66,11 @@ static void on_click_stop(GtkWidget *b, GdkEvent *e, gpointer d)
libsaria::audio::stop();
}
static void on_click_next(GtkWidget *b, GdkEvent *e, gpointer d)
{
libsaria::stack::next();
}
static GtkWidget *make_controls()
{
GtkWidget *box = gtk_hbox_new(FALSE, 0);
@ -71,6 +78,7 @@ static GtkWidget *make_controls()
play_button = make_button(GTK_STOCK_MEDIA_PLAY, on_click_play, true);
pause_button = make_button(GTK_STOCK_MEDIA_PAUSE, on_click_pause, false);
stop_button = make_button(GTK_STOCK_MEDIA_STOP, on_click_stop, true);
next_button = make_button(GTK_STOCK_MEDIA_NEXT, on_click_next, true);
pos_label = gtk_label_new("");
track_progress = gtk_hscale_new_with_range(0, 1, 1000000000);
@ -87,6 +95,7 @@ static GtkWidget *make_controls()
gtk_box_pack_start(GTK_BOX(box), play_button, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(box), pause_button, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(box), stop_button, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(box), next_button, FALSE, FALSE, 0);
gtk_widget_show(pos_label);
gtk_widget_show(dur_label);