1 // fg_environmentfx.cxx -- Sound effect management class implementation
3 // Started by David Megginson, October 2001
4 // (Reuses some code from main.cxx, probably by Curtis Olson)
6 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
25 #pragma warning (disable: 4786)
32 #include "environment_fx.hxx"
34 #include <Main/fg_props.hxx>
35 #include <Main/globals.hxx>
37 #include <simgear/misc/sg_path.hxx>
38 #include <simgear/sound/soundmgr_openal.hxx>
39 #include <simgear/nasal/cppbind/Ghost.hxx>
41 #include <boost/shared_ptr.hpp>
42 #include <boost/weak_ptr.hpp>
45 static std::string _refname = "EnvironmentFX";
46 typedef boost::shared_ptr<SGSampleGroup> SGSampleGroupRef;
47 typedef boost::shared_ptr<FGEnvironmentFX> FGEnvironmentFXRef;
50 FGEnvironmentFX::FGEnvironmentFX()
52 _enabled = fgGetNode("/sim/sound/environment/enabled", true);
53 _volume = fgGetNode("/sim/sound/environment/volume", true);
54 _smgr->add(this, _refname);
56 nasal::Ghost<FGEnvironmentFXRef>::init("sound")
57 .bases<SGSampleGroupRef>()
58 .method("add", &FGEnvironmentFX::add)
59 .method("position", &FGEnvironmentFX::position)
60 .method("pitch", &FGEnvironmentFX::pitch)
61 .method("volume", &FGEnvironmentFX::volume)
62 .method("properties", &FGEnvironmentFX::properties)
63 .method("play", &FGEnvironmentFX::play)
64 .method("stop", &FGEnvironmentFX::stop);
67 FGEnvironmentFX::~FGEnvironmentFX()
71 void FGEnvironmentFX::unbind()
74 _smgr->remove(_refname);
78 void FGEnvironmentFX::init()
85 void FGEnvironmentFX::reinit()
90 void FGEnvironmentFX::update (double dt)
96 if ( _enabled->getBoolValue() ) {
97 set_volume( _volume->getDoubleValue() );
100 SGSampleGroup::update(dt);
107 bool FGEnvironmentFX::add(const std::string& path_str, const std::string& refname)
113 SGPath path = globals->resolve_resource_path(path_str);
116 SG_LOG(SG_SOUND, SG_ALERT, "File not found: '" << path_str);
119 SG_LOG(SG_SOUND, SG_INFO, "Reading sound from " << path.str());
121 SGSharedPtr<SGSoundSample> sample;
122 sample = new SGSoundSample("", path);
123 if (sample->file_path().exists()) {
124 SGSampleGroup::add( sample, refname );
128 throw sg_io_exception("Environment FX: couldn't find file: '" + path.str() + "'");
135 void FGEnvironmentFX::position(const std::string& refname, double lon, double lat, double elevation)
137 SGSoundSample* sample = SGSampleGroup::find(refname);
140 SGGeod pos = SGGeod::fromDegFt(lon, lat, elevation);
141 sample->set_position( SGVec3d::fromGeod(pos) );
145 void FGEnvironmentFX::pitch(const std::string& refname, float pitch)
147 SGSoundSample* sample = SGSampleGroup::find(refname);
150 sample->set_volume( pitch );
154 void FGEnvironmentFX::volume(const std::string& refname, float volume)
156 SGSoundSample* sample = SGSampleGroup::find(refname);
159 sample->set_volume( volume );
163 void FGEnvironmentFX::properties(const std::string& refname, float reference_dist, float max_dist )
165 SGSoundSample* sample = SGSampleGroup::find(refname);
168 sample->set_reference_dist( reference_dist );
170 sample->set_max_dist( max_dist );
175 void FGEnvironmentFX::play(const std::string& refname, bool looping)
177 SGSampleGroup::play( refname, looping );
180 void FGEnvironmentFX::stop(const std::string& refname)
182 SGSampleGroup::stop( refname );
185 // end of fg_environmentfx.cxx