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>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/props/props.hxx>
37 #include <simgear/props/condition.hxx>
38 #include <simgear/structure/exception.hxx>
39 #include <simgear/misc/sg_path.hxx>
41 #include "sample_group.hxx"
42 #include "sample_openal.hxx"
46 // static double _snd_lin(double v) { return v; }
47 static double _snd_inv(double v) { return (v == 0) ? 1e99 : 1/v; }
48 static double _snd_abs(double v) { return (v >= 0) ? v : -v; }
49 static double _snd_sqrt(double v) { return sqrt(fabs(v)); }
50 static double _snd_log10(double v) { return log10(fabs(v)+1e-9); }
51 static double _snd_log(double v) { return log(fabs(v)+1e-9); }
52 // static double _snd_sqr(double v) { return v*v; }
53 // static double _snd_pow3(double v) { return v*v*v; }
67 SGXmlSound::SGXmlSound()
71 _mode(SGXmlSound::ONCE),
81 SGXmlSound::~SGXmlSound()
91 SGXmlSound::init( SGPropertyNode *root,
94 SGSampleGroup *avionics,
99 // set global sound properties
102 _name = node->getStringValue("name", "");
103 SG_LOG(SG_SOUND, SG_DEBUG, "Loading sound information for: " << _name );
105 string mode_str = node->getStringValue("mode", "");
106 if ( mode_str == "looped" ) {
107 _mode = SGXmlSound::LOOPED;
109 } else if ( mode_str == "in-transit" ) {
110 _mode = SGXmlSound::IN_TRANSIT;
113 _mode = SGXmlSound::ONCE;
116 bool is_avionics = false;
117 string type_str = node->getStringValue("type", "fx");
118 if ( type_str == "avionics" )
121 string propval = node->getStringValue("property", "");
123 _property = root->getNode(propval, true);
125 SGPropertyNode *condition = node->getChild("condition");
126 if (condition != NULL)
127 _condition = sgReadCondition(root, condition);
129 if (!_property && !_condition)
130 SG_LOG(SG_SOUND, SG_WARN,
131 " Neither a condition nor a property specified");
133 _delay = node->getDoubleValue("delay-sec", 0.0);
136 // set volume properties
140 std::vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
141 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
142 _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
144 propval = kids[i]->getStringValue("property", "");
146 volume.prop = root->getNode(propval, true);
148 string intern_str = kids[i]->getStringValue("internal", "");
149 if (intern_str == "dt_play")
150 volume.intern = &_dt_play;
151 else if (intern_str == "dt_stop")
152 volume.intern = &_dt_stop;
154 if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
155 if (volume.factor < 0.0) {
156 volume.factor = -volume.factor;
157 volume.subtract = true;
160 string type_str = kids[i]->getStringValue("type", "");
161 if ( type_str != "" ) {
163 for (int j=0; __sound_fn[j].fn; j++)
164 if ( type_str == __sound_fn[j].name ) {
165 volume.fn = __sound_fn[j].fn;
170 SG_LOG(SG_SOUND,SG_INFO,
171 " Unknown volume type, default to 'lin'");
174 volume.offset = kids[i]->getDoubleValue("offset", 0.0);
176 if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
177 SG_LOG( SG_SOUND, SG_WARN,
178 "Volume minimum value below 0. Forced to 0.");
180 volume.max = kids[i]->getDoubleValue("max", 0.0);
181 if (volume.max && (volume.max < volume.min) )
182 SG_LOG(SG_SOUND,SG_ALERT,
183 " Volume maximum below minimum. Neglected.");
185 _volume.push_back(volume);
190 // rule of thumb: make reference distance a 100th of the maximum distance.
191 float reference_dist = node->getDoubleValue("reference-dist", 60.0);
192 float max_dist = node->getDoubleValue("max-dist", 6000.0);
195 // set pitch properties
198 kids = node->getChildren("pitch");
199 for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
200 _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
202 propval = kids[i]->getStringValue("property", "");
204 pitch.prop = root->getNode(propval, true);
206 string intern_str = kids[i]->getStringValue("internal", "");
207 if (intern_str == "dt_play")
208 pitch.intern = &_dt_play;
209 else if (intern_str == "dt_stop")
210 pitch.intern = &_dt_stop;
212 if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
213 if (pitch.factor < 0.0) {
214 pitch.factor = -pitch.factor;
215 pitch.subtract = true;
218 string type_str = kids[i]->getStringValue("type", "");
219 if ( type_str != "" ) {
221 for (int j=0; __sound_fn[j].fn; j++)
222 if ( type_str == __sound_fn[j].name ) {
223 pitch.fn = __sound_fn[j].fn;
228 SG_LOG(SG_SOUND,SG_INFO,
229 " Unknown pitch type, default to 'lin'");
232 pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
234 if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
235 SG_LOG(SG_SOUND,SG_WARN,
236 " Pitch minimum value below 0. Forced to 0.");
238 pitch.max = kids[i]->getDoubleValue("max", 0.0);
239 if (pitch.max && (pitch.max < pitch.min) )
240 SG_LOG(SG_SOUND,SG_ALERT,
241 " Pitch maximum below minimum. Neglected");
243 _pitch.push_back(pitch);
250 SGVec3f offset_pos = SGVec3f::zeros();
251 SGPropertyNode_ptr prop = node->getChild("position");
252 if ( prop != NULL ) {
253 offset_pos[0] = -prop->getDoubleValue("x", 0.0);
254 offset_pos[1] = -prop->getDoubleValue("y", 0.0);
255 offset_pos[2] = -prop->getDoubleValue("z", 0.0);
261 SGVec3f dir = SGVec3f::zeros();
264 float outer_gain = 0.0;
265 prop = node->getChild("orientation");
266 if ( prop != NULL ) {
267 dir = SGVec3f(-prop->getFloatValue("x", 0.0),
268 -prop->getFloatValue("y", 0.0),
269 -prop->getFloatValue("z", 0.0));
270 inner = prop->getFloatValue("inner-angle", 360.0);
271 outer = prop->getFloatValue("outer-angle", 360.0);
272 outer_gain = prop->getFloatValue("outer-gain", 0.0);
276 // Initialize the sample
278 if ((is_avionics)&&(avionics)) {
283 string soundFileStr = node->getStringValue("path", "");
284 _sample = new SGSoundSample(soundFileStr.c_str(), path);
285 if (!_sample->file_path().exists()) {
286 throw sg_io_exception("XML sound: couldn't find file: '" + soundFileStr + "'");
289 _sample->set_relative_position( offset_pos );
290 _sample->set_direction( dir );
291 _sample->set_audio_cone( inner, outer, outer_gain );
292 _sample->set_reference_dist( reference_dist );
293 _sample->set_max_dist( max_dist );
294 _sample->set_volume( v );
295 _sample->set_pitch( p );
296 _sgrp->add( _sample, _name );
300 SGXmlSound::update (double dt)
302 double curr_value = 0.0;
305 // If the state changes to false, stop playing.
308 curr_value = _property->getDoubleValue();
312 // update initial value before detecting changes
313 _prev_value = curr_value;
317 // If a condition is defined, test whether it is FALSE,
319 // if a property is defined then test if it's value is FALSE
320 // or if the mode is IN_TRANSIT then
321 // test whether the current value matches the previous value.
322 if ( // Lisp, anyone?
323 (_condition && !_condition->test()) ||
324 (!_condition && _property &&
327 ( (_mode == SGXmlSound::IN_TRANSIT) && (curr_value == _prev_value) )
332 if ((_mode != SGXmlSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME))
334 if (_sample->is_playing()) {
335 SG_LOG(SG_SOUND, SG_DEBUG, "Stopping audio after " << _dt_play
336 << " sec: " << _name );
352 // mode is ONCE and the sound is still playing?
354 if (_active && (_mode == SGXmlSound::ONCE)) {
356 if (!_sample->is_playing()) {
366 // Update the playing time, cache the current value and
367 // clear the delay timer.
370 _prev_value = curr_value;
374 if (_dt_play < _delay)
381 int max = _volume.size();
383 double volume_offset = 0.0;
385 for(i = 0; i < max; i++) {
389 v = _volume[i].prop->getDoubleValue();
391 else if (_volume[i].intern)
392 v = *_volume[i].intern;
395 v = _volume[i].fn(v);
397 v *= _volume[i].factor;
399 if (_volume[i].max && (v > _volume[i].max))
402 else if (v < _volume[i].min)
405 if (_volume[i].subtract) // Hack!
406 volume = _volume[i].offset - v;
409 volume_offset += _volume[i].offset;
419 double pitch_offset = 0.0;
421 for(i = 0; i < max; i++) {
425 p = _pitch[i].prop->getDoubleValue();
427 else if (_pitch[i].intern)
428 p = *_pitch[i].intern;
433 p *= _pitch[i].factor;
435 if (_pitch[i].max && (p > _pitch[i].max))
438 else if (p < _pitch[i].min)
441 if (_pitch[i].subtract) // Hack!
442 pitch = _pitch[i].offset - p;
445 pitch_offset += _pitch[i].offset;
451 // Change sample state
454 double vol = volume_offset + volume;
456 SG_LOG(SG_SOUND, SG_DEBUG, "Sound volume too large for '"
457 << _name << "': " << vol << " -> clipping to 1.0");
460 _sample->set_volume(vol);
461 _sample->set_pitch( pitch_offset + pitch );
465 // Do we need to start playing the sample?
469 if (_mode == SGXmlSound::ONCE)
470 _sample->play(false);
475 SG_LOG(SG_SOUND, SG_DEBUG, "Playing audio after " << _dt_stop
476 << " sec: " << _name);
477 SG_LOG(SG_SOUND, SG_DEBUG,
478 "Playing " << ((_mode == ONCE) ? "once" : "looped"));