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>
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),
75 SGXmlSound::~SGXmlSound()
85 SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node,
86 SGSampleGroup *sgrp, SGSampleGroup *avionics,
87 const SGPath& currentDir)
91 // set global sound properties
94 _name = node->getStringValue("name", "");
95 SG_LOG(SG_GENERAL, SG_DEBUG, "Loading sound information for: " << _name );
97 string mode_str = node->getStringValue("mode", "");
98 if ( mode_str == "looped" ) {
99 _mode = SGXmlSound::LOOPED;
101 } else if ( mode_str == "in-transit" ) {
102 _mode = SGXmlSound::IN_TRANSIT;
105 _mode = SGXmlSound::ONCE;
108 bool is_avionics = false;
109 string type_str = node->getStringValue("type", "fx");
110 if ( type_str == "avionics" )
113 string propval = node->getStringValue("property", "");
115 _property = root->getNode(propval, true);
117 SGPropertyNode *condition = node->getChild("condition");
118 if (condition != NULL)
119 _condition = sgReadCondition(root, condition);
121 if (!_property && !_condition)
122 SG_LOG(SG_GENERAL, SG_WARN,
123 " Neither a condition nor a property specified");
125 _delay = node->getDoubleValue("delay-sec", 0.0);
128 // set volume properties
132 std::vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
133 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
134 _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
136 propval = kids[i]->getStringValue("property", "");
138 volume.prop = root->getNode(propval, true);
140 string intern_str = kids[i]->getStringValue("internal", "");
141 if (intern_str == "dt_play")
142 volume.intern = &_dt_play;
143 else if (intern_str == "dt_stop")
144 volume.intern = &_dt_stop;
146 if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
147 if (volume.factor < 0.0) {
148 volume.factor = -volume.factor;
149 volume.subtract = true;
152 string type_str = kids[i]->getStringValue("type", "");
153 if ( type_str != "" ) {
155 for (int j=0; __sound_fn[j].fn; j++)
156 if ( type_str == __sound_fn[j].name ) {
157 volume.fn = __sound_fn[j].fn;
162 SG_LOG(SG_GENERAL,SG_INFO,
163 " Unknown volume type, default to 'lin'");
166 volume.offset = kids[i]->getDoubleValue("offset", 0.0);
168 if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
169 SG_LOG( SG_GENERAL, SG_WARN,
170 "Volume minimum value below 0. Forced to 0.");
172 volume.max = kids[i]->getDoubleValue("max", 0.0);
173 if (volume.max && (volume.max < volume.min) )
174 SG_LOG(SG_GENERAL,SG_ALERT,
175 " Volume maximum below minimum. Neglected.");
177 _volume.push_back(volume);
182 // rule of thumb: make reference distance a 100th of the maximum distance.
183 float reference_dist = node->getDoubleValue("reference-dist", 60.0);
184 float max_dist = node->getDoubleValue("max-dist", 6000.0);
187 // set pitch properties
190 kids = node->getChildren("pitch");
191 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
192 _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
194 propval = kids[i]->getStringValue("property", "");
196 pitch.prop = root->getNode(propval, true);
198 string intern_str = kids[i]->getStringValue("internal", "");
199 if (intern_str == "dt_play")
200 pitch.intern = &_dt_play;
201 else if (intern_str == "dt_stop")
202 pitch.intern = &_dt_stop;
204 if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
205 if (pitch.factor < 0.0) {
206 pitch.factor = -pitch.factor;
207 pitch.subtract = true;
210 string type_str = kids[i]->getStringValue("type", "");
211 if ( type_str != "" ) {
213 for (int j=0; __sound_fn[j].fn; j++)
214 if ( type_str == __sound_fn[j].name ) {
215 pitch.fn = __sound_fn[j].fn;
220 SG_LOG(SG_GENERAL,SG_INFO,
221 " Unknown pitch type, default to 'lin'");
224 pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
226 if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
227 SG_LOG(SG_GENERAL,SG_WARN,
228 " Pitch minimum value below 0. Forced to 0.");
230 pitch.max = kids[i]->getDoubleValue("max", 0.0);
231 if (pitch.max && (pitch.max < pitch.min) )
232 SG_LOG(SG_GENERAL,SG_ALERT,
233 " Pitch maximum below minimum. Neglected");
235 _pitch.push_back(pitch);
242 SGVec3f offset_pos = SGVec3f::zeros();
243 SGPropertyNode_ptr prop = node->getChild("position");
244 if ( prop != NULL ) {
245 offset_pos[0] = -prop->getDoubleValue("x", 0.0);
246 offset_pos[1] = -prop->getDoubleValue("y", 0.0);
247 offset_pos[2] = -prop->getDoubleValue("z", 0.0);
253 SGVec3f dir = SGVec3f::zeros();
256 float outer_gain = 0.0;
257 prop = node->getChild("orientation");
258 if ( prop != NULL ) {
259 dir = SGVec3f(-prop->getFloatValue("x", 0.0),
260 -prop->getFloatValue("y", 0.0),
261 -prop->getFloatValue("z", 0.0));
262 inner = prop->getFloatValue("inner-angle", 360.0);
263 outer = prop->getFloatValue("outer-angle", 360.0);
264 outer_gain = prop->getFloatValue("outer-gain", 0.0);
268 // Initialize the sample
275 _sample = new SGSoundSample(node->getStringValue("path", ""), currentDir);
276 if (!_sample->file_path().exists()) {
277 throw sg_io_exception("XML sound: couldn't find file: " + _sample->file_path().str());
280 _sample->set_relative_position( offset_pos );
281 _sample->set_direction( dir );
282 _sample->set_audio_cone( inner, outer, outer_gain );
283 _sample->set_reference_dist( reference_dist );
284 _sample->set_max_dist( max_dist );
285 _sample->set_volume( v );
286 _sample->set_pitch( p );
287 _sgrp->add( _sample, _name );
291 SGXmlSound::update (double dt)
293 double curr_value = 0.0;
296 // If the state changes to false, stop playing.
299 curr_value = _property->getDoubleValue();
301 // If a condition is defined, test whether it is FALSE,
303 // if a property is defined then test if it's value is FALSE
304 // or if the mode is IN_TRANSIT then
305 // test whether the current value matches the previous value.
306 if ( // Lisp, anyone?
307 (_condition && !_condition->test()) ||
308 (!_condition && _property &&
311 ( (_mode == SGXmlSound::IN_TRANSIT) && (curr_value == _prev_value) )
316 if ((_mode != SGXmlSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME))
318 if (_sample->is_playing()) {
319 SG_LOG(SG_GENERAL, SG_DEBUG, "Stopping audio after " << _dt_play
320 << " sec: " << _name );
336 // mode is ONCE and the sound is still playing?
338 if (_active && (_mode == SGXmlSound::ONCE)) {
340 if (!_sample->is_playing()) {
350 // Update the playing time, cache the current value and
351 // clear the delay timer.
354 _prev_value = curr_value;
358 if (_dt_play < _delay)
365 int max = _volume.size();
367 double volume_offset = 0.0;
369 for(i = 0; i < max; i++) {
373 v = _volume[i].prop->getDoubleValue();
375 else if (_volume[i].intern)
376 v = *_volume[i].intern;
379 v = _volume[i].fn(v);
381 v *= _volume[i].factor;
383 if (_volume[i].max && (v > _volume[i].max))
386 else if (v < _volume[i].min)
389 if (_volume[i].subtract) // Hack!
390 volume = _volume[i].offset - v;
393 volume_offset += _volume[i].offset;
403 double pitch_offset = 0.0;
405 for(i = 0; i < max; i++) {
409 p = _pitch[i].prop->getDoubleValue();
411 else if (_pitch[i].intern)
412 p = *_pitch[i].intern;
417 p *= _pitch[i].factor;
419 if (_pitch[i].max && (p > _pitch[i].max))
422 else if (p < _pitch[i].min)
425 if (_pitch[i].subtract) // Hack!
426 pitch = _pitch[i].offset - p;
429 pitch_offset += _pitch[i].offset;
435 // Change sample state
438 double vol = volume_offset + volume;
440 SG_LOG(SG_GENERAL, SG_DEBUG, "Sound volume too large for '"
441 << _name << "': " << vol << " -> clipping to 1.0");
444 _sample->set_volume(vol);
445 _sample->set_pitch( pitch_offset + pitch );
449 // Do we need to start playing the sample?
453 if (_mode == SGXmlSound::ONCE)
454 _sample->play(false);
459 SG_LOG(SG_GENERAL, SG_DEBUG, "Playing audio after " << _dt_stop
460 << " sec: " << _name);
461 SG_LOG(SG_GENERAL, SG_DEBUG,
462 "Playing " << ((_mode == ONCE) ? "once" : "looped"));