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