]> git.tdb.fi Git - libs/al.git/commitdiff
Fix some uninitialized variables and memory leaks
authorMikko Rasa <tdb@tdb.fi>
Sun, 26 Jul 2009 17:55:52 +0000 (17:55 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 26 Jul 2009 17:55:52 +0000 (17:55 +0000)
Add a Jukebox class for playing background music

source/jukebox.cpp [new file with mode: 0644]
source/jukebox.h [new file with mode: 0644]
source/sound.cpp
source/source.cpp

diff --git a/source/jukebox.cpp b/source/jukebox.cpp
new file mode 100644 (file)
index 0000000..3cd5e08
--- /dev/null
@@ -0,0 +1,113 @@
+/* $Id$
+
+This file is part of libmspal
+Copyright © 2009  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <algorithm>
+#include <cstdlib>
+#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<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()
+{
+       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<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();
+               }
+       }
+       play();
+}
+
+void Jukebox::stop()
+{
+       streamer.stop();
+       delete sound;
+       sound=0;
+}
+
+void Jukebox::tick()
+{
+       streamer.tick();
+       if(sound && sound->eof())
+               next();
+}
+
+} // namespace AL
+} // namespace Msp
diff --git a/source/jukebox.h b/source/jukebox.h
new file mode 100644 (file)
index 0000000..2b4e0cd
--- /dev/null
@@ -0,0 +1,50 @@
+/* $Id$
+
+This file is part of libmspal
+Copyright © 2009  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifndef MSP_AL_JUKEBOX_H_
+#define MSP_AL_JUKEBOX_H_
+
+#include <list>
+#include <string>
+#include "source.h"
+#include "streamer.h"
+
+namespace Msp {
+namespace AL {
+
+class Sound;
+
+class Jukebox
+{
+private:
+       Source source;
+       Streamer streamer;
+       Sound *sound;
+       std::list<std::string> tracks;
+       std::list<std::string>::iterator current_track;
+       bool shuffle;
+
+public:
+       Jukebox();
+       ~Jukebox();
+
+       Source &get_source() { return source; }
+       void add_track(const std::string &);
+       void remove_track(const std::string &);
+       void clear_tracks();
+       void set_shuffle(bool);
+
+       void play();
+       void next();
+       void stop();
+       void tick();
+};
+
+} // namespace AL
+} // namespace Msp
+
+#endif
index 8b6af20fcde8fe2c0ea22951a8d138c3dece0289..15596053309f2ba436da99e3bd6bba69b4f892b4 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspal
 /* $Id$
 
 This file is part of libmspal
-Copyright © 2008 Mikko Rasa, Mikkosoft Productions
+Copyright © 2008-2009  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
 Distributed under the LGPL
 */
 
@@ -46,9 +46,10 @@ int memory_seek(void *src, ogg_int64_t offset, int whence)
        return memsrc.pos;
 }
 
        return memsrc.pos;
 }
 
-void memory_close(void *src)
+int memory_close(void *src)
 {
        delete reinterpret_cast<MemorySource *>(src);
 {
        delete reinterpret_cast<MemorySource *>(src);
+       return 0;
 }
 
 long memory_tell(void *src)
 }
 
 long memory_tell(void *src)
@@ -61,7 +62,7 @@ ov_callbacks memory_callbacks=
 {
        &memory_read,
        &memory_seek,
 {
        &memory_read,
        &memory_seek,
-       0,
+       &memory_close,
        &memory_tell
 };
 
        &memory_tell
 };
 
index ff04adfba0b51949d64ef369438a4aa63349194a..348b41e37cd6d98d12665a9cc6929b064a9db518 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspal
 /* $Id$
 
 This file is part of libmspal
-Copyright © 2008 Mikko Rasa, Mikkosoft Productions
+Copyright © 2008-2009  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
 Distributed under the LGPL
 */
 
@@ -122,14 +122,14 @@ void Source::clear_buffers()
 
 unsigned Source::get_buffers_queued() const
 {
 
 unsigned Source::get_buffers_queued() const
 {
-       int n;
+       int n=0;
        get_attribute(AL_BUFFERS_QUEUED, &n);
        return n;
 }
 
 unsigned Source::get_buffers_processed() const
 {
        get_attribute(AL_BUFFERS_QUEUED, &n);
        return n;
 }
 
 unsigned Source::get_buffers_processed() const
 {
-       int n;
+       int n=0;
        get_attribute(AL_BUFFERS_PROCESSED, &n);
        return n;
 }
        get_attribute(AL_BUFFERS_PROCESSED, &n);
        return n;
 }