]> git.mxchange.org Git - flightgear.git/commitdiff
Create FGSoundManager as a wrapper for SGSoundMgr_OpenAl
authorThorstenB <brehmt@gmail.com>
Mon, 17 Sep 2012 11:36:38 +0000 (13:36 +0200)
committerThorstenB <brehmt@gmail.com>
Mon, 17 Sep 2012 11:45:31 +0000 (13:45 +0200)
and sink all sound related code from main.cxx in there.

src/ATCDCL/ATC.cxx
src/Main/fg_init.cxx
src/Main/main.cxx
src/Sound/CMakeLists.txt
src/Sound/soundmanager.cxx [new file with mode: 0644]
src/Sound/soundmanager.hxx [new file with mode: 0644]

index 6aae329c946522c2aace1f31228d8629cfcf0bf4..982b151bc7e4f894fceff1df1c2ba9675c9d3c61 100644 (file)
@@ -188,6 +188,8 @@ void FGATC::Render(string& msg, const float volume,
     {
         NoRender(refname);
     }
+#else
+    bool useVoice = false;
 #endif    // ENABLE_AUDIO_SUPPORT
 
     if (!useVoice)
index 651b0d4438ecdbfa064dbbd63fa3fd329c26431c..82793ac2bbb13af1a544aed4affa779b3d36be61 100644 (file)
@@ -60,7 +60,6 @@
 #include <simgear/misc/interpolator.hxx>
 #include <simgear/scene/material/matlib.hxx>
 #include <simgear/scene/model/particles.hxx>
-#include <simgear/sound/soundmgr_openal.hxx>
 
 #include <Aircraft/controls.hxx>
 #include <Aircraft/replay.hxx>
@@ -98,6 +97,7 @@
 #include <Scenery/tilemgr.hxx>
 #include <Scripting/NasalSys.hxx>
 #include <Sound/voice.hxx>
+#include <Sound/soundmanager.hxx>
 #include <Systems/system_mgr.hxx>
 #include <Time/light.hxx>
 #include <Traffic/TrafficMgr.hxx>
@@ -1062,7 +1062,7 @@ bool fgInitSubsystems() {
     // to be updated in every loop.
     // Sound manager is updated last so it can use the CPU while the GPU
     // is processing the scenery (doubled the frame-rate for me) -EMH-
-    globals->add_subsystem("sound", new SGSoundMgr, SGSubsystemMgr::SOUND);
+    globals->add_subsystem("sound", new FGSoundManager, SGSubsystemMgr::SOUND);
 
     ////////////////////////////////////////////////////////////////////
     // Initialize the event manager subsystem.
index 21cc873961bbcf419c3e65588a6b71ece097d1c0..2ac958b587deacf9bd67c17df7248962c8d1a3c2 100644 (file)
@@ -47,7 +47,6 @@
 #include <simgear/magvar/magvar.hxx>
 #include <simgear/io/raw_socket.hxx>
 #include <simgear/scene/tsync/terrasync.hxx>
-#include <simgear/sound/soundmgr_openal.hxx>
 #include <simgear/math/SGMath.hxx>
 #include <simgear/math/sg_random.h>
 
@@ -55,7 +54,7 @@
 #include <Model/panelnode.hxx>
 #include <Scenery/scenery.hxx>
 #include <Scenery/tilemgr.hxx>
-#include <Sound/fg_fx.hxx>
+#include <Sound/soundmanager.hxx>
 #include <Time/TimeManager.hxx>
 #include <GUI/gui.h>
 #include <Viewer/CameraGroup.hxx>
@@ -86,72 +85,6 @@ int idle_state = 0;
 extern int _bootstrap_OSInit;
 
 
-void fgInitSoundManager()
-{
-    SGSoundMgr *smgr = globals->get_soundmgr();
-
-    smgr->bind();
-    smgr->init(fgGetString("/sim/sound/device-name", NULL));
-
-    vector <const char*>devices = smgr->get_available_devices();
-    for (unsigned int i=0; i<devices.size(); i++) {
-        SGPropertyNode *p = fgGetNode("/sim/sound/devices/device", i, true);
-        p->setStringValue(devices[i]);
-    }
-    devices.clear();
-}
-
-void fgSetNewSoundDevice(const char *device)
-{
-    SGSoundMgr *smgr = globals->get_soundmgr();
-    smgr->suspend();
-    smgr->stop();
-    smgr->init(device);
-    smgr->resume();
-}
-
-// Update sound manager state (init/suspend/resume) and propagate property values,
-// since the sound manager doesn't read any properties itself.
-// Actual sound update is triggered by the subsystem manager.
-static void fgUpdateSound(double dt)
-{
-#ifdef ENABLE_AUDIO_SUPPORT
-    static bool smgr_init = true;
-    static SGPropertyNode *sound_working = fgGetNode("/sim/sound/working");
-    if (smgr_init == true) {
-        if (sound_working->getBoolValue() == true) {
-            fgInitSoundManager();
-            smgr_init = false;
-        }
-    } else {
-        static SGPropertyNode *sound_enabled = fgGetNode("/sim/sound/enabled");
-        static SGSoundMgr *smgr = globals->get_soundmgr();
-        static bool smgr_enabled = true;
-
-        if (sound_working->getBoolValue() == false) {   // request to reinit
-           smgr->reinit();
-           smgr->resume();
-           sound_working->setBoolValue(true);
-        }
-
-        if (smgr_enabled != sound_enabled->getBoolValue()) {
-            if (smgr_enabled == true) { // request to suspend
-                smgr->suspend();
-                smgr_enabled = false;
-            } else {
-                smgr->resume();
-                smgr_enabled = true;
-            }
-        }
-
-        if (smgr_enabled == true) {
-            static SGPropertyNode *volume = fgGetNode("/sim/sound/volume");
-            smgr->set_volume(volume->getFloatValue());
-        }
-    }
-#endif
-}
-
 static void fgLoadInitialScenery()
 {
     static SGPropertyNode_ptr scenery_loaded
@@ -163,11 +96,6 @@ static void fgLoadInitialScenery()
              && fgGetBool("sim/fdm-initialized")) {
             fgSetBool("sim/sceneryloaded",true);
             fgSplashProgress("");
-            if (fgGetBool("/sim/sound/working")) {
-                globals->get_soundmgr()->activate();
-            }
-            globals->get_props()->tie("/sim/sound/devices/name",
-                  SGRawValueFunctions<const char *>(0, fgSetNewSoundDevice), false);
         }
         else
         {
@@ -200,10 +128,6 @@ static void fgMainLoop( void )
     globals->get_mag()->update( globals->get_aircraft_position(),
                                 globals->get_time_params()->getJD() );
 
-    // Propagate sound manager properties (note: actual update is triggered
-    // by the subsystem manager).
-    fgUpdateSound(sim_dt);
-
     // update all subsystems
     globals->get_subsystem_mgr()->update(sim_dt);
 
index 470e684c15f6f53e5dfe27ddcbde638eda7dd925..fc3960d5b0de6e23392257c57e24fb27820f83ee 100644 (file)
@@ -9,6 +9,7 @@ set(SOURCES
        sample_queue.cxx
        voice.cxx
        voiceplayer.cxx
+       soundmanager.cxx
        )
 
 set(HEADERS
@@ -20,6 +21,7 @@ set(HEADERS
        sample_queue.hxx
        voice.hxx
        voiceplayer.hxx
+       soundmanager.hxx
        )
-               
+
 flightgear_component(Sound "${SOURCES}" "${HEADERS}")
diff --git a/src/Sound/soundmanager.cxx b/src/Sound/soundmanager.cxx
new file mode 100644 (file)
index 0000000..6d3908b
--- /dev/null
@@ -0,0 +1,131 @@
+// soundmanager.cxx -- Wraps the SimGear OpenAl sound manager class
+//
+// Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+
+#include <simgear/sound/soundmgr_openal.hxx>
+
+#include "soundmanager.hxx"
+#include "Main/globals.hxx"
+#include "Main/fg_props.hxx"
+
+#include <vector>
+#include <string>
+
+#ifdef ENABLE_AUDIO_SUPPORT
+/**
+ * Listener class that monitors the sim state.
+ */
+class Listener : public SGPropertyChangeListener
+{
+public:
+    Listener(FGSoundManager *wrapper) : _wrapper(wrapper) {}
+    virtual void valueChanged (SGPropertyNode * node);
+
+private:
+    FGSoundManager * _wrapper;
+};
+
+void Listener::valueChanged(SGPropertyNode * node)
+{
+    _wrapper->activate(node->getBoolValue());
+}
+
+FGSoundManager::FGSoundManager()
+  : _is_initialized(false),
+    _listener(new Listener(this))
+{
+    SGPropertyNode_ptr scenery_loaded = fgGetNode("sim/sceneryloaded", true);
+    scenery_loaded->addChangeListener(_listener);
+}
+
+FGSoundManager::~FGSoundManager()
+{
+}
+
+
+void FGSoundManager::setNewSoundDevice(const char *device)
+{
+    SGSoundMgr *smgr = globals->get_soundmgr();
+    smgr->suspend();
+    smgr->stop();
+    smgr->init(device);
+    smgr->resume();
+}
+
+void FGSoundManager::init()
+{
+    globals->get_props()->tie("/sim/sound/devices/name",
+          SGRawValueFunctions<const char *>(0, FGSoundManager::setNewSoundDevice), false);
+}
+
+void FGSoundManager::bind()
+{
+    _sound_working = fgGetNode("/sim/sound/working");
+    _sound_enabled = fgGetNode("/sim/sound/enabled");
+    _volume        = fgGetNode("/sim/sound/volume");
+
+    // we intentionally do _not_ call SGSoundMgr::bind here, we'll do this later
+}
+
+void FGSoundManager::activate(bool State)
+{
+    if (_is_initialized &&
+        fgGetBool("/sim/sound/working"))
+    {
+        if (State)
+            SGSoundMgr::activate();
+    }
+}
+
+void FGSoundManager::runtime_init()
+{
+    SGSoundMgr::bind();
+    SGSoundMgr::init(fgGetString("/sim/sound/device-name", NULL));
+
+    std::vector <const char*>devices = get_available_devices();
+    for (unsigned int i=0; i<devices.size(); i++) {
+        SGPropertyNode *p = fgGetNode("/sim/sound/devices/device", i, true);
+        p->setStringValue(devices[i]);
+    }
+    devices.clear();
+}
+
+// Update sound manager and propagate property values,
+// since the sound manager doesn't read any properties itself.
+// Actual sound update is triggered by the subsystem manager.
+void FGSoundManager::update(double dt)
+{
+    if (!_is_initialized) {
+        if (_sound_working->getBoolValue()) {
+            runtime_init();
+            _is_initialized = true;
+        }
+    } else {
+        if (!_sound_working->getBoolValue()) {   // request to reinit
+            SGSoundMgr::reinit();
+           _sound_working->setBoolValue(true);
+        }
+
+        if (_sound_enabled->getBoolValue()) {
+            set_volume(_volume->getFloatValue());
+            SGSoundMgr::update(dt);
+        }
+    }
+}
+
+#endif // ENABLE_AUDIO_SUPPORT
diff --git a/src/Sound/soundmanager.hxx b/src/Sound/soundmanager.hxx
new file mode 100644 (file)
index 0000000..244b7b1
--- /dev/null
@@ -0,0 +1,69 @@
+// soundmanager.hxx -- Wraps the SimGear OpenAl sound manager class
+//
+// Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+
+#ifndef __FG_SOUNDMGR_HXX
+#define __FG_SOUNDMGR_HXX 1
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <simgear/props/props.hxx>
+#include <simgear/structure/subsystem_mgr.hxx>
+#include <simgear/sound/soundmgr_openal.hxx>
+
+class SGSoundMgr;
+class Listener;
+
+#ifdef ENABLE_AUDIO_SUPPORT
+class FGSoundManager : public SGSoundMgr
+{
+public:
+    FGSoundManager();
+    ~FGSoundManager();
+
+    void init(void);
+    void bind(void);
+    void update(double dt);
+
+    void runtime_init(void);
+    void activate(bool State);
+
+    static void setNewSoundDevice(const char *device);
+
+private:
+    bool _is_initialized;
+    SGPropertyNode_ptr _sound_working, _sound_enabled, _volume;
+    Listener* _listener;
+};
+#else
+
+// provide a dummy sound class
+class FGSoundManager : public SGSubsystem
+{
+public:
+    FGSoundManager() {}
+    ~FGSoundManager() {}
+
+    void update(double dt) {}
+};
+
+#endif // ENABLE_AUDIO_SUPPORT
+
+#endif // __FG_SOUNDMGR_HXX