]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Sound.cc
settings subsystem now using lua
[chaz/yoink] / src / Moof / Sound.cc
index 43714fe04872a93e968c0e44d0c7724f78b2d966..4cb291300850dec5c29e38a1808c42a986287789 100644 (file)
@@ -38,6 +38,7 @@
 #include "Log.hh"
 #include "Mippleton.hh"
 #include "Sound.hh"
+#include "Timer.hh"
 
 #define BUFFER_SIZE (64 * 1024)
 //#define BUFFER_SIZE (5*2048)
@@ -59,7 +60,6 @@ struct Sound::Impl
        
        class Buffer : public Mippleton<Buffer>
        {
-               FILE*                                   soundFile;
                OggVorbis_File                  oggStream;
                ALenum                                  audioFormat;
                ALsizei                                 audioFreq;
@@ -68,9 +68,9 @@ struct Sound::Impl
        public:
 
                Buffer(const std::string& name) :
-                       Mippleton<Buffer>(name),
-                       soundFile(0)
+                       Mippleton<Buffer>(name)
                {
+                       oggStream.datasource = 0;
                        openFile();
                }
 
@@ -82,7 +82,7 @@ struct Sound::Impl
                                objects.pop_back();
                        }
 
-                       if (soundFile)
+                       if (oggStream.datasource)
                        {
                                ov_clear(&oggStream);
                        }
@@ -91,30 +91,18 @@ struct Sound::Impl
 
                void openFile()
                {
-                       if (soundFile)
+                       if (oggStream.datasource)
                        {
                                ov_clear(&oggStream);
-                               soundFile = 0;
+                               oggStream.datasource = 0;
                        }
 
-                       //soundFile = Sound_NewSampleFromFile(soundFile::getPath(getName()).c_str(),
-                                       //0, BUFFER_SIZE);
-                       soundFile = fopen(Sound::getPath(getName()).c_str(), "rb");
-
-                       if (!soundFile)
-                       {
-                               logWarning("error while loading sound %s", getName().c_str());
-                               throw Exception(Exception::FILE_NOT_FOUND);
-                       }
-
-                       int result = ov_open(soundFile, &oggStream, NULL, 0);
+                       std::string filePath = Sound::getPath(getName());
+                       int result = ov_fopen((char*)filePath.c_str(), &oggStream);
 
                        if (result < 0)
                        {
-                               fclose(soundFile);
-                               soundFile = 0;
-
-                               logWarning("error while loading oggvorbis stream %s",
+                               logWarning("error while loading sound %s",
                                                getName().c_str());
                                throw Exception(Exception::BAD_AUDIO_FORMAT);
                        }
@@ -123,7 +111,6 @@ struct Sound::Impl
                        audioFormat = getAudioFormat(vorbisInfo);
                        audioFreq = vorbisInfo->rate;
 
-                       logDebug("    version: %d", vorbisInfo->version);
                        logDebug("   channels: %d", vorbisInfo->channels);
                        logDebug("  frequency: %d", vorbisInfo->rate);
                }
@@ -131,13 +118,12 @@ struct Sound::Impl
 
                void loadAll(ALuint source)
                {
-                       if (!soundFile) openFile();
-                       if (!soundFile) return;
+                       if (!oggStream.datasource) openFile();
+                       if (!oggStream.datasource) return;
 
                        char data[BUFFER_SIZE];
                        int size = 0;
 
-                       //unsigned decoded = Sound_DecodeAll(soundFile);
                        for (;;)
                        {
                                int section;
@@ -172,14 +158,14 @@ struct Sound::Impl
 
                        // don't need this anymore
                        ov_clear(&oggStream);
-                       soundFile = 0;
+                       oggStream.datasource = 0;
                }
 
 
-               void beginStream(ALuint source, int nBuffers = 4)
+               void beginStream(ALuint source, int nBuffers = 8)
                {
-                       if (!soundFile) openFile();
-                       if (!soundFile) return;
+                       if (!oggStream.datasource) openFile();
+                       if (!oggStream.datasource) return;
 
                        ALuint objs[nBuffers];
                        alGenBuffers(nBuffers, objs);
@@ -211,7 +197,6 @@ struct Sound::Impl
                        char data[BUFFER_SIZE];
                        int size = 0;
 
-                       //unsigned bytes = Sound_Decode(soundFile);
                        while (size < BUFFER_SIZE)
                        {
                                int section;
@@ -238,7 +223,7 @@ struct Sound::Impl
 
                inline void rewind()
                {
-                       if (!soundFile) openFile();
+                       if (!oggStream.datasource) openFile();
                        else ov_raw_seek(&oggStream, 0);
                }
 
@@ -315,6 +300,9 @@ struct Sound::Impl
                alSourcei(source_, AL_LOOPING, AL_FALSE);
                alSourcePlay(source_);
                playing_ = true;
+
+               streamTimer.init(boost::bind(&Impl::streamUpdate, this, _1, _2), 1.0,
+                               Timer::REPEAT);
        }
 
        inline void update()
@@ -462,6 +450,16 @@ struct Sound::Impl
 
        std::queue<BufferP>             queue_;
        std::vector<BufferP>    expired_;
+
+       Timer                                   streamTimer;
+
+       void streamUpdate(Timer& timer, Scalar t)
+       {
+               // don't let the music die!
+               update();
+               // TODO - might be nice to also allow using threads for streaming rather
+               // than a timer, probably as a compile-time option
+       }
 };
 
 Sound::Sound(const std::string& name) :
@@ -475,19 +473,12 @@ void Sound::play()
        impl_->play();
 }
 
-
 void Sound::stream()
 {
        // pass through
        impl_->stream();
 }
 
-void Sound::update(Scalar t, Scalar dt)
-{
-       // pass through
-       impl_->update();
-}
-
 
 void Sound::stop()
 {
@@ -533,13 +524,13 @@ bool Sound::isPlaying() const
        return impl_->isPlaying();
 }
 
-void Sound::setPosition(Vector3 position)
+void Sound::setPosition(const Vector3& position)
 {
        float p[3] = {position[0], position[1], position[2]};
        alSourcefv(impl_->source_, AL_POSITION, p);
 }
 
-void Sound::setVelocity(Vector3 velocity)
+void Sound::setVelocity(const Vector3& velocity)
 {
        float v[3] = {velocity[0], velocity[1], velocity[2]};
        alSourcefv(impl_->source_, AL_VELOCITY, v);
@@ -562,6 +553,31 @@ void Sound::setLooping(bool looping)
 }
 
 
+void Sound::setListenerPosition(const Vector3& position)
+{
+       alListener3f(AL_POSITION, float(position[0]), float(position[1]),
+                       float(position[2]));
+}
+
+void Sound::setListenerVelocity(const Vector3& velocity)
+{
+       alListener3f(AL_VELOCITY, float(velocity[0]), float(velocity[1]),
+                       float(velocity[2]));
+}
+
+void Sound::setListenerOrientation(const Vector3& forward, const Vector3& up)
+{
+       float vec[6];
+       vec[0] = float(forward[0]);
+       vec[1] = float(forward[1]);
+       vec[2] = float(forward[2]);
+       vec[3] = float(up[0]);
+       vec[4] = float(up[1]);
+       vec[5] = float(up[2]);
+       alListenerfv(AL_ORIENTATION, vec);
+}
+
+
 std::string Sound::getPath(const std::string& name)
 {
        std::string path = Resource::getPath("sounds/" + name + ".ogg");
This page took 0.023578 seconds and 4 git commands to generate.