]> git.mxchange.org Git - simgear.git/blob - simgear/sound/xmlsound.cxx
Add VS2010 project files
[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)+1e-9); }
45 static double _snd_log(double v)   { return log(fabs(v)+1e-9); }
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         {"inv", _snd_inv},
54         {"abs", _snd_abs},
55         {"sqrt", _snd_sqrt},
56         {"log", _snd_log10},
57         {"ln", _snd_log},
58         {"", NULL}
59 };
60
61 SGXmlSound::SGXmlSound()
62   : _sample(NULL),
63     _active(false),
64     _name(""),
65     _mode(SGXmlSound::ONCE),
66     _prev_value(0),
67     _dt_play(0.0),
68     _dt_stop(0.0),
69     _delay(0.0),
70     _stopping(0.0)
71 {
72 }
73
74 SGXmlSound::~SGXmlSound()
75 {
76     if (_sample)
77         _sample->stop();
78
79     _volume.clear();
80     _pitch.clear();
81 }
82
83 void
84 SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node,
85                  SGSampleGroup *sgrp, SGSampleGroup *avionics,
86                  const string &path)
87 {
88
89    //
90    // set global sound properties
91    //
92
93    _name = node->getStringValue("name", "");
94    SG_LOG(SG_GENERAL, SG_DEBUG, "Loading sound information for: " << _name );
95
96    string mode_str = node->getStringValue("mode", "");
97    if ( mode_str == "looped" ) {
98        _mode = SGXmlSound::LOOPED;
99
100    } else if ( mode_str == "in-transit" ) {
101        _mode = SGXmlSound::IN_TRANSIT;
102
103    } else {
104       _mode = SGXmlSound::ONCE;
105    }
106
107    bool is_avionics = false;
108    string type_str = node->getStringValue("type", "fx");
109    if ( type_str == "avionics" )
110       is_avionics = true;
111
112    string propval = node->getStringValue("property", "");
113    if (propval != "")
114       _property = root->getNode(propval, true);
115
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    std::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       propval = kids[i]->getStringValue("property", "");
136       if ( propval != "" )
137          volume.prop = root->getNode(propval, true);
138
139       string intern_str = kids[i]->getStringValue("internal", "");
140       if (intern_str == "dt_play")
141          volume.intern = &_dt_play;
142       else if (intern_str == "dt_stop")
143          volume.intern = &_dt_stop;
144
145       if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
146          if (volume.factor < 0.0) {
147             volume.factor = -volume.factor;
148             volume.subtract = true;
149          }
150
151       string type_str = kids[i]->getStringValue("type", "");
152       if ( type_str != "" ) {
153
154          for (int j=0; __sound_fn[j].fn; j++)
155            if ( type_str == __sound_fn[j].name ) {
156                volume.fn = __sound_fn[j].fn;
157                break;
158             }
159
160          if (!volume.fn)
161             SG_LOG(SG_GENERAL,SG_INFO,
162                    "  Unknown volume type, default to 'lin'");
163       }
164
165       volume.offset = kids[i]->getDoubleValue("offset", 0.0);
166
167       if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
168          SG_LOG( SG_GENERAL, SG_WARN,
169           "Volume minimum value below 0. Forced to 0.");
170
171       volume.max = kids[i]->getDoubleValue("max", 0.0);
172       if (volume.max && (volume.max < volume.min) )
173          SG_LOG(SG_GENERAL,SG_ALERT,
174                 "  Volume maximum below minimum. Neglected.");
175
176       _volume.push_back(volume);
177       v += volume.offset;
178
179    }
180
181    // rule of thumb: make reference distance a 100th of the maximum distance.
182    float reference_dist = node->getDoubleValue("reference-dist", 60.0);
183    float max_dist = node->getDoubleValue("max-dist", 6000.0);
184
185    //
186    // set pitch properties
187    //
188    float p = 0.0;
189    kids = node->getChildren("pitch");
190    for (i = 0; (i < kids.size()) && (i < SGXmlSound::MAXPROP); i++) {
191       _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
192
193       propval = kids[i]->getStringValue("property", "");
194       if (propval != "")
195          pitch.prop = root->getNode(propval, true);
196
197       string intern_str = kids[i]->getStringValue("internal", "");
198       if (intern_str == "dt_play")
199          pitch.intern = &_dt_play;
200       else if (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       string type_str = kids[i]->getStringValue("type", "");
210       if ( type_str != "" ) {
211
212          for (int j=0; __sound_fn[j].fn; j++) 
213             if ( 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    SGVec3f offset_pos = SGVec3f::zeros();
242    SGPropertyNode_ptr prop = node->getChild("position");
243    if ( prop != NULL ) {
244        offset_pos[0] = -prop->getDoubleValue("x", 0.0);
245        offset_pos[1] = -prop->getDoubleValue("y", 0.0);
246        offset_pos[2] = -prop->getDoubleValue("z", 0.0);
247    }
248
249    //
250    // Orientation
251    //
252    SGVec3f dir = SGVec3f::zeros();
253    float inner = 360.0;
254    float outer = 360.0;
255    float outer_gain = 0.0;
256    prop = node->getChild("orientation");
257    if ( prop != NULL ) {
258       dir = SGVec3f(-prop->getFloatValue("x", 0.0),
259                     -prop->getFloatValue("y", 0.0),
260                     -prop->getFloatValue("z", 0.0));
261       inner = prop->getFloatValue("inner-angle", 360.0);
262       outer = prop->getFloatValue("outer-angle", 360.0);
263       outer_gain = prop->getFloatValue("outer-gain", 0.0);
264    }
265
266    //
267    // Initialize the sample
268    //
269    if (is_avionics) {
270       _sgrp = avionics;
271    } else {
272       _sgrp = sgrp;
273    }
274    _sample = new SGSoundSample( path.c_str(), node->getStringValue("path", ""));
275    _sample->set_relative_position( offset_pos );
276    _sample->set_direction( dir );
277    _sample->set_audio_cone( inner, outer, outer_gain );
278    _sample->set_reference_dist( reference_dist );
279    _sample->set_max_dist( max_dist );
280    _sample->set_volume( v );
281    _sample->set_pitch( p );
282    _sgrp->add( _sample, _name );
283 }
284
285 void
286 SGXmlSound::update (double dt)
287 {
288    double curr_value = 0.0;
289
290    //
291    // If the state changes to false, stop playing.
292    //
293    if (_property)
294        curr_value = _property->getDoubleValue();
295
296    // If a condition is defined, test whether it is FALSE,
297    // else
298    //   if a property is defined then test if it's value is FALSE
299    //      or if the mode is IN_TRANSIT then
300    //            test whether the current value matches the previous value.
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        {
313            if (_sample->is_playing()) {
314                SG_LOG(SG_GENERAL, SG_DEBUG, "Stopping audio after " << _dt_play
315                       << " sec: " << _name );
316
317                _sample->stop();
318            }
319
320            _active = false;
321            _dt_stop += dt;
322            _dt_play = 0.0;
323        } else {
324            _stopping += dt;
325        }
326
327        return;
328    }
329
330    //
331    // mode is ONCE and the sound is still playing?
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    } else {
343
344       //
345       // Update the playing time, cache the current value and
346       // clear the delay timer.
347       //
348       _dt_play += dt;
349       _prev_value = curr_value;
350       _stopping = 0.0;
351    }
352
353    if (_dt_play < _delay)
354       return;
355
356    //
357    // Update the volume
358    //
359    int i;
360    int max = _volume.size();
361    double volume = 1.0;
362    double volume_offset = 0.0;
363
364    for(i = 0; i < max; i++) {
365       double v = 1.0;
366
367       if (_volume[i].prop)
368          v = _volume[i].prop->getDoubleValue();
369
370       else if (_volume[i].intern)
371          v = *_volume[i].intern;
372
373       if (_volume[i].fn)
374          v = _volume[i].fn(v);
375
376       v *= _volume[i].factor;
377
378       if (_volume[i].max && (v > _volume[i].max))
379          v = _volume[i].max;
380
381       else if (v < _volume[i].min)
382          v = _volume[i].min;
383
384       if (_volume[i].subtract)                          // Hack!
385          volume = _volume[i].offset - v;
386
387       else {
388          volume_offset += _volume[i].offset;
389          volume *= v;
390       }
391    }
392
393    //
394    // Update the pitch
395    //
396    max = _pitch.size();
397    double pitch = 1.0;
398    double pitch_offset = 0.0;
399
400    for(i = 0; i < max; i++) {
401       double p = 1.0;
402
403       if (_pitch[i].prop)
404          p = _pitch[i].prop->getDoubleValue();
405
406       else if (_pitch[i].intern)
407          p = *_pitch[i].intern;
408
409       if (_pitch[i].fn)
410          p = _pitch[i].fn(p);
411
412       p *= _pitch[i].factor;
413
414       if (_pitch[i].max && (p > _pitch[i].max))
415          p = _pitch[i].max;
416
417       else if (p < _pitch[i].min)
418          p = _pitch[i].min;
419
420       if (_pitch[i].subtract)                           // Hack!
421          pitch = _pitch[i].offset - p;
422
423       else {
424          pitch_offset += _pitch[i].offset;
425          pitch *= p;
426       }
427    }
428
429    //
430    // Change sample state
431    //
432
433    double vol = volume_offset + volume;
434    if (vol > 1.0) {
435       SG_LOG(SG_GENERAL, SG_DEBUG, "Sound volume too large for '"
436               << _name << "':  " << vol << "  ->  clipping to 1.0");
437       vol = 1.0;
438    }
439    _sample->set_volume(vol);
440    _sample->set_pitch( pitch_offset + pitch );
441
442
443    //
444    // Do we need to start playing the sample?
445    //
446    if (!_active) {
447
448       if (_mode == SGXmlSound::ONCE)
449          _sample->play(false);
450
451       else
452          _sample->play(true);
453
454       SG_LOG(SG_GENERAL, SG_DEBUG, "Playing audio after " << _dt_stop 
455                                    << " sec: " << _name);
456       SG_LOG(SG_GENERAL, SG_DEBUG,
457                          "Playing " << ((_mode == ONCE) ? "once" : "looped"));
458
459       _active = true;
460       _dt_stop = 0.0;
461    }
462 }