]> git.mxchange.org Git - simgear.git/blob - simgear/sound/xmlsound.cxx
fbb4504ac1de562574b2e8a3886dcc4fb827c6b3
[simgear.git] / simgear / sound / xmlsound.cxx
1 // sound.cxx -- Sound class implementation
2 //
3 // Started by Erik Hofman, February 2002
4 // (Reuses some code from  fg_fx.cxx created by David Megginson)
5 //
6 // Copyright (C) 2002  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
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.
12 //
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.
17 //
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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24 #include <simgear/compiler.h>
25
26 #ifdef SG_HAVE_STD_INCLUDES
27 #  include <cmath>
28 #else
29 #  include <math.h>
30 #endif
31 #include <string.h>
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/props/condition.hxx>
35 #include <simgear/math/fastmath.hxx>
36
37
38 #include "xmlsound.hxx"
39
40
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 (v < 0) ? sqrt(-v) : sqrt(v); }
45 static double _snd_log10(double v) { return (v < 1) ? 0 : fast_log10(v); }
46 static double _snd_log(double v)   { return (v < 1) ? 0 : fast_log(v); }
47 // static double _snd_sqr(double v)   { return v*v; }
48 // static double _snd_pow3(double v)  { return v*v*v; }
49
50 static const struct {
51         char *name;
52         double (*fn)(double);
53 } __sound_fn[] = {
54 //      {"lin", _snd_lin},
55         {"inv", _snd_inv},
56         {"abs", _snd_abs},
57         {"sqrt", _snd_sqrt},
58         {"log", _snd_log10},
59         {"ln", _snd_log},
60 //      {"sqr", _snd_sqr},
61 //      {"pow3", _snd_pow3},
62         {"", NULL}
63 };
64
65 SGXmlSound::SGXmlSound()
66   : _sample(NULL),
67     _condition(NULL),
68     _property(NULL),
69     _active(false),
70     _name(""),
71     _mode(SGXmlSound::ONCE),
72     _prev_value(0),
73     _dt_play(0.0),
74     _dt_stop(0.0),
75     _stopping(0.0)
76 {
77 }
78
79 SGXmlSound::~SGXmlSound()
80 {
81     _sample->stop();
82
83     delete _property;
84     delete _condition;
85
86     _volume.clear();
87     _pitch.clear();
88     delete _sample;
89 }
90
91 void
92 SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node, SGSoundMgr *sndmgr,
93                  const string &path)
94 {
95
96    //
97    // set global sound properties
98    //
99    
100    _name = node->getStringValue("name", "");
101    SG_LOG(SG_GENERAL, SG_INFO, "Loading sound information for: " << _name );
102
103    const char *mode_str = node->getStringValue("mode", "");
104    if ( !strcmp(mode_str, "looped") ) {
105        _mode = SGXmlSound::LOOPED;
106
107    } else if ( !strcmp(mode_str, "in-transit") ) {
108        _mode = SGXmlSound::IN_TRANSIT;
109
110    } else {
111       _mode = SGXmlSound::ONCE;
112
113       if ( strcmp(mode_str, "") )
114          SG_LOG(SG_GENERAL,SG_INFO, "  Unknown sound mode, default to 'once'");
115    }
116
117    _property = root->getNode(node->getStringValue("property", ""), true);
118    SGPropertyNode *condition = node->getChild("condition");
119    if (condition != NULL)
120       _condition = sgReadCondition(root, condition);
121
122    if (!_property && !_condition)
123       SG_LOG(SG_GENERAL, SG_WARN,
124              "  Neither a condition nor a property specified");
125
126    //
127    // set volume properties
128    //
129    unsigned int i;
130    float v = 0.0;
131    vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
132    for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
133       _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
134
135       if (strcmp(kids[i]->getStringValue("property"), ""))
136          volume.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
137
138       const char *intern_str = kids[i]->getStringValue("internal", "");
139       if (!strcmp(intern_str, "dt_play"))
140          volume.intern = &_dt_play;
141       else if (!strcmp(intern_str, "dt_stop"))
142          volume.intern = &_dt_stop;
143
144       if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
145          if (volume.factor < 0.0) {
146             volume.factor = -volume.factor;
147             volume.subtract = true;
148          }
149
150       const char *type_str = kids[i]->getStringValue("type", "");
151       if ( strcmp(type_str, "") ) {
152
153          for (int j=0; __sound_fn[j].fn; j++)
154            if ( !strcmp(type_str, __sound_fn[j].name) ) {
155                volume.fn = __sound_fn[j].fn;
156                break;
157             }
158
159          if (!volume.fn)
160             SG_LOG(SG_GENERAL,SG_INFO,
161                    "  Unknown volume type, default to 'lin'");
162       }
163
164       volume.offset = kids[i]->getDoubleValue("offset", 0.0);
165
166       if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
167          SG_LOG( SG_GENERAL, SG_WARN,
168           "Volume minimum value below 0. Forced to 0.");
169
170       volume.max = kids[i]->getDoubleValue("max", 0.0);
171       if (volume.max && (volume.max < volume.min) )
172          SG_LOG(SG_GENERAL,SG_ALERT,
173                 "  Volume maximum below minimum. Neglected.");
174
175       _volume.push_back(volume);
176       v += volume.offset;
177
178    }
179
180    float reference_dist = node->getDoubleValue("reference-dist", 500.0);
181    float max_dist = node->getDoubleValue("max-dist", 3000.0);
182  
183    //
184    // set pitch properties
185    //
186    float p = 0.0;
187    kids = node->getChildren("pitch");
188    for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
189       _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
190
191       if (strcmp(kids[i]->getStringValue("property", ""), ""))
192          pitch.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
193
194       const char *intern_str = kids[i]->getStringValue("internal", "");
195       if (!strcmp(intern_str, "dt_play"))
196          pitch.intern = &_dt_play;
197       else if (!strcmp(intern_str, "dt_stop"))
198          pitch.intern = &_dt_stop;
199
200       if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
201          if (pitch.factor < 0.0) {
202             pitch.factor = -pitch.factor;
203             pitch.subtract = true;
204          }
205
206       const char *type_str = kids[i]->getStringValue("type", "");
207       if ( strcmp(type_str, "") ) {
208
209          for (int j=0; __sound_fn[j].fn; j++) 
210             if ( !strcmp(type_str, __sound_fn[j].name) ) {
211                pitch.fn = __sound_fn[j].fn;
212                break;
213             }
214
215          if (!pitch.fn)
216             SG_LOG(SG_GENERAL,SG_INFO,
217                    "  Unknown pitch type, default to 'lin'");
218       }
219      
220       pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
221
222       if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
223          SG_LOG(SG_GENERAL,SG_WARN,
224                 "  Pitch minimum value below 0. Forced to 0.");
225
226       pitch.max = kids[i]->getDoubleValue("max", 0.0);
227       if (pitch.max && (pitch.max < pitch.min) )
228          SG_LOG(SG_GENERAL,SG_ALERT,
229                 "  Pitch maximum below minimum. Neglected");
230
231       _pitch.push_back(pitch);
232       p += pitch.offset;
233    }
234
235    //
236    // Relative position
237    //
238    sgVec3 offset_pos;
239    sgSetVec3( offset_pos, 0.0, 0.0, 0.0 );
240    SGPropertyNode_ptr pos = node->getChild("position");
241    if ( pos != NULL ) {
242        offset_pos[0] = pos->getDoubleValue("x", 0.0);
243        offset_pos[1] = pos->getDoubleValue("y", 0.0);
244        offset_pos[2] = pos->getDoubleValue("z", 0.0);
245    }
246
247    //
248    // Orientation
249    //
250    sgVec3 dir;
251    float inner, outer, outer_gain;
252    sgSetVec3( dir, 0.0, 0.0, 0.0 );
253    inner = outer = 360.0;
254    outer_gain = 0.0;
255    pos = node->getChild("orientation");
256    if ( pos != NULL ) {
257       dir[0] = pos->getDoubleValue("x", 0.0);
258       dir[1] = pos->getDoubleValue("y", 0.0);
259       dir[2] = pos->getDoubleValue("z", 0.0);
260       inner = pos->getDoubleValue("inner-angle", 360.0);
261       outer = pos->getDoubleValue("outer-angle", 360.0);
262       outer_gain = pos->getDoubleValue("outer-gain", 0.0);
263    }
264    
265
266    //
267    // Initialize the sample
268    //
269    _mgr = sndmgr;
270    if ( (_sample = _mgr->find(_name)) == NULL ) {
271        _sample = new SGSoundSample( path.c_str(),
272                                     node->getStringValue("path", ""),
273                                     true );
274
275        _mgr->add( _sample, _name );
276    }
277
278    _sample->set_offset_pos( offset_pos );
279    _sample->set_orientation(dir, inner, outer, outer_gain);
280    _sample->set_volume(v);
281    _sample->set_reference_dist( reference_dist );
282    _sample->set_max_dist( max_dist );
283    _sample->set_pitch(p);
284 }
285
286 void
287 SGXmlSound::update (double dt)
288 {
289    double curr_value = 0.0;
290
291    //
292    // If the state changes to false, stop playing.
293    //
294    if (_property)
295        curr_value = _property->getDoubleValue();
296
297    if (                                                 // Lisp, anyone?
298        (_condition && !_condition->test()) ||
299        (!_condition && _property &&
300         (
301          !curr_value ||
302          ( (_mode == SGXmlSound::IN_TRANSIT) && (curr_value == _prev_value) )
303          )
304         )
305        )
306    {
307        if ((_mode != SGXmlSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME)) {
308            if (_sample->is_playing()) {
309                SG_LOG(SG_GENERAL, SG_INFO, "Stopping audio after " << _dt_play
310                       << " sec: " << _name );
311
312                _sample->stop();
313            }
314
315            _active = false;
316            _dt_stop += dt;
317            _dt_play = 0.0;
318        } else {
319            _stopping += dt;
320        }
321
322        return;
323    }
324
325    //
326    // If the mode is ONCE and the sound is still playing,
327    //  we have nothing to do anymore.
328    //
329    if (_active && (_mode == SGXmlSound::ONCE)) {
330
331       if (!_sample->is_playing()) {
332          _dt_stop += dt;
333          _dt_play = 0.0;
334       } else {
335          _dt_play += dt;
336       }
337
338       return;
339    }
340
341    //
342    // Update the playing time, cache the current value and
343    // clear the delay timer.
344    //
345    _dt_play += dt;
346    _prev_value = curr_value;
347    _stopping = 0.0;
348
349    //
350    // Update the volume
351    //
352    int i;
353    int max = _volume.size();
354    double volume = 1.0;
355    double volume_offset = 0.0;
356
357    for(i = 0; i < max; i++) {
358       double v = 1.0;
359
360       if (_volume[i].prop)
361          v = _volume[i].prop->getDoubleValue();
362
363       else if (_volume[i].intern)
364          v = *_volume[i].intern;
365
366       if (_volume[i].fn)
367          v = _volume[i].fn(v);
368
369       v *= _volume[i].factor;
370
371       if (_volume[i].max && (v > _volume[i].max))
372          v = _volume[i].max;
373
374       else if (v < _volume[i].min)
375          v = _volume[i].min;
376
377       if (_volume[i].subtract)                          // Hack!
378          volume = _volume[i].offset - v;
379
380       else {
381          volume_offset += _volume[i].offset;
382          volume *= v;
383       }
384    }
385
386    //
387    // Update the pitch
388    //
389    max = _pitch.size();
390    double pitch = 1.0;
391    double pitch_offset = 0.0;
392
393    for(i = 0; i < max; i++) {
394       double p = 1.0;
395
396       if (_pitch[i].prop)
397          p = _pitch[i].prop->getDoubleValue();
398
399       else if (_pitch[i].intern)
400          p = *_pitch[i].intern;
401
402       if (_pitch[i].fn)
403          p = _pitch[i].fn(p);
404
405       p *= _pitch[i].factor;
406
407       if (_pitch[i].max && (p > _pitch[i].max))
408          p = _pitch[i].max;
409
410       else if (p < _pitch[i].min)
411          p = _pitch[i].min;
412
413       if (_pitch[i].subtract)                           // Hack!
414          pitch = _pitch[i].offset - p;
415
416       else {
417          pitch_offset += _pitch[i].offset;
418          pitch *= p;
419       }
420    }
421
422    //
423    // Change sample state
424    //
425    _sample->set_pitch( pitch_offset + pitch );
426    if ((volume_offset + volume ) > 1.0)
427    {
428       _sample->set_volume( 1.0 );
429       SG_LOG(SG_GENERAL, SG_WARN,
430              "Volume larger than 1.0 in configuration for '" << _name
431               << "', clipping.");
432    } else 
433       _sample->set_volume( volume_offset + volume );
434
435
436    //
437    // Do we need to start playing the sample?
438    //
439    if (!_active) {
440
441       if (_mode == SGXmlSound::ONCE)
442          _sample->play(false);
443
444       else
445          _sample->play(true);
446
447       SG_LOG(SG_GENERAL, SG_INFO, "Playing audio after " << _dt_stop 
448                                    << " sec: " << _name);
449       SG_LOG(SG_GENERAL, SG_BULK,
450                          "Playing " << ((_mode == ONCE) ? "once" : "looped"));
451
452       _active = true;
453       _dt_stop = 0.0;
454    }
455 }