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;
--- /dev/null
+/* $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
--- /dev/null
+/* $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
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()));
}
-void Source::queue_buffers(const vector<Buffer *> &buffers)
+void Source::queue_buffers(const vector<const Buffer *> &buffers)
{
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());
}
-void Source::queue_buffer(Buffer &buffer)
+void Source::queue_buffer(const Buffer &buffer)
{
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
class Buffer;
+enum SourceState
+{
+ INITIAL = AL_INITIAL,
+ PLAYING = AL_PLAYING,
+ PAUSED = AL_PAUSED,
+ STOPPED = AL_STOPPED
+};
+
class Source
{
private:
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