]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sound.cxx
Added "SG" prefixes to match other SimGear classes.
[simgear.git] / simgear / sound / sound.cxx
1 // sound.cxx -- 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 #include <simgear/props/condition.hxx>
35
36
37 #include "sound.hxx"
38
39
40 // static double _snd_lin(double v)   { return v; }
41 static double _snd_inv(double v)   { return (v == 0) ? 1e99 : 1/v; }
42 static double _snd_abs(double v)   { return (v >= 0) ? v : -v; }
43 static double _snd_sqrt(double v)  { return (v < 0) ? sqrt(-v) : sqrt(v); }
44 static double _snd_log10(double v) { return (v < 1) ? 0 : log10(v); }
45 static double _snd_log(double v)   { return (v < 1) ? 0 : log(v); }
46 // static double _snd_sqr(double v)   { return pow(v, 2); }
47 // static double _snd_pow3(double v)  { return pow(v, 3); }
48
49 static const struct {
50         char *name;
51         double (*fn)(double);
52 } __sound_fn[] = {
53 //      {"lin", _snd_lin},
54         {"inv", _snd_inv},
55         {"abs", _snd_abs},
56         {"sqrt", _snd_sqrt},
57         {"log", _snd_log10},
58         {"ln", _snd_log},
59 //      {"sqr", _snd_sqr},
60 //      {"pow3", _snd_pow3},
61         {"", NULL}
62 };
63
64 SGSound::SGSound()
65   : _sample(NULL),
66     _condition(NULL),
67     _property(NULL),
68     _active(false),
69     _name(""),
70     _mode(SGSound::ONCE),
71     _prev_value(0),
72     _dt_play(0.0),
73     _dt_stop(0.0),
74     _stopping(0.0)
75 {
76 }
77
78 SGSound::~SGSound()
79 {
80    _mgr->get_scheduler()->stopSample(_sample->get_sample());
81
82    if (_property)
83       delete _property;
84
85    if (_condition)
86       delete _condition;
87
88    _volume.clear();
89    _pitch.clear();
90    delete _sample;
91 }
92
93 void
94 SGSound::init(SGPropertyNode *root, SGPropertyNode *node, SGSoundMgr *sndmgr,
95               const string &path)
96 {
97
98    //
99    // set global sound properties
100    //
101    
102    _name = node->getStringValue("name", "");
103    SG_LOG(SG_GENERAL, SG_INFO, "Loading sound information for: " << _name );
104
105    const char *mode_str = node->getStringValue("mode", "");
106    if ( !strcmp(mode_str, "looped") ) {
107        _mode = SGSound::LOOPED;
108
109    } else if ( !strcmp(mode_str, "in-transit") ) {
110        _mode = SGSound::IN_TRANSIT;
111
112    } else {
113       _mode = SGSound::ONCE;
114
115       if ( strcmp(mode_str, "") )
116          SG_LOG(SG_GENERAL,SG_INFO, "  Unknown sound mode, default to 'once'");
117    }
118
119    _property = root->getNode(node->getStringValue("property", ""), true);
120    SGPropertyNode *condition = node->getChild("condition");
121    if (condition != NULL)
122       _condition = fgReadCondition(root, condition);
123
124    if (!_property && !_condition)
125       SG_LOG(SG_GENERAL, SG_WARN,
126              "  Neither a condition nor a property specified");
127
128    //
129    // set volume properties
130    //
131    unsigned int i;
132    float v = 0.0;
133    vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
134    for (i = 0; (i < kids.size()) && (i < SGSound::MAXPROP); i++) {
135       _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
136
137       if (strcmp(kids[i]->getStringValue("property"), ""))
138          volume.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
139
140       const char *intern_str = kids[i]->getStringValue("internal", "");
141       if (!strcmp(intern_str, "dt_play"))
142          volume.intern = &_dt_play;
143       else if (!strcmp(intern_str, "dt_stop"))
144          volume.intern = &_dt_stop;
145
146       if ((volume.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
147          if (volume.factor < 0.0) {
148             volume.factor = -volume.factor;
149             volume.subtract = true;
150          }
151
152       const char *type_str = kids[i]->getStringValue("type", "");
153       if ( strcmp(type_str, "") ) {
154
155          for (int j=0; __sound_fn[j].fn; j++)
156            if ( !strcmp(type_str, __sound_fn[j].name) ) {
157                volume.fn = __sound_fn[j].fn;
158                break;
159             }
160
161          if (!volume.fn)
162             SG_LOG(SG_GENERAL,SG_INFO,
163                    "  Unknown volume type, default to 'lin'");
164       }
165
166       volume.offset = kids[i]->getDoubleValue("offset", 0.0);
167
168       if ((volume.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
169          SG_LOG( SG_GENERAL, SG_WARN,
170           "Volume minimum value below 0. Forced to 0.");
171
172       volume.max = kids[i]->getDoubleValue("max", 0.0);
173       if (volume.max && (volume.max < volume.min) )
174          SG_LOG(SG_GENERAL,SG_ALERT,
175                 "  Volume maximum below minimum. Neglected.");
176
177       _volume.push_back(volume);
178       v += volume.offset;
179
180    }
181
182
183    //
184    // set pitch properties
185    //
186    float p = 0.0;
187    kids = node->getChildren("pitch");
188    for (i = 0; (i < kids.size()) && (i < SGSound::MAXPROP); i++) {
189       _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
190
191       if (strcmp(kids[i]->getStringValue("property", ""), ""))
192          pitch.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
193
194       const char *intern_str = kids[i]->getStringValue("internal", "");
195       if (!strcmp(intern_str, "dt_play"))
196          pitch.intern = &_dt_play;
197       else if (!strcmp(intern_str, "dt_stop"))
198          pitch.intern = &_dt_stop;
199
200       if ((pitch.factor = kids[i]->getDoubleValue("factor", 1.0)) != 0.0)
201          if (pitch.factor < 0.0) {
202             pitch.factor = -pitch.factor;
203             pitch.subtract = true;
204          }
205
206       const char *type_str = kids[i]->getStringValue("type", "");
207       if ( strcmp(type_str, "") ) {
208
209          for (int j=0; __sound_fn[j].fn; j++) 
210             if ( !strcmp(type_str, __sound_fn[j].name) ) {
211                pitch.fn = __sound_fn[j].fn;
212                break;
213             }
214
215          if (!pitch.fn)
216             SG_LOG(SG_GENERAL,SG_INFO,
217                    "  Unknown pitch type, default to 'lin'");
218       }
219      
220       pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
221
222       if ((pitch.min = kids[i]->getDoubleValue("min", 0.0)) < 0.0)
223          SG_LOG(SG_GENERAL,SG_WARN,
224                 "  Pitch minimum value below 0. Forced to 0.");
225
226       pitch.max = kids[i]->getDoubleValue("max", 0.0);
227       if (pitch.max && (pitch.max < pitch.min) )
228          SG_LOG(SG_GENERAL,SG_ALERT,
229                 "  Pitch maximum below minimum. Neglected");
230
231       _pitch.push_back(pitch);
232       p += pitch.offset;
233    }
234
235    //
236    // Initialize the sample
237    //
238    _mgr = sndmgr;
239    if ((_sample = _mgr->find(_name)) == NULL)
240       _sample = _mgr->add(_name, path.c_str(), node->getStringValue("path", ""));
241
242    _sample->set_volume(v);
243    _sample->set_pitch(p);
244 }
245
246 void
247 SGSound::update (double dt)
248 {
249    double curr_value = 0.0;
250
251    //
252    // If the state changes to false, stop playing.
253    //
254    if (_property)
255        curr_value = _property->getDoubleValue();
256
257    if (                                                 // Lisp, anyone?
258        (_condition && !_condition->test()) ||
259        (!_condition && _property &&
260         (
261          !curr_value ||
262          ( (_mode == SGSound::IN_TRANSIT) && (curr_value == _prev_value) )
263          )
264         )
265        )
266    {
267        if ((_mode != SGSound::IN_TRANSIT) || (_stopping > MAX_TRANSIT_TIME)) {
268            if (_sample->is_playing()) {
269                SG_LOG(SG_GENERAL, SG_INFO, "Stopping audio after " << _dt_play
270                       << " sec: " << _name );
271
272                _sample->stop( _mgr->get_scheduler() );
273            }
274
275            _active = false;
276            _dt_stop += dt;
277            _dt_play = 0.0;
278        } else {
279            _stopping += dt;
280        }
281
282        return;
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 == SGSound::ONCE)) {
290
291       if (!_sample->is_playing()) {
292          _dt_stop += dt;
293          _dt_play = 0.0;
294       } else {
295          _dt_play += dt;
296       }
297
298       return;
299    }
300
301    //
302    // Update the playing time, cache the current value and
303    // clear the delay timer.
304    //
305    _dt_play += dt;
306    _prev_value = curr_value;
307    _stopping = 0.0;
308
309    //
310    // Update the volume
311    //
312    int i;
313    int max = _volume.size();
314    double volume = 1.0;
315    double volume_offset = 0.0;
316
317    for(i = 0; i < max; i++) {
318       double v = 1.0;
319
320       if (_volume[i].prop)
321          v = _volume[i].prop->getDoubleValue();
322
323       else if (_volume[i].intern)
324          v = *_volume[i].intern;
325
326       if (_volume[i].fn)
327          v = _volume[i].fn(v);
328
329       v *= _volume[i].factor;
330
331       if (_volume[i].max && (v > _volume[i].max))
332          v = _volume[i].max;
333
334       else if (v < _volume[i].min)
335          v = _volume[i].min;
336
337       if (_volume[i].subtract)                          // Hack!
338          volume = _volume[i].offset - v;
339
340       else {
341          volume_offset += _volume[i].offset;
342          volume *= v;
343       }
344    }
345
346    //
347    // Update the pitch
348    //
349    max = _pitch.size();
350    double pitch = 1.0;
351    double pitch_offset = 0.0;
352
353    for(i = 0; i < max; i++) {
354       double p = 1.0;
355
356       if (_pitch[i].prop)
357          p = _pitch[i].prop->getDoubleValue();
358
359       else if (_pitch[i].intern)
360          p = *_pitch[i].intern;
361
362       if (_pitch[i].fn)
363          p = _pitch[i].fn(p);
364
365       p *= _pitch[i].factor;
366
367       if (_pitch[i].max && (p > _pitch[i].max))
368          p = _pitch[i].max;
369
370       else if (p < _pitch[i].min)
371          p = _pitch[i].min;
372
373       if (_pitch[i].subtract)                           // Hack!
374          pitch = _pitch[i].offset - p;
375
376       else {
377          pitch_offset += _pitch[i].offset;
378          pitch *= p;
379       }
380    }
381
382    //
383    // Change sample state
384    //
385    _sample->set_pitch( pitch_offset + pitch );
386    _sample->set_volume( volume_offset + volume );
387
388
389    //
390    // Do we need to start playing the sample?
391    //
392    if (!_active) {
393
394       if (_mode == SGSound::ONCE)
395          _sample->play(_mgr->get_scheduler(), false);
396
397       else
398          _sample->play(_mgr->get_scheduler(), true);
399
400       SG_LOG(SG_GENERAL, SG_INFO, "Playing audio after " << _dt_stop 
401                                    << " sec: " << _name);
402       SG_LOG(SG_GENERAL, SG_BULK,
403                          "Playing " << ((_mode == ONCE) ? "once" : "looped"));
404
405       _active = true;
406       _dt_stop = 0.0;
407    }
408 }