155 lines
3.2 KiB
C++
155 lines
3.2 KiB
C++
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <list>
|
|
using namespace std;
|
|
|
|
#include <libsaria/audio.h>
|
|
#include <libsaria/prefs.h>
|
|
#include <libsaria/print.h>
|
|
#include "library.h"
|
|
|
|
static list<TrackTag *> play_list;
|
|
static list<TrackTag *>::iterator cur_track = play_list.end();
|
|
|
|
static string format(string s)
|
|
{
|
|
string res;
|
|
char c, diff = 'a' - 'A';
|
|
|
|
/*
|
|
* TODO: Break into words based on spaces and format each word
|
|
* before joining. Turn numbers into their word form (3 -> three,
|
|
* 20 -> twenty)
|
|
*/
|
|
for (unsigned int i = 0; i < s.size(); i++) {
|
|
c = s[i];
|
|
if ( (c >= 'a') && (c <= 'z') )
|
|
res += c;
|
|
else if ( (c >= 'A') && (c <= 'Z') )
|
|
res += (c + diff);
|
|
if ( (c >= '0') && (c <= '9') )
|
|
res += c;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
static inline bool compare_tracks(TrackTag *one, TrackTag *two)
|
|
{
|
|
return one->get_track() < two->get_track();
|
|
}
|
|
|
|
static inline bool compare_albums(TrackTag *one, TrackTag *two)
|
|
{
|
|
string s_one = format(one->get_album());
|
|
string s_two = format(two->get_album());
|
|
if (s_one < s_two)
|
|
return true;
|
|
else if (s_one == s_two)
|
|
return compare_tracks(one, two);
|
|
return false;
|
|
}
|
|
|
|
static bool compare_tracktags(TrackTag *one, TrackTag *two)
|
|
{
|
|
string s_one = format(one->get_artist());
|
|
string s_two = format(two->get_artist());
|
|
if (s_one == "")
|
|
return false;
|
|
if ( (s_one < s_two) || (s_two == "") )
|
|
return true;
|
|
else if (s_one == s_two)
|
|
return compare_albums(one, two);
|
|
return false;
|
|
}
|
|
|
|
static bool find_id(ino_t &inode, list<TrackTag *>::iterator &it)
|
|
{
|
|
for (it = play_list.begin(); it != play_list.end(); it++) {
|
|
if ((*it)->get_inode() == inode)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static void insert_list(list<TrackTag> *tag_list)
|
|
{
|
|
list<TrackTag>::iterator it;
|
|
for (it = tag_list->begin(); it != tag_list->end(); it++)
|
|
play_list.push_back(&(*it));
|
|
}
|
|
|
|
static void random_next()
|
|
{
|
|
unsigned int index = rand() % play_list.size();
|
|
cur_track = play_list.begin();
|
|
for (unsigned int i = 0; i < index; i++)
|
|
cur_track++;
|
|
}
|
|
|
|
static void sequential_next()
|
|
{
|
|
cur_track++;
|
|
if (cur_track == play_list.end())
|
|
cur_track = play_list.begin();
|
|
}
|
|
|
|
namespace libsaria
|
|
{
|
|
|
|
void library::rebuild_list()
|
|
{
|
|
list<LibraryPath> *path_list = get_path_list();
|
|
list<LibraryPath>::iterator it;
|
|
|
|
play_list.clear();
|
|
for (it = path_list->begin(); it != path_list->end(); it++)
|
|
insert_list(it->get_list());
|
|
play_list.sort(compare_tracktags);
|
|
}
|
|
|
|
void library::for_each(SourceModel *model)
|
|
{
|
|
list<TrackTag *>::iterator it;
|
|
for (it = play_list.begin(); it != play_list.end(); it++) {
|
|
Track track = Track(*it);
|
|
model->insert(track);
|
|
}
|
|
}
|
|
|
|
void library::play_id(ino_t &id)
|
|
{
|
|
list<TrackTag *>::iterator it;
|
|
if (find_id(id, it)) {
|
|
cur_track = it;
|
|
libsaria::audio::load((*it)->get_filepath());
|
|
}
|
|
}
|
|
|
|
bool library::get_info(ino_t &id, void (*func)(Track &))
|
|
{
|
|
list<TrackTag *>::iterator it;
|
|
if (find_id(id, it)) {
|
|
Track tag(*it);
|
|
func(tag);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void library::next()
|
|
{
|
|
if (libsaria::prefs::get_bool("random") == true)
|
|
random_next();
|
|
else
|
|
sequential_next();
|
|
libsaria::audio::load((*cur_track)->get_filepath());
|
|
}
|
|
|
|
unsigned int library::size()
|
|
{
|
|
return play_list.size();
|
|
}
|
|
|
|
}; /* Namespace: libsaria */
|