]> git.mxchange.org Git - simgear.git/blob - simgear/sound/xmlsound.cxx
58be2356a971cb905cb21ef27a1cb36992502833
[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 - curt@flightgear.org
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     if (_property)
84         delete _property;
85
86     if (_condition)
87         delete _condition;
88
89     _volume.clear();
90     _pitch.clear();
91     delete _sample;
92 }
93
94 void
95 SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node, SGSoundMgr *sndmgr,
96                  const string &path)
97 {
98
99    //
100    // set global sound properties
101    //
102    
103    _name = node->getStringValue("name", "");
104    SG_LOG(SG_GENERAL, SG_INFO, "Loading sound information for: " << _name );
105
106    const char *mode_str = node->getStringValue("mode", "");
107    if ( !strcmp(mode_str, "looped") ) {
108        _mode = SGXmlSound::LOOPED;
109
110    } else if ( !strcmp(mode_str, "in-transit") ) {
111        _mode = SGXmlSound::IN_TRANSIT;
112
113    } else {
114       _mode = SGXmlSound::ONCE;
115
116       if ( strcmp(mode_str, "") )
117          SG_LOG(SG_GENERAL,SG_INFO, "  Unknown sound mode, default to 'once'");
118    }
119
120    _property = root->getNode(node->getStringValue("property", ""), true);
121    SGPropertyNode *condition = node->getChild("condition");
122    if (condition != NULL)
123       _condition = sgReadCondition(root, condition);
124
125    if (!_property && !_condition)
126       SG_LOG(SG_GENERAL, SG_WARN,
127              "  Neither a condition nor a property specified");
128
129    //
130    // set volume properties
131    //
132    unsigned int i;
133    float v = 0.0;
134    vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
135    for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
136       _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
137
138       if (strcmp(kids[i]->getStringValue("property"), ""))
139          volume.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
140
141       const char *intern_str = kids[i]->getStringValue("internal", "");
142       if (!strcmp(intern_str, "dt_play"))
143          volume.intern = &_dt_play;
144       else if (!strcmp(intern_str, "dt_stop"))
145          volume.intern = &_dt_stop;
146
147       if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
148          if (volume.factor < 0.0) {
149             volume.factor = -volume.factor;
150             volume.subtract = true;
151          }
152
153       const char *type_str = kids[i]->getStringValue("type", "");
154       if ( strcmp(type_str, "") ) {
155
156          for (int j=0; __sound_fn[j].fn; j++)
157            if ( !strcmp(type_str, __sound_fn[j].name) ) {
158                volume.fn = __sound_fn[j].fn;
159                break;
160             }
161
162          if (!volume.fn)
163             SG_LOG(SG_GENERAL,SG_INFO,
164                    "  Unknown volume type, default to 'lin'");
165       }
166
167       volume.offset = kids[i]->getDoubleValue("offset", 0.0);
168
169       if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
170          SG_LOG( SG_GENERAL, SG_WARN,
171           "Volume minimum value below 0. Forced to 0.");
172
173       volume.max = kids[i]->getDoubleValue("max", 0.0);
174       if (volume.max && (volume.max < volume.min) )
175          SG_LOG(SG_GENERAL,SG_ALERT,
176                 "  Volume maximum below minimum. Neglected.");
177
178       _volume.push_back(volume);
179       v += volume.offset;
180
181    }
182
183    float reference_dist = node->getDoubleValue("reference-dist", 500.0);
184    float max_dist = node->getDoubleValue("max-dist", 3000.0);
185  
186    //
187    // set pitch properties
188    //
189    float p = 0.0;
190    kids = node->getChildren("pitch");
191    for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
192       _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
193
194       if (strcmp(kids[i]->getStringValue("property", ""), ""))
195          pitch.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
196
197       const char *intern_str = kids[i]->getStringValue("internal", "");
198       if (!strcmp(intern_str, "dt_play"))
199          pitch.intern = &_dt_play;
200       else if (!strcmp(intern_str, "dt_stop"))
201          pitch.intern = &_dt_stop;
202
203       if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
204          if (pitch.factor < 0.0) {
205             pitch.factor = -pitch.factor;
206             pitch.subtract = true;
207          }
208
209       const char *type_str = kids[i]->getStringValue("type", "");
210       if ( strcmp(type_str, "") ) {
211
212          for (int j=0; __sound_fn[j].fn; j++) 
213             if ( !strcmp(type_str, __sound_fn[j].name) ) {
214                pitch.fn = __sound_fn[j].fn;
215                break;
216             }
217
218          if (!pitch.fn)
219             SG_LOG(SG_GENERAL,SG_INFO,
220                    "  Unknown pitch type, default to 'lin'");
221       }
222      
223       pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
224
225       if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
226          SG_LOG(SG_GENERAL,SG_WARN,
227                 "  Pitch minimum value below 0. Forced to 0.");
228
229       pitch.max = kids[i]->getDoubleValue("max", 0.0);
230       if (pitch.max && (pitch.max < pitch.min) )
231          SG_LOG(SG_GENERAL,SG_ALERT,
232                 "  Pitch maximum below minimum. Neglected");
233
234       _pitch.push_back(pitch);
235       p += pitch.offset;
236    }
237
238    //
239    // Relative position
240    //
241    sgVec3 offset_pos;
242    sgSetVec3( offset_pos, 0.0, 0.0, 0.0 );
243    SGPropertyNode_ptr pos = node->getChild("position");
244    if ( pos != NULL ) {
245        offset_pos[0] = pos->getDoubleValue("x", 0.0);
246        offset_pos[1] = pos->getDoubleValue("y", 0.0);
247        offset_pos[2] = pos->getDoubleValue("z", 0.0);
248    }
249
250    //
251    // Initialize the sample
252    //
253    _mgr = sndmgr;
254    if ( (_sample = _mgr->find(_name)) == NULL ) {
255        _sample = new SGSoundSample( path.c_str(),
256                                     node->getStringValue("path", ""),
257                                     true );
258
259        _mgr->add( _sample, _name );
260    }
261
262    _sample->set_offset_pos( offset_pos );
263    _sample->set_volume(v);
264    _sample->set_reference_dist( reference_dist );
265    _sample->set_max_dist( max_dist );
266    _sample->set_pitch(p);
267 }
268
269 void
270 SGXmlSound::update (double dt)
271 {
272    double curr_value = 0.0;
273
274    //
275    // If the state changes to false, stop playing.
276    //
277    if (_property)
278        curr_value = _property->getDoubleValue();
279
280    if (                                                 // Lisp, anyone?
281        (_condition && !_condition->test()) ||
282        (!_condition && _property &&
283         (
284          !curr_value ||
285          ( (_mode == SGXmlSound::IN_TRANSIT) && (curr_value == _prev_value) )
286          )
287         )
288        )
289    {
290        if ((_mode != SGXmlSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME)) {
291            if (_sample->is_playing()) {
292                SG_LOG(SG_GENERAL, SG_INFO, "Stopping audio after " << _dt_play
293                       << " sec: " << _name );
294
295                _sample->stop();
296            }
297
298            _active = false;
299            _dt_stop += dt;
300            _dt_play = 0.0;
301        } else {
302            _stopping += dt;
303        }
304
305        return;
306    }
307
308    //
309    // If the mode is ONCE and the sound is still playing,
310    //  we have nothing to do anymore.
311    //
312    if (_active && (_mode == SGXmlSound::ONCE)) {
313
314       if (!_sample->is_playing()) {
315          _dt_stop += dt;
316          _dt_play = 0.0;
317       } else {
318          _dt_play += dt;
319       }
320
321       return;
322    }
323
324    //
325    // Update the playing time, cache the current value and
326    // clear the delay timer.
327    //
328    _dt_play += dt;
329    _prev_value = curr_value;
330    _stopping = 0.0;
331
332    //
333    // Update the volume
334    //
335    int i;
336    int max = _volume.size();
337    double volume = 1.0;
338    double volume_offset = 0.0;
339
340    for(i = 0; i < max; i++) {
341       double v = 1.0;
342
343       if (_volume[i].prop)
344          v = _volume[i].prop->getDoubleValue();
345
346       else if (_volume[i].intern)
347          v = *_volume[i].intern;
348
349       if (_volume[i].fn)
350          v = _volume[i].fn(v);
351
352       v *= _volume[i].factor;
353
354       if (_volume[i].max && (v > _volume[i].max))
355          v = _volume[i].max;
356
357       else if (v < _volume[i].min)
358          v = _volume[i].min;
359
360       if (_volume[i].subtract)                          // Hack!
361          volume = _volume[i].offset - v;
362
363       else {
364          volume_offset += _volume[i].offset;
365          volume *= v;
366       }
367    }
368
369    //
370    // Update the pitch
371    //
372    max = _pitch.size();
373    double pitch = 1.0;
374    double pitch_offset = 0.0;
375
376    for(i = 0; i < max; i++) {
377       double p = 1.0;
378
379       if (_pitch[i].prop)
380          p = _pitch[i].prop->getDoubleValue();
381
382       else if (_pitch[i].intern)
383          p = *_pitch[i].intern;
384
385       if (_pitch[i].fn)
386          p = _pitch[i].fn(p);
387
388       p *= _pitch[i].factor;
389
390       if (_pitch[i].max && (p > _pitch[i].max))
391          p = _pitch[i].max;
392
393       else if (p < _pitch[i].min)
394          p = _pitch[i].min;
395
396       if (_pitch[i].subtract)                           // Hack!
397          pitch = _pitch[i].offset - p;
398
399       else {
400          pitch_offset += _pitch[i].offset;
401          pitch *= p;
402       }
403    }
404
405    //
406    // Change sample state
407    //
408    _sample->set_pitch( pitch_offset + pitch );
409    _sample->set_volume( volume_offset + volume );
410
411
412    //
413    // Do we need to start playing the sample?
414    //
415    if (!_active) {
416
417       if (_mode == SGXmlSound::ONCE)
418          _sample->play(false);
419
420       else
421          _sample->play(true);
422
423       SG_LOG(SG_GENERAL, SG_INFO, "Playing audio after " << _dt_stop 
424                                    << " sec: " << _name);
425       SG_LOG(SG_GENERAL, SG_BULK,
426                          "Playing " << ((_mode == ONCE) ? "once" : "looped"));
427
428       _active = true;
429       _dt_stop = 0.0;
430    }
431 }