1 // scenefx.cxx -- Scenery sound effect management implementation
3 // Started by Erik Hofman, November 2015
5 // Copyright (C) 2015 Erik Hofman <erik@ehofman.com>
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #pragma warning (disable: 4786)
34 #include "scenefx.hxx"
36 #include <Main/fg_props.hxx>
37 #include <Main/globals.hxx>
39 #include <simgear/props/props_io.hxx>
40 #include <simgear/misc/sg_path.hxx>
41 #include <simgear/sound/soundmgr_openal.hxx>
42 #include <simgear/nasal/cppbind/Ghost.hxx>
43 #include <simgear/sound/xmlsound.hxx>
46 #include <boost/shared_ptr.hpp>
47 #include <boost/weak_ptr.hpp>
50 static std::string _refname = "SceneFX";
51 typedef boost::shared_ptr<SGSampleGroup> SGSampleGroupRef;
52 typedef boost::shared_ptr<FGSceneFX> FGSceneFXRef;
55 FGSceneFX::FGSceneFX()
57 _props = _props = globals->get_props();
58 _enabled = fgGetNode("/sim/sound/scene/enabled", true);
59 _volume = fgGetNode("/sim/sound/scene/volume", true);
60 _damping = fgGetNode("/sim/sound/model-damping", true);
61 _smgr->add(this, _refname);
63 nasal::Ghost<FGSceneFXRef>::init("soundfx.scene")
64 .bases<SGSampleGroupRef>()
65 .method("load", &FGSceneFX::load)
66 .method("damping", &FGSceneFX::model_damping);
69 void FGSceneFX::unbind()
73 _smgr->remove(_refname);
77 FGSceneFX::~FGSceneFX()
79 for (unsigned int i = 0; i < _sound.size(); i++ ) {
85 void FGSceneFX::init()
91 SGPath path(globals->get_fg_root());
92 path.append("Sounds/sounds.xml");
96 readProperties(path.str(), &root);
97 } catch (const sg_exception &) {
98 SG_LOG(SG_SOUND, SG_ALERT,
99 "Error reading file '" << path.str() << '\'');
103 SGPropertyNode *node = root.getNode("fx");
105 for (int i = 0; i < node->nChildren(); ++i) {
106 SGXmlSound *soundfx = new SGXmlSound();
109 soundfx->init( _props, node->getChild(i), this, 0, path.dir() );
110 _sound.push_back( soundfx );
111 } catch ( sg_exception &e ) {
112 SG_LOG(SG_SOUND, SG_ALERT, e.getFormattedMessage());
119 void FGSceneFX::reinit()
121 for ( unsigned int i = 0; i < _sound.size(); i++ ) {
128 void FGSceneFX::update (double dt)
134 if (_enabled->getBoolValue())
136 float fact = 1.0f - _damping->getFloatValue();
137 set_volume(_volume->getFloatValue() * fact);
140 // update sound effects if not paused
141 for ( unsigned int i = 0; i < _sound.size(); i++ ) {
142 _sound[i]->update(dt);
145 SGSampleGroup::update(dt);
152 /* Sets the scene properties from the models point of view */
153 void FGSceneFX::model_damping(float damping)
155 _damping->setFloatValue(damping);
158 /* (Over)load the sound file for a particular ref. name */
159 bool FGSceneFX::load(const std::string& refname, const std::string& path_str)
165 SGPath path = globals->resolve_resource_path(path_str);
166 if (!path.isNull() && path.exists())
168 SGSoundSample* sample;
172 sample = find(refname, i);
174 sample->set_sample_name(path_str);
182 throw sg_io_exception("SceneFX: couldn't find file: '" + path.str() + "'");
187 /* Control the sounds from the generating side. */
188 size_t FGSceneFX::add(const std::string& refname)
194 unsigned int num = 0;
197 name = full_name(refname, num++);
198 } while(SGSampleGroup::exists(name));
200 SGSharedPtr<SGSoundSample> sample;
201 sample = new SGSoundSample();
202 SGSampleGroup::add(sample, name);
207 bool FGSceneFX::remove(const std::string& refname, size_t num)
209 std::string name = full_name(refname, num);
210 return SGSampleGroup::remove(name);
213 void FGSceneFX::position(const std::string& refname, size_t num, double lon, double lat, double elevation)
215 SGSoundSample* sample = find(refname, num);
218 SGGeod pos = SGGeod::fromDegFt(lon, lat, elevation);
219 sample->set_position(SGVec3d::fromGeod(pos));
223 void FGSceneFX::pitch(const std::string& refname, size_t num, float pitch)
225 SGSoundSample* sample = find(refname, num);
227 sample->set_pitch(pitch);
231 void FGSceneFX::volume(const std::string& refname, size_t num, float volume)
233 SGSoundSample* sample = find(refname, num);
236 sample->set_volume(volume);
240 void FGSceneFX::properties(const std::string& refname, size_t num, float reference_dist, float maximum_dist)
242 SGSoundSample* sample = find(refname, num);
245 float fact = 1.0f - _damping->getFloatValue();
246 sample->set_reference_dist(reference_dist * fact);
247 if (maximum_dist > 0) {
248 sample->set_max_dist(maximum_dist * fact);
253 void FGSceneFX::play(const std::string& refname, size_t num, bool looping)
255 SGSampleGroup::play(full_name(refname, num), looping);
258 void FGSceneFX::stop(const std::string& refname, size_t num)
260 SGSampleGroup::stop(full_name(refname, num));
263 const char* FGSceneFX::full_name(const std::string& refname, size_t num)
265 static char nstr[128];
266 snprintf(nstr, 127, "%s_%4zu", refname.c_str(), num);
270 SGSoundSample *FGSceneFX::find(const std::string& refname, size_t num)
272 return SGSampleGroup::find( full_name(refname, num) );
275 // end of scenefx.cxx