]> git.mxchange.org Git - simgear.git/blob - simgear/sound/xmlsound.cxx
xmlsound: warning--
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifdef HAVE_CONFIG_H
25 #  include <simgear_config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <string.h>
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/props/condition.hxx>
34 #include <simgear/math/SGMath.hxx>
35
36
37 #include "xmlsound.hxx"
38
39
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)); }
45 static double _snd_log(double v)   { return log(fabs(v)); }
46 // static double _snd_sqr(double v)   { return v*v; }
47 // static double _snd_pow3(double v)  { return v*v*v; }
48
49 static const struct {
50         const char *name;
51         double (*fn)(double);
52 } __sound_fn[] = {
53 //      {"lin", _snd_lin},
54         {"inv", _snd_inv},
55         {"abs", _snd_abs},
56         {"sqrt", _snd_sqrt},
57         {"log", _snd_log10},
58         {"ln", _snd_log},
59 //      {"sqr", _snd_sqr},
60 //      {"pow3", _snd_pow3},
61         {"", NULL}
62 };
63
64 SGXmlSound::SGXmlSound()
65   : _sample(NULL),
66     _condition(NULL),
67     _active(false),
68     _name(""),
69     _mode(SGXmlSound::ONCE),
70     _prev_value(0),
71     _dt_play(0.0),
72     _dt_stop(0.0),
73     _delay(0.0),
74     _stopping(0.0)
75 {
76 }
77
78 SGXmlSound::~SGXmlSound()
79 {
80     if (_sample)
81         _sample->stop();
82
83     delete _condition;
84
85     _volume.clear();
86     _pitch.clear();
87 }
88
89 void
90 SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node, SGSoundMgr *sndmgr,
91                  const string &path)
92 {
93
94    //
95    // set global sound properties
96    //
97    
98    _name = node->getStringValue("name", "");
99    SG_LOG(SG_GENERAL, SG_INFO, "Loading sound information for: " << _name );
100
101    const char *mode_str = node->getStringValue("mode", "");
102    if ( !strcmp(mode_str, "looped") ) {
103        _mode = SGXmlSound::LOOPED;
104
105    } else if ( !strcmp(mode_str, "in-transit") ) {
106        _mode = SGXmlSound::IN_TRANSIT;
107
108    } else {
109       _mode = SGXmlSound::ONCE;
110
111       if ( strcmp(mode_str, "") )
112          SG_LOG(SG_GENERAL,SG_INFO, "  Unknown sound mode, default to 'once'");
113    }
114
115    _property = root->getNode(node->getStringValue("property", ""), true);
116    SGPropertyNode *condition = node->getChild("condition");
117    if (condition != NULL)
118       _condition = sgReadCondition(root, condition);
119
120    if (!_property && !_condition)
121       SG_LOG(SG_GENERAL, SG_WARN,
122              "  Neither a condition nor a property specified");
123
124    _delay = node->getDoubleValue("delay-sec", 0.0);
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    // Initialize the sample
267    //
268    _mgr = sndmgr;
269    if ( (_sample = _mgr->find(_name)) == NULL ) {
270        // FIXME: Does it make sense to overwrite a previous entry's
271        // configuration just because a new entry has the same name?
272        // Note that we can't match on identical "path" because we the
273        // new entry could be at a different location with different
274        // configuration so we need a new sample which creates a new
275        // "alSource".  The semantics of what is going on here seems
276        // confused and needs to be thought through more carefully.
277         _sample = new SGSoundSample( path.c_str(),
278                                     node->getStringValue("path", ""),
279                                     false );
280
281        _mgr->add( _sample, _name );
282    }
283
284    _sample->set_offset_pos( offset_pos );
285    _sample->set_orientation(dir, inner, outer, outer_gain);
286    _sample->set_volume(v);
287    _sample->set_reference_dist( reference_dist );
288    _sample->set_max_dist( max_dist );
289    _sample->set_pitch(p);
290 }
291
292 void
293 SGXmlSound::update (double dt)
294 {
295    double curr_value = 0.0;
296
297    //
298    // If the state changes to false, stop playing.
299    //
300    if (_property)
301        curr_value = _property->getDoubleValue();
302
303    if (                                                 // Lisp, anyone?
304        (_condition && !_condition->test()) ||
305        (!_condition && _property &&
306         (
307          !curr_value ||
308          ( (_mode == SGXmlSound::IN_TRANSIT) && (curr_value == _prev_value) )
309          )
310         )
311        )
312    {
313        if ((_mode != SGXmlSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME)) {
314            if (_sample->is_playing()) {
315                SG_LOG(SG_GENERAL, SG_INFO, "Stopping audio after " << _dt_play
316                       << " sec: " << _name );
317
318                _sample->stop();
319            }
320
321            _active = false;
322            _dt_stop += dt;
323            _dt_play = 0.0;
324        } else {
325            _stopping += dt;
326        }
327
328        return;
329    }
330
331    //
332    // mode is ONCE and the sound is still playing?
333    //
334    if (_active && (_mode == SGXmlSound::ONCE)) {
335
336       if (!_sample->is_playing()) {
337          _dt_stop += dt;
338          _dt_play = 0.0;
339       } else {
340          _dt_play += dt;
341       }
342
343    } else {
344
345       //
346       // Update the playing time, cache the current value and
347       // clear the delay timer.
348       //
349       _dt_play += dt;
350       _prev_value = curr_value;
351       _stopping = 0.0;
352    }
353
354    if (_dt_play < _delay)
355       return;
356
357    //
358    // Update the volume
359    //
360    int i;
361    int max = _volume.size();
362    double volume = 1.0;
363    double volume_offset = 0.0;
364
365    for(i = 0; i < max; i++) {
366       double v = 1.0;
367
368       if (_volume[i].prop)
369          v = _volume[i].prop->getDoubleValue();
370
371       else if (_volume[i].intern)
372          v = *_volume[i].intern;
373
374       if (_volume[i].fn)
375          v = _volume[i].fn(v);
376
377       v *= _volume[i].factor;
378
379       if (_volume[i].max && (v > _volume[i].max))
380          v = _volume[i].max;
381
382       else if (v < _volume[i].min)
383          v = _volume[i].min;
384
385       if (_volume[i].subtract)                          // Hack!
386          volume = _volume[i].offset - v;
387
388       else {
389          volume_offset += _volume[i].offset;
390          volume *= v;
391       }
392    }
393
394    //
395    // Update the pitch
396    //
397    max = _pitch.size();
398    double pitch = 1.0;
399    double pitch_offset = 0.0;
400
401    for(i = 0; i < max; i++) {
402       double p = 1.0;
403
404       if (_pitch[i].prop)
405          p = _pitch[i].prop->getDoubleValue();
406
407       else if (_pitch[i].intern)
408          p = *_pitch[i].intern;
409
410       if (_pitch[i].fn)
411          p = _pitch[i].fn(p);
412
413       p *= _pitch[i].factor;
414
415       if (_pitch[i].max && (p > _pitch[i].max))
416          p = _pitch[i].max;
417
418       else if (p < _pitch[i].min)
419          p = _pitch[i].min;
420
421       if (_pitch[i].subtract)                           // Hack!
422          pitch = _pitch[i].offset - p;
423
424       else {
425          pitch_offset += _pitch[i].offset;
426          pitch *= p;
427       }
428    }
429
430    //
431    // Change sample state
432    //
433
434    double vol = volume_offset + volume;
435    if (vol > 1.0) {
436       SG_LOG(SG_GENERAL, SG_WARN, "Sound volume too large for '"
437               << _name << "':  " << vol << "  ->  clipping to 1.0");
438       vol = 1.0;
439    }
440    _sample->set_volume(vol);
441    _sample->set_pitch( pitch_offset + pitch );
442
443
444    //
445    // Do we need to start playing the sample?
446    //
447    if (!_active) {
448
449       if (_mode == SGXmlSound::ONCE)
450          _sample->play(false);
451
452       else
453          _sample->play(true);
454
455       SG_LOG(SG_GENERAL, SG_INFO, "Playing audio after " << _dt_stop 
456                                    << " sec: " << _name);
457       SG_LOG(SG_GENERAL, SG_BULK,
458                          "Playing " << ((_mode == ONCE) ? "once" : "looped"));
459
460       _active = true;
461       _dt_stop = 0.0;
462    }
463 }