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