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/props.hxx>
34 #include <simgear/props/condition.hxx>
35 #include <simgear/math/SGMath.hxx>
36 #include <simgear/structure/exception.hxx>
37 #include <simgear/misc/sg_path.hxx>
39 #include "xmlsound.hxx"
42 // static double _snd_lin(double v) { return v; }
43 static double _snd_inv(double v) { return (v == 0) ? 1e99 : 1/v; }
44 static double _snd_abs(double v) { return (v >= 0) ? v : -v; }
45 static double _snd_sqrt(double v) { return sqrt(fabs(v)); }
46 static double _snd_log10(double v) { return log10(fabs(v)+1e-9); }
47 static double _snd_log(double v) { return log(fabs(v)+1e-9); }
48 // static double _snd_sqr(double v) { return v*v; }
49 // static double _snd_pow3(double v) { return v*v*v; }
63 SGXmlSound::SGXmlSound()
67 _mode(SGXmlSound::ONCE),
77 SGXmlSound::~SGXmlSound()
87 SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node,
88 SGSampleGroup *sgrp, SGSampleGroup *avionics,
89 const SGPath& currentDir)
93 // set global sound properties
96 _name = node->getStringValue("name", "");
97 SG_LOG(SG_GENERAL, SG_DEBUG, "Loading sound information for: " << _name );
99 string mode_str = node->getStringValue("mode", "");
100 if ( mode_str == "looped" ) {
101 _mode = SGXmlSound::LOOPED;
103 } else if ( mode_str == "in-transit" ) {
104 _mode = SGXmlSound::IN_TRANSIT;
107 _mode = SGXmlSound::ONCE;
110 bool is_avionics = false;
111 string type_str = node->getStringValue("type", "fx");
112 if ( type_str == "avionics" )
115 string propval = node->getStringValue("property", "");
117 _property = root->getNode(propval, true);
119 SGPropertyNode *condition = node->getChild("condition");
120 if (condition != NULL)
121 _condition = sgReadCondition(root, condition);
123 if (!_property && !_condition)
124 SG_LOG(SG_GENERAL, SG_WARN,
125 " Neither a condition nor a property specified");
127 _delay = node->getDoubleValue("delay-sec", 0.0);
130 // set volume properties
134 std::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 propval = kids[i]->getStringValue("property", "");
140 volume.prop = root->getNode(propval, true);
142 string intern_str = kids[i]->getStringValue("internal", "");
143 if (intern_str == "dt_play")
144 volume.intern = &_dt_play;
145 else if (intern_str == "dt_stop")
146 volume.intern = &_dt_stop;
148 if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
149 if (volume.factor < 0.0) {
150 volume.factor = -volume.factor;
151 volume.subtract = true;
154 string type_str = kids[i]->getStringValue("type", "");
155 if ( type_str != "" ) {
157 for (int j=0; __sound_fn[j].fn; j++)
158 if ( type_str == __sound_fn[j].name ) {
159 volume.fn = __sound_fn[j].fn;
164 SG_LOG(SG_GENERAL,SG_INFO,
165 " Unknown volume type, default to 'lin'");
168 volume.offset = kids[i]->getDoubleValue("offset", 0.0);
170 if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
171 SG_LOG( SG_GENERAL, SG_WARN,
172 "Volume minimum value below 0. Forced to 0.");
174 volume.max = kids[i]->getDoubleValue("max", 0.0);
175 if (volume.max && (volume.max < volume.min) )
176 SG_LOG(SG_GENERAL,SG_ALERT,
177 " Volume maximum below minimum. Neglected.");
179 _volume.push_back(volume);
184 // rule of thumb: make reference distance a 100th of the maximum distance.
185 float reference_dist = node->getDoubleValue("reference-dist", 60.0);
186 float max_dist = node->getDoubleValue("max-dist", 6000.0);
189 // set pitch properties
192 kids = node->getChildren("pitch");
193 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
194 _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
196 propval = kids[i]->getStringValue("property", "");
198 pitch.prop = root->getNode(propval, true);
200 string intern_str = kids[i]->getStringValue("internal", "");
201 if (intern_str == "dt_play")
202 pitch.intern = &_dt_play;
203 else if (intern_str == "dt_stop")
204 pitch.intern = &_dt_stop;
206 if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
207 if (pitch.factor < 0.0) {
208 pitch.factor = -pitch.factor;
209 pitch.subtract = true;
212 string type_str = kids[i]->getStringValue("type", "");
213 if ( type_str != "" ) {
215 for (int j=0; __sound_fn[j].fn; j++)
216 if ( type_str == __sound_fn[j].name ) {
217 pitch.fn = __sound_fn[j].fn;
222 SG_LOG(SG_GENERAL,SG_INFO,
223 " Unknown pitch type, default to 'lin'");
226 pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
228 if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
229 SG_LOG(SG_GENERAL,SG_WARN,
230 " Pitch minimum value below 0. Forced to 0.");
232 pitch.max = kids[i]->getDoubleValue("max", 0.0);
233 if (pitch.max && (pitch.max < pitch.min) )
234 SG_LOG(SG_GENERAL,SG_ALERT,
235 " Pitch maximum below minimum. Neglected");
237 _pitch.push_back(pitch);
244 SGVec3f offset_pos = SGVec3f::zeros();
245 SGPropertyNode_ptr prop = node->getChild("position");
246 if ( prop != NULL ) {
247 offset_pos[0] = -prop->getDoubleValue("x", 0.0);
248 offset_pos[1] = -prop->getDoubleValue("y", 0.0);
249 offset_pos[2] = -prop->getDoubleValue("z", 0.0);
255 SGVec3f dir = SGVec3f::zeros();
258 float outer_gain = 0.0;
259 prop = node->getChild("orientation");
260 if ( prop != NULL ) {
261 dir = SGVec3f(-prop->getFloatValue("x", 0.0),
262 -prop->getFloatValue("y", 0.0),
263 -prop->getFloatValue("z", 0.0));
264 inner = prop->getFloatValue("inner-angle", 360.0);
265 outer = prop->getFloatValue("outer-angle", 360.0);
266 outer_gain = prop->getFloatValue("outer-gain", 0.0);
270 // Initialize the sample
277 string soundFileStr = node->getStringValue("path", "");
278 _sample = new SGSoundSample(soundFileStr.c_str(), currentDir);
279 if (!_sample->file_path().exists()) {
280 throw sg_io_exception("XML sound: couldn't find file: '" + soundFileStr + "'");
283 _sample->set_relative_position( offset_pos );
284 _sample->set_direction( dir );
285 _sample->set_audio_cone( inner, outer, outer_gain );
286 _sample->set_reference_dist( reference_dist );
287 _sample->set_max_dist( max_dist );
288 _sample->set_volume( v );
289 _sample->set_pitch( p );
290 _sgrp->add( _sample, _name );
294 SGXmlSound::update (double dt)
296 double curr_value = 0.0;
299 // If the state changes to false, stop playing.
302 curr_value = _property->getDoubleValue();
306 // update initial value before detecting changes
307 _prev_value = curr_value;
311 // If a condition is defined, test whether it is FALSE,
313 // if a property is defined then test if it's value is FALSE
314 // or if the mode is IN_TRANSIT then
315 // test whether the current value matches the previous value.
316 if ( // Lisp, anyone?
317 (_condition && !_condition->test()) ||
318 (!_condition && _property &&
321 ( (_mode == SGXmlSound::IN_TRANSIT) && (curr_value == _prev_value) )
326 if ((_mode != SGXmlSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME))
328 if (_sample->is_playing()) {
329 SG_LOG(SG_GENERAL, SG_DEBUG, "Stopping audio after " << _dt_play
330 << " sec: " << _name );
346 // mode is ONCE and the sound is still playing?
348 if (_active && (_mode == SGXmlSound::ONCE)) {
350 if (!_sample->is_playing()) {
360 // Update the playing time, cache the current value and
361 // clear the delay timer.
364 _prev_value = curr_value;
368 if (_dt_play < _delay)
375 int max = _volume.size();
377 double volume_offset = 0.0;
379 for(i = 0; i < max; i++) {
383 v = _volume[i].prop->getDoubleValue();
385 else if (_volume[i].intern)
386 v = *_volume[i].intern;
389 v = _volume[i].fn(v);
391 v *= _volume[i].factor;
393 if (_volume[i].max && (v > _volume[i].max))
396 else if (v < _volume[i].min)
399 if (_volume[i].subtract) // Hack!
400 volume = _volume[i].offset - v;
403 volume_offset += _volume[i].offset;
413 double pitch_offset = 0.0;
415 for(i = 0; i < max; i++) {
419 p = _pitch[i].prop->getDoubleValue();
421 else if (_pitch[i].intern)
422 p = *_pitch[i].intern;
427 p *= _pitch[i].factor;
429 if (_pitch[i].max && (p > _pitch[i].max))
432 else if (p < _pitch[i].min)
435 if (_pitch[i].subtract) // Hack!
436 pitch = _pitch[i].offset - p;
439 pitch_offset += _pitch[i].offset;
445 // Change sample state
448 double vol = volume_offset + volume;
450 SG_LOG(SG_GENERAL, SG_DEBUG, "Sound volume too large for '"
451 << _name << "': " << vol << " -> clipping to 1.0");
454 _sample->set_volume(vol);
455 _sample->set_pitch( pitch_offset + pitch );
459 // Do we need to start playing the sample?
463 if (_mode == SGXmlSound::ONCE)
464 _sample->play(false);
469 SG_LOG(SG_GENERAL, SG_DEBUG, "Playing audio after " << _dt_stop
470 << " sec: " << _name);
471 SG_LOG(SG_GENERAL, SG_DEBUG,
472 "Playing " << ((_mode == ONCE) ? "once" : "looped"));