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