Browse Source

Merge branch 'new' into dev

dmuratshin 9 years ago
parent
commit
1207b11c33

+ 1 - 1
src/sound/OggStream.h

@@ -18,7 +18,7 @@ namespace oxygine
 
         bool init(const void* data, unsigned int len);
         bool init(const char* path);
-        bool init(file::handle h, bool close);
+        bool init(file::handle h, bool close) override;
 
         void reset();
         void release();

+ 24 - 1
src/sound/ResSound.cpp

@@ -26,7 +26,20 @@ namespace oxygine
         return rs;
     }
 
-    ResSound::ResSound(): _sound(0), _streaming(false)
+    ResSound* ResSound::create(std::vector<unsigned char>& data, bool swap)
+    {
+        ResSound* rs = new ResSound;
+        rs->init(data, swap);
+        return rs;
+    }
+
+    ResSound* ResSound::create(const std::vector<unsigned char>& data_)
+    {
+        std::vector<unsigned char>& data = const_cast<std::vector<unsigned char>& >(data_);
+        return create(data, false);
+    }
+
+    ResSound::ResSound() : _sound(0), _streaming(false)
     {
 
     }
@@ -45,6 +58,16 @@ namespace oxygine
         return true;
     }
 
+    bool ResSound::init(std::vector<unsigned char>& data, bool swap)
+    {
+        _streaming = false;
+        _sound = SoundSystem::get()->createSound(data, swap);
+        if (_sound)
+            _sound->setRes(this);
+
+        return _sound != 0;
+    }
+
     bool ResSound::init(CreateResourceContext& context)
     {
         std::string file = context.walker.getNode().attribute("file").as_string();

+ 3 - 0
src/sound/ResSound.h

@@ -12,11 +12,14 @@ namespace oxygine
     public:
         static Resource* createResSound(CreateResourceContext& context);
         static ResSound* create(const std::string& file, bool streaming);
+        static ResSound* create(std::vector<unsigned char>& data, bool swap);
+        static ResSound* create(const std::vector<unsigned char>& data);
 
         ResSound();
         ~ResSound();
 
         bool init(const std::string& file, bool streaming);
+        bool init(std::vector<unsigned char>& data, bool swap);
         bool init(CreateResourceContext& context);
 
         Sound*              getSound() const;

+ 3 - 1
src/sound/SoundInstance.h

@@ -90,13 +90,15 @@ namespace oxygine
         void _updateVolume();
         void _setHanleVolume(float v);
 
+
+        SoundHandle* _handle;
+
     private:
         SoundPlayer* _player;
         EventCallback _cbDone;
         EventCallback _cbAboutDone;
 
         Channel* _channel;
-        SoundHandle* _handle;
 
         bool _finished;
         bool _fadedOut;

+ 15 - 13
src/sound/SoundPlayer.cpp

@@ -81,22 +81,11 @@ namespace oxygine
     }
 
 
-
-
-
-    spSoundInstance SoundPlayer::play(Resource* res, const PlayOptions& opt)
+    spSoundInstance SoundPlayer::play(Sound* snd, const PlayOptions& opt)
     {
-        ResSound* ressound = safeCast<ResSound*>(res);
-        if (!ressound || !ressound->getSound())
-            return 0;
-
-
-        SoundHandle* handle = SoundHandleOAL::create(ressound->getSound());
+        SoundHandle* handle = SoundHandleOAL::create(snd);
         spSoundInstance s = new SoundInstance(this, handle);
 
-        s->setName(res->getName());
-        handle->setName(s->getName());
-
         s->setPitch(opt._pitch);
         s->setLoop(opt._looped);
         if (opt._seek)
@@ -112,6 +101,19 @@ namespace oxygine
         return s;
     }
 
+
+    spSoundInstance SoundPlayer::play(Resource* res, const PlayOptions& opt)
+    {
+        ResSound* ressound = safeCast<ResSound*>(res);
+        if (!ressound || !ressound->getSound())
+            return 0;
+
+        spSoundInstance ins = play(ressound->getSound());
+        ins->setName(res->getName());
+        ins->_handle->setName(res->getName());
+        return ins;
+    }
+
     spSoundInstance SoundPlayer::play(const std::string& id, const PlayOptions& opt)
     {
         if (!_resources)

+ 2 - 0
src/sound/SoundPlayer.h

@@ -10,6 +10,7 @@ namespace oxygine
     class ResSound;
     class Resources;
     class Channel;
+    class Sound;
     struct sound_desc;
 
     using namespace std;
@@ -54,6 +55,7 @@ namespace oxygine
 
 
         spSoundInstance play(Resource*, const PlayOptions& = PlayOptions());
+        spSoundInstance play(Sound*, const PlayOptions& = PlayOptions());
         spSoundInstance play(const std::string& id, const PlayOptions& = PlayOptions());
         spSoundInstance continuePlay(Resource*, Channel*, const PlayOptions& = PlayOptions());
 

+ 4 - 0
src/sound/SoundStream.h

@@ -1,4 +1,5 @@
 #pragma once
+#include "core/file.h"
 namespace oxygine
 {
     class SoundStream
@@ -7,6 +8,9 @@ namespace oxygine
         SoundStream(): _channels(0), _rate(0), _duration(0), _pcm(0), _ended(true) {}
         virtual ~SoundStream() {}
 
+        virtual bool init(file::handle, bool close) = 0;
+        virtual bool init(const void* data, unsigned int len) = 0;
+
         virtual int decodeNextBlock(bool looped, void* data, int bufferSize) = 0;
         virtual void decodeAll(void* data, int bufferSize) = 0;
         virtual void reset() = 0;

+ 1 - 0
src/sound/SoundSystem.h

@@ -29,6 +29,7 @@ namespace oxygine
         virtual void stop() = 0;
 
         virtual Sound* createSound(const char* file, bool streaming) = 0;
+        virtual Sound* createSound(std::vector<unsigned char>& data, bool swap) = 0;
         virtual void update() = 0;
     };
 }

+ 5 - 3
src/sound/WavStream.cpp

@@ -37,7 +37,7 @@ namespace oxygine
         _fh = 0;
     }
 
-    void WavStream::init(file::handle fh, bool close)
+    bool WavStream::init(file::handle fh, bool close)
     {
         _fh = fh;
         _close = close;
@@ -53,12 +53,14 @@ namespace oxygine
         _dataPos = 0;
         OX_ASSERT(header.Subchunk2Size == _pcm * _channels * 2);
         _ended = false;
+
+        return true;
     }
 
-    void WavStream::init(const void* data, size_t size)
+    bool  WavStream::init(const void* data, size_t size)
     {
         _memfile.init(data, size);
-        init(&_memfile, false);
+        return init(&_memfile, false);
     }
 
     void WavStream::init(const char* name)

+ 2 - 2
src/sound/WavStream.h

@@ -11,9 +11,9 @@ namespace oxygine
         WavStream();
         ~WavStream();
 
-        void init(file::handle fh, bool close);
+        bool init(file::handle fh, bool close) override;
         void init(const char* name);
-        void init(const void* data, size_t size);
+        bool init(const void* data, size_t size);
 
         int  decodeNextBlock(bool looped, void* data, int bufferSize) override;
         void decodeAll(void* data, int bufferSize) override;

+ 35 - 41
src/sound/oal/SoundOAL.cpp

@@ -15,29 +15,17 @@ namespace oxygine
 
     SoundOAL::SoundOAL(const string& path, file::handle fh): _alBuffer(0), _format(0), _timeMS(0), _fileName(path), _type(Unknown)
     {
-        OggStream oggStream;
-        WavStream wavStream;
         char header[4];
         if (file::read(fh, header, 4) != 4)
             return;
 
         file::seek(fh, 0, SEEK_SET);
 
-        SoundStream* stream = 0;
-
-        if (header[0] == 'R' && header[1] == 'I'  && header[2] == 'F' && header[3] == 'F')
-        {
-            stream = &wavStream;
-            wavStream.init(fh, false);
-            _type = Wav;
-        }
-        else if (header[0] == 'O' && header[1] == 'g'  && header[2] == 'g' && header[3] == 'S')
-        {
-            stream = &oggStream;
-            oggStream.init(fh, false);
-            _type = Ogg;
-        }
 
+        OggStream oggStream;
+        WavStream wavStream;
+        SoundStream* stream = init0(header, oggStream, wavStream);
+        stream->init(fh, false);
 
         bool streaming = _init(*stream);
         if (streaming)
@@ -54,6 +42,37 @@ namespace oxygine
         file::close(fh);
     }
 
+    SoundOAL::SoundOAL(std::vector<unsigned char>& data, bool swap)
+    {
+        if (swap)
+            std::swap(_fileBuffer, data);
+        else
+            _fileBuffer = data;
+
+        OggStream oggStream;
+        WavStream wavStream;
+        SoundStream* stream = init0((char*)&_fileBuffer.front(), oggStream, wavStream);
+        stream->init(&_fileBuffer.front(), _fileBuffer.size());
+        _init(*stream);
+    }
+
+    SoundStream* SoundOAL::init0(char header[4], OggStream& oggStream, WavStream& wavStream)
+    {
+        SoundStream* stream = 0;
+        if (header[0] == 'R' && header[1] == 'I'  && header[2] == 'F' && header[3] == 'F')
+        {
+            stream = &wavStream;
+            _type = Wav;
+        }
+        else if (header[0] == 'O' && header[1] == 'g'  && header[2] == 'g' && header[3] == 'S')
+        {
+            stream = &oggStream;
+            _type = Ogg;
+        }
+
+        return stream;
+    }
+
     SoundOAL::~SoundOAL()
     {
         if (_alBuffer)
@@ -108,31 +127,6 @@ namespace oxygine
         return false;
     }
 
-    void SoundOAL::init(std::vector<unsigned char>& buffer, bool swap)
-    {
-        OggStream stream;
-        stream.init(&buffer.front(), (unsigned int)buffer.size());
-
-        bool streaming = _init(stream);
-        if (streaming)
-        {
-            if (swap)
-                buffer.swap(_fileBuffer);
-            else
-                _fileBuffer = buffer;
-        }
-    }
-
-    bool SoundOAL::init(const char* path)
-    {
-        _fileName = path;
-
-        OggStream stream;
-        bool res = stream.init(path);
-
-        _init(stream);
-        return res;
-    }
 
     int SoundOAL::getDuration() const
     {

+ 3 - 4
src/sound/oal/SoundOAL.h

@@ -20,12 +20,9 @@ namespace oxygine
         };
 
         SoundOAL(const std::string& path, file::handle fh);
+        SoundOAL(std::vector<unsigned char>& data, bool swap);
         ~SoundOAL();
 
-        void init(std::vector<unsigned char>& buffer, bool swap);
-        bool init(const char* path);
-
-
         unsigned int    getAlBuffer() const {return _alBuffer;}
         int             getDuration() const;
         unsigned int    getFormat() const {return _format;}
@@ -38,6 +35,8 @@ namespace oxygine
     private:
         bool _init(SoundStream& stream);
 
+        SoundStream* init0(char header[4], OggStream& oggStream, WavStream& wavStream);
+
         unsigned int _format;
         int _timeMS;
 

+ 8 - 0
src/sound/oal/SoundSystemOAL.cpp

@@ -207,6 +207,14 @@ namespace oxygine
         return sound;
     }
 
+    oxygine::SoundOAL* SoundSystemOAL::createSound(std::vector<unsigned char>& data, bool swap)
+    {
+        if (!_context)
+            return 0;
+
+        return new SoundOAL(data, swap);
+    }
+
     void SoundSystemOAL::update()
     {
         if (!_device)

+ 2 - 1
src/sound/oal/SoundSystemOAL.h

@@ -19,7 +19,8 @@ namespace oxygine
         void resume();
         void stop();
 
-        SoundOAL* createSound(const char* path, bool streaming);
+        SoundOAL* createSound(const char* path, bool streaming) override;
+        SoundOAL* createSound(std::vector<unsigned char>& data, bool swap) override;
 
 
         bool        isAvailable() const {return _context != 0;}