1 // fg_sound.hxx -- 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
32 #include <simgear/debug/logstream.hxx>
34 #include <Main/fg_props.hxx>
36 #include "fg_sound.hxx"
38 FGSound::FGSound(const SGPropertyNode * node)
56 FGSoundMgr * mgr = globals->get_soundmgr();
57 vector<const SGPropertyNode *> kids;
62 _property = fgGetNode(_node->getStringValue("property"), true);
65 // seet sound global properties
67 _name = _node->getStringValue("name");
69 if ((_factor = _node->getFloatValue("factor")) == 0.0)
72 SG_LOG(SG_GENERAL, SG_INFO,
73 "Loading sound information for: " << _name );
75 if (_node->getStringValue("mode") == "looped") {
76 _mode = FGSound::LOOPED;
78 _mode = FGSound::ONCE;
79 if (_node->getStringValue("mode") != (string)"once")
80 SG_LOG( SG_GENERAL, SG_INFO, "Unknown sound mode, default to 'once'");
83 if (_node->getStringValue("type") == "flipflop") {
84 _type = FGSound::FLIPFLOP;
85 } else if (_node->getStringValue("type") == "inverted") {
86 _type = FGSound::INVERTED;
88 _type = FGSound::LEVEL;
89 if (_node->getStringValue("type") != (string)"level")
90 SG_LOG( SG_GENERAL, SG_INFO, "Unknown sound type, default to 'level'");
96 // set position properties
98 _pos.dist = _node->getFloatValue("dist");
99 _pos.hor = _node->getFloatValue("pos_hor");
100 _pos.vert = _node->getFloatValue("pos_vert");
104 // set volume properties
106 kids = _node->getChildren("volume");
107 for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
110 if ((volume.prop=fgGetNode(kids[i]->getStringValue("property"), true))
112 volume.prop = fgGetNode("/null", true);
114 if ((volume.factor = kids[i]->getFloatValue("factor")) == 0.0)
117 if (volume.factor < 0.0) {
118 volume.factor = -volume.factor;
119 volume.subtract = true;
121 volume.subtract = false;
123 if (kids[i]->getStringValue("type") == "log")
124 volume.type = FGSound::LOG;
126 else if (kids[i]->getStringValue("type") == "ln")
127 volume.type = FGSound::LN;
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'");
136 if ((volume.offset = kids[i]->getFloatValue("offset")) == 0.0)
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.");
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;
151 _volume.push_back(volume);
158 // set pitch properties
160 kids = _node->getChildren("pitch");
162 for (i = 0; (i < kids.size()) && (i < FGSound::MAXPROP); i++) {
165 if ((pitch.prop = fgGetNode(kids[i]->getStringValue("property"), true))
167 pitch.prop = fgGetNode("/null", true);
169 if ((pitch.factor = kids[i]->getFloatValue("factor")) == 0.0)
172 if (kids[i]->getStringValue("type") == "log")
173 pitch.type = FGSound::LOG;
175 else if (kids[i]->getStringValue("type") == "ln")
176 pitch.type = FGSound::LN;
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'");
185 if ((pitch.offset = kids[i]->getFloatValue("offset")) == 0.0)
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.");
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;
200 _pitch.push_back(pitch);
205 // Initialize the sample
207 _sample = new FGSimpleSound(_node->getStringValue("path"));
208 _sample->set_volume(v);
209 _sample->set_pitch(p);
211 if (!mgr->exists(_name))
212 mgr->add(_sample, _name);
226 FGSound::update (int dt)
229 FGSoundMgr * mgr = globals->get_soundmgr();
232 // Do we have something to do?
238 i = _property->getFloatValue() * _factor;
239 if (_type == FGSound::INVERTED)
242 if ((_type == FGSound::LEVEL) || (_type == FGSound::INVERTED)) {
245 if (mgr->is_playing(_name)) {
246 SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
253 if (_active && (_mode == FGSound::ONCE))
256 } else { // FGSound::FLIPFLOP
258 if ((bool)i == _active)
262 // Check for state changes.
263 // If the state changed, and the sound is still playing: stop playing.
265 if (mgr->is_playing(_name)) {
266 SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
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();
279 if (_volume[i].type == FGSound::LOG)
282 if (_volume[i].type == FGSound::LN)
285 v *= _volume[i].factor;
287 if (v > _volume[i].max)
290 if (v < _volume[i].min)
291 v = 0; // v = _volume[i].min;
293 if (_volume[i].subtract) // Hack!
294 volume = _volume[i].offset - v;
296 volume_offset += _volume[i].offset;
305 double pitch = 1.0, pitch_offset = 0.0;
306 for(i = 0; i < max; i++) {
307 double p = _pitch[i].prop->getDoubleValue();
309 if (_pitch[i].type == FGSound::LOG)
312 if (_pitch[i].type == FGSound::LN)
315 p *= _pitch[i].factor;
317 if (p > _pitch[i].max)
320 if (p < _pitch[i].min)
324 pitch_offset += _pitch[i].offset;
328 // Change sample state
330 _sample->set_pitch( pitch_offset + pitch );
331 _sample->set_volume( volume_offset + volume );
334 // Do we need to start playing the sample?
336 if ((!_active) || (_type == FGSound::FLIPFLOP)) {
338 // This is needed for FGSound::FLIPFLOP and it works for
339 // FGSound::LEVEl. Doing it this way saves an extra 'if'.
343 if (_mode == FGSound::ONCE)
344 mgr->play_once(_name);
346 mgr->play_looped(_name);
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);