X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fjukebox.cpp;fp=source%2Fjukebox.cpp;h=3cd5e080848ff0aeb517136774e2883aab9498bf;hb=abef5eab53c6c75cd602fdabdec94259d1523858;hp=0000000000000000000000000000000000000000;hpb=2ff6e1cc7fcae3099b127a886360d7919a4b3efa;p=libs%2Fal.git diff --git a/source/jukebox.cpp b/source/jukebox.cpp new file mode 100644 index 0000000..3cd5e08 --- /dev/null +++ b/source/jukebox.cpp @@ -0,0 +1,113 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2009 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include +#include +#include "jukebox.h" +#include "sound.h" + +using namespace std; + +namespace Msp { +namespace AL { + +Jukebox::Jukebox(): + streamer(source), + sound(0), + shuffle(false) +{ } + +Jukebox::~Jukebox() +{ + streamer.stop(); + delete sound; +} + +void Jukebox::add_track(const string &trk) +{ + bool was_empty=tracks.empty(); + tracks.push_back(trk); + if(was_empty) + current_track=tracks.begin(); +} + +void Jukebox::remove_track(const string &trk) +{ + list::iterator i=find(tracks.begin(), tracks.end(), trk); + if(i!=tracks.end()) + { + if(i==current_track) + next(); + tracks.erase(i); + } +} + +void Jukebox::clear_tracks() +{ + stop(); + tracks.clear(); +} + +void Jukebox::set_shuffle(bool s) +{ + shuffle=s; +} + +void Jukebox::play() +{ + if(tracks.empty() || sound) + return; + + sound=new AL::Sound; + sound->open_file(*current_track); + streamer.play(*sound); +} + +void Jukebox::next() +{ + stop(); + if(tracks.size()>1) + { + if(shuffle) + { + while(1) + { + list::iterator i=tracks.begin(); + advance(i, rand()%tracks.size()); + if(i!=current_track) + { + current_track = i; + break; + } + } + } + else + { + ++current_track; + if(current_track==tracks.end()) + current_track=tracks.begin(); + } + } + play(); +} + +void Jukebox::stop() +{ + streamer.stop(); + delete sound; + sound=0; +} + +void Jukebox::tick() +{ + streamer.tick(); + if(sound && sound->eof()) + next(); +} + +} // namespace AL +} // namespace Msp