1 // fg_sound.cxx -- 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},
89 FGSound::init(SGPropertyNode *node)
93 // set global sound properties
96 _name = node->getStringValue("name", "");
97 SG_LOG(SG_GENERAL, SG_INFO, "Loading sound information for: " << _name );
99 const char *mode_str = node->getStringValue("mode", "");
100 if ( !strcmp(mode_str, "looped") ) {
101 _mode = FGSound::LOOPED;
103 } else if ( !strcmp(mode_str, "in-transit") ) {
104 _mode = FGSound::IN_TRANSIT;
107 _mode = FGSound::ONCE;
109 if ( strcmp(mode_str, "") )
110 SG_LOG(SG_GENERAL,SG_INFO, " Unknown sound mode, default to 'once'");
113 _property = fgGetNode(node->getStringValue("property", ""), true);
114 SGPropertyNode *condition = node->getChild("condition");
115 if (condition != NULL)
116 _condition = fgReadCondition(condition);
118 if (!_property && !_condition)
119 SG_LOG(SG_GENERAL, SG_WARN,
120 " Neither a condition nor a property specified");
123 // set volume properties
127 vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
128 for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
129 _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
131 if (strcmp(kids[i]->getStringValue("property"), ""))
132 volume.prop = fgGetNode(kids[i]->getStringValue("property", ""), true);
134 const char *intern_str = kids[i]->getStringValue("internal", "");
135 if (!strcmp(intern_str, "dt_play"))
136 volume.intern = &_dt_play;
137 else if (!strcmp(intern_str, "dt_stop"))
138 volume.intern = &_dt_stop;
140 if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
141 if (volume.factor < 0.0) {
142 volume.factor = -volume.factor;
143 volume.subtract = true;
146 const char *type_str = kids[i]->getStringValue("type", "");
147 if ( strcmp(type_str, "") ) {
149 for (int j=0; __fg_snd_fn[j].fn; j++)
150 if ( !strcmp(type_str, __fg_snd_fn[j].name) ) {
151 volume.fn = __fg_snd_fn[j].fn;
156 SG_LOG(SG_GENERAL,SG_INFO,
157 " Unknown volume type, default to 'lin'");
160 volume.offset = kids[i]->getDoubleValue("offset", 0.0);
162 if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
163 SG_LOG( SG_GENERAL, SG_WARN,
164 "Volume minimum value below 0. Forced to 0.");
166 volume.max = kids[i]->getDoubleValue("max", 0.0);
167 if (volume.max && (volume.max < volume.min) )
168 SG_LOG(SG_GENERAL,SG_ALERT,
169 " Volume maximum below minimum. Neglected.");
171 _volume.push_back(volume);
178 // set pitch properties
181 kids = node->getChildren("pitch");
182 for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
183 _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
185 if (strcmp(kids[i]->getStringValue("property", ""), ""))
186 pitch.prop = fgGetNode(kids[i]->getStringValue("property", ""), true);
188 const char *intern_str = kids[i]->getStringValue("internal", "");
189 if (!strcmp(intern_str, "dt_play"))
190 pitch.intern = &_dt_play;
191 else if (!strcmp(intern_str, "dt_stop"))
192 pitch.intern = &_dt_stop;
194 if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
195 if (pitch.factor < 0.0) {
196 pitch.factor = -pitch.factor;
197 pitch.subtract = true;
200 const char *type_str = kids[i]->getStringValue("type", "");
201 if ( strcmp(type_str, "") ) {
203 for (int j=0; __fg_snd_fn[j].fn; j++)
204 if ( !strcmp(type_str, __fg_snd_fn[j].name) ) {
205 pitch.fn = __fg_snd_fn[j].fn;
210 SG_LOG(SG_GENERAL,SG_INFO,
211 " Unknown pitch type, default to 'lin'");
214 pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
216 if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
217 SG_LOG(SG_GENERAL,SG_WARN,
218 " Pitch minimum value below 0. Forced to 0.");
220 pitch.max = kids[i]->getDoubleValue("max", 0.0);
221 if (pitch.max && (pitch.max < pitch.min) )
222 SG_LOG(SG_GENERAL,SG_ALERT,
223 " Pitch maximum below minimum. Neglected");
225 _pitch.push_back(pitch);
230 // Initialize the sample
232 _mgr = globals->get_soundmgr();
233 if ((_sample = _mgr->find(_name)) == NULL)
234 _sample = _mgr->add(_name, node->getStringValue("path", ""));
236 _sample->set_volume(v);
237 _sample->set_pitch(p);
251 FGSound::update (double dt)
253 double curr_value = 0.0;
256 // If the state changes to false, stop playing.
259 curr_value = _property->getDoubleValue();
261 if ( // Lisp, anyone?
262 (_condition && !_condition->test()) ||
263 (!_condition && _property &&
266 ( (_mode == FGSound::IN_TRANSIT) && (curr_value == _prev_value) )
272 if (_sample->is_playing()) {
273 SG_LOG(SG_GENERAL, SG_INFO, "Stopping audio after " << _dt_play
274 << " sec: " << _name );
275 _sample->stop( _mgr->get_scheduler() );
287 // If the mode is ONCE and the sound is still playing,
288 // we have nothing to do anymore.
290 if (_active && (_mode == FGSound::ONCE)) {
292 if (!_sample->is_playing()) {
303 // Update playing time and cache the current value.
306 _prev_value = curr_value;
312 int max = _volume.size();
314 double volume_offset = 0.0;
316 for(i = 0; i < max; i++) {
320 v = _volume[i].prop->getDoubleValue();
322 else if (_volume[i].intern)
323 v = *_volume[i].intern;
326 v = _volume[i].fn(v);
328 v *= _volume[i].factor;
330 if (_volume[i].max && (v > _volume[i].max))
333 else if (v < _volume[i].min)
336 if (_volume[i].subtract) // Hack!
337 volume = _volume[i].offset - v;
340 volume_offset += _volume[i].offset;
350 double pitch_offset = 0.0;
352 for(i = 0; i < max; i++) {
356 p = _pitch[i].prop->getDoubleValue();
358 else if (_pitch[i].intern)
359 p = *_pitch[i].intern;
364 p *= _pitch[i].factor;
366 if (_pitch[i].max && (p > _pitch[i].max))
369 else if (p < _pitch[i].min)
372 if (_pitch[i].subtract) // Hack!
373 pitch = _pitch[i].offset - p;
376 pitch_offset += _pitch[i].offset;
382 // Change sample state
384 _sample->set_pitch( pitch_offset + pitch );
385 _sample->set_volume( volume_offset + volume );
389 // Do we need to start playing the sample?
393 if (_mode == FGSound::ONCE)
394 _sample->play(_mgr->get_scheduler(), false);
397 _sample->play(_mgr->get_scheduler(), true);
399 SG_LOG(SG_GENERAL, SG_INFO, "Playing audio after " << _dt_stop
400 << " sec: " << _name);
401 SG_LOG(SG_GENERAL, SG_BULK,
402 "Playing " << ((_mode == ONCE) ? "once" : "looped"));