]> git.tdb.fi Git - libs/al.git/blobdiff - source/jukebox.cpp
Fix a memory leak
[libs/al.git] / source / jukebox.cpp
index 3cd5e080848ff0aeb517136774e2883aab9498bf..d79e4c1420804631ade9d91818ff932f6d3f003f 100644 (file)
@@ -1,14 +1,9 @@
-/* $Id$
-
-This file is part of libmspal
-Copyright © 2009  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #include <algorithm>
 #include <cstdlib>
+#include <stdexcept>
 #include "jukebox.h"
-#include "sound.h"
+#include "playlist.h"
+#include "sounddecoder.h"
 
 using namespace std;
 
@@ -17,95 +12,73 @@ namespace AL {
 
 Jukebox::Jukebox():
        streamer(source),
-       sound(0),
-       shuffle(false)
+       in(0),
+       decoder(0),
+       playlist(0),
+       current(0)
 { }
 
 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();
+       delete decoder;
+       delete in;
 }
 
-void Jukebox::remove_track(const string &trk)
-{
-       list<string>::iterator i=find(tracks.begin(), tracks.end(), trk);
-       if(i!=tracks.end())
-       {
-               if(i==current_track)
-                       next();
-               tracks.erase(i);
-       }
-}
-
-void Jukebox::clear_tracks()
+void Jukebox::set_playlist(const Playlist *p)
 {
+       bool was_playing = decoder;
        stop();
-       tracks.clear();
+       playlist = p;
+       if(playlist && was_playing)
+               play();
 }
 
-void Jukebox::set_shuffle(bool s)
+void Jukebox::play()
 {
-       shuffle=s;
+       if(!playlist || playlist->empty() || decoder)
+               return;
+
+       in = playlist->open(current);
+       decoder = SoundDecoder::open_io(*in);
+       streamer.play(*decoder);
 }
 
-void Jukebox::play()
+void Jukebox::next()
 {
-       if(tracks.empty() || sound)
+       if(!playlist || playlist->empty())
                return;
 
-       sound=new AL::Sound;
-       sound->open_file(*current_track);
-       streamer.play(*sound);
+       stop();
+       current = playlist->advance(current, 1);
+       signal_track_changed.emit(current);
+       play();
 }
 
-void Jukebox::next()
+void Jukebox::previous()
 {
+       if(!playlist || playlist->empty())
+               return;
+
        stop();
-       if(tracks.size()>1)
-       {
-               if(shuffle)
-               {
-                       while(1)
-                       {
-                               list<string>::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();
-               }
-       }
+       current = playlist->advance(current, -1);
+       signal_track_changed.emit(current);
        play();
 }
 
 void Jukebox::stop()
 {
        streamer.stop();
-       delete sound;
-       sound=0;
+       delete decoder;
+       decoder = 0;
+       delete in;
+       in = 0;
 }
 
 void Jukebox::tick()
 {
        streamer.tick();
-       if(sound && sound->eof())
+       if(decoder && decoder->eof())
                next();
 }