From 72a8ee9c7d0ff973b20a27184b0c493d3fd5ae59 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 22 Feb 2008 22:49:26 +0000 Subject: [PATCH] Add more methods to Source Add class SoundScape for managing Sources --- source/sound.cpp | 2 +- source/soundscape.cpp | 52 ++++++++++++++++++++++++++++++++++++++++ source/soundscape.h | 35 +++++++++++++++++++++++++++ source/source.cpp | 55 +++++++++++++++++++++++++++++++++++++++---- source/source.h | 23 +++++++++++++++--- 5 files changed, 159 insertions(+), 8 deletions(-) create mode 100644 source/soundscape.cpp create mode 100644 source/soundscape.h diff --git a/source/sound.cpp b/source/sound.cpp index bc4db1e..0c094d0 100644 --- a/source/sound.cpp +++ b/source/sound.cpp @@ -28,7 +28,7 @@ Sound::~Sound() void Sound::open(const string &fn) { if(ov_fopen(const_cast(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; diff --git a/source/soundscape.cpp b/source/soundscape.cpp new file mode 100644 index 0000000..694c884 --- /dev/null +++ b/source/soundscape.cpp @@ -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::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::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 index 0000000..3eca7ea --- /dev/null +++ b/source/soundscape.h @@ -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 + +namespace Msp { +namespace AL { + +class Buffer; +class Source; + +class SoundScape +{ +private: + std::list sources; + +public: + ~SoundScape(); + + void add_source(Source *); + Source *play(const Buffer &, float, float, float); + void tick(); +}; + +} // namespace AL +} // namespace Msp + +#endif diff --git a/source/source.cpp b/source/source.cpp index fec965c..2ba1f4a 100644 --- a/source/source.cpp +++ b/source/source.cpp @@ -43,25 +43,72 @@ void Source::attribute(ALenum attr, const float *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(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(buffer.get_id())); } -void Source::queue_buffers(const vector &buffers) +void Source::queue_buffers(const vector &buffers) { vector ids; ids.reserve(buffers.size()); - for(vector::const_iterator i=buffers.begin(); i!=buffers.end(); ++i) + for(vector::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 diff --git a/source/source.h b/source/source.h index 234dcb8..29118c5 100644 --- a/source/source.h +++ b/source/source.h @@ -16,6 +16,14 @@ namespace AL { class Buffer; +enum SourceState +{ + INITIAL = AL_INITIAL, + PLAYING = AL_PLAYING, + PAUSED = AL_PAUSED, + STOPPED = AL_STOPPED +}; + class Source { private: @@ -29,9 +37,18 @@ public: 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 &); - 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 &); + void queue_buffer(const Buffer &); + void play(); + void pause(); + void stop(); + void rewind(); }; } // namespace AL -- 2.45.2