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"
42 // static double _fg_lin(double v) { return v; };
43 static double _fg_inv(double v) { return (v == 0) ? 1e99 : 1/v; };
44 static double _fg_abs(double v) { return (v >= 0) ? v : -v; };
45 static double _fg_sqrt(double v) { return (v < 0) ? sqrt(-v) : sqrt(v); };
46 static double _fg_log10(double v) { return (v < 1) ? 0 : log10(v+1); };
47 static double _fg_log(double v) { return (v < 1) ? 0 : log(v+1); };
48 // static double _fg_sqr(double v) { return pow(v, 2); };
49 // static double _fg_pow3(double v) { return pow(v, 3); };
62 // {"pow3", _fg_pow3},
66 FGSound::FGSound(const SGPropertyNode * node)
71 _type(FGSound::LEVEL),
86 _property = fgGetNode(_node->getStringValue("property"), true);
89 // seet sound global properties
91 _name = _node->getStringValue("name");
93 if ((_factor = _node->getFloatValue("factor")) == 0.0)
96 if ((_offset = _node->getFloatValue("offset")) == 0.0)
99 SG_LOG(SG_GENERAL, SG_INFO,
100 "Loading sound information for: " << _name );
102 string mode_str = _node->getStringValue("mode");
103 if (mode_str == "looped") {
104 _mode = FGSound::LOOPED;
106 _mode = FGSound::ONCE;
107 if (mode_str != (string)"once")
108 SG_LOG( SG_GENERAL, SG_INFO, "Unknown sound mode, default to 'once'");
111 string type_str = _node->getStringValue("type");
112 if (type_str == "flipflop") {
113 _type = FGSound::FLIPFLOP;
114 } else if (type_str== "inverted") {
115 _type = FGSound::INVERTED;
116 } else if (type_str == "raise") {
117 _type = FGSound::RAISE;
118 } else if (type_str == "fall") {
119 _type = FGSound::FALL;
121 _type = FGSound::LEVEL;
122 if (type_str != (string)"level")
123 SG_LOG( SG_GENERAL, SG_INFO, "Unknown sound type, default to 'level'");
128 // set position properties
130 _pos.dist = _node->getFloatValue("dist");
131 _pos.hor = _node->getFloatValue("pos_hor");
132 _pos.vert = _node->getFloatValue("pos_vert");
136 // set volume properties
140 vector<const SGPropertyNode *> kids = _node->getChildren("volume");
141 for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
144 if ((volume.prop=fgGetNode(kids[i]->getStringValue("property"), true))
146 volume.prop = fgGetNode("/null", true);
148 if ((volume.factor = kids[i]->getFloatValue("factor")) == 0.0)
151 if (volume.factor < 0.0) {
152 volume.factor = -volume.factor;
153 volume.subtract = true;
155 volume.subtract = false;
158 for (int j=0; __fg_snd_fn[j].fn; j++)
159 if (__fg_snd_fn[j].name == kids[i]->getStringValue("type")) {
160 volume.fn = __fg_snd_fn[j].fn;
165 SG_LOG( SG_GENERAL, SG_INFO, "Unknown volume type, default to 'lin'");
167 if ((volume.offset = kids[i]->getFloatValue("offset")) == 0.0)
170 if ((volume.min = kids[i]->getFloatValue("min")) < 0.0) {
171 SG_LOG( SG_GENERAL, SG_WARN,
172 "Volume minimum value below 0. Forced to 0.");
176 if ((volume.max = kids[i]->getFloatValue("max")) <= volume.min) {
177 SG_LOG( SG_GENERAL, SG_ALERT,
178 "Volume maximum value below minimum value. Forced above minimum.");
179 volume.max = volume.min + 5.0;
182 _volume.push_back(volume);
189 // set pitch properties
192 kids = _node->getChildren("pitch");
193 for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
196 if ((pitch.prop = fgGetNode(kids[i]->getStringValue("property"), true))
198 pitch.prop = fgGetNode("/null", true);
200 if ((pitch.factor = kids[i]->getFloatValue("factor")) == 0.0)
204 for (int j=0; __fg_snd_fn[j].fn; j++)
205 if(__fg_snd_fn[j].name == kids[i]->getStringValue("type")) {
206 pitch.fn = __fg_snd_fn[j].fn;
211 SG_LOG( SG_GENERAL, SG_INFO, "Unknown pitch type, default to 'lin'");
213 if ((pitch.offset = kids[i]->getFloatValue("offset")) == 0.0)
216 if ((pitch.min = kids[i]->getFloatValue("min")) < 0.0) {
217 SG_LOG( SG_GENERAL, SG_WARN,
218 "Pitch minimum value below 0. Forced to 0.");
222 if ((pitch.max = kids[i]->getFloatValue("max")) <= pitch.min) {
223 SG_LOG( SG_GENERAL, SG_ALERT,
224 "Pitch maximum value below minimum value. Forced above minimum.");
225 pitch.max = pitch.min + 5.0;
228 _pitch.push_back(pitch);
233 // Initialize the sample
235 FGSoundMgr * mgr = globals->get_soundmgr();
236 if ((_sample = mgr->find(_name)) == NULL)
237 _sample = mgr->add(_name, _node->getStringValue("path"));
239 _sample->set_volume(v);
240 _sample->set_pitch(p);
254 FGSound::update (int dt)
256 FGSoundMgr * mgr = globals->get_soundmgr();
259 // Do we have something to do?
265 if ((_type == FGSound::LEVEL) || (_type == FGSound::INVERTED)) {
267 bool check = (int)(_offset + _property->getFloatValue() * _factor);
268 if (_type == FGSound::INVERTED)
272 // If the state changes to false, stop playing.
276 SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
277 _sample->stop( mgr->get_scheduler(), false );
285 // If the sound is already playing we have nothing to do.
287 if (_active && (_mode == FGSound::ONCE))
290 } else { // FLIPFLOP, RAISE, FALL
292 bool check = (int)(_offset + _property->getFloatValue() * _factor);
293 if (check == _active)
297 // Check for state changes.
298 // If the state changed, and the sound is still playing: stop playing.
300 if (_sample->is_playing()) {
301 SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
302 _sample->stop( mgr->get_scheduler() );
305 if ( ((_type == FGSound::RAISE) && !check) ||
306 ((_type == FGSound::FALL) && check) )
314 int max = _volume.size();
317 double volume = 1.0, volume_offset = 0.0;
318 for(i = 0; i < max; i++) {
319 double v = _volume[i].prop->getDoubleValue();
322 v = _volume[i].fn(v);
324 v *= _volume[i].factor;
326 if (v > _volume[i].max)
329 if (v < _volume[i].min)
330 v = 0; // v = _volume[i].min;
332 if (_volume[i].subtract) // Hack!
333 volume = _volume[i].offset - v;
335 volume_offset += _volume[i].offset;
344 double pitch = 1.0, pitch_offset = 0.0;
345 for(i = 0; i < max; i++) {
346 double p = _pitch[i].prop->getDoubleValue();
351 p *= _pitch[i].factor;
353 if (p > _pitch[i].max)
356 if (p < _pitch[i].min)
359 if (_pitch[i].subtract) // Hack!
360 pitch = _pitch[i].offset - p;
362 pitch_offset += _pitch[i].offset;
368 // Change sample state
370 _sample->set_pitch( pitch_offset + pitch );
371 _sample->set_volume( volume_offset + volume );
374 // Do we need to start playing the sample?
376 if (_active && ((_type == FGSound::LEVEL) || (_type == FGSound::INVERTED)))
380 // This is needed for FGSound::FLIPFLOP and it works for
381 // FGSound::LEVEl. Doing it this way saves an extra 'if'.
385 if (_mode == FGSound::ONCE)
386 _sample->play(mgr->get_scheduler(), false);
388 _sample->play(mgr->get_scheduler(), true);
390 SG_LOG(SG_GENERAL, SG_INFO, "Starting audio playback for: " << _name);
391 SG_LOG(SG_GENERAL, SG_BULK,
392 "Playing " << ((_mode == ONCE) ? "once" : "looped"));