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 const char *mode_str = node->getStringValue("mode", "");
97 if ( !strcmp(mode_str, "looped") ) {
98 _mode = SGXmlSound::LOOPED;
100 } else if ( !strcmp(mode_str, "in-transit") ) {
101 _mode = SGXmlSound::IN_TRANSIT;
104 _mode = SGXmlSound::ONCE;
107 bool is_avionics = false;
108 const char *type_str = node->getStringValue("type", "fx");
109 if ( !strcmp(type_str, "avionics") )
112 _property = root->getNode(node->getStringValue("property", ""), true);
113 SGPropertyNode *condition = node->getChild("condition");
114 if (condition != NULL)
115 _condition = sgReadCondition(root, condition);
117 if (!_property && !_condition)
118 SG_LOG(SG_GENERAL, SG_WARN,
119 " Neither a condition nor a property specified");
121 _delay = node->getDoubleValue("delay-sec", 0.0);
124 // set volume properties
128 std::vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
129 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
130 _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
132 if (strcmp(kids[i]->getStringValue("property"), ""))
133 volume.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
135 const char *intern_str = kids[i]->getStringValue("internal", "");
136 if (!strcmp(intern_str, "dt_play"))
137 volume.intern = &_dt_play;
138 else if (!strcmp(intern_str, "dt_stop"))
139 volume.intern = &_dt_stop;
141 if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
142 if (volume.factor < 0.0) {
143 volume.factor = -volume.factor;
144 volume.subtract = true;
147 const char *type_str = kids[i]->getStringValue("type", "");
148 if ( strcmp(type_str, "") ) {
150 for (int j=0; __sound_fn[j].fn; j++)
151 if ( !strcmp(type_str, __sound_fn[j].name) ) {
152 volume.fn = __sound_fn[j].fn;
157 SG_LOG(SG_GENERAL,SG_INFO,
158 " Unknown volume type, default to 'lin'");
161 volume.offset = kids[i]->getDoubleValue("offset", 0.0);
163 if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
164 SG_LOG( SG_GENERAL, SG_WARN,
165 "Volume minimum value below 0. Forced to 0.");
167 volume.max = kids[i]->getDoubleValue("max", 0.0);
168 if (volume.max && (volume.max < volume.min) )
169 SG_LOG(SG_GENERAL,SG_ALERT,
170 " Volume maximum below minimum. Neglected.");
172 _volume.push_back(volume);
177 // rule of thumb: make reference distance a 100th of the maximum distance.
178 float reference_dist = node->getDoubleValue("reference-dist", 60.0);
179 float max_dist = node->getDoubleValue("max-dist", 6000.0);
182 // set pitch properties
185 kids = node->getChildren("pitch");
186 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
187 _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
189 if (strcmp(kids[i]->getStringValue("property", ""), ""))
190 pitch.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
192 const char *intern_str = kids[i]->getStringValue("internal", "");
193 if (!strcmp(intern_str, "dt_play"))
194 pitch.intern = &_dt_play;
195 else if (!strcmp(intern_str, "dt_stop"))
196 pitch.intern = &_dt_stop;
198 if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
199 if (pitch.factor < 0.0) {
200 pitch.factor = -pitch.factor;
201 pitch.subtract = true;
204 const char *type_str = kids[i]->getStringValue("type", "");
205 if ( strcmp(type_str, "") ) {
207 for (int j=0; __sound_fn[j].fn; j++)
208 if ( !strcmp(type_str, __sound_fn[j].name) ) {
209 pitch.fn = __sound_fn[j].fn;
214 SG_LOG(SG_GENERAL,SG_INFO,
215 " Unknown pitch type, default to 'lin'");
218 pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
220 if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
221 SG_LOG(SG_GENERAL,SG_WARN,
222 " Pitch minimum value below 0. Forced to 0.");
224 pitch.max = kids[i]->getDoubleValue("max", 0.0);
225 if (pitch.max && (pitch.max < pitch.min) )
226 SG_LOG(SG_GENERAL,SG_ALERT,
227 " Pitch maximum below minimum. Neglected");
229 _pitch.push_back(pitch);
236 SGVec3f offset_pos = SGVec3f::zeros();
237 SGPropertyNode_ptr prop = node->getChild("position");
238 if ( prop != NULL ) {
239 offset_pos[0] = -prop->getDoubleValue("x", 0.0);
240 offset_pos[1] = -prop->getDoubleValue("y", 0.0);
241 offset_pos[2] = -prop->getDoubleValue("z", 0.0);
247 SGVec3f dir = SGVec3f::zeros();
250 float outer_gain = 0.0;
251 prop = node->getChild("orientation");
252 if ( prop != NULL ) {
253 dir = SGVec3f(-prop->getFloatValue("x", 0.0),
254 -prop->getFloatValue("y", 0.0),
255 -prop->getFloatValue("z", 0.0));
256 inner = prop->getFloatValue("inner-angle", 360.0);
257 outer = prop->getFloatValue("outer-angle", 360.0);
258 outer_gain = prop->getFloatValue("outer-gain", 0.0);
262 // Initialize the sample
269 _sample = new SGSoundSample( path.c_str(), node->getStringValue("path", ""));
270 _sample->set_relative_position( offset_pos );
271 _sample->set_direction( dir );
272 _sample->set_audio_cone( inner, outer, outer_gain );
273 _sample->set_reference_dist( reference_dist );
274 _sample->set_max_dist( max_dist );
275 _sample->set_volume( v );
276 _sample->set_pitch( p );
277 _sgrp->add( _sample, _name );
281 SGXmlSound::update (double dt)
283 double curr_value = 0.0;
286 // If the state changes to false, stop playing.
289 curr_value = _property->getDoubleValue();
291 // If a condition is defined, test whether it is FALSE,
293 // if a property is defined then test if it's value is FALSE
294 // or if the mode is IN_TRANSIT then
295 // test whether the current value matches the previous value.
296 if ( // Lisp, anyone?
297 (_condition && !_condition->test()) ||
298 (!_condition && _property &&
301 ( (_mode == SGXmlSound::IN_TRANSIT) && (curr_value == _prev_value) )
306 if ((_mode != SGXmlSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME))
308 if (_sample->is_playing()) {
309 SG_LOG(SG_GENERAL, SG_DEBUG, "Stopping audio after " << _dt_play
310 << " sec: " << _name );
326 // mode is ONCE and the sound is still playing?
328 if (_active && (_mode == SGXmlSound::ONCE)) {
330 if (!_sample->is_playing()) {
340 // Update the playing time, cache the current value and
341 // clear the delay timer.
344 _prev_value = curr_value;
348 if (_dt_play < _delay)
355 int max = _volume.size();
357 double volume_offset = 0.0;
359 for(i = 0; i < max; i++) {
363 v = _volume[i].prop->getDoubleValue();
365 else if (_volume[i].intern)
366 v = *_volume[i].intern;
369 v = _volume[i].fn(v);
371 v *= _volume[i].factor;
373 if (_volume[i].max && (v > _volume[i].max))
376 else if (v < _volume[i].min)
379 if (_volume[i].subtract) // Hack!
380 volume = _volume[i].offset - v;
383 volume_offset += _volume[i].offset;
393 double pitch_offset = 0.0;
395 for(i = 0; i < max; i++) {
399 p = _pitch[i].prop->getDoubleValue();
401 else if (_pitch[i].intern)
402 p = *_pitch[i].intern;
407 p *= _pitch[i].factor;
409 if (_pitch[i].max && (p > _pitch[i].max))
412 else if (p < _pitch[i].min)
415 if (_pitch[i].subtract) // Hack!
416 pitch = _pitch[i].offset - p;
419 pitch_offset += _pitch[i].offset;
425 // Change sample state
428 double vol = volume_offset + volume;
430 SG_LOG(SG_GENERAL, SG_DEBUG, "Sound volume too large for '"
431 << _name << "': " << vol << " -> clipping to 1.0");
434 _sample->set_volume(vol);
435 _sample->set_pitch( pitch_offset + pitch );
439 // Do we need to start playing the sample?
443 if (_mode == SGXmlSound::ONCE)
444 _sample->play(false);
449 SG_LOG(SG_GENERAL, SG_DEBUG, "Playing audio after " << _dt_stop
450 << " sec: " << _name);
451 SG_LOG(SG_GENERAL, SG_DEBUG,
452 "Playing " << ((_mode == ONCE) ? "once" : "looped"));