Forráskód Böngészése

fallback to Null device if oal initialization failed

[email protected] 8 éve
szülő
commit
852a1e2f97

+ 3 - 0
CMakeLists.txt

@@ -30,6 +30,8 @@ set(SRC
 	src/sound/null/SoundSystemNull.h
 	src/sound/null/SoundNull.cpp
 	src/sound/null/SoundNull.h
+	src/sound/null/SoundHandleNull.cpp
+	src/sound/null/SoundHandleNull.h
 	)
 
 if(EMSCRIPTEN)
@@ -41,6 +43,7 @@ if(EMSCRIPTEN)
 		src/sound/emscripten/SoundHandleEmscripten.cpp
 		src/sound/emscripten/SoundEmscripten.h
 		src/sound/emscripten/SoundEmscripten.cpp
+
 	)
 
 else(EMSCRIPTEN)

+ 1 - 1
src/sound/SoundSystem.h

@@ -22,7 +22,7 @@ namespace oxygine
         static void free();
 
 
-        virtual void init(int channels) = 0;
+        void init(int) {}
         virtual void release() = 0;
         virtual void pause() = 0;
         virtual void resume() = 0;

+ 2 - 2
src/sound/emscripten/SoundEmscripten.cpp

@@ -3,7 +3,7 @@
 
 namespace oxygine
 {
-    SoundEmscripten::SoundEmscripten(const char *file):_path(file)
+    SoundEmscripten::SoundEmscripten(const char* file): _path(file)
     {
 
     }
@@ -20,7 +20,7 @@ namespace oxygine
 
     SoundHandle* SoundEmscripten::createSH()
     {
-        SoundHandleEmscripten *handle = new SoundHandleEmscripten(_path);
+        SoundHandleEmscripten* handle = new SoundHandleEmscripten(_path);
 
         return handle;
     }

+ 1 - 1
src/sound/emscripten/SoundEmscripten.h

@@ -5,7 +5,7 @@ namespace oxygine
     class SoundEmscripten : public Sound
     {
     public:
-        SoundEmscripten(const char *file);
+        SoundEmscripten(const char* file);
         ~SoundEmscripten();
 
         int getDuration() const  override;

+ 7 - 7
src/sound/emscripten/SoundHandleEmscripten.cpp

@@ -10,7 +10,7 @@
 
 namespace oxygine
 {
-    SoundHandleEmscripten::SoundHandleEmscripten(const std::string &path): _handle(0), _path(path)
+    SoundHandleEmscripten::SoundHandleEmscripten(const std::string& path): _handle(0), _path(path)
     {
 
     }
@@ -37,7 +37,7 @@ namespace oxygine
         {
             return sound.update($0);
         }, _handle);
-        
+
         if (r == 1)
         {
             _stop();
@@ -62,7 +62,7 @@ namespace oxygine
             {
                 sound.resume($0);
             }, _handle);
-        }   
+        }
         else
         {
             _play();
@@ -87,8 +87,8 @@ namespace oxygine
         }, _handle, _volume);
     }
 
-    
-    void SoundHandleEmscripten::_updatePitch() 
+
+    void SoundHandleEmscripten::_updatePitch()
     {
     }
 
@@ -100,11 +100,11 @@ namespace oxygine
         }, _handle, _looping);
     }
 
-    void SoundHandleEmscripten::_init() 
+    void SoundHandleEmscripten::_init()
     {
     }
 
-    void SoundHandleEmscripten::_setPosition(int tm) 
+    void SoundHandleEmscripten::_setPosition(int tm)
     {
     }
 

+ 2 - 2
src/sound/emscripten/SoundHandleEmscripten.h

@@ -9,7 +9,7 @@ namespace oxygine
     class SoundHandleEmscripten : public SoundHandle
     {
     public:
-        SoundHandleEmscripten(const std::string &path);
+        SoundHandleEmscripten(const std::string& path);
         ~SoundHandleEmscripten();
 
     protected:
@@ -19,7 +19,7 @@ namespace oxygine
         void _updatePitch() override;
         void _updateLoop() override;
         void _update() override;
-        
+
         void _init() override;
         void _play() override;
         void _pause() override;

+ 4 - 8
src/sound/emscripten/SoundSystemEmscripten.cpp

@@ -16,6 +16,10 @@ namespace oxygine
 
     SoundSystemEmscripten::SoundSystemEmscripten()
     {
+        EM_ASM_ARGS(
+        {
+            sound.init();
+        }, 0);
     }
 
     SoundSystemEmscripten::~SoundSystemEmscripten()
@@ -23,14 +27,6 @@ namespace oxygine
 
     }
 
-    void SoundSystemEmscripten::init(int channels_num)
-    {
-        EM_ASM_ARGS(
-        {
-            sound.init();
-        }, 0);
-    }
-
     void SoundSystemEmscripten::release()
     {
         //_channels._channels.clear();

+ 0 - 1
src/sound/emscripten/SoundSystemEmscripten.h

@@ -13,7 +13,6 @@ namespace oxygine
         SoundSystemEmscripten();
         ~SoundSystemEmscripten();
 
-        void init(int channels) override;
         void release() override;
         void pause() override;
         void resume() override;

+ 52 - 0
src/sound/null/SoundHandleNull.cpp

@@ -0,0 +1,52 @@
+#include "SoundHandleNull.h"
+
+namespace oxygine
+{
+    SoundHandleNull::SoundHandleNull(int duration): _duration(duration), _lastUpdate(0), _pos(0)
+    {
+
+    }
+
+    timeMS SoundHandleNull::_getPosition() const
+    {
+        return _pos;
+    }
+
+    void SoundHandleNull::_update()
+    {
+        int tm = getTimeMS();
+        int dt = tm - _lastUpdate;
+
+        _pos += dt;
+        _lastUpdate = tm;
+
+        if (_pos >= _duration && !_looping)
+        {
+            _state = ended;
+        }
+    }
+
+    void SoundHandleNull::_init()
+    {
+
+    }
+
+    void SoundHandleNull::_play()
+    {
+        _pos = 0;
+        _lastUpdate = getTimeMS();
+    }
+
+    void SoundHandleNull::_pause()
+    {
+
+    }
+    void SoundHandleNull::_resume()
+    {
+
+    }
+
+    void SoundHandleNull::_stop()
+    {
+    }
+}

+ 31 - 0
src/sound/null/SoundHandleNull.h

@@ -0,0 +1,31 @@
+#pragma once
+#include "../SoundHandle.h"
+
+namespace oxygine
+{
+    class Sound;
+
+    class SoundHandleNull : public SoundHandle
+    {
+    public:
+        SoundHandleNull(int duration);
+
+    protected:
+
+        void _update() override;
+        void _init() override;
+        void _play() override;
+        void _pause() override;
+        void _resume() override;
+        void _stop() override;
+        void _setPosition(int tm) {}
+
+
+        int _duration;
+        int _pos;
+        int _lastUpdate;
+
+
+        timeMS _getPosition() const;
+    };
+}

+ 3 - 2
src/sound/null/SoundNull.cpp

@@ -1,7 +1,8 @@
 #include "SoundNull.h"
+#include "SoundHandleNull.h"
 namespace oxygine
 {
-    SoundNull::SoundNull()
+    SoundNull::SoundNull(int duration): _duration(duration)
     {
     }
 
@@ -17,6 +18,6 @@ namespace oxygine
 
     SoundHandle* SoundNull::createSH()
     {
-        return 0;
+        return new SoundHandleNull(_duration);
     }
 }

+ 3 - 1
src/sound/null/SoundNull.h

@@ -6,12 +6,14 @@ namespace oxygine
     class SoundNull: public Sound
     {
     public:
-        SoundNull();
+        SoundNull(int duration);
         ~SoundNull();
 
         int getDuration() const override;
 
         SoundHandle* createSH() override;
+
     private:
+        int _duration;
     };
 }

+ 10 - 6
src/sound/null/SoundSystemNull.cpp

@@ -1,14 +1,11 @@
 #include "SoundSystemNull.h"
 #include "SoundNull.h"
+#include "../OggStream.h"
 
 namespace oxygine
 {
     SoundSystem* SoundSystem::instance = 0;
 
-    void SoundSystemNull::init(int channels)
-    {
-
-    }
 
     void SoundSystemNull::release()
     {
@@ -33,12 +30,19 @@ namespace oxygine
 
     Sound* SoundSystemNull::createSound(std::vector<unsigned char>& data, bool swap)
     {
-        return new SoundNull;
+        OggStream oggStream;
+        oggStream.init(&data.front(), data.size());
+
+
+        return new SoundNull(oggStream.getDuration());
     }
 
     Sound* SoundSystemNull::createSound(const char* file, bool)
     {
-        return new SoundNull;
+        OggStream oggStream;
+        oggStream.init(file);
+
+        return new SoundNull(oggStream.getDuration());
     }
 
 

+ 0 - 1
src/sound/null/SoundSystemNull.h

@@ -8,7 +8,6 @@ namespace oxygine
     class SoundSystemNull : public SoundSystem
     {
     public:
-        void init(int channels) override;
         void release() override;
         void pause() override;
         void resume() override;

+ 56 - 51
src/sound/oal/SoundSystemOAL.cpp

@@ -28,13 +28,67 @@ namespace oxygine
     SoundSystem* SoundSystem::create()
     {
         if (!SoundSystem::instance)
-            SoundSystem::instance = new SoundSystemOAL;
+        {
+
+            ALCdevice* device = 0;
+
+#ifdef __ANDROID__
+            device = alcOpenDevice("opensles");
+#else
+            device = alcOpenDevice(0);
+
+            if (!device)
+            {
+                sleep(100);
+                device = alcOpenDevice(0);
+            }
+#endif
+
+            device = 0;
+
+            if (device)
+            {
+                ALCcontext* context = alcCreateContext(device, 0);
+                if (context)
+                    SoundSystem::instance = new SoundSystemOAL(device, context);
+            }
+
+
+            if (!SoundSystem::instance)
+            {
+                OX_ASSERT(0);
+                log::error("can't create SoundSystemOAL");
+
+                SoundSystem::instance = new SoundSystemNull;
+            }
+        }
 
         return SoundSystem::instance;
     }
 
-    SoundSystemOAL::SoundSystemOAL(): _device(0), _context(0), _volume(1.0)
+    SoundSystemOAL::SoundSystemOAL(ALCdevice* device, ALCcontext* context): _device(device), _context(context), _volume(1.0)
     {
+
+        log::messageln("SoundSystemOAL init");
+
+        alcMakeContextCurrent(_context);
+        OAL_CHECK();
+
+        /*
+        ALCint nummono, numstereo;
+        alcGetIntegerv(_device, ALC_MONO_SOURCES, 1, &nummono);
+        alcGetIntegerv(_device, ALC_STEREO_SOURCES, 1, &numstereo);
+        */
+
+        ALuint sources[6];
+        alGenSources(6, sources);
+
+        _freeSources.assign(sources, sources + 6);
+        _sources = _freeSources;
+
+        StreamingSoundHandleOAL::runThread();
+        OAL_CHECK();
+
 #ifdef __S3E__
 //      alcInit();
 #endif
@@ -118,55 +172,6 @@ namespace oxygine
         OAL_CHECK();
     }
 
-    void SoundSystemOAL::init(int channels_num)
-    {
-        log::messageln("SoundSystemOAL init");
-#ifdef __ANDROID__
-        _device = alcOpenDevice("opensles");
-#else
-        _device = alcOpenDevice(0);
-
-        if (!_device)
-        {
-            sleep(100);
-            _device = alcOpenDevice(0);
-        }
-#endif
-        if (!_device)
-        {
-            OX_ASSERT(0);
-            log::error("can't create alc device");
-            return;
-        }
-
-        _context = alcCreateContext(_device, 0);
-
-        if (!_context)
-        {
-            OX_ASSERT(0);
-            log::error("can't create alc context");
-            return;
-        }
-
-        alcMakeContextCurrent(_context);
-        OAL_CHECK();
-
-        /*
-        ALCint nummono, numstereo;
-        alcGetIntegerv(_device, ALC_MONO_SOURCES, 1, &nummono);
-        alcGetIntegerv(_device, ALC_STEREO_SOURCES, 1, &numstereo);
-        */
-
-        ALuint sources[6];
-        alGenSources(6, sources);
-
-        _freeSources.assign(sources, sources + 6);
-        _sources = _freeSources;
-
-        StreamingSoundHandleOAL::runThread();
-        OAL_CHECK();
-    }
-
     void SoundSystemOAL::release()
     {
         stop();

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

@@ -9,10 +9,9 @@ namespace oxygine
     class SoundSystemOAL : public SoundSystem
     {
     public:
-        SoundSystemOAL();
+        SoundSystemOAL(ALCdevice* device, ALCcontext* context);
         ~SoundSystemOAL();
 
-        void init(int channels_num) override;
         void release() override;
 
         void pause() override;