]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_sound.cxx
Oops, make sure we are generating an ocean tile if we can't load a terrain
[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         string 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    if (_node->getStringValue("mode") == "looped") {
103        _mode = FGSound::LOOPED;
104    } else {
105       _mode = FGSound::ONCE;
106       if (_node->getStringValue("mode") != (string)"once")
107         SG_LOG( SG_GENERAL, SG_INFO, "Unknown sound mode, default to 'once'");
108    }
109
110    if (_node->getStringValue("type") == "flipflop") {
111       _type = FGSound::FLIPFLOP;
112
113    } else if (_node->getStringValue("type") == "inverted") {
114       _type = FGSound::INVERTED;
115
116    } else if (_node->getStringValue("type") == "raise") {
117       _type = FGSound::RAISE;
118
119    } else if (_node->getStringValue("type") == "fall") {
120       _type = FGSound::FALL;
121
122    } else {
123       _type = FGSound::LEVEL;
124       if (_node->getStringValue("type") != (string)"level")
125         SG_LOG( SG_GENERAL, SG_INFO, "Unknown sound type, default to 'level'");
126    }
127
128
129 #if 0
130    //
131    // set position properties
132    //
133    _pos.dist = _node->getFloatValue("dist");
134    _pos.hor = _node->getFloatValue("pos_hor");
135    _pos.vert = _node->getFloatValue("pos_vert"); 
136 #endif
137
138    //
139    // set volume properties
140    //
141    unsigned int i;
142    float v = 0.0;
143    vector<const SGPropertyNode *> kids = _node->getChildren("volume");
144    for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
145       _snd_prop volume;
146
147       if ((volume.prop=fgGetNode(kids[i]->getStringValue("property"), true))
148           == 0)
149          volume.prop = fgGetNode("/null", true);
150
151       if ((volume.factor = kids[i]->getFloatValue("factor")) == 0.0)
152          volume.factor = 1.0;
153       else 
154          if (volume.factor < 0.0) {
155             volume.factor = -volume.factor;
156             volume.subtract = true;
157          } else
158             volume.subtract = false;
159
160       volume.fn = NULL;
161       for (int j=0; __fg_snd_fn[j].fn; j++)
162          if (__fg_snd_fn[j].name == kids[i]->getStringValue("type")) {
163             volume.fn = __fg_snd_fn[j].fn;
164             break;
165       }
166
167       if (!volume.fn)
168          SG_LOG( SG_GENERAL, SG_INFO, "Unknown volume type, default to 'lin'");
169
170       if ((volume.offset = kids[i]->getFloatValue("offset")) == 0.0)
171          volume.offset = 0.0;
172
173       if ((volume.min = kids[i]->getFloatValue("min")) < 0.0) {
174          SG_LOG( SG_GENERAL, SG_WARN,
175           "Volume minimum value below 0. Forced to 0.");
176          volume.min = 0.0;
177       }
178
179       if ((volume.max = kids[i]->getFloatValue("max")) <= volume.min) {
180          SG_LOG( SG_GENERAL, SG_ALERT,
181           "Volume maximum value below minimum value. Forced above minimum.");
182         volume.max = volume.min + 5.0;
183       }
184
185       _volume.push_back(volume);
186       v += volume.offset;
187
188    }
189
190
191    //
192    // set pitch properties
193    //
194    float p = 0.0;
195    kids = _node->getChildren("pitch");
196    for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
197       _snd_prop pitch;
198
199       if ((pitch.prop = fgGetNode(kids[i]->getStringValue("property"), true))
200           == 0)
201          pitch.prop = fgGetNode("/null", true);
202
203       if ((pitch.factor = kids[i]->getFloatValue("factor")) == 0.0)
204          pitch.factor = 1.0;
205
206       pitch.fn = NULL;
207       for (int j=0; __fg_snd_fn[j].fn; j++) 
208          if(__fg_snd_fn[j].name == kids[i]->getStringValue("type")) {
209             pitch.fn = __fg_snd_fn[j].fn;
210             break;
211       }
212
213       if (!pitch.fn)
214          SG_LOG( SG_GENERAL, SG_INFO, "Unknown pitch type, default to 'lin'");
215      
216       if ((pitch.offset = kids[i]->getFloatValue("offset")) == 0.0)
217          pitch.offset = 1.0;
218
219       if ((pitch.min = kids[i]->getFloatValue("min")) < 0.0) {
220          SG_LOG( SG_GENERAL, SG_WARN,
221               "Pitch minimum value below 0. Forced to 0.");
222          pitch.min = 0.0;
223       }
224
225       if ((pitch.max = kids[i]->getFloatValue("max")) <= pitch.min) {
226          SG_LOG( SG_GENERAL, SG_ALERT,
227               "Pitch maximum value below minimum value. Forced above minimum.");
228          pitch.max = pitch.min + 5.0;
229       }
230
231       _pitch.push_back(pitch);
232       p += pitch.offset;
233    }
234
235    //
236    // Initialize the sample
237    //
238    FGSoundMgr * mgr = globals->get_soundmgr();
239    if (mgr->find(_name) == NULL) {
240       _sample = mgr->add(_name, _node->getStringValue("path"));
241       _sample->set_volume(v);
242       _sample->set_pitch(p);
243
244    } else {
245       _sample = mgr->find(_name);
246       _sample->set_volume(_sample->get_volume() + v);
247       _sample->set_pitch(_sample->get_pitch() + p);
248    }
249 }
250
251 void
252 FGSound::bind ()
253 {
254 }
255
256 void
257 FGSound::unbind ()
258 {
259 }
260
261 void
262 FGSound::update (int dt)
263 {
264    FGSoundMgr * mgr = globals->get_soundmgr();
265
266    //
267    // Do we have something to do?
268    //
269
270    // if (!_property)
271    //   return;
272
273    if ((_type == FGSound::LEVEL)  || (_type == FGSound::INVERTED)) {
274
275       bool check = (int)(_offset + _property->getFloatValue() * _factor);
276       if (_type == FGSound::INVERTED)
277          check = !check;
278
279       //
280       // If the state changes to false, stop playing.
281       //
282       if (check == 0) {
283          _active = false;
284          if (mgr->is_playing(_name)) {
285             SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
286             mgr->stop(_name);
287          }
288
289          return;
290       }
291
292       //
293       // If the sound is already playing we have nothing to do.
294       //
295       if (_active && (_mode == FGSound::ONCE))
296          return;
297
298    } else {             // FLIPFLOP, RAISE, FALL
299
300       bool check = (int)(_offset + _property->getFloatValue() * _factor);
301       if ((bool)check == _active)
302             return;
303
304       //
305       // Check for state changes.
306       // If the state changed, and the sound is still playing: stop playing.
307       //
308       if (mgr->is_playing(_name)) {
309          SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
310          mgr->stop(_name);
311       }
312
313       if ( ((_type == FGSound::RAISE) && !check) ||
314            ((_type == FGSound::FALL) && check) )
315          return;
316    }
317
318    //
319    // Update the volume
320    //
321    int max = _volume.size();
322
323    int i;
324    double volume = 1.0, volume_offset = 0.0;
325    for(i = 0; i < max; i++) {
326       double v = _volume[i].prop->getDoubleValue();
327
328       if (_volume[i].fn)
329          v = _volume[i].fn(v);
330
331       v *= _volume[i].factor;
332
333       if (v > _volume[i].max)
334          v = _volume[i].max;
335       else
336          if (v < _volume[i].min) 
337             v = 0;  // v = _volume[i].min;
338
339       if (_volume[i].subtract)                          // Hack!
340          volume = _volume[i].offset - v;
341       else {
342          volume_offset += _volume[i].offset;
343          volume *= v;
344       }
345    }
346
347    //
348    // Update the pitch
349    //
350    max = _pitch.size();
351    double pitch = 1.0, pitch_offset = 0.0;
352    for(i = 0; i < max; i++) {
353       double p = _pitch[i].prop->getDoubleValue();
354
355       if (_pitch[i].fn)
356          p = _pitch[i].fn(p);
357
358       p *= _pitch[i].factor;
359
360       if (p > _pitch[i].max)
361          p = _pitch[i].max;
362       else
363          if (p < _pitch[i].min) 
364             p = _pitch[i].min;
365
366       if (_pitch[i].subtract)                           // Hack!
367          pitch = _pitch[i].offset - p;
368       else {
369          pitch_offset += _pitch[i].offset;
370          pitch *= p;
371       }
372    }
373
374    //
375    // Change sample state
376    //
377    _sample->set_pitch( pitch_offset + pitch );
378    _sample->set_volume( volume_offset + volume );
379
380    //
381    // Do we need to start playing the sample?
382    //
383    if (_active && ((_type == FGSound::LEVEL) || (_type == FGSound::INVERTED)))
384       return;
385
386    //
387    // This is needed for FGSound::FLIPFLOP and it works for 
388    // FGSound::LEVEl. Doing it this way saves an extra 'if'.
389    //
390    _active = !_active;
391
392    if (_mode == FGSound::ONCE)
393       mgr->play_once(_name);
394    else
395       mgr->play_looped(_name);
396
397    SG_LOG(SG_GENERAL, SG_INFO, "Starting audio playback for: " << _name);
398    SG_LOG(SG_GENERAL, SG_BULK,
399     "Playing " << ((_mode == ONCE) ? "once" : "looped"));
400    SG_LOG(SG_GENERAL, SG_BULK, "Initial volume: " << volume_offset);
401    SG_LOG(SG_GENERAL, SG_BULK, "Initial pitch: " << pitch_offset);
402 }