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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 # include <simgear_config.h>
28 #include <simgear/compiler.h>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/props/condition.hxx>
34 #include <simgear/math/SGMath.hxx>
37 #include "xmlsound.hxx"
40 // static double _snd_lin(double v) { return v; }
41 static double _snd_inv(double v) { return (v == 0) ? 1e99 : 1/v; }
42 static double _snd_abs(double v) { return (v >= 0) ? v : -v; }
43 static double _snd_sqrt(double v) { return sqrt(fabs(v)); }
44 static double _snd_log10(double v) { return log10(fabs(v)+1e-9); }
45 static double _snd_log(double v) { return log(fabs(v)+1e-9); }
46 // static double _snd_sqr(double v) { return v*v; }
47 // static double _snd_pow3(double v) { return v*v*v; }
61 SGXmlSound::SGXmlSound()
65 _mode(SGXmlSound::ONCE),
74 SGXmlSound::~SGXmlSound()
84 SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node,
85 SGSampleGroup *sgrp, SGSampleGroup *avionics,
90 // set global sound properties
93 _name = node->getStringValue("name", "");
94 SG_LOG(SG_GENERAL, SG_DEBUG, "Loading sound information for: " << _name );
96 string mode_str = node->getStringValue("mode", "");
97 if ( mode_str == "looped" ) {
98 _mode = SGXmlSound::LOOPED;
100 } else if ( mode_str == "in-transit" ) {
101 _mode = SGXmlSound::IN_TRANSIT;
104 _mode = SGXmlSound::ONCE;
107 bool is_avionics = false;
108 string type_str = node->getStringValue("type", "fx");
109 if ( type_str == "avionics" )
112 string propval = node->getStringValue("property", "");
114 _property = root->getNode(propval, true);
116 SGPropertyNode *condition = node->getChild("condition");
117 if (condition != NULL)
118 _condition = sgReadCondition(root, condition);
120 if (!_property && !_condition)
121 SG_LOG(SG_GENERAL, SG_WARN,
122 " Neither a condition nor a property specified");
124 _delay = node->getDoubleValue("delay-sec", 0.0);
127 // set volume properties
131 std::vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
132 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
133 _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
135 propval = kids[i]->getStringValue("property", "");
137 volume.prop = root->getNode(propval, true);
139 string intern_str = kids[i]->getStringValue("internal", "");
140 if (intern_str == "dt_play")
141 volume.intern = &_dt_play;
142 else if (intern_str == "dt_stop")
143 volume.intern = &_dt_stop;
145 if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
146 if (volume.factor < 0.0) {
147 volume.factor = -volume.factor;
148 volume.subtract = true;
151 string type_str = kids[i]->getStringValue("type", "");
152 if ( type_str != "" ) {
154 for (int j=0; __sound_fn[j].fn; j++)
155 if ( type_str == __sound_fn[j].name ) {
156 volume.fn = __sound_fn[j].fn;
161 SG_LOG(SG_GENERAL,SG_INFO,
162 " Unknown volume type, default to 'lin'");
165 volume.offset = kids[i]->getDoubleValue("offset", 0.0);
167 if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
168 SG_LOG( SG_GENERAL, SG_WARN,
169 "Volume minimum value below 0. Forced to 0.");
171 volume.max = kids[i]->getDoubleValue("max", 0.0);
172 if (volume.max && (volume.max < volume.min) )
173 SG_LOG(SG_GENERAL,SG_ALERT,
174 " Volume maximum below minimum. Neglected.");
176 _volume.push_back(volume);
181 // rule of thumb: make reference distance a 100th of the maximum distance.
182 float reference_dist = node->getDoubleValue("reference-dist", 60.0);
183 float max_dist = node->getDoubleValue("max-dist", 6000.0);
186 // set pitch properties
189 kids = node->getChildren("pitch");
190 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
191 _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
193 propval = kids[i]->getStringValue("property", "");
195 pitch.prop = root->getNode(propval, true);
197 string intern_str = kids[i]->getStringValue("internal", "");
198 if (intern_str == "dt_play")
199 pitch.intern = &_dt_play;
200 else if (intern_str == "dt_stop")
201 pitch.intern = &_dt_stop;
203 if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
204 if (pitch.factor < 0.0) {
205 pitch.factor = -pitch.factor;
206 pitch.subtract = true;
209 string type_str = kids[i]->getStringValue("type", "");
210 if ( type_str != "" ) {
212 for (int j=0; __sound_fn[j].fn; j++)
213 if ( type_str == __sound_fn[j].name ) {
214 pitch.fn = __sound_fn[j].fn;
219 SG_LOG(SG_GENERAL,SG_INFO,
220 " Unknown pitch type, default to 'lin'");
223 pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
225 if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
226 SG_LOG(SG_GENERAL,SG_WARN,
227 " Pitch minimum value below 0. Forced to 0.");
229 pitch.max = kids[i]->getDoubleValue("max", 0.0);
230 if (pitch.max && (pitch.max < pitch.min) )
231 SG_LOG(SG_GENERAL,SG_ALERT,
232 " Pitch maximum below minimum. Neglected");
234 _pitch.push_back(pitch);
241 SGVec3f offset_pos = SGVec3f::zeros();
242 SGPropertyNode_ptr prop = node->getChild("position");
243 if ( prop != NULL ) {
244 offset_pos[0] = -prop->getDoubleValue("x", 0.0);
245 offset_pos[1] = -prop->getDoubleValue("y", 0.0);
246 offset_pos[2] = -prop->getDoubleValue("z", 0.0);
252 SGVec3f dir = SGVec3f::zeros();
255 float outer_gain = 0.0;
256 prop = node->getChild("orientation");
257 if ( prop != NULL ) {
258 dir = SGVec3f(-prop->getFloatValue("x", 0.0),
259 -prop->getFloatValue("y", 0.0),
260 -prop->getFloatValue("z", 0.0));
261 inner = prop->getFloatValue("inner-angle", 360.0);
262 outer = prop->getFloatValue("outer-angle", 360.0);
263 outer_gain = prop->getFloatValue("outer-gain", 0.0);
267 // Initialize the sample
274 _sample = new SGSoundSample( path.c_str(), node->getStringValue("path", ""));
275 _sample->set_relative_position( offset_pos );
276 _sample->set_direction( dir );
277 _sample->set_audio_cone( inner, outer, outer_gain );
278 _sample->set_reference_dist( reference_dist );
279 _sample->set_max_dist( max_dist );
280 _sample->set_volume( v );
281 _sample->set_pitch( p );
282 _sgrp->add( _sample, _name );
286 SGXmlSound::update (double dt)
288 double curr_value = 0.0;
291 // If the state changes to false, stop playing.
294 curr_value = _property->getDoubleValue();
296 // If a condition is defined, test whether it is FALSE,
298 // if a property is defined then test if it's value is FALSE
299 // or if the mode is IN_TRANSIT then
300 // test whether the current value matches the previous value.
301 if ( // Lisp, anyone?
302 (_condition && !_condition->test()) ||
303 (!_condition && _property &&
306 ( (_mode == SGXmlSound::IN_TRANSIT) && (curr_value == _prev_value) )
311 if ((_mode != SGXmlSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME))
313 if (_sample->is_playing()) {
314 SG_LOG(SG_GENERAL, SG_DEBUG, "Stopping audio after " << _dt_play
315 << " sec: " << _name );
331 // mode is ONCE and the sound is still playing?
333 if (_active && (_mode == SGXmlSound::ONCE)) {
335 if (!_sample->is_playing()) {
345 // Update the playing time, cache the current value and
346 // clear the delay timer.
349 _prev_value = curr_value;
353 if (_dt_play < _delay)
360 int max = _volume.size();
362 double volume_offset = 0.0;
364 for(i = 0; i < max; i++) {
368 v = _volume[i].prop->getDoubleValue();
370 else if (_volume[i].intern)
371 v = *_volume[i].intern;
374 v = _volume[i].fn(v);
376 v *= _volume[i].factor;
378 if (_volume[i].max && (v > _volume[i].max))
381 else if (v < _volume[i].min)
384 if (_volume[i].subtract) // Hack!
385 volume = _volume[i].offset - v;
388 volume_offset += _volume[i].offset;
398 double pitch_offset = 0.0;
400 for(i = 0; i < max; i++) {
404 p = _pitch[i].prop->getDoubleValue();
406 else if (_pitch[i].intern)
407 p = *_pitch[i].intern;
412 p *= _pitch[i].factor;
414 if (_pitch[i].max && (p > _pitch[i].max))
417 else if (p < _pitch[i].min)
420 if (_pitch[i].subtract) // Hack!
421 pitch = _pitch[i].offset - p;
424 pitch_offset += _pitch[i].offset;
430 // Change sample state
433 double vol = volume_offset + volume;
435 SG_LOG(SG_GENERAL, SG_DEBUG, "Sound volume too large for '"
436 << _name << "': " << vol << " -> clipping to 1.0");
439 _sample->set_volume(vol);
440 _sample->set_pitch( pitch_offset + pitch );
444 // Do we need to start playing the sample?
448 if (_mode == SGXmlSound::ONCE)
449 _sample->play(false);
454 SG_LOG(SG_GENERAL, SG_DEBUG, "Playing audio after " << _dt_stop
455 << " sec: " << _name);
456 SG_LOG(SG_GENERAL, SG_DEBUG,
457 "Playing " << ((_mode == ONCE) ? "once" : "looped"));