1 // 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>
34 #include <simgear/props/condition.hxx>
35 #include <simgear/math/fastmath.hxx>
38 #include "xmlsound.hxx"
41 // static double _snd_lin(double v) { return v; }
42 static double _snd_inv(double v) { return (v == 0) ? 1e99 : 1/v; }
43 static double _snd_abs(double v) { return (v >= 0) ? v : -v; }
44 static double _snd_sqrt(double v) { return (v < 0) ? sqrt(-v) : sqrt(v); }
45 static double _snd_log10(double v) { return (v < 1) ? 0 : fast_log10(v); }
46 static double _snd_log(double v) { return (v < 1) ? 0 : fast_log(v); }
47 // static double _snd_sqr(double v) { return v*v; }
48 // static double _snd_pow3(double v) { return v*v*v; }
61 // {"pow3", _snd_pow3},
65 SGXmlSound::SGXmlSound()
71 _mode(SGXmlSound::ONCE),
79 SGXmlSound::~SGXmlSound()
95 SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node, SGSoundMgr *sndmgr,
100 // set global sound properties
103 _name = node->getStringValue("name", "");
104 SG_LOG(SG_GENERAL, SG_INFO, "Loading sound information for: " << _name );
106 const char *mode_str = node->getStringValue("mode", "");
107 if ( !strcmp(mode_str, "looped") ) {
108 _mode = SGXmlSound::LOOPED;
110 } else if ( !strcmp(mode_str, "in-transit") ) {
111 _mode = SGXmlSound::IN_TRANSIT;
114 _mode = SGXmlSound::ONCE;
116 if ( strcmp(mode_str, "") )
117 SG_LOG(SG_GENERAL,SG_INFO, " Unknown sound mode, default to 'once'");
120 _property = root->getNode(node->getStringValue("property", ""), true);
121 SGPropertyNode *condition = node->getChild("condition");
122 if (condition != NULL)
123 _condition = sgReadCondition(root, condition);
125 if (!_property && !_condition)
126 SG_LOG(SG_GENERAL, SG_WARN,
127 " Neither a condition nor a property specified");
130 // set volume properties
134 vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
135 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
136 _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
138 if (strcmp(kids[i]->getStringValue("property"), ""))
139 volume.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
141 const char *intern_str = kids[i]->getStringValue("internal", "");
142 if (!strcmp(intern_str, "dt_play"))
143 volume.intern = &_dt_play;
144 else if (!strcmp(intern_str, "dt_stop"))
145 volume.intern = &_dt_stop;
147 if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
148 if (volume.factor < 0.0) {
149 volume.factor = -volume.factor;
150 volume.subtract = true;
153 const char *type_str = kids[i]->getStringValue("type", "");
154 if ( strcmp(type_str, "") ) {
156 for (int j=0; __sound_fn[j].fn; j++)
157 if ( !strcmp(type_str, __sound_fn[j].name) ) {
158 volume.fn = __sound_fn[j].fn;
163 SG_LOG(SG_GENERAL,SG_INFO,
164 " Unknown volume type, default to 'lin'");
167 volume.offset = kids[i]->getDoubleValue("offset", 0.0);
169 if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
170 SG_LOG( SG_GENERAL, SG_WARN,
171 "Volume minimum value below 0. Forced to 0.");
173 volume.max = kids[i]->getDoubleValue("max", 0.0);
174 if (volume.max && (volume.max < volume.min) )
175 SG_LOG(SG_GENERAL,SG_ALERT,
176 " Volume maximum below minimum. Neglected.");
178 _volume.push_back(volume);
185 // set pitch properties
188 kids = node->getChildren("pitch");
189 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
190 _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
192 if (strcmp(kids[i]->getStringValue("property", ""), ""))
193 pitch.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
195 const char *intern_str = kids[i]->getStringValue("internal", "");
196 if (!strcmp(intern_str, "dt_play"))
197 pitch.intern = &_dt_play;
198 else if (!strcmp(intern_str, "dt_stop"))
199 pitch.intern = &_dt_stop;
201 if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
202 if (pitch.factor < 0.0) {
203 pitch.factor = -pitch.factor;
204 pitch.subtract = true;
207 const char *type_str = kids[i]->getStringValue("type", "");
208 if ( strcmp(type_str, "") ) {
210 for (int j=0; __sound_fn[j].fn; j++)
211 if ( !strcmp(type_str, __sound_fn[j].name) ) {
212 pitch.fn = __sound_fn[j].fn;
217 SG_LOG(SG_GENERAL,SG_INFO,
218 " Unknown pitch type, default to 'lin'");
221 pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
223 if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
224 SG_LOG(SG_GENERAL,SG_WARN,
225 " Pitch minimum value below 0. Forced to 0.");
227 pitch.max = kids[i]->getDoubleValue("max", 0.0);
228 if (pitch.max && (pitch.max < pitch.min) )
229 SG_LOG(SG_GENERAL,SG_ALERT,
230 " Pitch maximum below minimum. Neglected");
232 _pitch.push_back(pitch);
237 // Initialize the sample
240 if ( (_sample = _mgr->find(_name)) == NULL ) {
241 _sample = new SGSoundSample( path.c_str(),
242 node->getStringValue("path", ""),
244 _mgr->add( _sample, _name );
247 _sample->set_volume(v);
248 _sample->set_pitch(p);
252 SGXmlSound::update (double dt)
254 double curr_value = 0.0;
257 // If the state changes to false, stop playing.
260 curr_value = _property->getDoubleValue();
262 if ( // Lisp, anyone?
263 (_condition && !_condition->test()) ||
264 (!_condition && _property &&
267 ( (_mode == SGXmlSound::IN_TRANSIT) && (curr_value == _prev_value) )
272 if ((_mode != SGXmlSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME)) {
273 if (_sample->is_playing()) {
274 SG_LOG(SG_GENERAL, SG_INFO, "Stopping audio after " << _dt_play
275 << " sec: " << _name );
291 // If the mode is ONCE and the sound is still playing,
292 // we have nothing to do anymore.
294 if (_active && (_mode == SGXmlSound::ONCE)) {
296 if (!_sample->is_playing()) {
307 // Update the playing time, cache the current value and
308 // clear the delay timer.
311 _prev_value = curr_value;
318 int max = _volume.size();
320 double volume_offset = 0.0;
322 for(i = 0; i < max; i++) {
326 v = _volume[i].prop->getDoubleValue();
328 else if (_volume[i].intern)
329 v = *_volume[i].intern;
332 v = _volume[i].fn(v);
334 v *= _volume[i].factor;
336 if (_volume[i].max && (v > _volume[i].max))
339 else if (v < _volume[i].min)
342 if (_volume[i].subtract) // Hack!
343 volume = _volume[i].offset - v;
346 volume_offset += _volume[i].offset;
356 double pitch_offset = 0.0;
358 for(i = 0; i < max; i++) {
362 p = _pitch[i].prop->getDoubleValue();
364 else if (_pitch[i].intern)
365 p = *_pitch[i].intern;
370 p *= _pitch[i].factor;
372 if (_pitch[i].max && (p > _pitch[i].max))
375 else if (p < _pitch[i].min)
378 if (_pitch[i].subtract) // Hack!
379 pitch = _pitch[i].offset - p;
382 pitch_offset += _pitch[i].offset;
388 // Change sample state
390 _sample->set_pitch( pitch_offset + pitch );
391 _sample->set_volume( volume_offset + volume );
395 // Do we need to start playing the sample?
399 if (_mode == SGXmlSound::ONCE)
400 _sample->play(false);
405 SG_LOG(SG_GENERAL, SG_INFO, "Playing audio after " << _dt_stop
406 << " sec: " << _name);
407 SG_LOG(SG_GENERAL, SG_BULK,
408 "Playing " << ((_mode == ONCE) ? "once" : "looped"));