]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Sound.cc
ray-scene intersection
[chaz/yoink] / src / Moof / Sound.cc
index 4275ea4485900c67e147aeb04165b5f6c2496b30..3aec3f6aa90aa85f4c0f40936508a6ae01a2fff7 100644 (file)
@@ -28,8 +28,8 @@
 
 #include <cstdio>
 #include <deque>
+#include <list>
 #include <string>
-#include <vector>
 
 #include <AL/al.h>
 #include <vorbis/codec.h>
@@ -209,17 +209,19 @@ public:
                // make sure the engine is initialized
                Engine::getInstance();
 
-               ALfloat zero[] = {0.0f, 0.0f, 0.0f};
-               
+               mIsLoaded = false;
+               mIsPlaying = false;
+               mIsLooping = false;
+
                alGenSources(1, &mSource);
 
+               ALfloat zero[] = {0.0f, 0.0f, 0.0f};
                alSourcef(mSource,  AL_PITCH, 1.0f);
                alSourcef(mSource,  AL_GAIN, 1.0f);
                alSourcefv(mSource, AL_POSITION, zero);
                alSourcefv(mSource, AL_VELOCITY, zero);
 
-               mIsPlaying = false;
-               mIsLooping = false;
+               alSourcei(mSource, AL_LOOPING, mIsLooping);
        }
 
        ~Impl()
@@ -228,10 +230,10 @@ public:
 
                alDeleteSources(1, &mSource);
 
-               while (!mBufferObjects.empty())
+               while (!mBuffers.empty())
                {
-                       alDeleteBuffers(1, &mBufferObjects.back());
-                       mBufferObjects.pop_back();
+                       alDeleteBuffers(1, &mBuffers.back());
+                       mBuffers.pop_back();
                }
        }
 
@@ -240,49 +242,50 @@ public:
        {
                if (mQueue.empty()) return;
 
-               ALenum type;
-               alGetSourcei(mSource, AL_SOURCE_TYPE, &type);
-
-               if (type != AL_STATIC)
-               {
-                       mQueue.front()->loadAll(mSource);
-               }
+               if (!mIsLoaded) mQueue.front()->loadAll(mSource);
 
-               alSourcei(mSource, AL_LOOPING, mIsLooping);
                alSourcePlay(mSource);
-               mIsPlaying = true;
+               mIsLoaded = true;
        }
 
 
-       void stream()
+       void playStream()
        {
-               stop();
+               if (mQueue.empty()) return;
 
-               alSourcei(mSource, AL_BUFFER, AL_NONE);
-               mQueue.front()->rewind();
-               beginStream();
+               if (!mIsPlaying)
+               {
+                       alSourcei(mSource, AL_LOOPING, false);
+                       bufferStream();
+               }
+
+               if (!mStreamTimer.isValid())
+               {
+                       mStreamTimer.init(boost::bind(&Impl::streamUpdate, this, _1, _2),
+                                       1.0, Timer::REPEAT);
+               }
 
-               alSourcei(mSource, AL_LOOPING, AL_FALSE);
                alSourcePlay(mSource);
                mIsPlaying = true;
-
-               mStreamTimer.init(boost::bind(&Impl::streamUpdate, this, _1, _2), 1.0,
-                               Timer::REPEAT);
        }
 
-       void beginStream()
+       void bufferStream()
        {
                ALuint buffer;
-               for (int i = mBufferObjects.size(); i < 8; ++i)
+               for (int i = mBuffers.size(); i <= 8; ++i)
                {
                        alGenBuffers(1, &buffer);
-                       mBufferObjects.push_back(buffer);
-               }
-               for (int i = 0; i < 8; ++i)
-               {
-                       buffer = mBufferObjects[i];
-                       mQueue.front()->stream(buffer);
-                       alSourceQueueBuffers(mSource, 1, &buffer);
+
+                       if (mQueue.front()->stream(buffer))
+                       {
+                               alSourceQueueBuffers(mSource, 1, &buffer);
+                               mBuffers.push_back(buffer);
+                       }
+                       else
+                       {
+                               alDeleteBuffers(1, &buffer);
+                               break;
+                       }
                }
        }
 
@@ -317,6 +320,9 @@ public:
                                        mQueue.front()->stream(bufferObj);
                                        alSourceQueueBuffers(mSource, 1, &bufferObj);
                                        logInfo("loading new buffer");
+
+                                       // queue up any unused buffers
+                                       bufferStream();
                                }
                                else if (mIsLooping)
                                {
@@ -327,6 +333,12 @@ public:
                                        alSourceQueueBuffers(mSource, 1, &bufferObj);
                                        logInfo("looping same buffer");
                                }
+                               else
+                               {
+                                       // nothing more to play, stopping...
+                                       mIsPlaying = false;
+                                       std::remove(mBuffers.begin(), mBuffers.end(), bufferObj);
+                               }
                        }
                }
 
@@ -358,38 +370,21 @@ public:
                mStreamTimer.invalidate();
        }
 
-       void resume()
-       {
-               alSourcePlay(mSource);
-               mIsPlaying = true;
-
-               ALenum type;
-               alGetSourcei(mSource, AL_SOURCE_TYPE, &type);
-
-               if (type == AL_STREAMING)
-               {
-                       mStreamTimer.init(boost::bind(&Impl::streamUpdate, this, _1, _2),
-                                       1.0, Timer::REPEAT);
-               }
-       }
-
 
        void setSample(const std::string& name)
        {
-               bool playing = isPlaying();
-               ALenum type;
-               alGetSourcei(mSource, AL_SOURCE_TYPE, &type);
-
                stop();
+               alSourcei(mSource, AL_BUFFER, AL_NONE);
+
                mQueue.clear();
+               mIsLoaded = false;
 
-               //alSourcei(mSource, AL_BUFFER, AL_NONE);
                enqueue(name);
 
-               if (playing)
+               while (!mBuffers.empty())
                {
-                       if (type == AL_STREAMING) stream();
-                       else                      play();
+                       alDeleteBuffers(1, &mBuffers.back());
+                       mBuffers.pop_back();
                }
        }
 
@@ -435,8 +430,9 @@ public:
 
 
        ALuint                                  mSource;
-       std::vector<ALuint>             mBufferObjects;
+       std::list<ALuint>               mBuffers;
 
+       bool                                    mIsLoaded;
        bool                                    mIsPlaying;
        bool                                    mIsLooping;
 
@@ -455,19 +451,19 @@ Sound::Sound(const std::string& name) :
        mImpl(new Sound::Impl(name)) {}
 
 
-void Sound::play()
+void Sound::setSample(const std::string& name)
 {
        // pass through
-       mImpl->play();
+       mImpl->setSample(name);
 }
 
-void Sound::stream()
+
+void Sound::play()
 {
        // pass through
-       mImpl->stream();
+       mImpl->play();
 }
 
-
 void Sound::stop()
 {
        // pass through
@@ -480,48 +476,30 @@ void Sound::pause()
        mImpl->pause();
 }
 
-void Sound::resume()
-{
-       // pass through
-       mImpl->resume();
-}
 
 void Sound::toggle()
 {
-       if (mImpl->mIsPlaying) pause();
-       else resume();
+       if (isPlaying()) pause();
+       else play();
 }
 
-
-void Sound::setSample(const std::string& name)
-{
-       // pass through
-       mImpl->setSample(name);
-}
-
-void Sound::enqueue(const std::string& name)
-{
-       // pass through
-       mImpl->enqueue(name);
-}
-
-
 bool Sound::isPlaying() const
 {
        // pass through
        return mImpl->isPlaying();
 }
 
+
 void Sound::setPosition(const Vector3& position)
 {
-       float p[3] = {position[0], position[1], position[2]};
-       alSourcefv(mImpl->mSource, AL_POSITION, p);
+       float vec[3] = {position[0], position[1], position[2]};
+       alSourcefv(mImpl->mSource, AL_POSITION, vec);
 }
 
 void Sound::setVelocity(const Vector3& velocity)
 {
-       float v[3] = {velocity[0], velocity[1], velocity[2]};
-       alSourcefv(mImpl->mSource, AL_VELOCITY, v);
+       float vec[3] = {velocity[0], velocity[1], velocity[2]};
+       alSourcefv(mImpl->mSource, AL_VELOCITY, vec);
 }
 
 void Sound::setGain(Scalar gain)
@@ -543,14 +521,18 @@ void Sound::setLooping(bool looping)
 
 void Sound::setListenerPosition(const Vector3& position)
 {
-       alListener3f(AL_POSITION, float(position[0]), float(position[1]),
-                       float(position[2]));
+       //alListener3f(AL_POSITION, float(position[0]), float(position[1]),
+                       //float(position[2]));
+       float vec[] = {position[0], position[1], position[2]};
+       alListenerfv(AL_POSITION, vec);
 }
 
 void Sound::setListenerVelocity(const Vector3& velocity)
 {
-       alListener3f(AL_VELOCITY, float(velocity[0]), float(velocity[1]),
-                       float(velocity[2]));
+       //alListener3f(AL_VELOCITY, float(velocity[0]), float(velocity[1]),
+                       //float(velocity[2]));
+       float vec[] = {velocity[0], velocity[1], velocity[2]};
+       alListenerfv(AL_VELOCITY, vec);
 }
 
 void Sound::setListenerOrientation(const Vector3& forward, const Vector3& up)
@@ -573,6 +555,23 @@ std::string Sound::getPath(const std::string& name)
 }
 
 
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+
+void SoundStream::enqueue(const std::string& name)
+{
+       // pass through
+       mImpl->enqueue(name);
+}
+
+
+void SoundStream::play()
+{
+       // pass through
+       mImpl->playStream();
+}
+
+
 } // namespace Mf
 
 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.027644 seconds and 4 git commands to generate.