]> git.mxchange.org Git - flightgear.git/blob - src/Sound/soundmanager.cxx
Revive master sound enable switch.
[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     _enabled(false),
51     _listener(new Listener(this))
52 {
53     SGPropertyNode_ptr scenery_loaded = fgGetNode("sim/sceneryloaded", true);
54     scenery_loaded->addChangeListener(_listener);
55 }
56
57 FGSoundManager::~FGSoundManager()
58 {
59 }
60
61 void FGSoundManager::init()
62 {
63     _sound_working = fgGetNode("/sim/sound/working");
64     _sound_enabled = fgGetNode("/sim/sound/enabled");
65     _volume        = fgGetNode("/sim/sound/volume");
66     _device_name   = fgGetNode("/sim/sound/device-name");
67
68     _currentView   = fgGetNode("sim/current-view");
69     _viewPosLon    = fgGetNode("sim/current-view/viewer-lon-deg");
70     _viewPosLat    = fgGetNode("sim/current-view/viewer-lat-deg");
71     _viewPosElev   = fgGetNode("sim/current-view/viewer-elev-ft");
72   
73     _velocityNorthFPS = fgGetNode("velocities/speed-north-fps", true);
74     _velocityEastFPS  = fgGetNode("velocities/speed-east-fps", true);
75     _velocityDownFPS  = fgGetNode("velocities/speed-down-fps", true);
76   
77     reinit();
78 }
79
80 void FGSoundManager::reinit()
81 {
82     _is_initialized = false;
83
84     if (_is_initialized && !_sound_working->getBoolValue())
85     {
86         // shutdown sound support
87         stop();
88         return;
89     }
90
91     if (!_sound_working->getBoolValue())
92     {
93         return;
94     }
95
96     update_device_list();
97
98     select_device(_device_name->getStringValue());
99     SGSoundMgr::reinit();
100     _is_initialized = true;
101
102     activate(fgGetBool("sim/sceneryloaded", true));
103 }
104
105 void FGSoundManager::activate(bool State)
106 {
107     if (_is_initialized)
108     {
109         if (State)
110         {
111             SGSoundMgr::activate();
112         }
113     }
114 }
115
116 void FGSoundManager::update_device_list()
117 {
118     std::vector <const char*>devices = get_available_devices();
119     for (unsigned int i=0; i<devices.size(); i++) {
120         SGPropertyNode *p = fgGetNode("/sim/sound/devices/device", i, true);
121         p->setStringValue(devices[i]);
122     }
123     devices.clear();
124 }
125
126 bool FGSoundManager::stationary() const
127 {
128   // this is an ugly hack to decide if the *viewer* is stationary,
129   // since we don't model the viewer velocity directly.
130   return (_currentView->getDoubleValue("x-offset-m") == 0.0) &&
131          (_currentView->getDoubleValue("y-offset-m") == 0.0) &&
132          (_currentView->getDoubleValue("z-offset-m") == 0.0);
133 }
134
135 // Update sound manager and propagate property values,
136 // since the sound manager doesn't read any properties itself.
137 // Actual sound update is triggered by the subsystem manager.
138 void FGSoundManager::update(double dt)
139 {
140     if (_is_initialized && _sound_working->getBoolValue())
141     {
142         bool enabled = _sound_enabled->getBoolValue();
143         if (enabled != _enabled)
144         {
145             if (enabled)
146                 resume();
147             else
148                 suspend();
149             _enabled = enabled;
150         }
151         if (enabled)
152         {
153             SGGeod viewPosGeod(SGGeod::fromDegFt(_viewPosLon->getDoubleValue(),
154                                                  _viewPosLat->getDoubleValue(),
155                                                  _viewPosElev->getDoubleValue()));
156             SGVec3d cartPos = SGVec3d::fromGeod(viewPosGeod);
157
158             set_position(cartPos, viewPosGeod);
159
160             SGQuatd viewOrientation;
161             for (int i=0; i<4; ++i) {
162               viewOrientation[i] = _currentView->getChild("raw-orientation", i, true)->getDoubleValue();
163             }
164
165             set_orientation( viewOrientation );
166
167             SGVec3d velocity(SGVec3d::zeros());
168             if (!stationary()) {
169               velocity = SGVec3d(_velocityNorthFPS->getDoubleValue(),
170                                  _velocityEastFPS->getDoubleValue(),
171                                  _velocityDownFPS->getDoubleValue() );
172             }
173
174             set_velocity( velocity );
175
176             set_volume(_volume->getFloatValue());
177             SGSoundMgr::update(dt);
178         }
179     }
180 }
181
182 #endif // ENABLE_AUDIO_SUPPORT