FlightGear next
fg_fx.cxx
Go to the documentation of this file.
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#ifdef _MSC_VER
25#pragma warning (disable: 4786)
26#endif
27
28#ifdef HAVE_CONFIG_H
29# include <config.h>
30#endif
31
32#include "fg_fx.hxx"
33
34#include <Main/fg_props.hxx>
35#include <Main/globals.hxx>
38#include <algorithm>
39
40#include <simgear/debug/ErrorReportingCallback.hxx>
41#include <simgear/misc/sg_path.hxx>
42#include <simgear/props/props.hxx>
43#include <simgear/props/props_io.hxx>
44#include <simgear/sound/xmlsound.hxx>
45
46FGFX::FGFX ( const std::string &refname, SGPropertyNode *props ) :
47 _props( props )
48{
49 if (!props) {
50 _is_aimodel = false;
51 _props = globals->get_props();
52 _enabled = fgGetNode("/sim/sound/effects/enabled", true);
53 _volume = fgGetNode("/sim/sound/effects/volume", true);
54 } else {
55 _is_aimodel = true;
56 _enabled = _props->getNode("/sim/sound/aimodels/enabled", true);
57 _enabled->setBoolValue(fgGetBool("/sim/sound/effects/enabled"));
58 _volume = _props->getNode("/sim/sound/aimodels/volume", true);
59 _volume->setFloatValue(fgGetFloat("/sim/sound/effects/volume"));
60 }
61
62 _avionics_enabled = _props->getNode("sim/sound/avionics/enabled", true);
63 _avionics_volume = _props->getNode("sim/sound/avionics/volume", true);
64 _avionics_ext = _props->getNode("sim/sound/avionics/external-view", true);
65 _internal = _props->getNode("sim/current-view/internal", true);
66
67 _atc_enabled = _props->getNode("sim/sound/atc/enabled", true);
68 _atc_volume = _props->getNode("sim/sound/atc/volume", true);
69 _atc_ext = _props->getNode("sim/sound/atc/external-view", true);
70
71 _machwave_active = _props->getNode("sim/sound/machwave/active", true);
72 _machwave_volume = _props->getNode("sim/sound/machwave/offset-m", true);
73
74 _smgr = globals->get_subsystem<FGSoundManager>();
75 if (!_smgr) {
76 return;
77 }
78 _active = _smgr->is_active();
79
80 _refname = refname;
81 _smgr->add(this, refname);
82
83 if (!_is_aimodel) // only for the main aircraft
84 {
85 _avionics = _smgr->find("avionics", true);
86 _avionics->tie_to_listener();
87
88 _atc = _smgr->find("atc", true);
89 _atc->tie_to_listener();
90 }
91}
92
94{
95 if (_smgr)
96 {
97 _smgr->remove(_refname);
98 }
99
100 // because SGXmlSound has an owning ref back to us, we need to
101 // clear these here, or we will never get destroyed
102 std::for_each(_sound.begin(), _sound.end(), [](const SGXmlSound* snd) { delete snd; });
103 _sound.clear();
104}
105
107{
108}
109
110void
112{
113 if (!_smgr) {
114 return;
115 }
116
117 SGPropertyNode *node = _props->getNode("sim/sound", true);
118
119 std::string path_str = node->getStringValue("path");
120 if (path_str.empty()) {
121 SG_LOG(SG_SOUND, SG_ALERT, "No path in sim/sound/path");
122 return;
123 }
124
125 SGPath path = globals->resolve_aircraft_path(path_str);
126 if (path.isNull())
127 {
128 simgear::reportFailure(simgear::LoadFailure::NotFound, simgear::ErrorCode::AudioFX,
129 "Failed to find FX XML file:" + path_str, sg_location{path_str});
130 SG_LOG(SG_SOUND, SG_ALERT,
131 "File not found: '" << path_str);
132 return;
133 }
134 SG_LOG(SG_SOUND, SG_INFO, "Reading sound " << node->getNameString()
135 << " from " << path);
136
137 SGPropertyNode root;
138 try {
139 readProperties(path, &root);
140 } catch (const sg_exception& e) {
141 simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::AudioFX,
142 "Failure loading FX XML:" + e.getFormattedMessage(), e.getLocation());
143 return;
144 }
145
146 node = root.getNode("fx");
147 if(node) {
148 for (int i = 0; i < node->nChildren(); ++i) {
149 std::unique_ptr<SGXmlSound> soundfx{new SGXmlSound};
150
151 try {
152 bool ok = soundfx->init( _props, node->getChild(i), this, _avionics,
153 path.dir() );
154 if (ok) {
155 // take the pointer out of the unique ptr so it's not deleted
156 _sound.push_back( soundfx.release() );
157 }
158 } catch ( sg_exception &e ) {
159 SG_LOG(SG_SOUND, SG_ALERT, e.getFormattedMessage());
160 simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::AudioFX,
161 "Failure creating Audio FX:" + e.getFormattedMessage(), path);
162 }
163 }
164 }
165}
166
167
168void
170{
171 SGSampleGroup::stop();
172 std::for_each(_sound.begin(), _sound.end(), [](const SGXmlSound* snd) { delete snd; });
173 _sound.clear();
174 init();
175 SGSampleGroup::resume();
176}
177
178
179void
180FGFX::update (double dt)
181{
182 if (!_smgr) {
183 return;
184 }
185
186 if (!_active && _smgr->is_active())
187 {
188 _active = true;
189 for ( unsigned int i = 0; i < _sound.size(); i++ ) {
190 _sound[i]->start();
191 }
192 }
193
194
195 if ( _enabled->getBoolValue() ) {
196 if ( _avionics)
197 {
198 const bool e = _avionics_enabled->getBoolValue();
199 if (e && (_avionics_ext->getBoolValue() || _internal->getBoolValue())) {
200 // avionics sound is enabled
201 _avionics->resume(); // no-op if already in resumed state
202 _avionics->set_volume( _avionics_volume->getFloatValue() );
203 }
204 else
205 _avionics->suspend();
206 }
207
208 if ( _atc)
209 {
210 const bool e = _atc_enabled->getBoolValue();
211 if (e && (_atc_ext->getBoolValue() || _internal->getBoolValue())) {
212 // ATC sound is enabled
213 _atc->resume(); // no-op if already in resumed state
214 _atc->set_volume( _atc_volume->getFloatValue() );
215 }
216 else
217 _atc->suspend();
218 }
219
220 _machwave_active->setBoolValue(_mInCone);
221 _machwave_volume->setFloatValue(_mOffset_m);
222
223 set_volume( _volume->getDoubleValue() );
224 resume();
225
226 // update sound effects if not paused
227 for ( unsigned int i = 0; i < _sound.size(); i++ ) {
228 _sound[i]->update(dt);
229 }
230
231 SGSampleGroup::update(dt);
232 }
233 else
234 suspend();
235}
236
237// end of fg_fx.cxx
bool suspend
Definition JSBSim.cpp:93
#define i(x)
FGFX(const std::string &refname, SGPropertyNode *props=0)
Definition fg_fx.cxx:46
void reinit()
Definition fg_fx.cxx:169
void init()
Definition fg_fx.cxx:111
virtual ~FGFX()
Definition fg_fx.cxx:106
void unbind()
Definition fg_fx.cxx:93
void update(double dt) override
Definition fg_fx.cxx:180
FGGlobals * globals
Definition globals.cxx:142
bool fgGetBool(char const *name, bool def)
Get a bool value for a property.
Definition proptest.cpp:25
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27
float fgGetFloat(const char *name, float defaultValue)
Get a float value for a property.
Definition proptest.cpp:29