1 // soundmanager.cxx -- Wraps the SimGear OpenAl sound manager class
3 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
20 #include <simgear/sound/soundmgr_openal.hxx>
22 #include "soundmanager.hxx"
23 #include "Main/globals.hxx"
24 #include "Main/fg_props.hxx"
29 #ifdef ENABLE_AUDIO_SUPPORT
31 * Listener class that monitors the sim state.
33 class Listener : public SGPropertyChangeListener
36 Listener(FGSoundManager *wrapper) : _wrapper(wrapper) {}
37 virtual void valueChanged (SGPropertyNode * node);
40 FGSoundManager* _wrapper;
43 void Listener::valueChanged(SGPropertyNode * node)
45 _wrapper->activate(node->getBoolValue());
48 FGSoundManager::FGSoundManager()
49 : _is_initialized(false),
50 _listener(new Listener(this))
52 SGPropertyNode_ptr scenery_loaded = fgGetNode("sim/sceneryloaded", true);
53 scenery_loaded->addChangeListener(_listener);
56 FGSoundManager::~FGSoundManager()
60 void FGSoundManager::init()
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");
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");
72 _velocityNorthFPS = fgGetNode("velocities/speed-north-fps", true);
73 _velocityEastFPS = fgGetNode("velocities/speed-east-fps", true);
74 _velocityDownFPS = fgGetNode("velocities/speed-down-fps", true);
79 void FGSoundManager::reinit()
81 _is_initialized = false;
83 if (_is_initialized && !_sound_working->getBoolValue())
85 // shutdown sound support
90 if (!_sound_working->getBoolValue())
97 select_device(_device_name->getStringValue());
99 _is_initialized = true;
101 activate(fgGetBool("sim/sceneryloaded", true));
104 void FGSoundManager::activate(bool State)
110 SGSoundMgr::activate();
115 void FGSoundManager::update_device_list()
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]);
125 bool FGSoundManager::stationary() const
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);
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)
139 if (_is_initialized && _sound_working->getBoolValue() && _sound_enabled->getBoolValue())
141 SGGeod viewPosGeod(SGGeod::fromDegFt(_viewPosLon->getDoubleValue(),
142 _viewPosLat->getDoubleValue(),
143 _viewPosElev->getDoubleValue()));
144 SGVec3d cartPos = SGVec3d::fromGeod(viewPosGeod);
146 set_position(cartPos, viewPosGeod);
148 SGQuatd viewOrientation;
149 for (int i=0; i<4; ++i) {
150 viewOrientation[i] = _currentView->getChild("raw-orientation", i, true)->getDoubleValue();
153 set_orientation( viewOrientation );
155 SGVec3d velocity(SGVec3d::zeros());
157 velocity = SGVec3d(_velocityNorthFPS->getDoubleValue(),
158 _velocityEastFPS->getDoubleValue(),
159 _velocityDownFPS->getDoubleValue() );
162 set_velocity( velocity );
164 set_volume(_volume->getFloatValue());
165 SGSoundMgr::update(dt);
169 #endif // ENABLE_AUDIO_SUPPORT