]> git.mxchange.org Git - simgear.git/blob - simgear/sound/xmlsound.cxx
Updates to remove unneeded and old version of zlib source.
[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 #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 #define LOG_ABS(x) (((x) > 1) ? (x) : (((x) < -1) ? -(x) : 0))
42 // static double _snd_lin(double v)   { return v; }
43 static double _snd_inv(double v)   { return (v == 0) ? 1e99 : 1/v; }
44 static double _snd_abs(double v)   { return (v >= 0) ? v : -v; }
45 static double _snd_sqrt(double v)  { return (v < 0) ? sqrt(-v) : sqrt(v); }
46 static double _snd_log10(double v) { return fast_log10( LOG_ABS(v) ); }
47 static double _snd_log(double v)   { return fast_log( LOG_ABS(v) ); }
48 // static double _snd_sqr(double v)   { return v*v; }
49 // static double _snd_pow3(double v)  { return v*v*v; }
50
51 static const struct {
52         char *name;
53         double (*fn)(double);
54 } __sound_fn[] = {
55 //      {"lin", _snd_lin},
56         {"inv", _snd_inv},
57         {"abs", _snd_abs},
58         {"sqrt", _snd_sqrt},
59         {"log", _snd_log10},
60         {"ln", _snd_log},
61 //      {"sqr", _snd_sqr},
62 //      {"pow3", _snd_pow3},
63         {"", NULL}
64 };
65
66 SGXmlSound::SGXmlSound()
67   : _sample(NULL),
68     _condition(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     if (_sample)
82         _sample->stop();
83
84     delete _condition;
85
86     _volume.clear();
87     _pitch.clear();
88 }
89
90 void
91 SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node, SGSoundMgr *sndmgr,
92                  const string &path)
93 {
94
95    //
96    // set global sound properties
97    //
98    
99    _name = node->getStringValue("name", "");
100    SG_LOG(SG_GENERAL, SG_INFO, "Loading sound information for: " << _name );
101
102    const char *mode_str = node->getStringValue("mode", "");
103    if ( !strcmp(mode_str, "looped") ) {
104        _mode = SGXmlSound::LOOPED;
105
106    } else if ( !strcmp(mode_str, "in-transit") ) {
107        _mode = SGXmlSound::IN_TRANSIT;
108
109    } else {
110       _mode = SGXmlSound::ONCE;
111
112       if ( strcmp(mode_str, "") )
113          SG_LOG(SG_GENERAL,SG_INFO, "  Unknown sound mode, default to 'once'");
114    }
115
116    _property = root->getNode(node->getStringValue("property", ""), true);
117    SGPropertyNode *condition = node->getChild("condition");
118    if (condition != NULL)
119       _condition = sgReadCondition(root, condition);
120
121    if (!_property && !_condition)
122       SG_LOG(SG_GENERAL, SG_WARN,
123              "  Neither a condition nor a property specified");
124
125    //
126    // set volume properties
127    //
128    unsigned int i;
129    float v = 0.0;
130    vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
131    for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
132       _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
133
134       if (strcmp(kids[i]->getStringValue("property"), ""))
135          volume.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
136
137       const char *intern_str = kids[i]->getStringValue("internal", "");
138       if (!strcmp(intern_str, "dt_play"))
139          volume.intern = &_dt_play;
140       else if (!strcmp(intern_str, "dt_stop"))
141          volume.intern = &_dt_stop;
142
143       if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
144          if (volume.factor < 0.0) {
145             volume.factor = -volume.factor;
146             volume.subtract = true;
147          }
148
149       const char *type_str = kids[i]->getStringValue("type", "");
150       if ( strcmp(type_str, "") ) {
151
152          for (int j=0; __sound_fn[j].fn; j++)
153            if ( !strcmp(type_str, __sound_fn[j].name) ) {
154                volume.fn = __sound_fn[j].fn;
155                break;
156             }
157
158          if (!volume.fn)
159             SG_LOG(SG_GENERAL,SG_INFO,
160                    "  Unknown volume type, default to 'lin'");
161       }
162
163       volume.offset = kids[i]->getDoubleValue("offset", 0.0);
164
165       if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
166          SG_LOG( SG_GENERAL, SG_WARN,
167           "Volume minimum value below 0. Forced to 0.");
168
169       volume.max = kids[i]->getDoubleValue("max", 0.0);
170       if (volume.max && (volume.max < volume.min) )
171          SG_LOG(SG_GENERAL,SG_ALERT,
172                 "  Volume maximum below minimum. Neglected.");
173
174       _volume.push_back(volume);
175       v += volume.offset;
176
177    }
178
179    float reference_dist = node->getDoubleValue("reference-dist", 500.0);
180    float max_dist = node->getDoubleValue("max-dist", 3000.0);
181  
182    //
183    // set pitch properties
184    //
185    float p = 0.0;
186    kids = node->getChildren("pitch");
187    for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
188       _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
189
190       if (strcmp(kids[i]->getStringValue("property", ""), ""))
191          pitch.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
192
193       const char *intern_str = kids[i]->getStringValue("internal", "");
194       if (!strcmp(intern_str, "dt_play"))
195          pitch.intern = &_dt_play;
196       else if (!strcmp(intern_str, "dt_stop"))
197          pitch.intern = &_dt_stop;
198
199       if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
200          if (pitch.factor < 0.0) {
201             pitch.factor = -pitch.factor;
202             pitch.subtract = true;
203          }
204
205       const char *type_str = kids[i]->getStringValue("type", "");
206       if ( strcmp(type_str, "") ) {
207
208          for (int j=0; __sound_fn[j].fn; j++) 
209             if ( !strcmp(type_str, __sound_fn[j].name) ) {
210                pitch.fn = __sound_fn[j].fn;
211                break;
212             }
213
214          if (!pitch.fn)
215             SG_LOG(SG_GENERAL,SG_INFO,
216                    "  Unknown pitch type, default to 'lin'");
217       }
218      
219       pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
220
221       if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
222          SG_LOG(SG_GENERAL,SG_WARN,
223                 "  Pitch minimum value below 0. Forced to 0.");
224
225       pitch.max = kids[i]->getDoubleValue("max", 0.0);
226       if (pitch.max && (pitch.max < pitch.min) )
227          SG_LOG(SG_GENERAL,SG_ALERT,
228                 "  Pitch maximum below minimum. Neglected");
229
230       _pitch.push_back(pitch);
231       p += pitch.offset;
232    }
233
234    //
235    // Relative position
236    //
237    sgVec3 offset_pos;
238    sgSetVec3( offset_pos, 0.0, 0.0, 0.0 );
239    SGPropertyNode_ptr pos = node->getChild("position");
240    if ( pos != NULL ) {
241        offset_pos[0] = pos->getDoubleValue("x", 0.0);
242        offset_pos[1] = pos->getDoubleValue("y", 0.0);
243        offset_pos[2] = pos->getDoubleValue("z", 0.0);
244    }
245
246    //
247    // Orientation
248    //
249    sgVec3 dir;
250    float inner, outer, outer_gain;
251    sgSetVec3( dir, 0.0, 0.0, 0.0 );
252    inner = outer = 360.0;
253    outer_gain = 0.0;
254    pos = node->getChild("orientation");
255    if ( pos != NULL ) {
256       dir[0] = pos->getDoubleValue("x", 0.0);
257       dir[1] = pos->getDoubleValue("y", 0.0);
258       dir[2] = pos->getDoubleValue("z", 0.0);
259       inner = pos->getDoubleValue("inner-angle", 360.0);
260       outer = pos->getDoubleValue("outer-angle", 360.0);
261       outer_gain = pos->getDoubleValue("outer-gain", 0.0);
262    }
263    
264    //
265    // Initialize the sample
266    //
267    _mgr = sndmgr;
268    if ( (_sample = _mgr->find(_name)) == NULL ) {
269        // FIXME: Does it make sense to overwrite a previous entry's
270        // configuration just because a new entry has the same name?
271        // Note that we can't match on identical "path" because we the
272        // new entry could be at a different location with different
273        // configuration so we need a new sample which creates a new
274        // "alSource".  The semantics of what is going on here seems
275        // confused and needs to be thought through more carefully.
276         _sample = new SGSoundSample( path.c_str(),
277                                     node->getStringValue("path", "") );
278
279        _mgr->add( _sample, _name );
280    }
281
282    _sample->set_offset_pos( offset_pos );
283    _sample->set_orientation(dir, inner, outer, outer_gain);
284    _sample->set_volume(v);
285    _sample->set_reference_dist( reference_dist );
286    _sample->set_max_dist( max_dist );
287    _sample->set_pitch(p);
288 }
289
290 void
291 SGXmlSound::update (double dt)
292 {
293    double curr_value = 0.0;
294
295    //
296    // If the state changes to false, stop playing.
297    //
298    if (_property)
299        curr_value = _property->getDoubleValue();
300
301    if (                                                 // Lisp, anyone?
302        (_condition && !_condition->test()) ||
303        (!_condition && _property &&
304         (
305          !curr_value ||
306          ( (_mode == SGXmlSound::IN_TRANSIT) && (curr_value == _prev_value) )
307          )
308         )
309        )
310    {
311        if ((_mode != SGXmlSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME)) {
312            if (_sample->is_playing()) {
313                SG_LOG(SG_GENERAL, SG_INFO, "Stopping audio after " << _dt_play
314                       << " sec: " << _name );
315
316                _sample->stop();
317            }
318
319            _active = false;
320            _dt_stop += dt;
321            _dt_play = 0.0;
322        } else {
323            _stopping += dt;
324        }
325
326        return;
327    }
328
329    //
330    // If the mode is ONCE and the sound is still playing,
331    //  we have nothing to do anymore.
332    //
333    if (_active && (_mode == SGXmlSound::ONCE)) {
334
335       if (!_sample->is_playing()) {
336          _dt_stop += dt;
337          _dt_play = 0.0;
338       } else {
339          _dt_play += dt;
340       }
341
342       return;
343    }
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    // Update the volume
355    //
356    int i;
357    int max = _volume.size();
358    double volume = 1.0;
359    double volume_offset = 0.0;
360
361    for(i = 0; i < max; i++) {
362       double v = 1.0;
363
364       if (_volume[i].prop)
365          v = _volume[i].prop->getDoubleValue();
366
367       else if (_volume[i].intern)
368          v = *_volume[i].intern;
369
370       if (_volume[i].fn)
371          v = _volume[i].fn(v);
372
373       v *= _volume[i].factor;
374
375       if (_volume[i].max && (v > _volume[i].max))
376          v = _volume[i].max;
377
378       else if (v < _volume[i].min)
379          v = _volume[i].min;
380
381       if (_volume[i].subtract)                          // Hack!
382          volume = _volume[i].offset - v;
383
384       else {
385          volume_offset += _volume[i].offset;
386          volume *= v;
387       }
388    }
389
390    //
391    // Update the pitch
392    //
393    max = _pitch.size();
394    double pitch = 1.0;
395    double pitch_offset = 0.0;
396
397    for(i = 0; i < max; i++) {
398       double p = 1.0;
399
400       if (_pitch[i].prop)
401          p = _pitch[i].prop->getDoubleValue();
402
403       else if (_pitch[i].intern)
404          p = *_pitch[i].intern;
405
406       if (_pitch[i].fn)
407          p = _pitch[i].fn(p);
408
409       p *= _pitch[i].factor;
410
411       if (_pitch[i].max && (p > _pitch[i].max))
412          p = _pitch[i].max;
413
414       else if (p < _pitch[i].min)
415          p = _pitch[i].min;
416
417       if (_pitch[i].subtract)                           // Hack!
418          pitch = _pitch[i].offset - p;
419
420       else {
421          pitch_offset += _pitch[i].offset;
422          pitch *= p;
423       }
424    }
425
426    //
427    // Change sample state
428    //
429    _sample->set_pitch( pitch_offset + pitch );
430    if ((volume_offset + volume ) > 1.0)
431    {
432       _sample->set_volume( 1.0 );
433       SG_LOG(SG_GENERAL, SG_WARN,
434              "Volume larger than 1.0 in configuration for '" << _name
435               << "', clipping.");
436    } else 
437       _sample->set_volume( volume_offset + volume );
438
439
440    //
441    // Do we need to start playing the sample?
442    //
443    if (!_active) {
444
445       if (_mode == SGXmlSound::ONCE)
446          _sample->play(false);
447
448       else
449          _sample->play(true);
450
451       SG_LOG(SG_GENERAL, SG_INFO, "Playing audio after " << _dt_stop 
452                                    << " sec: " << _name);
453       SG_LOG(SG_GENERAL, SG_BULK,
454                          "Playing " << ((_mode == ONCE) ? "once" : "looped"));
455
456       _active = true;
457       _dt_stop = 0.0;
458    }
459 }