]> git.mxchange.org Git - flightgear.git/blobdiff - src/Sound/soundmanager.cxx
Make the view-manager and sound-manager independent.
[flightgear.git] / src / Sound / soundmanager.cxx
index 6d3908b3cd43e6572f6ad58a111fa33b3138addc..2a41ade13b61570f2a171514a04b9459fafc8877 100644 (file)
@@ -37,7 +37,7 @@ public:
     virtual void valueChanged (SGPropertyNode * node);
 
 private:
-    FGSoundManager * _wrapper;
+    FGSoundManager* _wrapper;
 };
 
 void Listener::valueChanged(SGPropertyNode * node)
@@ -57,46 +57,63 @@ 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");
+    _device_name   = fgGetNode("/sim/sound/device-name");
+
+    _currentView = fgGetNode("sim/current-view");
+    _viewPosLon    = fgGetNode("sim/current-view/viewer-lon-deg");
+    _viewPosLat    = fgGetNode("sim/current-view/viewer-lat-deg");
+    _viewPosElev   = fgGetNode("sim/current-view/viewer-elev-ft");
+  
+    _velocityNorthFPS = fgGetNode("velocities/speed-north-fps", true);
+    _velocityEastFPS = fgGetNode("velocities/speed-east-fps", true);
+    _velocityDownFPS = fgGetNode("velocities/speed-down-fps", true);
+  
+    reinit();
+}
+
+void FGSoundManager::reinit()
+{
+    _is_initialized = false;
+
+    if (_is_initialized && !_sound_working->getBoolValue())
+    {
+        // shutdown sound support
+        stop();
+        return;
+    }
 
-    // we intentionally do _not_ call SGSoundMgr::bind here, we'll do this later
+    if (!_sound_working->getBoolValue())
+    {
+        return;
+    }
+
+    update_device_list();
+
+    select_device(_device_name->getStringValue());
+    SGSoundMgr::reinit();
+    _is_initialized = true;
+
+    activate(fgGetBool("sim/sceneryloaded", true));
 }
 
 void FGSoundManager::activate(bool State)
 {
-    if (_is_initialized &&
-        fgGetBool("/sim/sound/working"))
+    if (_is_initialized)
     {
         if (State)
+        {
             SGSoundMgr::activate();
+        }
     }
 }
 
-void FGSoundManager::runtime_init()
+void FGSoundManager::update_device_list()
 {
-    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);
@@ -105,26 +122,47 @@ void FGSoundManager::runtime_init()
     devices.clear();
 }
 
+bool FGSoundManager::stationary() const
+{
+  // this is an ugly hack to decide if the *viewer* is stationary,
+  // since we don't model the viewer velocity directly.
+  return (_currentView->getDoubleValue("x-offset-m") == 0.0) &&
+         (_currentView->getDoubleValue("y-offset-m") == 0.0) &&
+         (_currentView->getDoubleValue("z-offset-m") == 0.0);
+}
+
 // 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 (_is_initialized && _sound_working->getBoolValue() && _sound_enabled->getBoolValue())
+    {
+        SGGeod viewPosGeod(SGGeod::fromDegFt(_viewPosLon->getDoubleValue(),
+                                             _viewPosLat->getDoubleValue(),
+                                             _viewPosElev->getDoubleValue()));
+        SGVec3d cartPos = SGVec3d::fromGeod(viewPosGeod);
+        
+        set_position(cartPos, viewPosGeod);
+        
+        SGQuatd viewOrientation;
+        for (int i=0; i<4; ++i) {
+          viewOrientation[i] = _currentView->getChild("raw-orientation", i, true)->getDoubleValue();
         }
-
-        if (_sound_enabled->getBoolValue()) {
-            set_volume(_volume->getFloatValue());
-            SGSoundMgr::update(dt);
+        
+        set_orientation( viewOrientation );
+        
+        SGVec3d velocity(SGVec3d::zeros());
+        if (!stationary()) {
+          velocity = SGVec3d(_velocityNorthFPS->getDoubleValue(),
+                             _velocityEastFPS->getDoubleValue(),
+                             _velocityDownFPS->getDoubleValue() );
         }
+        
+        set_velocity( velocity );
+      
+        set_volume(_volume->getFloatValue());
+        SGSoundMgr::update(dt);
     }
 }