]> git.mxchange.org Git - flightgear.git/blob - src/Sound/soundmanager.cxx
Make the sound-manager optional in a few places.
[flightgear.git] / src / Sound / soundmanager.cxx
1 // soundmanager.cxx -- Wraps the SimGear OpenAl sound manager class
2 //
3 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19
20 #include <simgear/sound/soundmgr_openal.hxx>
21
22 #include "soundmanager.hxx"
23 #include "Main/globals.hxx"
24 #include "Main/fg_props.hxx"
25
26 #include <vector>
27 #include <string>
28
29 #ifdef ENABLE_AUDIO_SUPPORT
30 /**
31  * Listener class that monitors the sim state.
32  */
33 class Listener : public SGPropertyChangeListener
34 {
35 public:
36     Listener(FGSoundManager *wrapper) : _wrapper(wrapper) {}
37     virtual void valueChanged (SGPropertyNode * node);
38
39 private:
40     FGSoundManager* _wrapper;
41 };
42
43 void Listener::valueChanged(SGPropertyNode * node)
44 {
45     _wrapper->activate(node->getBoolValue());
46 }
47
48 FGSoundManager::FGSoundManager()
49   : _is_initialized(false),
50     _listener(new Listener(this))
51 {
52     SGPropertyNode_ptr scenery_loaded = fgGetNode("sim/sceneryloaded", true);
53     scenery_loaded->addChangeListener(_listener);
54 }
55
56 FGSoundManager::~FGSoundManager()
57 {
58 }
59
60 void FGSoundManager::init()
61 {
62     _sound_working = fgGetNode("/sim/sound/working");
63     _sound_enabled = fgGetNode("/sim/sound/enabled");
64     _volume        = fgGetNode("/sim/sound/volume");
65     _device_name   = fgGetNode("/sim/sound/device-name");
66
67     _currentView = fgGetNode("sim/current-view");
68     _viewPosLon    = fgGetNode("sim/current-view/viewer-lon-deg");
69     _viewPosLat    = fgGetNode("sim/current-view/viewer-lat-deg");
70     _viewPosElev   = fgGetNode("sim/current-view/viewer-elev-ft");
71   
72     _velocityNorthFPS = fgGetNode("velocities/speed-north-fps", true);
73     _velocityEastFPS = fgGetNode("velocities/speed-east-fps", true);
74     _velocityDownFPS = fgGetNode("velocities/speed-down-fps", true);
75   
76     reinit();
77 }
78
79 void FGSoundManager::reinit()
80 {
81     _is_initialized = false;
82
83     if (_is_initialized && !_sound_working->getBoolValue())
84     {
85         // shutdown sound support
86         stop();
87         return;
88     }
89
90     if (!_sound_working->getBoolValue())
91     {
92         return;
93     }
94
95     update_device_list();
96
97     select_device(_device_name->getStringValue());
98     SGSoundMgr::reinit();
99     _is_initialized = true;
100
101     activate(fgGetBool("sim/sceneryloaded", true));
102 }
103
104 void FGSoundManager::activate(bool State)
105 {
106     if (_is_initialized)
107     {
108         if (State)
109         {
110             SGSoundMgr::activate();
111         }
112     }
113 }
114
115 void FGSoundManager::update_device_list()
116 {
117     std::vector <const char*>devices = get_available_devices();
118     for (unsigned int i=0; i<devices.size(); i++) {
119         SGPropertyNode *p = fgGetNode("/sim/sound/devices/device", i, true);
120         p->setStringValue(devices[i]);
121     }
122     devices.clear();
123 }
124
125 bool FGSoundManager::stationary() const
126 {
127   // this is an ugly hack to decide if the *viewer* is stationary,
128   // since we don't model the viewer velocity directly.
129   return (_currentView->getDoubleValue("x-offset-m") == 0.0) &&
130          (_currentView->getDoubleValue("y-offset-m") == 0.0) &&
131          (_currentView->getDoubleValue("z-offset-m") == 0.0);
132 }
133
134 // Update sound manager and propagate property values,
135 // since the sound manager doesn't read any properties itself.
136 // Actual sound update is triggered by the subsystem manager.
137 void FGSoundManager::update(double dt)
138 {
139     if (_is_initialized && _sound_working->getBoolValue() && _sound_enabled->getBoolValue())
140     {
141         SGGeod viewPosGeod(SGGeod::fromDegFt(_viewPosLon->getDoubleValue(),
142                                              _viewPosLat->getDoubleValue(),
143                                              _viewPosElev->getDoubleValue()));
144         SGVec3d cartPos = SGVec3d::fromGeod(viewPosGeod);
145         
146         set_position(cartPos, viewPosGeod);
147         
148         SGQuatd viewOrientation;
149         for (int i=0; i<4; ++i) {
150           viewOrientation[i] = _currentView->getChild("raw-orientation", i, true)->getDoubleValue();
151         }
152         
153         set_orientation( viewOrientation );
154         
155         SGVec3d velocity(SGVec3d::zeros());
156         if (!stationary()) {
157           velocity = SGVec3d(_velocityNorthFPS->getDoubleValue(),
158                              _velocityEastFPS->getDoubleValue(),
159                              _velocityDownFPS->getDoubleValue() );
160         }
161         
162         set_velocity( velocity );
163       
164         set_volume(_volume->getFloatValue());
165         SGSoundMgr::update(dt);
166     }
167 }
168
169 #endif // ENABLE_AUDIO_SUPPORT