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 if (_node->getStringValue("mode") == "looped") {
103 _mode = FGSound::LOOPED;
105 _mode = FGSound::ONCE;
106 if (_node->getStringValue("mode") != (string)"once")
107 SG_LOG( SG_GENERAL, SG_INFO, "Unknown sound mode, default to 'once'");
110 if (_node->getStringValue("type") == "flipflop") {
111 _type = FGSound::FLIPFLOP;
113 } else if (_node->getStringValue("type") == "inverted") {
114 _type = FGSound::INVERTED;
116 } else if (_node->getStringValue("type") == "raise") {
117 _type = FGSound::RAISE;
119 } else if (_node->getStringValue("type") == "fall") {
120 _type = FGSound::FALL;
123 _type = FGSound::LEVEL;
124 if (_node->getStringValue("type") != (string)"level")
125 SG_LOG( SG_GENERAL, SG_INFO, "Unknown sound type, default to 'level'");
131 // set position properties
133 _pos.dist = _node->getFloatValue("dist");
134 _pos.hor = _node->getFloatValue("pos_hor");
135 _pos.vert = _node->getFloatValue("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 if ((volume.prop=fgGetNode(kids[i]->getStringValue("property"), true))
149 volume.prop = fgGetNode("/null", true);
151 if ((volume.factor = kids[i]->getFloatValue("factor")) == 0.0)
154 if (volume.factor < 0.0) {
155 volume.factor = -volume.factor;
156 volume.subtract = true;
158 volume.subtract = false;
161 for (int j=0; __fg_snd_fn[j].fn; j++)
162 if (__fg_snd_fn[j].name == kids[i]->getStringValue("type")) {
163 volume.fn = __fg_snd_fn[j].fn;
168 SG_LOG( SG_GENERAL, SG_INFO, "Unknown volume type, default to 'lin'");
170 if ((volume.offset = kids[i]->getFloatValue("offset")) == 0.0)
173 if ((volume.min = kids[i]->getFloatValue("min")) < 0.0) {
174 SG_LOG( SG_GENERAL, SG_WARN,
175 "Volume minimum value below 0. Forced to 0.");
179 if ((volume.max = kids[i]->getFloatValue("max")) <= volume.min) {
180 SG_LOG( SG_GENERAL, SG_ALERT,
181 "Volume maximum value below minimum value. Forced above minimum.");
182 volume.max = volume.min + 5.0;
185 _volume.push_back(volume);
192 // set pitch properties
195 kids = _node->getChildren("pitch");
196 for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
199 if ((pitch.prop = fgGetNode(kids[i]->getStringValue("property"), true))
201 pitch.prop = fgGetNode("/null", true);
203 if ((pitch.factor = kids[i]->getFloatValue("factor")) == 0.0)
207 for (int j=0; __fg_snd_fn[j].fn; j++)
208 if(__fg_snd_fn[j].name == kids[i]->getStringValue("type")) {
209 pitch.fn = __fg_snd_fn[j].fn;
214 SG_LOG( SG_GENERAL, SG_INFO, "Unknown pitch type, default to 'lin'");
216 if ((pitch.offset = kids[i]->getFloatValue("offset")) == 0.0)
219 if ((pitch.min = kids[i]->getFloatValue("min")) < 0.0) {
220 SG_LOG( SG_GENERAL, SG_WARN,
221 "Pitch minimum value below 0. Forced to 0.");
225 if ((pitch.max = kids[i]->getFloatValue("max")) <= pitch.min) {
226 SG_LOG( SG_GENERAL, SG_ALERT,
227 "Pitch maximum value below minimum value. Forced above minimum.");
228 pitch.max = pitch.min + 5.0;
231 _pitch.push_back(pitch);
236 // Initialize the sample
238 FGSoundMgr * mgr = globals->get_soundmgr();
239 if (mgr->find(_name) == NULL) {
240 _sample = mgr->add(_name, _node->getStringValue("path"));
241 _sample->set_volume(v);
242 _sample->set_pitch(p);
245 _sample = mgr->find(_name);
246 _sample->set_volume(_sample->get_volume() + v);
247 _sample->set_pitch(_sample->get_pitch() + p);
262 FGSound::update (int dt)
264 FGSoundMgr * mgr = globals->get_soundmgr();
267 // Do we have something to do?
273 if ((_type == FGSound::LEVEL) || (_type == FGSound::INVERTED)) {
275 bool check = (int)(_offset + _property->getFloatValue() * _factor);
276 if (_type == FGSound::INVERTED)
280 // If the state changes to false, stop playing.
284 if (mgr->is_playing(_name)) {
285 SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
293 // If the sound is already playing we have nothing to do.
295 if (_active && (_mode == FGSound::ONCE))
298 } else { // FLIPFLOP, RAISE, FALL
300 bool check = (int)(_offset + _property->getFloatValue() * _factor);
301 if (check == _active)
305 // Check for state changes.
306 // If the state changed, and the sound is still playing: stop playing.
308 if (mgr->is_playing(_name)) {
309 SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
313 if ( ((_type == FGSound::RAISE) && !check) ||
314 ((_type == FGSound::FALL) && check) )
321 int max = _volume.size();
324 double volume = 1.0, volume_offset = 0.0;
325 for(i = 0; i < max; i++) {
326 double v = _volume[i].prop->getDoubleValue();
329 v = _volume[i].fn(v);
331 v *= _volume[i].factor;
333 if (v > _volume[i].max)
336 if (v < _volume[i].min)
337 v = 0; // v = _volume[i].min;
339 if (_volume[i].subtract) // Hack!
340 volume = _volume[i].offset - v;
342 volume_offset += _volume[i].offset;
351 double pitch = 1.0, pitch_offset = 0.0;
352 for(i = 0; i < max; i++) {
353 double p = _pitch[i].prop->getDoubleValue();
358 p *= _pitch[i].factor;
360 if (p > _pitch[i].max)
363 if (p < _pitch[i].min)
366 if (_pitch[i].subtract) // Hack!
367 pitch = _pitch[i].offset - p;
369 pitch_offset += _pitch[i].offset;
375 // Change sample state
377 _sample->set_pitch( pitch_offset + pitch );
378 _sample->set_volume( volume_offset + volume );
381 // Do we need to start playing the sample?
383 if (_active && ((_type == FGSound::LEVEL) || (_type == FGSound::INVERTED)))
387 // This is needed for FGSound::FLIPFLOP and it works for
388 // FGSound::LEVEl. Doing it this way saves an extra 'if'.
392 if (_mode == FGSound::ONCE)
393 _sample->play(mgr->get_scheduler(), false);
395 _sample->play(mgr->get_scheduler(), true);
397 SG_LOG(SG_GENERAL, SG_INFO, "Starting audio playback for: " << _name);
398 SG_LOG(SG_GENERAL, SG_BULK,
399 "Playing " << ((_mode == ONCE) ? "once" : "looped"));
400 SG_LOG(SG_GENERAL, SG_BULK, "Initial volume: " << volume_offset);
401 SG_LOG(SG_GENERAL, SG_BULK, "Initial pitch: " << pitch_offset);