]> git.tdb.fi Git - libs/al.git/commitdiff
Add more methods to Source
authorMikko Rasa <tdb@tdb.fi>
Fri, 22 Feb 2008 22:49:26 +0000 (22:49 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 22 Feb 2008 22:49:26 +0000 (22:49 +0000)
Add class SoundScape for managing Sources

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

index bc4db1efa5c86fefc5b0fe6f4b16c531e7483367..0c094d0572d5a5042d53933956656048cfad5455 100644 (file)
@@ -28,7 +28,7 @@ Sound::~Sound()
 void Sound::open(const string &fn)
 {
        if(ov_fopen(const_cast<char *>(fn.c_str()), &ovfile)<0)
 void Sound::open(const string &fn)
 {
        if(ov_fopen(const_cast<char *>(fn.c_str()), &ovfile)<0)
-               throw Exception("Could not open ogg vorbis file");
+               throw Exception("Could not open ogg vorbis file "+fn);
 
        vorbis_info *info=ov_info(&ovfile, -1);
        freq=info->rate;
 
        vorbis_info *info=ov_info(&ovfile, -1);
        freq=info->rate;
diff --git a/source/soundscape.cpp b/source/soundscape.cpp
new file mode 100644 (file)
index 0000000..694c884
--- /dev/null
@@ -0,0 +1,52 @@
+/* $Id$
+
+This file is part of libmspal
+Copyright © 2008 Mikko Rasa, Mikkosoft Productions
+Diestributed under the LGPL
+*/
+
+#include "source.h"
+#include "soundscape.h"
+
+using namespace std;
+
+namespace Msp {
+namespace AL {
+
+SoundScape::~SoundScape()
+{
+       for(list<Source *>::iterator i=sources.begin(); i!=sources.end(); ++i)
+               delete *i;
+}
+
+void SoundScape::add_source(Source *src)
+{
+       sources.push_back(src);
+}
+
+Source *SoundScape::play(const Buffer &buf, float x, float y, float z)
+{
+       Source *src=new Source;
+       add_source(src);
+       src->set_buffer(buf);
+       src->set_position(x, y, z);
+       src->play();
+       return src;
+}
+
+void SoundScape::tick()
+{
+       for(list<Source *>::iterator i=sources.begin(); i!=sources.end();)
+       {
+               if((*i)->get_state()==STOPPED)
+               {
+                       delete *i;
+                       i=sources.erase(i);
+               }
+               else
+                       ++i;
+       }
+}
+
+} // namespace AL
+} // namespace Msp
diff --git a/source/soundscape.h b/source/soundscape.h
new file mode 100644 (file)
index 0000000..3eca7ea
--- /dev/null
@@ -0,0 +1,35 @@
+/* $Id$
+
+This file is part of libmspal
+Copyright © 2008 Mikko Rasa, Mikkosoft Productions
+Diestributed under the LGPL
+*/
+
+#ifndef MSP_AL_SOUNDSCAPE_H_
+#define MSP_AL_SOUNDSCAPE_H_
+
+#include <list>
+
+namespace Msp {
+namespace AL {
+
+class Buffer;
+class Source;
+
+class SoundScape
+{
+private:
+       std::list<Source *> sources;
+
+public:
+       ~SoundScape();
+
+       void add_source(Source *);
+       Source *play(const Buffer &, float, float, float);
+       void tick();
+};
+
+} // namespace AL
+} // namespace Msp
+
+#endif
index fec965c5d8ede45e6487b8a2f0e70a03ab233624..2ba1f4a2260fbd38148f0d4526e3d75ac20ffa06 100644 (file)
@@ -43,25 +43,72 @@ void Source::attribute(ALenum attr, const float *v)
        alSourcefv(id, attr, v);
 }
 
        alSourcefv(id, attr, v);
 }
 
-void Source::set_buffer(Buffer &buffer)
+void Source::get_attr(ALenum attr, int *v) const
+{
+       alGetSourcei(id, attr, v);
+}
+
+void Source::get_attr(ALenum attr, float *v) const
+{
+       alGetSourcef(id, attr, v);
+}
+
+SourceState Source::get_state() const
+{
+       int state;
+       get_attr(AL_SOURCE_STATE, &state);
+       return static_cast<SourceState>(state);
+}
+
+void Source::set_position(float x, float y, float z)
+{
+       attribute(AL_POSITION, x, y, z);
+}
+
+void Source::set_looping(bool l)
+{
+       attribute(AL_LOOPING, l);
+}
+
+void Source::set_buffer(const Buffer &buffer)
 {
        attribute(AL_BUFFER, static_cast<int>(buffer.get_id()));
 }
 
 {
        attribute(AL_BUFFER, static_cast<int>(buffer.get_id()));
 }
 
-void Source::queue_buffers(const vector<Buffer *> &buffers)
+void Source::queue_buffers(const vector<const Buffer *> &buffers)
 {
        vector<uint> ids;
        ids.reserve(buffers.size());
 {
        vector<uint> ids;
        ids.reserve(buffers.size());
-       for(vector<Buffer *>::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
+       for(vector<const Buffer *>::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
                ids.push_back((*i)->get_id());
        alSourceQueueBuffers(id, ids.size(), &ids.front());
 }
 
                ids.push_back((*i)->get_id());
        alSourceQueueBuffers(id, ids.size(), &ids.front());
 }
 
-void Source::queue_buffer(Buffer &buffer)
+void Source::queue_buffer(const Buffer &buffer)
 {
        uint bid=buffer.get_id();
        alSourceQueueBuffers(id, 1, &bid);
 }
 
 {
        uint bid=buffer.get_id();
        alSourceQueueBuffers(id, 1, &bid);
 }
 
+void Source::play()
+{
+       alSourcePlay(id);
+}
+
+void Source::pause()
+{
+       alSourcePause(id);
+}
+
+void Source::stop()
+{
+       alSourceStop(id);
+}
+
+void Source::rewind()
+{
+       alSourceRewind(id);
+}
+
 } // namespace AL
 } // namespace Msp
 } // namespace AL
 } // namespace Msp
index 234dcb832f4a2d8e9b4ca0bf66af3e87ea3bbf37..29118c5ea0d6dd18d2b7abd82b2ae1862d305700 100644 (file)
@@ -16,6 +16,14 @@ namespace AL {
 
 class Buffer;
 
 
 class Buffer;
 
+enum SourceState
+{
+       INITIAL = AL_INITIAL,
+       PLAYING = AL_PLAYING,
+       PAUSED  = AL_PAUSED,
+       STOPPED = AL_STOPPED
+};
+
 class Source
 {
 private:
 class Source
 {
 private:
@@ -29,9 +37,18 @@ public:
        void attribute(ALenum, float);
        void attribute(ALenum, float, float, float);
        void attribute(ALenum, const float *);
        void attribute(ALenum, float);
        void attribute(ALenum, float, float, float);
        void attribute(ALenum, const float *);
-       void set_buffer(Buffer &);
-       void queue_buffers(const std::vector<Buffer *> &);
-       void queue_buffer(Buffer &);
+       void get_attr(ALenum, int *) const;
+       void get_attr(ALenum, float *) const;
+       SourceState get_state() const;
+       void set_position(float, float, float);
+       void set_looping(bool);
+       void set_buffer(const Buffer &);
+       void queue_buffers(const std::vector<const Buffer *> &);
+       void queue_buffer(const Buffer &);
+       void play();
+       void pause();
+       void stop();
+       void rewind();
 };
 
 } // namespace AL
 };
 
 } // namespace AL