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 "xmlsound.hxx"
31 #include <simgear/compiler.h>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/props/props.hxx>
38 #include <simgear/props/condition.hxx>
39 #include <simgear/structure/exception.hxx>
40 #include <simgear/misc/sg_path.hxx>
42 #include "sample_group.hxx"
43 #include "sample_openal.hxx"
47 // static double _snd_lin(double v) { return v; }
48 static double _snd_inv(double v) { return (v == 0) ? 1e99 : 1/v; }
49 static double _snd_abs(double v) { return (v >= 0) ? v : -v; }
50 static double _snd_sqrt(double v) { return sqrt(fabs(v)); }
51 static double _snd_log10(double v) { return log10(fabs(v)+1e-9); }
52 static double _snd_log(double v) { return log(fabs(v)+1e-9); }
53 // static double _snd_sqr(double v) { return v*v; }
54 // static double _snd_pow3(double v) { return v*v*v; }
68 SGXmlSound::SGXmlSound()
72 _mode(SGXmlSound::ONCE),
82 SGXmlSound::~SGXmlSound()
92 SGXmlSound::init( SGPropertyNode *root,
95 SGSampleGroup *avionics,
100 // set global sound properties
103 _name = node->getStringValue("name", "");
104 SG_LOG(SG_SOUND, SG_DEBUG, "Loading sound information for: " << _name );
106 string mode_str = node->getStringValue("mode", "");
107 if ( mode_str == "looped" ) {
108 _mode = SGXmlSound::LOOPED;
110 } else if ( mode_str == "in-transit" ) {
111 _mode = SGXmlSound::IN_TRANSIT;
114 _mode = SGXmlSound::ONCE;
117 bool is_avionics = false;
118 string type_str = node->getStringValue("type", "fx");
119 if ( type_str == "avionics" )
122 string propval = node->getStringValue("property", "");
124 _property = root->getNode(propval, true);
126 SGPropertyNode *condition = node->getChild("condition");
127 if (condition != NULL)
128 _condition = sgReadCondition(root, condition);
130 if (!_property && !_condition)
131 SG_LOG(SG_SOUND, SG_WARN,
132 " Neither a condition nor a property specified");
134 _delay = node->getDoubleValue("delay-sec", 0.0);
137 // set volume properties
141 std::vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
142 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
143 _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
145 propval = kids[i]->getStringValue("property", "");
147 volume.prop = root->getNode(propval, true);
149 string intern_str = kids[i]->getStringValue("internal", "");
150 if (intern_str == "dt_play")
151 volume.intern = &_dt_play;
152 else if (intern_str == "dt_stop")
153 volume.intern = &_dt_stop;
155 if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
156 if (volume.factor < 0.0) {
157 volume.factor = -volume.factor;
158 volume.subtract = true;
161 string type_str = kids[i]->getStringValue("type", "");
162 if ( type_str != "" ) {
164 for (int j=0; __sound_fn[j].fn; j++)
165 if ( type_str == __sound_fn[j].name ) {
166 volume.fn = __sound_fn[j].fn;
171 SG_LOG(SG_SOUND,SG_INFO,
172 " Unknown volume type, default to 'lin'");
175 volume.offset = kids[i]->getDoubleValue("offset", 0.0);
177 if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
178 SG_LOG( SG_SOUND, SG_WARN,
179 "Volume minimum value below 0. Forced to 0.");
181 volume.max = kids[i]->getDoubleValue("max", 0.0);
182 if (volume.max && (volume.max < volume.min) )
183 SG_LOG(SG_SOUND,SG_ALERT,
184 " Volume maximum below minimum. Neglected.");
186 _volume.push_back(volume);
191 // rule of thumb: make reference distance a 100th of the maximum distance.
192 float reference_dist = node->getDoubleValue("reference-dist", 60.0);
193 float max_dist = node->getDoubleValue("max-dist", 6000.0);
196 // set pitch properties
199 kids = node->getChildren("pitch");
200 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
201 _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
203 propval = kids[i]->getStringValue("property", "");
205 pitch.prop = root->getNode(propval, true);
207 string intern_str = kids[i]->getStringValue("internal", "");
208 if (intern_str == "dt_play")
209 pitch.intern = &_dt_play;
210 else if (intern_str == "dt_stop")
211 pitch.intern = &_dt_stop;
213 if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
214 if (pitch.factor < 0.0) {
215 pitch.factor = -pitch.factor;
216 pitch.subtract = true;
219 string type_str = kids[i]->getStringValue("type", "");
220 if ( type_str != "" ) {
222 for (int j=0; __sound_fn[j].fn; j++)
223 if ( type_str == __sound_fn[j].name ) {
224 pitch.fn = __sound_fn[j].fn;
229 SG_LOG(SG_SOUND,SG_INFO,
230 " Unknown pitch type, default to 'lin'");
233 pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
235 if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
236 SG_LOG(SG_SOUND,SG_WARN,
237 " Pitch minimum value below 0. Forced to 0.");
239 pitch.max = kids[i]->getDoubleValue("max", 0.0);
240 if (pitch.max && (pitch.max < pitch.min) )
241 SG_LOG(SG_SOUND,SG_ALERT,
242 " Pitch maximum below minimum. Neglected");
244 _pitch.push_back(pitch);
251 SGVec3f offset_pos = SGVec3f::zeros();
252 SGPropertyNode_ptr prop = node->getChild("position");
253 SGPropertyNode_ptr pos_prop[3];
254 if ( prop != NULL ) {
255 offset_pos[0] = -prop->getDoubleValue("x", 0.0);
256 offset_pos[1] = -prop->getDoubleValue("y", 0.0);
257 offset_pos[2] = -prop->getDoubleValue("z", 0.0);
259 pos_prop[0] = prop->getChild("x");
260 if (pos_prop[0]) pos_prop[0] = pos_prop[0]->getNode("property");
262 pos_prop[0] = root->getNode(pos_prop[0]->getStringValue(), true);
264 pos_prop[1] = prop->getChild("y");
265 if (pos_prop[1]) pos_prop[1] = pos_prop[1]->getNode("property");
267 pos_prop[1] = root->getNode(pos_prop[1]->getStringValue(), true);
269 pos_prop[2] = prop->getChild("z");
270 if (pos_prop[2]) pos_prop[2] = pos_prop[1]->getNode("property");
272 pos_prop[2] = root->getNode(pos_prop[2]->getStringValue(), true);
279 SGVec3f dir = SGVec3f::zeros();
282 float outer_gain = 0.0;
283 prop = node->getChild("orientation");
284 if ( prop != NULL ) {
285 dir = SGVec3f(-prop->getFloatValue("x", 0.0),
286 -prop->getFloatValue("y", 0.0),
287 -prop->getFloatValue("z", 0.0));
288 inner = prop->getFloatValue("inner-angle", 360.0);
289 outer = prop->getFloatValue("outer-angle", 360.0);
290 outer_gain = prop->getFloatValue("outer-gain", 0.0);
294 // Initialize the sample
296 if ((is_avionics)&&(avionics)) {
301 string soundFileStr = node->getStringValue("path", "");
302 _sample = new SGSoundSample(soundFileStr.c_str(), path);
303 if (!_sample->file_path().exists()) {
304 throw sg_io_exception("XML sound: couldn't find file: '" + soundFileStr + "'");
308 _sample->set_relative_position( offset_pos );
309 _sample->set_position_properties( pos_prop );
310 _sample->set_direction( dir );
311 _sample->set_audio_cone( inner, outer, outer_gain );
312 _sample->set_reference_dist( reference_dist );
313 _sample->set_max_dist( max_dist );
314 _sample->set_volume( v );
315 _sample->set_pitch( p );
316 _sgrp->add( _sample, _name );
320 SGXmlSound::update (double dt)
322 double curr_value = 0.0;
325 // If the state changes to false, stop playing.
328 curr_value = _property->getDoubleValue();
332 // update initial value before detecting changes
333 _prev_value = curr_value;
337 // If a condition is defined, test whether it is FALSE,
339 // if a property is defined then test if it's value is FALSE
340 // or if the mode is IN_TRANSIT then
341 // test whether the current value matches the previous value.
342 if ( // Lisp, anyone?
343 (_condition && !_condition->test()) ||
344 (!_condition && _property &&
347 ( (_mode == SGXmlSound::IN_TRANSIT) && (curr_value == _prev_value) )
352 if ((_mode != SGXmlSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME))
354 if (_sample->is_playing()) {
355 SG_LOG(SG_SOUND, SG_DEBUG, "Stopping audio after " << _dt_play
356 << " sec: " << _name );
372 // mode is ONCE and the sound is still playing?
374 if (_active && (_mode == SGXmlSound::ONCE)) {
376 if (!_sample->is_playing()) {
386 // Update the playing time, cache the current value and
387 // clear the delay timer.
390 _prev_value = curr_value;
394 if (_dt_play < _delay)
401 int max = _volume.size();
403 double volume_offset = 0.0;
405 for(i = 0; i < max; i++) {
409 v = _volume[i].prop->getDoubleValue();
411 else if (_volume[i].intern)
412 v = *_volume[i].intern;
415 v = _volume[i].fn(v);
417 v *= _volume[i].factor;
419 if (_volume[i].max && (v > _volume[i].max))
422 else if (v < _volume[i].min)
425 if (_volume[i].subtract) // Hack!
426 volume = _volume[i].offset - v;
429 volume_offset += _volume[i].offset;
439 double pitch_offset = 0.0;
441 for(i = 0; i < max; i++) {
445 p = _pitch[i].prop->getDoubleValue();
447 else if (_pitch[i].intern)
448 p = *_pitch[i].intern;
453 p *= _pitch[i].factor;
455 if (_pitch[i].max && (p > _pitch[i].max))
458 else if (p < _pitch[i].min)
461 if (_pitch[i].subtract) // Hack!
462 pitch = _pitch[i].offset - p;
465 pitch_offset += _pitch[i].offset;
471 // Change sample state
474 double vol = volume_offset + volume;
476 SG_LOG(SG_SOUND, SG_DEBUG, "Sound volume too large for '"
477 << _name << "': " << vol << " -> clipping to 1.0");
480 _sample->set_volume(vol);
481 _sample->set_pitch( pitch_offset + pitch );
485 // Do we need to start playing the sample?
489 if (_mode == SGXmlSound::ONCE)
490 _sample->play(false);
495 SG_LOG(SG_SOUND, SG_DEBUG, "Playing audio after " << _dt_stop
496 << " sec: " << _name);
497 SG_LOG(SG_SOUND, SG_DEBUG,
498 "Playing " << ((_mode == ONCE) ? "once" : "looped"));