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/structure/exception.hxx>
36 #include <simgear/misc/sg_path.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 sqrt(fabs(v)); }
45 static double _snd_log10(double v) { return log10(fabs(v)+1e-9); }
46 static double _snd_log(double v) { return log(fabs(v)+1e-9); }
47 // static double _snd_sqr(double v) { return v*v; }
48 // static double _snd_pow3(double v) { return v*v*v; }
62 SGXmlSound::SGXmlSound()
66 _mode(SGXmlSound::ONCE),
76 SGXmlSound::~SGXmlSound()
86 SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node,
87 SGSampleGroup *sgrp, SGSampleGroup *avionics,
88 const SGPath& currentDir)
92 // set global sound properties
95 _name = node->getStringValue("name", "");
96 SG_LOG(SG_SOUND, SG_DEBUG, "Loading sound information for: " << _name );
98 string mode_str = node->getStringValue("mode", "");
99 if ( mode_str == "looped" ) {
100 _mode = SGXmlSound::LOOPED;
102 } else if ( mode_str == "in-transit" ) {
103 _mode = SGXmlSound::IN_TRANSIT;
106 _mode = SGXmlSound::ONCE;
109 bool is_avionics = false;
110 string type_str = node->getStringValue("type", "fx");
111 if ( type_str == "avionics" )
114 string propval = node->getStringValue("property", "");
116 _property = root->getNode(propval, true);
118 SGPropertyNode *condition = node->getChild("condition");
119 if (condition != NULL)
120 _condition = sgReadCondition(root, condition);
122 if (!_property && !_condition)
123 SG_LOG(SG_SOUND, SG_WARN,
124 " Neither a condition nor a property specified");
126 _delay = node->getDoubleValue("delay-sec", 0.0);
129 // set volume properties
133 std::vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
134 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
135 _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
137 propval = kids[i]->getStringValue("property", "");
139 volume.prop = root->getNode(propval, true);
141 string intern_str = kids[i]->getStringValue("internal", "");
142 if (intern_str == "dt_play")
143 volume.intern = &_dt_play;
144 else if (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 string type_str = kids[i]->getStringValue("type", "");
154 if ( type_str != "" ) {
156 for (int j=0; __sound_fn[j].fn; j++)
157 if ( type_str == __sound_fn[j].name ) {
158 volume.fn = __sound_fn[j].fn;
163 SG_LOG(SG_SOUND,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_SOUND, 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_SOUND,SG_ALERT,
176 " Volume maximum below minimum. Neglected.");
178 _volume.push_back(volume);
183 // rule of thumb: make reference distance a 100th of the maximum distance.
184 float reference_dist = node->getDoubleValue("reference-dist", 60.0);
185 float max_dist = node->getDoubleValue("max-dist", 6000.0);
188 // set pitch properties
191 kids = node->getChildren("pitch");
192 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
193 _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
195 propval = kids[i]->getStringValue("property", "");
197 pitch.prop = root->getNode(propval, true);
199 string intern_str = kids[i]->getStringValue("internal", "");
200 if (intern_str == "dt_play")
201 pitch.intern = &_dt_play;
202 else if (intern_str == "dt_stop")
203 pitch.intern = &_dt_stop;
205 if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
206 if (pitch.factor < 0.0) {
207 pitch.factor = -pitch.factor;
208 pitch.subtract = true;
211 string type_str = kids[i]->getStringValue("type", "");
212 if ( type_str != "" ) {
214 for (int j=0; __sound_fn[j].fn; j++)
215 if ( type_str == __sound_fn[j].name ) {
216 pitch.fn = __sound_fn[j].fn;
221 SG_LOG(SG_SOUND,SG_INFO,
222 " Unknown pitch type, default to 'lin'");
225 pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
227 if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
228 SG_LOG(SG_SOUND,SG_WARN,
229 " Pitch minimum value below 0. Forced to 0.");
231 pitch.max = kids[i]->getDoubleValue("max", 0.0);
232 if (pitch.max && (pitch.max < pitch.min) )
233 SG_LOG(SG_SOUND,SG_ALERT,
234 " Pitch maximum below minimum. Neglected");
236 _pitch.push_back(pitch);
243 SGVec3f offset_pos = SGVec3f::zeros();
244 SGPropertyNode_ptr prop = node->getChild("position");
245 if ( prop != NULL ) {
246 offset_pos[0] = -prop->getDoubleValue("x", 0.0);
247 offset_pos[1] = -prop->getDoubleValue("y", 0.0);
248 offset_pos[2] = -prop->getDoubleValue("z", 0.0);
254 SGVec3f dir = SGVec3f::zeros();
257 float outer_gain = 0.0;
258 prop = node->getChild("orientation");
259 if ( prop != NULL ) {
260 dir = SGVec3f(-prop->getFloatValue("x", 0.0),
261 -prop->getFloatValue("y", 0.0),
262 -prop->getFloatValue("z", 0.0));
263 inner = prop->getFloatValue("inner-angle", 360.0);
264 outer = prop->getFloatValue("outer-angle", 360.0);
265 outer_gain = prop->getFloatValue("outer-gain", 0.0);
269 // Initialize the sample
271 if ((is_avionics)&&(avionics)) {
276 string soundFileStr = node->getStringValue("path", "");
277 _sample = new SGSoundSample(soundFileStr.c_str(), currentDir);
278 if (!_sample->file_path().exists()) {
279 throw sg_io_exception("XML sound: couldn't find file: '" + soundFileStr + "'");
282 _sample->set_relative_position( offset_pos );
283 _sample->set_direction( dir );
284 _sample->set_audio_cone( inner, outer, outer_gain );
285 _sample->set_reference_dist( reference_dist );
286 _sample->set_max_dist( max_dist );
287 _sample->set_volume( v );
288 _sample->set_pitch( p );
289 _sgrp->add( _sample, _name );
293 SGXmlSound::update (double dt)
295 double curr_value = 0.0;
298 // If the state changes to false, stop playing.
301 curr_value = _property->getDoubleValue();
305 // update initial value before detecting changes
306 _prev_value = curr_value;
310 // If a condition is defined, test whether it is FALSE,
312 // if a property is defined then test if it's value is FALSE
313 // or if the mode is IN_TRANSIT then
314 // test whether the current value matches the previous value.
315 if ( // Lisp, anyone?
316 (_condition && !_condition->test()) ||
317 (!_condition && _property &&
320 ( (_mode == SGXmlSound::IN_TRANSIT) && (curr_value == _prev_value) )
325 if ((_mode != SGXmlSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME))
327 if (_sample->is_playing()) {
328 SG_LOG(SG_SOUND, SG_DEBUG, "Stopping audio after " << _dt_play
329 << " sec: " << _name );
345 // mode is ONCE and the sound is still playing?
347 if (_active && (_mode == SGXmlSound::ONCE)) {
349 if (!_sample->is_playing()) {
359 // Update the playing time, cache the current value and
360 // clear the delay timer.
363 _prev_value = curr_value;
367 if (_dt_play < _delay)
374 int max = _volume.size();
376 double volume_offset = 0.0;
378 for(i = 0; i < max; i++) {
382 v = _volume[i].prop->getDoubleValue();
384 else if (_volume[i].intern)
385 v = *_volume[i].intern;
388 v = _volume[i].fn(v);
390 v *= _volume[i].factor;
392 if (_volume[i].max && (v > _volume[i].max))
395 else if (v < _volume[i].min)
398 if (_volume[i].subtract) // Hack!
399 volume = _volume[i].offset - v;
402 volume_offset += _volume[i].offset;
412 double pitch_offset = 0.0;
414 for(i = 0; i < max; i++) {
418 p = _pitch[i].prop->getDoubleValue();
420 else if (_pitch[i].intern)
421 p = *_pitch[i].intern;
426 p *= _pitch[i].factor;
428 if (_pitch[i].max && (p > _pitch[i].max))
431 else if (p < _pitch[i].min)
434 if (_pitch[i].subtract) // Hack!
435 pitch = _pitch[i].offset - p;
438 pitch_offset += _pitch[i].offset;
444 // Change sample state
447 double vol = volume_offset + volume;
449 SG_LOG(SG_SOUND, SG_DEBUG, "Sound volume too large for '"
450 << _name << "': " << vol << " -> clipping to 1.0");
453 _sample->set_volume(vol);
454 _sample->set_pitch( pitch_offset + pitch );
458 // Do we need to start playing the sample?
462 if (_mode == SGXmlSound::ONCE)
463 _sample->play(false);
468 SG_LOG(SG_SOUND, SG_DEBUG, "Playing audio after " << _dt_stop
469 << " sec: " << _name);
470 SG_LOG(SG_SOUND, SG_DEBUG,
471 "Playing " << ((_mode == ONCE) ? "once" : "looped"));