]> git.mxchange.org Git - flightgear.git/blob - src/Sound/scenefx.cxx
Interim windows build fix
[flightgear.git] / src / Sound / scenefx.cxx
1 // scenefx.cxx -- Scenery sound effect management implementation
2 //
3 // Started by Erik Hofman, November 2015
4 //
5 // Copyright (C) 2015  Erik Hofman <erik@ehofman.com>
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifdef _MSC_VER
24 #pragma warning (disable: 4786)
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <stdio.h>
32 #include <string>
33
34 #include "scenefx.hxx"
35
36 #include <Main/fg_props.hxx>
37 #include <Main/globals.hxx>
38
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>
44
45
46 #include <boost/shared_ptr.hpp>
47 #include <boost/weak_ptr.hpp>
48
49
50 static std::string _refname = "SceneFX";
51 typedef boost::shared_ptr<SGSampleGroup> SGSampleGroupRef;
52 typedef boost::shared_ptr<FGSceneFX> FGSceneFXRef;
53
54
55 FGSceneFX::FGSceneFX()
56 {
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);
62
63     nasal::Ghost<FGSceneFXRef>::init("soundfx.scene")
64       .bases<SGSampleGroupRef>()
65       .method("load", &FGSceneFX::load)
66       .method("damping", &FGSceneFX::model_damping);
67 }
68
69 void FGSceneFX::unbind()
70 {
71     if (_smgr)
72     {
73         _smgr->remove(_refname);
74     }
75 }
76
77 FGSceneFX::~FGSceneFX()
78 {
79     for (unsigned int i = 0; i < _sound.size(); i++ ) {
80         delete _sound[i];
81     }
82     _sound.clear();
83 }
84
85 void FGSceneFX::init()
86 {
87     if (!_smgr) {
88         return;
89     }
90
91     SGPath path(globals->get_fg_root());
92     path.append("Sounds/sounds.xml");
93
94     SGPropertyNode root;
95     try {
96         readProperties(path.str(), &root);
97     } catch (const sg_exception &) {
98         SG_LOG(SG_SOUND, SG_ALERT,
99                "Error reading file '" << path.str() << '\'');
100         return;
101     }
102
103     SGPropertyNode *node = root.getNode("fx");
104     if(node) {
105         for (int i = 0; i < node->nChildren(); ++i) {
106             SGXmlSound *soundfx = new SGXmlSound();
107
108             try {
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());
113                 delete soundfx;
114             }
115         }
116     }
117 }
118
119 void FGSceneFX::reinit()
120 {
121     for ( unsigned int i = 0; i < _sound.size(); i++ ) {
122         delete _sound[i];
123     }
124     _sound.clear();
125     init();
126 }
127
128 void FGSceneFX::update (double dt)
129 {
130     if (!_smgr) {
131         return;
132     }
133
134     if (_enabled->getBoolValue())
135     {
136         float fact = 1.0f - _damping->getFloatValue();
137         set_volume(_volume->getFloatValue() * fact);
138         resume();
139
140         // update sound effects if not paused
141         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
142             _sound[i]->update(dt);
143         }
144
145         SGSampleGroup::update(dt);
146     }
147     else {
148         suspend();
149     }
150 }
151
152 /* Sets the scene properties from the models point of view      */
153 void FGSceneFX::model_damping(float damping)
154 {
155     _damping->setFloatValue(damping);
156 }
157
158 /* (Over)load the sound file for a particular ref. name         */
159 bool FGSceneFX::load(const std::string& refname, const std::string& path_str)
160 {
161     if (!_smgr) {
162         return false;
163     }
164
165     SGPath path = globals->resolve_resource_path(path_str);
166     if (!path.isNull() && path.exists())
167     {
168         SGSoundSample* sample;
169         unsigned int i = 0;
170         do
171         {
172             sample = find(refname, i);
173             if (sample) {
174                 sample->set_sample_name(path_str);
175             }
176         }
177         while(sample);
178
179         return true;
180     }
181
182     throw sg_io_exception("SceneFX: couldn't find file: '" + path.str() + "'");
183
184     return false;
185 }
186
187 /* Control the sounds from the generating side.                 */
188 size_t FGSceneFX::add(const std::string& refname)
189 {
190     if (!_smgr) {
191         return false;
192     }
193
194     unsigned int num = 0;
195     std::string name;
196     do {
197         name = full_name(refname, num++);
198     } while(SGSampleGroup::exists(name));
199
200     SGSharedPtr<SGSoundSample> sample;
201     sample = new SGSoundSample();
202     SGSampleGroup::add(sample, name);
203
204     return num;
205 }
206
207 bool FGSceneFX::remove(const std::string& refname, size_t num)
208 {
209     std::string name = full_name(refname, num);
210     return SGSampleGroup::remove(name);
211 }
212
213 void FGSceneFX::position(const std::string& refname, size_t num, double lon, double lat, double elevation)
214 {
215     SGSoundSample* sample = find(refname, num);
216     if (sample)
217     {
218         SGGeod pos = SGGeod::fromDegFt(lon, lat, elevation);
219         sample->set_position(SGVec3d::fromGeod(pos));
220     }
221 }
222
223 void FGSceneFX::pitch(const std::string& refname, size_t num, float pitch)
224 {
225     SGSoundSample* sample = find(refname, num);
226     if (sample) {
227         sample->set_pitch(pitch);
228     }
229 }
230
231 void FGSceneFX::volume(const std::string& refname, size_t num, float volume)
232 {
233     SGSoundSample* sample = find(refname, num);
234     if (sample)
235     {
236         sample->set_volume(volume);
237     }
238 }
239
240 void FGSceneFX::properties(const std::string& refname, size_t num, float reference_dist, float maximum_dist)
241 {
242     SGSoundSample* sample = find(refname, num);
243     if (sample)
244     {
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);
249         }
250     }
251 }
252
253 void FGSceneFX::play(const std::string& refname, size_t num, bool looping)
254 {
255     SGSampleGroup::play(full_name(refname, num), looping);
256 }
257
258 void FGSceneFX::stop(const std::string& refname, size_t num)
259 {
260     SGSampleGroup::stop(full_name(refname, num));
261 }
262
263 const char* FGSceneFX::full_name(const std::string& refname, size_t num)
264 {
265     static char nstr[128];
266     snprintf(nstr, 127, "%s_%4zu", refname.c_str(), num);
267     return nstr;
268 }
269
270 SGSoundSample *FGSceneFX::find(const std::string& refname, size_t num)
271 {
272     return SGSampleGroup::find( full_name(refname, num) );
273 }
274
275 // end of scenefx.cxx