1 // sound.cxx -- Sound class implementation
3 // Started by Erik Hofman, February 2002
4 // (Reuses some code from fg_fx.cxx created by David Megginson)
6 // Copyright (C) 2002 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
24 #include <simgear/compiler.h>
26 #ifdef SG_HAVE_STD_INCLUDES
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/props/condition.hxx>
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); }
60 // {"pow3", _snd_pow3},
80 _mgr->get_scheduler()->stopSample(_sample->get_sample());
94 Sound::init(SGPropertyNode *root, SGPropertyNode *node, SoundMgr *sndmgr,
99 // set global sound properties
102 _name = node->getStringValue("name", "");
103 SG_LOG(SG_GENERAL, SG_INFO, "Loading sound information for: " << _name );
105 const char *mode_str = node->getStringValue("mode", "");
106 if ( !strcmp(mode_str, "looped") ) {
107 _mode = Sound::LOOPED;
109 } else if ( !strcmp(mode_str, "in-transit") ) {
110 _mode = Sound::IN_TRANSIT;
115 if ( strcmp(mode_str, "") )
116 SG_LOG(SG_GENERAL,SG_INFO, " Unknown sound mode, default to 'once'");
119 _property = root->getNode(node->getStringValue("property", ""), true);
120 SGPropertyNode *condition = node->getChild("condition");
121 if (condition != NULL)
122 _condition = fgReadCondition(root, condition);
124 if (!_property && !_condition)
125 SG_LOG(SG_GENERAL, SG_WARN,
126 " Neither a condition nor a property specified");
129 // set volume properties
133 vector<SGPropertyNode_ptr> kids = node->getChildren("volume");
134 for (i = 0; (i < kids.size()) && (i < Sound::MAXPROP); i++) {
135 _snd_prop volume = {NULL, NULL, NULL, 1.0, 0.0, 0.0, 0.0, false};
137 if (strcmp(kids[i]->getStringValue("property"), ""))
138 volume.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
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;
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;
152 const char *type_str = kids[i]->getStringValue("type", "");
153 if ( strcmp(type_str, "") ) {
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;
162 SG_LOG(SG_GENERAL,SG_INFO,
163 " Unknown volume type, default to 'lin'");
166 volume.offset = kids[i]->getDoubleValue("offset", 0.0);
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.");
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.");
177 _volume.push_back(volume);
184 // set pitch properties
187 kids = node->getChildren("pitch");
188 for (i = 0; (i < kids.size()) && (i < Sound::MAXPROP); i++) {
189 _snd_prop pitch = {NULL, NULL, NULL, 1.0, 1.0, 0.0, 0.0, false};
191 if (strcmp(kids[i]->getStringValue("property", ""), ""))
192 pitch.prop = root->getNode(kids[i]->getStringValue("property", ""), true);
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;
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;
206 const char *type_str = kids[i]->getStringValue("type", "");
207 if ( strcmp(type_str, "") ) {
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;
216 SG_LOG(SG_GENERAL,SG_INFO,
217 " Unknown pitch type, default to 'lin'");
220 pitch.offset = kids[i]->getDoubleValue("offset", 1.0);
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.");
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");
231 _pitch.push_back(pitch);
236 // Initialize the sample
239 if ((_sample = _mgr->find(_name)) == NULL)
240 _sample = _mgr->add(_name, path.c_str(), node->getStringValue("path", ""));
242 _sample->set_volume(v);
243 _sample->set_pitch(p);
247 Sound::update (double dt)
249 double curr_value = 0.0;
252 // If the state changes to false, stop playing.
255 curr_value = _property->getDoubleValue();
257 if ( // Lisp, anyone?
258 (_condition && !_condition->test()) ||
259 (!_condition && _property &&
262 ( (_mode == Sound::IN_TRANSIT) && (curr_value == _prev_value) )
267 if ((_mode != Sound::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 );
272 _sample->stop( _mgr->get_scheduler() );
286 // If the mode is ONCE and the sound is still playing,
287 // we have nothing to do anymore.
289 if (_active && (_mode == Sound::ONCE)) {
291 if (!_sample->is_playing()) {
302 // Update the playing time, cache the current value and
303 // clear the delay timer.
306 _prev_value = curr_value;
313 int max = _volume.size();
315 double volume_offset = 0.0;
317 for(i = 0; i < max; i++) {
321 v = _volume[i].prop->getDoubleValue();
323 else if (_volume[i].intern)
324 v = *_volume[i].intern;
327 v = _volume[i].fn(v);
329 v *= _volume[i].factor;
331 if (_volume[i].max && (v > _volume[i].max))
334 else if (v < _volume[i].min)
337 if (_volume[i].subtract) // Hack!
338 volume = _volume[i].offset - v;
341 volume_offset += _volume[i].offset;
351 double pitch_offset = 0.0;
353 for(i = 0; i < max; i++) {
357 p = _pitch[i].prop->getDoubleValue();
359 else if (_pitch[i].intern)
360 p = *_pitch[i].intern;
365 p *= _pitch[i].factor;
367 if (_pitch[i].max && (p > _pitch[i].max))
370 else if (p < _pitch[i].min)
373 if (_pitch[i].subtract) // Hack!
374 pitch = _pitch[i].offset - p;
377 pitch_offset += _pitch[i].offset;
383 // Change sample state
385 _sample->set_pitch( pitch_offset + pitch );
386 _sample->set_volume( volume_offset + volume );
390 // Do we need to start playing the sample?
394 if (_mode == Sound::ONCE)
395 _sample->play(_mgr->get_scheduler(), false);
398 _sample->play(_mgr->get_scheduler(), true);
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"));