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); };
45 static double _fg_log(double v) { return (v < 1) ? 0 : log(v); };
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},
81 FGSound::init(SGPropertyNode *node)
85 // set global sound properties
88 _name = node->getStringValue("name");
89 SG_LOG(SG_GENERAL, SG_INFO, "Loading sound information for: " << _name );
91 const char *mode_str = node->getStringValue("mode");
92 if ( !strcmp(mode_str, "looped") ) {
93 _mode = FGSound::LOOPED;
95 } else if ( !strcmp(mode_str, "in-transit") ) {
96 _mode = FGSound::IN_TRANSIT;
99 _mode = FGSound::ONCE;
101 if ( strcmp(mode_str, "") )
102 SG_LOG(SG_GENERAL,SG_INFO, " Unknown sound mode, default to 'once'");
105 _property = fgGetNode(node->getStringValue("property"), true);
106 SGPropertyNode *condition = node->getChild("condition");
107 if (condition != NULL)
108 _condition = fgReadCondition(condition);
110 if (!_property && !_condition)
111 SG_LOG(SG_GENERAL, SG_WARN,
112 " Neither a condition nor a property specified");
115 // set volume properties
119 vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
120 for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
123 volume.prop = fgGetNode(kids[i]->getStringValue("property"), true);
125 if ((volume.factor = kids[i]->getDoubleValue("factor")) == 0.0)
129 if (volume.factor < 0.0) {
130 volume.factor = -volume.factor;
131 volume.subtract = true;
134 volume.subtract = false;
137 const char *type_str = kids[i]->getStringValue("type");
138 if ( strcmp(type_str, "") ) {
140 for (int j=0; __fg_snd_fn[j].fn; j++)
141 if ( !strcmp(type_str, __fg_snd_fn[j].name) ) {
142 volume.fn = __fg_snd_fn[j].fn;
147 SG_LOG(SG_GENERAL,SG_INFO,
148 " Unknown volume type, default to 'lin'");
151 volume.offset = kids[i]->getDoubleValue("offset");
153 if ((volume.min = kids[i]->getDoubleValue("min")) < 0.0) {
154 SG_LOG( SG_GENERAL, SG_WARN,
155 "Volume minimum value below 0. Forced to 0.");
160 volume.max = kids[i]->getDoubleValue("max");
161 if (volume.max && (volume.max < volume.min) ) {
162 SG_LOG(SG_GENERAL,SG_ALERT,
163 " Volume maximum below minimum. Neglected.");
168 _volume.push_back(volume);
175 // set pitch properties
178 kids = node->getChildren("pitch");
179 for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
182 pitch.prop = fgGetNode(kids[i]->getStringValue("property"), true);
184 if ((pitch.factor = kids[i]->getDoubleValue("factor")) == 0.0)
188 if (pitch.factor < 0.0) {
189 pitch.factor = -pitch.factor;
190 pitch.subtract = true;
193 pitch.subtract = false;
196 const char *type_str = kids[i]->getStringValue("type");
197 if ( strcmp(type_str, "") ) {
199 for (int j=0; __fg_snd_fn[j].fn; j++)
200 if ( !strcmp(type_str, __fg_snd_fn[j].name) ) {
201 pitch.fn = __fg_snd_fn[j].fn;
206 SG_LOG(SG_GENERAL,SG_INFO,
207 " Unknown pitch type, default to 'lin'");
210 if ((pitch.offset = kids[i]->getDoubleValue("offset")) == 0.0)
213 if ((pitch.min = kids[i]->getDoubleValue("min")) < 0.0) {
214 SG_LOG(SG_GENERAL,SG_WARN,
215 " Pitch minimum value below 0. Forced to 0.");
220 pitch.max = kids[i]->getDoubleValue("max");
221 if (pitch.max && (pitch.max < pitch.min) ) {
222 SG_LOG(SG_GENERAL,SG_ALERT,
223 " Pitch maximum below minimum. Neglected");
228 _pitch.push_back(pitch);
233 // Initialize the sample
235 _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 (double dt)
256 double curr_value = 0.0;
259 // If the state changes to false, stop playing.
262 curr_value = _property->getDoubleValue();
264 if ( // Lisp, anyone?
265 (_condition && !_condition->test()) ||
266 (_property && !_condition &&
269 ( (_mode == FGSound::IN_TRANSIT) && (curr_value == _prev_value) )
276 if (_sample->is_playing()) {
277 SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
278 _sample->stop( _mgr->get_scheduler() );
286 // If the mode is ONCE and the sound is still playing,
287 // we have nothing to do anymore.
289 if (_active && (_mode == FGSound::ONCE))
293 // Cache current value;
295 _prev_value = curr_value;
301 int max = _volume.size();
303 double volume_offset = 0.0;
305 for(i = 0; i < max; i++) {
306 double v = _volume[i].prop->getDoubleValue();
309 v = _volume[i].fn(v);
311 v *= _volume[i].factor;
313 if (_volume[i].max && (v > _volume[i].max))
316 else if (v < _volume[i].min)
319 if (_volume[i].subtract) // Hack!
320 volume = _volume[i].offset - v;
323 volume_offset += _volume[i].offset;
333 double pitch_offset = 0.0;
335 for(i = 0; i < max; i++) {
336 double p = _pitch[i].prop->getDoubleValue();
341 p *= _pitch[i].factor;
343 if (_pitch[i].max && (p > _pitch[i].max))
346 else if (p < _pitch[i].min)
349 if (_pitch[i].subtract) // Hack!
350 pitch = _pitch[i].offset - p;
353 pitch_offset += _pitch[i].offset;
359 // Change sample state
361 _sample->set_pitch( pitch_offset + pitch );
362 _sample->set_volume( volume_offset + volume );
366 // Do we need to start playing the sample?
371 if (_mode == FGSound::ONCE)
372 _sample->play(_mgr->get_scheduler(), false);
375 _sample->play(_mgr->get_scheduler(), true);
377 SG_LOG(SG_GENERAL, SG_INFO, "Starting audio playback for: " << _name);
378 SG_LOG(SG_GENERAL, SG_BULK,
379 "Playing " << ((_mode == ONCE) ? "once" : "looped"));