]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_sound.cxx
Load ground ATC frequency data, and map all stations by bucket index
[flightgear.git] / src / Sound / fg_sound.cxx
1 // fg_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
35 #include <Main/fg_props.hxx>
36
37 #include "fg_sound.hxx"
38
39
40 // static double _fg_lin(double v)   { return v; }
41 static double _fg_inv(double v)   { return (v == 0) ? 1e99 : 1/v; }
42 static double _fg_abs(double v)   { return (v >= 0) ? v : -v; }
43 static double _fg_sqrt(double v)  { return (v < 0) ? sqrt(-v) : sqrt(v); }
44 static double _fg_log10(double v) { return (v < 1) ? 0 : log10(v); }
45 static double _fg_log(double v)   { return (v < 1) ? 0 : log(v); }
46 // static double _fg_sqr(double v)   { return pow(v, 2); }
47 // static double _fg_pow3(double v)  { return pow(v, 3); }
48
49 static const struct {
50         char *name;
51         double (*fn)(double);
52 } __fg_snd_fn[] = {
53 //      {"lin", _fg_lin},
54         {"inv", _fg_inv},
55         {"abs", _fg_abs},
56         {"sqrt", _fg_sqrt},
57         {"log", _fg_log10},
58         {"ln", _fg_log},
59 //      {"sqr", _fg_sqr},
60 //      {"pow3", _fg_pow3},
61         {"", NULL}
62 };
63
64 FGSound::FGSound()
65   : _sample(NULL),
66     _condition(NULL),
67     _property(NULL),
68     _active(false),
69     _name(""),
70     _mode(FGSound::ONCE),
71     _prev_value(0),
72     _dt_play(0.0),
73     _dt_stop(0.0),
74     _stopping(0.0)
75 {
76 }
77
78 FGSound::~FGSound()
79 {
80    if (_property)
81       delete _property;
82
83    if (_condition)
84       delete _condition;
85
86    _volume.clear();
87    _pitch.clear();
88
89    delete _sample;
90 }
91
92 void
93 FGSound::init(SGPropertyNode *node)
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 = FGSound::LOOPED;
106
107    } else if ( !strcmp(mode_str, "in-transit") ) {
108        _mode = FGSound::IN_TRANSIT;
109
110    } else {
111       _mode = FGSound::ONCE;
112
113       if ( strcmp(mode_str, "") )
114          SG_LOG(SG_GENERAL,SG_INFO, "  Unknown sound mode, default to 'once'");
115    }
116
117    _property = fgGetNode(node->getStringValue("property", ""), true);
118    SGPropertyNode *condition = node->getChild("condition");
119    if (condition != NULL)
120       _condition = fgReadCondition(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 < FGSound::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 = fgGetNode(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; __fg_snd_fn[j].fn; j++)
154            if ( !strcmp(type_str, __fg_snd_fn[j].name) ) {
155                volume.fn = __fg_snd_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
181    //
182    // set pitch properties
183    //
184    float p = 0.0;
185    kids = node->getChildren("pitch");
186    for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
187       _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
188
189       if (strcmp(kids[i]->getStringValue("property", ""), ""))
190          pitch.prop = fgGetNode(kids[i]->getStringValue("property", ""), true);
191
192       const char *intern_str = kids[i]->getStringValue("internal", "");
193       if (!strcmp(intern_str, "dt_play"))
194          pitch.intern = &_dt_play;
195       else if (!strcmp(intern_str, "dt_stop"))
196          pitch.intern = &_dt_stop;
197
198       if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
199          if (pitch.factor < 0.0) {
200             pitch.factor = -pitch.factor;
201             pitch.subtract = true;
202          }
203
204       const char *type_str = kids[i]->getStringValue("type", "");
205       if ( strcmp(type_str, "") ) {
206
207          for (int j=0; __fg_snd_fn[j].fn; j++) 
208             if ( !strcmp(type_str, __fg_snd_fn[j].name) ) {
209                pitch.fn = __fg_snd_fn[j].fn;
210                break;
211             }
212
213          if (!pitch.fn)
214             SG_LOG(SG_GENERAL,SG_INFO,
215                    "  Unknown pitch type, default to 'lin'");
216       }
217      
218       pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
219
220       if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
221          SG_LOG(SG_GENERAL,SG_WARN,
222                 "  Pitch minimum value below 0. Forced to 0.");
223
224       pitch.max = kids[i]->getDoubleValue("max", 0.0);
225       if (pitch.max && (pitch.max < pitch.min) )
226          SG_LOG(SG_GENERAL,SG_ALERT,
227                 "  Pitch maximum below minimum. Neglected");
228
229       _pitch.push_back(pitch);
230       p += pitch.offset;
231    }
232
233    //
234    // Initialize the sample
235    //
236    _mgr = globals->get_soundmgr();
237    if ((_sample = _mgr->find(_name)) == NULL)
238       _sample = _mgr->add(_name, node->getStringValue("path", ""));
239
240    _sample->set_volume(v);
241    _sample->set_pitch(p);
242 }
243
244 void
245 FGSound::bind ()
246 {
247 }
248
249 void
250 FGSound::unbind ()
251 {
252 }
253
254 void
255 FGSound::update (double dt)
256 {
257    double curr_value = 0.0;
258
259    //
260    // If the state changes to false, stop playing.
261    //
262    if (_property)
263       curr_value = _property->getDoubleValue();
264
265    if (                                                 // Lisp, anyone?
266          (_condition && !_condition->test()) ||
267          (!_condition && _property &&
268             (
269                !curr_value ||
270                ( (_mode == FGSound::IN_TRANSIT) && (curr_value == _prev_value) )
271             )
272          )
273       )
274    {
275
276       if ((_mode != FGSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME)) {
277
278          if (_sample->is_playing()) {
279             SG_LOG(SG_GENERAL, SG_INFO, "Stopping audio after " << _dt_play
280                                         << " sec: " << _name );
281
282             _sample->stop( _mgr->get_scheduler() );
283          }
284
285          _active = false;
286          _dt_stop += dt;
287          _dt_play = 0.0;
288
289
290       } else
291          _stopping += dt;
292
293       return;
294
295    }
296
297    //
298    // If the mode is ONCE and the sound is still playing,
299    //  we have nothing to do anymore.
300    //
301    if (_active && (_mode == FGSound::ONCE)) {
302
303       if (!_sample->is_playing()) {
304          _dt_stop += dt;
305          _dt_play = 0.0;
306
307       } else
308          _dt_play += dt;
309
310       return;
311    }
312
313    //
314    // Update the playing time, cache the current value and
315    // clear the delay timer.
316    //
317    _dt_play += dt;
318    _prev_value = curr_value;
319    _stopping = 0.0;
320
321    //
322    // Update the volume
323    //
324    int i;
325    int max = _volume.size();
326    double volume = 1.0;
327    double volume_offset = 0.0;
328
329    for(i = 0; i < max; i++) {
330       double v = 1.0;
331
332       if (_volume[i].prop)
333          v = _volume[i].prop->getDoubleValue();
334
335       else if (_volume[i].intern)
336          v = *_volume[i].intern;
337
338       if (_volume[i].fn)
339          v = _volume[i].fn(v);
340
341       v *= _volume[i].factor;
342
343       if (_volume[i].max && (v > _volume[i].max))
344          v = _volume[i].max;
345
346       else if (v < _volume[i].min)
347          v = _volume[i].min;
348
349       if (_volume[i].subtract)                          // Hack!
350          volume = _volume[i].offset - v;
351
352       else {
353          volume_offset += _volume[i].offset;
354          volume *= v;
355       }
356    }
357
358    //
359    // Update the pitch
360    //
361    max = _pitch.size();
362    double pitch = 1.0;
363    double pitch_offset = 0.0;
364
365    for(i = 0; i < max; i++) {
366       double p = 1.0;
367
368       if (_pitch[i].prop)
369          p = _pitch[i].prop->getDoubleValue();
370
371       else if (_pitch[i].intern)
372          p = *_pitch[i].intern;
373
374       if (_pitch[i].fn)
375          p = _pitch[i].fn(p);
376
377       p *= _pitch[i].factor;
378
379       if (_pitch[i].max && (p > _pitch[i].max))
380          p = _pitch[i].max;
381
382       else if (p < _pitch[i].min)
383          p = _pitch[i].min;
384
385       if (_pitch[i].subtract)                           // Hack!
386          pitch = _pitch[i].offset - p;
387
388       else {
389          pitch_offset += _pitch[i].offset;
390          pitch *= p;
391       }
392    }
393
394    //
395    // Change sample state
396    //
397    _sample->set_pitch( pitch_offset + pitch );
398    _sample->set_volume( volume_offset + volume );
399
400
401    //
402    // Do we need to start playing the sample?
403    //
404    if (!_active) {
405
406       if (_mode == FGSound::ONCE)
407          _sample->play(_mgr->get_scheduler(), false);
408
409       else
410          _sample->play(_mgr->get_scheduler(), true);
411
412       SG_LOG(SG_GENERAL, SG_INFO, "Playing audio after " << _dt_stop 
413                                    << " sec: " << _name);
414       SG_LOG(SG_GENERAL, SG_BULK,
415                          "Playing " << ((_mode == ONCE) ? "once" : "looped"));
416
417       _active = true;
418       _dt_stop = 0.0;
419    }
420 }