]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_sound.cxx
626a2ce2bf3dacb98c5dd14c460890917cde03c5
[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 STL_STRING
32
33 #include <simgear/debug/logstream.hxx>
34
35 #include <Main/fg_props.hxx>
36
37 #include "fg_sound.hxx"
38
39 SG_USING_STD(string);
40
41
42 // static double _fg_lin(double v)   { return v; };
43 static double _fg_inv(double v)   { return (v == 0) ? 1e99 : 1/v; };
44 static double _fg_abs(double v)   { return (v >= 0) ? v : -v; };
45 static double _fg_sqrt(double v)  { return (v < 0) ? sqrt(-v) : sqrt(v); };
46 static double _fg_log10(double v) { return (v < 1) ? 0 : log10(v+1); };
47 static double _fg_log(double v)   { return (v < 1) ? 0 : log(v+1); };
48 // static double _fg_sqr(double v)   { return pow(v, 2); };
49 // static double _fg_pow3(double v)  { return pow(v, 3); };
50
51 static const struct {
52         char *name;
53         double (*fn)(double);
54 } __fg_snd_fn[] = {
55 //      {"lin", _fg_lin},
56         {"inv", _fg_inv},
57         {"abs", _fg_abs},
58         {"sqrt", _fg_sqrt},
59         {"log", _fg_log10},
60         {"ln", _fg_log},
61 //      {"sqr", _fg_sqr},
62 //      {"pow3", _fg_pow3},
63         {"", NULL}
64 };
65
66 FGSound::FGSound(const SGPropertyNode * node)
67   : _node(node),
68     _sample(NULL),
69     _active(false),
70     _mode(FGSound::ONCE),
71     _type(FGSound::LEVEL),
72     _name(""),  
73     _factor(1.0)
74 {
75 }
76
77 FGSound::~FGSound()
78 {
79    delete _sample;
80 }
81
82 void
83 FGSound::init()
84 {
85
86    _property = fgGetNode(_node->getStringValue("property"), true);
87
88    //
89    // seet sound global properties
90    //
91    _name = _node->getStringValue("name");
92
93    if ((_factor = _node->getFloatValue("factor")) == 0.0)
94       _factor = 1.0;
95
96    if ((_offset = _node->getFloatValue("offset")) == 0.0)
97       _offset = 0.0;
98
99    SG_LOG(SG_GENERAL, SG_INFO,
100     "Loading sound information for: " << _name );
101
102    string mode_str = _node->getStringValue("mode");
103    if (mode_str == "looped") {
104        _mode = FGSound::LOOPED;
105    } else {
106       _mode = FGSound::ONCE;
107       if (mode_str != (string)"once")
108         SG_LOG( SG_GENERAL, SG_INFO, "Unknown sound mode, default to 'once'");
109    }
110
111    string type_str = _node->getStringValue("type");
112    if (type_str == "flipflop") {
113       _type = FGSound::FLIPFLOP;
114    } else if (type_str== "inverted") {
115       _type = FGSound::INVERTED;
116    } else if (type_str == "raise") {
117       _type = FGSound::RAISE;
118    } else if (type_str == "fall") {
119       _type = FGSound::FALL;
120    } else {
121       _type = FGSound::LEVEL;
122       if (type_str != (string)"level")
123         SG_LOG( SG_GENERAL, SG_INFO, "Unknown sound type, default to 'level'");
124    }
125
126 #if 0
127    //
128    // set position properties
129    //
130    _pos.dist = _node->getFloatValue("dist");
131    _pos.hor = _node->getFloatValue("pos_hor");
132    _pos.vert = _node->getFloatValue("pos_vert"); 
133 #endif
134
135    //
136    // set volume properties
137    //
138    unsigned int i;
139    float v = 0.0;
140    vector<const SGPropertyNode *> kids = _node->getChildren("volume");
141    for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
142       _snd_prop volume;
143
144       if ((volume.prop=fgGetNode(kids[i]->getStringValue("property"), true))
145           == 0)
146          volume.prop = fgGetNode("/null", true);
147
148       if ((volume.factor = kids[i]->getFloatValue("factor")) == 0.0)
149          volume.factor = 1.0;
150       else 
151          if (volume.factor < 0.0) {
152             volume.factor = -volume.factor;
153             volume.subtract = true;
154          } else
155             volume.subtract = false;
156
157       volume.fn = NULL;
158       for (int j=0; __fg_snd_fn[j].fn; j++)
159          if (__fg_snd_fn[j].name == kids[i]->getStringValue("type")) {
160             volume.fn = __fg_snd_fn[j].fn;
161             break;
162       }
163
164       if (!volume.fn)
165          SG_LOG( SG_GENERAL, SG_INFO, "Unknown volume type, default to 'lin'");
166
167       if ((volume.offset = kids[i]->getFloatValue("offset")) == 0.0)
168          volume.offset = 0.0;
169
170       if ((volume.min = kids[i]->getFloatValue("min")) < 0.0) {
171          SG_LOG( SG_GENERAL, SG_WARN,
172           "Volume minimum value below 0. Forced to 0.");
173          volume.min = 0.0;
174       }
175
176       if ((volume.max = kids[i]->getFloatValue("max")) <= volume.min) {
177          SG_LOG( SG_GENERAL, SG_ALERT,
178           "Volume maximum value below minimum value. Forced above minimum.");
179         volume.max = volume.min + 5.0;
180       }
181
182       _volume.push_back(volume);
183       v += volume.offset;
184
185    }
186
187
188    //
189    // set pitch properties
190    //
191    float p = 0.0;
192    kids = _node->getChildren("pitch");
193    for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
194       _snd_prop pitch;
195
196       if ((pitch.prop = fgGetNode(kids[i]->getStringValue("property"), true))
197           == 0)
198          pitch.prop = fgGetNode("/null", true);
199
200       if ((pitch.factor = kids[i]->getFloatValue("factor")) == 0.0)
201          pitch.factor = 1.0;
202
203       pitch.fn = NULL;
204       for (int j=0; __fg_snd_fn[j].fn; j++) 
205          if(__fg_snd_fn[j].name == kids[i]->getStringValue("type")) {
206             pitch.fn = __fg_snd_fn[j].fn;
207             break;
208       }
209
210       if (!pitch.fn)
211          SG_LOG( SG_GENERAL, SG_INFO, "Unknown pitch type, default to 'lin'");
212      
213       if ((pitch.offset = kids[i]->getFloatValue("offset")) == 0.0)
214          pitch.offset = 1.0;
215
216       if ((pitch.min = kids[i]->getFloatValue("min")) < 0.0) {
217          SG_LOG( SG_GENERAL, SG_WARN,
218               "Pitch minimum value below 0. Forced to 0.");
219          pitch.min = 0.0;
220       }
221
222       if ((pitch.max = kids[i]->getFloatValue("max")) <= pitch.min) {
223          SG_LOG( SG_GENERAL, SG_ALERT,
224               "Pitch maximum value below minimum value. Forced above minimum.");
225          pitch.max = pitch.min + 5.0;
226       }
227
228       _pitch.push_back(pitch);
229       p += pitch.offset;
230    }
231
232    //
233    // Initialize the sample
234    //
235    FGSoundMgr * 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 (int dt)
255 {
256    FGSoundMgr * mgr = globals->get_soundmgr();
257
258    //
259    // Do we have something to do?
260    //
261
262    // if (!_property)
263    //   return;
264
265    if ((_type == FGSound::LEVEL)  || (_type == FGSound::INVERTED)) {
266
267       bool check = (int)(_offset + _property->getFloatValue() * _factor);
268       if (_type == FGSound::INVERTED)
269          check = !check;
270
271       //
272       // If the state changes to false, stop playing.
273       //
274       if (!check) {
275          if (_active) {
276             SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
277             _sample->stop( mgr->get_scheduler(), false );
278             _active = false;
279          }
280
281          return;
282       }
283
284       //
285       // If the sound is already playing we have nothing to do.
286       //
287       if (_active && (_mode == FGSound::ONCE))
288          return;
289
290    } else {             // FLIPFLOP, RAISE, FALL
291
292       bool check = (int)(_offset + _property->getFloatValue() * _factor);
293       if (check == _active)
294             return;
295
296       //
297       // Check for state changes.
298       // If the state changed, and the sound is still playing: stop playing.
299       //
300       if (_sample->is_playing()) {
301          SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
302          _sample->stop( mgr->get_scheduler() );
303       }
304
305       if ( ((_type == FGSound::RAISE) && !check) ||
306            ((_type == FGSound::FALL) && check) )
307          return;
308
309    }
310
311       //
312       // Update the volume
313       //
314       int max = _volume.size();
315
316       int i;
317       double volume = 1.0, volume_offset = 0.0;
318       for(i = 0; i < max; i++) {
319          double v = _volume[i].prop->getDoubleValue();
320
321          if (_volume[i].fn)
322             v = _volume[i].fn(v);
323
324          v *= _volume[i].factor;
325
326          if (v > _volume[i].max)
327             v = _volume[i].max;
328          else
329             if (v < _volume[i].min) 
330                v = 0;  // v = _volume[i].min;
331
332          if (_volume[i].subtract)                               // Hack!
333             volume = _volume[i].offset - v;
334          else {
335             volume_offset += _volume[i].offset;
336             volume *= v;
337          }
338       }
339
340       //
341       // Update the pitch
342       //
343       max = _pitch.size();
344       double pitch = 1.0, pitch_offset = 0.0;
345       for(i = 0; i < max; i++) {
346          double p = _pitch[i].prop->getDoubleValue();
347
348          if (_pitch[i].fn)
349             p = _pitch[i].fn(p);
350
351          p *= _pitch[i].factor;
352
353          if (p > _pitch[i].max)
354             p = _pitch[i].max;
355          else
356             if (p < _pitch[i].min) 
357                p = _pitch[i].min;
358
359          if (_pitch[i].subtract)                                // Hack!
360             pitch = _pitch[i].offset - p;
361          else {
362             pitch_offset += _pitch[i].offset;
363             pitch *= p;
364          }
365       }
366
367       //
368       // Change sample state
369       //
370       _sample->set_pitch( pitch_offset + pitch );
371       _sample->set_volume( volume_offset + volume );
372
373    //
374    // Do we need to start playing the sample?
375    //
376    if (_active && ((_type == FGSound::LEVEL) || (_type == FGSound::INVERTED)))
377          return;
378
379    //
380    // This is needed for FGSound::FLIPFLOP and it works for 
381    // FGSound::LEVEl. Doing it this way saves an extra 'if'.
382    //
383    _active = !_active;
384
385    if (_mode == FGSound::ONCE)
386       _sample->play(mgr->get_scheduler(), false);
387    else
388       _sample->play(mgr->get_scheduler(), true);
389
390    SG_LOG(SG_GENERAL, SG_INFO, "Starting audio playback for: " << _name);
391    SG_LOG(SG_GENERAL, SG_BULK,
392     "Playing " << ((_mode == ONCE) ? "once" : "looped"));
393 }