]> git.mxchange.org Git - flightgear.git/blob - src/Sound/environment_fx.cxx
Add Nasal bindings
[flightgear.git] / src / Sound / environment_fx.cxx
1 // fg_environmentfx.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 "environment_fx.hxx"
33
34 #include <Main/fg_props.hxx>
35 #include <Main/globals.hxx>
36
37 #include <simgear/misc/sg_path.hxx>
38 #include <simgear/sound/soundmgr_openal.hxx>
39 #include <simgear/nasal/cppbind/Ghost.hxx>
40
41 #include <boost/shared_ptr.hpp>
42 #include <boost/weak_ptr.hpp>
43
44
45 static std::string _refname = "EnvironmentFX";
46 typedef boost::shared_ptr<SGSampleGroup> SGSampleGroupRef;
47 typedef boost::shared_ptr<FGEnvironmentFX> FGEnvironmentFXRef;
48
49
50 FGEnvironmentFX::FGEnvironmentFX()
51 {
52     _enabled = fgGetNode("/sim/sound/environment/enabled", true);
53     _volume = fgGetNode("/sim/sound/environment/volume", true);
54     _smgr->add(this, _refname);
55
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);
65 }
66
67 FGEnvironmentFX::~FGEnvironmentFX()
68 {
69 }
70
71 void FGEnvironmentFX::unbind()
72 {
73     if (_smgr) {
74         _smgr->remove(_refname);
75     }
76 }
77
78 void FGEnvironmentFX::init()
79 {
80     if (!_smgr) {
81         return;
82     }
83 }
84
85 void FGEnvironmentFX::reinit()
86 {
87     init();
88 }
89
90 void FGEnvironmentFX::update (double dt)
91 {
92     if (!_smgr) {
93         return;
94     }
95
96     if ( _enabled->getBoolValue() ) {
97         set_volume( _volume->getDoubleValue() );
98         resume();
99
100         SGSampleGroup::update(dt);
101     }
102     else {
103         suspend();
104     }
105 }
106
107 bool FGEnvironmentFX::add(const std::string& path_str, const std::string& refname)
108 {
109     if (!_smgr) {
110         return false;
111     }
112
113     SGPath path = globals->resolve_resource_path(path_str);
114     if (path.isNull())
115     {
116         SG_LOG(SG_SOUND, SG_ALERT, "File not found: '" << path_str);
117         return false;
118     }
119     SG_LOG(SG_SOUND, SG_INFO, "Reading sound from " << path.str());
120
121     SGSharedPtr<SGSoundSample> sample;
122     sample = new SGSoundSample("", path);
123     if (sample->file_path().exists()) {
124         SGSampleGroup::add( sample, refname );
125     }
126     else
127     {
128         throw sg_io_exception("Environment FX: couldn't find file: '" + path.str() + "'");
129         delete sample;
130         return false;
131     }
132     return true;
133 }
134
135 void FGEnvironmentFX::position(const std::string& refname, double lon, double lat, double elevation)
136 {
137     SGSoundSample* sample = SGSampleGroup::find(refname);
138     if (sample)
139     {
140         SGGeod pos = SGGeod::fromDegFt(lon, lat, elevation);
141         sample->set_position( SGVec3d::fromGeod(pos) );
142     }
143 }
144
145 void FGEnvironmentFX::pitch(const std::string& refname, float pitch)
146 {
147     SGSoundSample* sample = SGSampleGroup::find(refname);
148     if (sample)
149     {
150         sample->set_volume( pitch );
151     }
152 }
153
154 void FGEnvironmentFX::volume(const std::string& refname, float volume)
155 {
156     SGSoundSample* sample = SGSampleGroup::find(refname);
157     if (sample)
158     {
159         sample->set_volume( volume );
160     }
161 }
162
163 void FGEnvironmentFX::properties(const std::string& refname, float reference_dist, float max_dist )
164 {
165     SGSoundSample* sample = SGSampleGroup::find(refname);
166     if (sample)
167     {
168         sample->set_reference_dist( reference_dist );
169         if (max_dist > 0) {
170             sample->set_max_dist( max_dist );
171         }
172     }
173 }
174
175 void FGEnvironmentFX::play(const std::string& refname, bool looping)
176 {
177     SGSampleGroup::play( refname, looping );
178 }
179
180 void FGEnvironmentFX::stop(const std::string& refname)
181 {
182     SGSampleGroup::stop( refname );
183 }
184
185 // end of fg_environmentfx.cxx