1 // fg_sound.hxx -- Sound class implementation
3 // Started by Erik Hofman, February 2002
4 // (Reuses some code from fg_fx.cxx created by David Megginson)
6 // Copyright (C) 2002 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/compiler.h>
26 #ifdef SG_HAVE_STD_INCLUDES
33 #include <simgear/debug/logstream.hxx>
35 #include <Main/fg_props.hxx>
37 #include "fg_sound.hxx"
40 // static double _fg_lin(double v) { return v; };
41 static double _fg_inv(double v) { return (v == 0) ? 1e99 : 1/v; };
42 static double _fg_abs(double v) { return (v >= 0) ? v : -v; };
43 static double _fg_sqrt(double v) { return (v < 0) ? sqrt(-v) : sqrt(v); };
44 static double _fg_log10(double v) { return (v < 1) ? 0 : log10(v+1); };
45 static double _fg_log(double v) { return (v < 1) ? 0 : log(v+1); };
46 // static double _fg_sqr(double v) { return pow(v, 2); };
47 // static double _fg_pow3(double v) { return pow(v, 3); };
60 // {"pow3", _fg_pow3},
64 FGSound::FGSound(const SGPropertyNode * node)
69 _type(FGSound::LEVEL),
84 _property = fgGetNode(_node->getStringValue("property"), true);
87 // set global sound properties
89 _name = _node->getStringValue("name");
91 if ((_factor = _node->getDoubleValue("factor")) == 0.0)
94 _offset = _node->getDoubleValue("offset");
96 SG_LOG(SG_GENERAL, SG_INFO, "Loading sound information for: " << _name );
98 const char *mode_str = _node->getStringValue("mode");
99 if ( !strcmp(mode_str,"looped") ) {
100 _mode = FGSound::LOOPED;
103 _mode = FGSound::ONCE;
105 if ( strcmp(mode_str, "") )
106 SG_LOG(SG_GENERAL,SG_INFO, " Unknown sound mode, default to 'once'");
109 const char *type_str = _node->getStringValue("type");
110 if ( !strcmp(type_str, "flipflop") ) {
111 _type = FGSound::FLIPFLOP;
113 } else if ( !strcmp(type_str, "inverted") ) {
114 _type = FGSound::INVERTED;
116 } else if ( !strcmp(type_str, "raise") ) {
117 _type = FGSound::RAISE;
119 } else if ( !strcmp(type_str, "fall") ) {
120 _type = FGSound::FALL;
123 _type = FGSound::LEVEL;
125 if ( strcmp(type_str, "") )
126 SG_LOG(SG_GENERAL,SG_INFO, " Unknown sound type, default to 'level'");
131 // set position properties
133 _pos.dist = _node->getDoubleValue("dist");
134 _pos.hor = _node->getDoubleValue("pos_hor");
135 _pos.vert = _node->getDoubleValue("pos_vert");
139 // set volume properties
143 vector<const SGPropertyNode *> kids = _node->getChildren("volume");
144 for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
147 volume.prop = fgGetNode(kids[i]->getStringValue("property"), true);
149 if ((volume.factor = kids[i]->getDoubleValue("factor")) == 0.0)
153 if (volume.factor < 0.0) {
154 volume.factor = -volume.factor;
155 volume.subtract = true;
158 volume.subtract = false;
161 const char *type_str = kids[i]->getStringValue("type");
162 if ( strcmp(type_str, "") ) {
164 for (int j=0; __fg_snd_fn[j].fn; j++)
165 if ( !strcmp(type_str, __fg_snd_fn[j].name) ) {
166 volume.fn = __fg_snd_fn[j].fn;
171 SG_LOG(SG_GENERAL,SG_INFO,
172 " Unknown volume type, default to 'lin'");
175 volume.offset = kids[i]->getDoubleValue("offset");
177 if ((volume.min = kids[i]->getDoubleValue("min")) < 0.0) {
178 SG_LOG( SG_GENERAL, SG_WARN,
179 "Volume minimum value below 0. Forced to 0.");
184 volume.max = kids[i]->getDoubleValue("max");
185 if (volume.max && (volume.max < volume.min) ) {
186 SG_LOG(SG_GENERAL,SG_ALERT,
187 " Volume maximum below minimum. Neglected.");
192 _volume.push_back(volume);
199 // set pitch properties
202 kids = _node->getChildren("pitch");
203 for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
206 pitch.prop = fgGetNode(kids[i]->getStringValue("property"), true);
208 if ((pitch.factor = kids[i]->getDoubleValue("factor")) == 0.0)
212 if (pitch.factor < 0.0) {
213 pitch.factor = -pitch.factor;
214 pitch.subtract = true;
217 pitch.subtract = false;
220 const char *type_str = kids[i]->getStringValue("type");
221 if ( strcmp(type_str, "") ) {
223 for (int j=0; __fg_snd_fn[j].fn; j++)
224 if ( !strcmp(type_str, __fg_snd_fn[j].name) ) {
225 pitch.fn = __fg_snd_fn[j].fn;
230 SG_LOG(SG_GENERAL,SG_INFO,
231 " Unknown pitch type, default to 'lin'");
234 if ((pitch.offset = kids[i]->getDoubleValue("offset")) == 0.0)
237 if ((pitch.min = kids[i]->getDoubleValue("min")) < 0.0) {
238 SG_LOG(SG_GENERAL,SG_WARN,
239 " Pitch minimum value below 0. Forced to 0.");
244 pitch.max = kids[i]->getDoubleValue("max");
245 if (pitch.max && (pitch.max < pitch.min) ) {
246 SG_LOG(SG_GENERAL,SG_ALERT,
247 " Pitch maximum below minimum. Neglected");
252 _pitch.push_back(pitch);
257 // Initialize the sample
259 FGSoundMgr * mgr = globals->get_soundmgr();
260 if ((_sample = mgr->find(_name)) == NULL)
261 _sample = mgr->add(_name, _node->getStringValue("path"));
263 _sample->set_volume(v);
264 _sample->set_pitch(p);
278 FGSound::update (int dt)
280 FGSoundMgr * mgr = globals->get_soundmgr();
283 // Do we have something to do?
289 if ((_type == FGSound::LEVEL) || (_type == FGSound::INVERTED)) {
292 // use an integer to get false when: -1 < check < 1
294 bool check = (int)(_offset + _property->getDoubleValue() * _factor);
295 if (_type == FGSound::INVERTED)
299 // If the state changes to false, stop playing.
303 SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
304 _sample->stop( mgr->get_scheduler(), false );
312 // If the sound is already playing we have nothing to do.
314 if (_active && (_mode == FGSound::ONCE))
317 } else { // FLIPFLOP, RAISE, FALL
319 bool check = (int)(_offset + _property->getDoubleValue() * _factor);
320 if (check == _active)
324 // Check for state changes.
325 // If the state changed, and the sound is still playing: stop playing.
327 if (_sample->is_playing()) {
328 SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
329 _sample->stop( mgr->get_scheduler() );
332 if ( ((_type == FGSound::RAISE) && !check) ||
333 ((_type == FGSound::FALL) && check) )
344 max = _volume.size();
346 double volume_offset = 0.0;
348 for(i = 0; i < max; i++) {
349 double v = _volume[i].prop->getDoubleValue();
352 v = _volume[i].fn(v);
354 v *= _volume[i].factor;
356 if (!_volume[i].max && (v > _volume[i].max))
359 else if (!_volume[i].min && (v < _volume[i].min))
362 if (_volume[i].subtract) // Hack!
363 volume = _volume[i].offset - v;
365 volume_offset += _volume[i].offset;
375 double pitch_offset = 0.0;
377 for(i = 0; i < max; i++) {
378 double p = _pitch[i].prop->getDoubleValue();
383 p *= _pitch[i].factor;
385 if (!_pitch[i].max && (p > _pitch[i].max))
388 else if (!_pitch[i].min && (p < _pitch[i].min))
391 if (_pitch[i].subtract) // Hack!
392 pitch = _pitch[i].offset - p;
394 pitch_offset += _pitch[i].offset;
400 // Change sample state
402 _sample->set_pitch( pitch_offset + pitch );
403 _sample->set_volume( volume_offset + volume );
407 // Do we need to start playing the sample?
409 if (_active && ((_type == FGSound::LEVEL) || (_type == FGSound::INVERTED)))
413 // This is needed for FGSound::FLIPFLOP and it works for
414 // FGSound::LEVEl. Doing it this way saves an extra 'if'.
418 if (_mode == FGSound::ONCE)
419 _sample->play(mgr->get_scheduler(), false);
421 _sample->play(mgr->get_scheduler(), true);
423 SG_LOG(SG_GENERAL, SG_INFO, "Starting audio playback for: " << _name);
424 SG_LOG(SG_GENERAL, SG_BULK,
425 "Playing " << ((_mode == ONCE) ? "once" : "looped"));