| 1 | // fg_fx.cxx -- Sound effect management class implementation |
| 2 | // |
| 3 | // Started by David Megginson, October 2001 |
| 4 | // (Reuses some code from main.cxx, probably by Curtis Olson) |
| 5 | // |
| 6 | // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt |
| 7 | // |
| 8 | // This program is free software; you can redistribute it and/or |
| 9 | // modify it under the terms of the GNU General Public License as |
| 10 | // published by the Free Software Foundation; either version 2 of the |
| 11 | // License, or (at your option) any later version. |
| 12 | // |
| 13 | // This program is distributed in the hope that it will be useful, but |
| 14 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this program; if not, write to the Free Software |
| 20 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | // |
| 22 | // $Id$ |
| 23 | |
| 24 | #include "simgear/structure/exception.hxx" |
| 25 | #ifdef _MSC_VER |
| 26 | #pragma warning (disable: 4786) |
| 27 | #endif |
| 28 | |
| 29 | #ifdef HAVE_CONFIG_H |
| 30 | # include <config.h> |
| 31 | #endif |
| 32 | |
| 33 | #include "fg_fx.hxx" |
| 34 | |
| 35 | #include <Main/fg_props.hxx> |
| 36 | #include <Main/globals.hxx> |
| 37 | #include <Main/sentryIntegration.hxx> |
| 38 | #include <Sound/soundmanager.hxx> |
| 39 | #include <algorithm> |
| 40 | |
| 41 | #include <simgear/debug/ErrorReportingCallback.hxx> |
| 42 | #include <simgear/misc/sg_path.hxx> |
| 43 | #include <simgear/props/props.hxx> |
| 44 | #include <simgear/props/props_io.hxx> |
| 45 | #include <simgear/sound/xmlsound.hxx> |
| 46 | |
| 47 | FGFX::FGFX ( const std::string &refname, SGPropertyNode *props ) : |
| 48 | _props( props ) |
| 49 | { |
| 50 | if (!props) { |
| 51 | _is_aimodel = false; |
| 52 | _props = globals->get_props(); |
| 53 | _enabled = fgGetNode("/sim/sound/effects/enabled", true); |
| 54 | _volume = fgGetNode("/sim/sound/effects/volume", true); |
| 55 | } else { |
| 56 | _is_aimodel = true; |
| 57 | _enabled = _props->getNode("/sim/sound/aimodels/enabled", true); |
| 58 | _enabled->setBoolValue(fgGetBool("/sim/sound/effects/enabled")); |
| 59 | _volume = _props->getNode("/sim/sound/aimodels/volume", true); |
| 60 | _volume->setFloatValue(fgGetFloat("/sim/sound/effects/volume")); |
| 61 | } |
| 62 | |
| 63 | _avionics_enabled = _props->getNode("sim/sound/avionics/enabled", true); |
| 64 | _avionics_volume = _props->getNode("sim/sound/avionics/volume", true); |
| 65 | _avionics_ext = _props->getNode("sim/sound/avionics/external-view", true); |
| 66 | _internal = _props->getNode("sim/current-view/internal", true); |
| 67 | |
| 68 | _atc_enabled = _props->getNode("sim/sound/atc/enabled", true); |
| 69 | _atc_volume = _props->getNode("sim/sound/atc/volume", true); |
| 70 | _atc_ext = _props->getNode("sim/sound/atc/external-view", true); |
| 71 | |
| 72 | _machwave_active = _props->getNode("sim/sound/machwave/active", true); |
| 73 | _machwave_volume = _props->getNode("sim/sound/machwave/offset-m", true); |
| 74 | |
| 75 | _smgr = globals->get_subsystem<FGSoundManager>(); |
| 76 | if (!_smgr) { |
| 77 | return; |
| 78 | } |
| 79 | _active = _smgr->is_active(); |
| 80 | |
| 81 | _refname = refname; |
| 82 | _smgr->add(this, refname); |
| 83 | |
| 84 | if (!_is_aimodel) // only for the main aircraft |
| 85 | { |
| 86 | _avionics = _smgr->find("avionics", true); |
| 87 | _avionics->tie_to_listener(); |
| 88 | |
| 89 | _atc = _smgr->find("atc", true); |
| 90 | _atc->tie_to_listener(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void FGFX::shutdown() |
| 95 | { |
| 96 | _smgr->remove(_refname); |
| 97 | _xmlSounds.clear(); |
| 98 | } |
| 99 | |
| 100 | FGFX::~FGFX () |
| 101 | { |
| 102 | // check that shutdown has been called |
| 103 | assert(!_smgr->exists(_refname)); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | FGFX::init() |
| 108 | { |
| 109 | if (!_smgr) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | SGPropertyNode *node = _props->getNode("sim/sound", true); |
| 114 | |
| 115 | std::string path_str = node->getStringValue("path"); |
| 116 | if (path_str.empty()) { |
| 117 | SG_LOG(SG_SOUND, SG_ALERT, "No path in sim/sound/path"); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | SGPath path = globals->resolve_aircraft_path(path_str); |
| 122 | if (path.isNull()) |
| 123 | { |
| 124 | simgear::reportFailure(simgear::LoadFailure::NotFound, simgear::ErrorCode::AudioFX, |
| 125 | "Failed to find FX XML file:" + path_str, sg_location{path_str}); |
| 126 | SG_LOG(SG_SOUND, SG_ALERT, |
| 127 | "File not found: '" << path_str); |
| 128 | return; |
| 129 | } |
| 130 | SG_LOG(SG_SOUND, SG_INFO, "Reading sound " << node->getNameString() |
| 131 | << " from " << path); |
| 132 | |
| 133 | SGPropertyNode root; |
| 134 | try { |
| 135 | readProperties(path, &root); |
| 136 | } catch (const sg_exception& e) { |
| 137 | simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::AudioFX, |
| 138 | "Failure loading FX XML:" + e.getFormattedMessage(), e.getLocation()); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | node = root.getNode("fx"); |
| 143 | if(node) { |
| 144 | for (int i = 0; i < node->nChildren(); ++i) { |
| 145 | SGXmlSoundRef soundfx{new SGXmlSound}; |
| 146 | auto child = node->getChild(i); |
| 147 | try { |
| 148 | bool ok = soundfx->init(_props, child, this, _avionics, path.dir()); |
| 149 | if (ok) { |
| 150 | _xmlSounds.push_back(soundfx); |
| 151 | } |
| 152 | } catch (const sg_exception& e) { |
| 153 | simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::AudioFX, |
| 154 | "Failure creating Audio FX:" + e.getFormattedMessage(), sg_location{child}); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | FGFX::update (double dt) |
| 162 | { |
| 163 | // FGFX::update is called via the SGSoundManager update(), which |
| 164 | // calls update on all its sample-groups. |
| 165 | if (!_smgr) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | if (!_active && _smgr->is_active()) |
| 170 | { |
| 171 | _active = true; |
| 172 | for (auto s : _xmlSounds) { |
| 173 | s->start(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | |
| 178 | if ( _enabled->getBoolValue() ) { |
| 179 | if ( _avionics) |
| 180 | { |
| 181 | const bool e = _avionics_enabled->getBoolValue(); |
| 182 | if (e && (_avionics_ext->getBoolValue() || _internal->getBoolValue())) { |
| 183 | // avionics sound is enabled |
| 184 | _avionics->resume(); // no-op if already in resumed state |
| 185 | _avionics->set_volume( _avionics_volume->getFloatValue() ); |
| 186 | } |
| 187 | else |
| 188 | _avionics->suspend(); |
| 189 | } |
| 190 | |
| 191 | if ( _atc) |
| 192 | { |
| 193 | const bool e = _atc_enabled->getBoolValue(); |
| 194 | if (e && (_atc_ext->getBoolValue() || _internal->getBoolValue())) { |
| 195 | // ATC sound is enabled |
| 196 | _atc->resume(); // no-op if already in resumed state |
| 197 | _atc->set_volume( _atc_volume->getFloatValue() ); |
| 198 | } |
| 199 | else |
| 200 | _atc->suspend(); |
| 201 | } |
| 202 | |
| 203 | _machwave_active->setBoolValue(_mInCone); |
| 204 | _machwave_volume->setFloatValue(_mOffset_m); |
| 205 | |
| 206 | set_volume( _volume->getDoubleValue() ); |
| 207 | resume(); |
| 208 | |
| 209 | // update sound effects if not paused |
| 210 | for (auto xs : _xmlSounds) { |
| 211 | xs->update(dt); |
| 212 | } |
| 213 | |
| 214 | SGSampleGroup::update(dt); |
| 215 | } |
| 216 | else |
| 217 | suspend(); |
| 218 | } |
| 219 | |
| 220 | // end of fg_fx.cxx |