]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
be9378c73d19441d60eb5acd876b3d0ce3c3391e
[flightgear.git] / src / Sound / fg_fx.cxx
1 // fg_fx.cxx -- Sound effect management class implementation
2 //
3 // Started by David Megginson, October 2001
4 // (Reuses some code from main.cxx, probably by Curtis Olson)
5 //
6 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifdef _MSC_VER
25 #pragma warning (disable: 4786)
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include "fg_fx.hxx"
33
34 #include <Main/fg_props.hxx>
35
36 #include <simgear/props/props.hxx>
37 #include <simgear/props/props_io.hxx>
38 #include <simgear/misc/sg_path.hxx>
39 #include <simgear/sound/soundmgr_openal.hxx>
40 #include <simgear/sound/xmlsound.hxx>
41
42 FGFX::FGFX ( SGSoundMgr *smgr, const std::string &refname, SGPropertyNode *props ) :
43     _props( props )
44 {
45     if (!props) {
46         _is_aimodel = false;
47         _props = globals->get_props();
48         _enabled = fgGetNode("/sim/sound/effects/enabled", true);
49         _volume = fgGetNode("/sim/sound/effects/volume", true);
50     } else {
51         _is_aimodel = true;
52         _enabled = _props->getNode("/sim/sound/aimodels/enabled", true);
53         _enabled->setBoolValue(fgGetBool("/sim/sound/effects/enabled"));
54          _volume = _props->getNode("/sim/sound/aimodels/volume", true);
55         _volume->setFloatValue(fgGetFloat("/sim/sound/effects/volume"));
56     }
57
58     _avionics_enabled = _props->getNode("sim/sound/avionics/enabled", true);
59     _avionics_volume = _props->getNode("sim/sound/avionics/volume", true);
60     _avionics_ext = _props->getNode("sim/sound/avionics/external-view", true);
61     _internal = _props->getNode("sim/current-view/internal", true);
62
63     SGSampleGroup::_smgr = smgr;
64     SGSampleGroup::_refname = refname;
65     SGSampleGroup::_smgr->add(this, refname);
66
67     if (!_is_aimodel)
68     {
69         _avionics = _smgr->find("avionics", true);
70         _avionics->tie_to_listener();
71     }
72 }
73
74
75 FGFX::~FGFX ()
76 {
77     for (unsigned int i = 0; i < _sound.size(); i++ ) {
78         delete _sound[i];
79     }
80     _sound.clear();
81 }
82
83
84 void
85 FGFX::init()
86 {
87     SGPropertyNode *node = _props->getNode("sim/sound", true);
88
89     std::string path_str = node->getStringValue("path");
90     if (path_str.empty()) {
91         SG_LOG(SG_SOUND, SG_ALERT, "No path in sim/sound/path");
92         return;
93     }
94     
95     SGPath path = globals->resolve_aircraft_path(path_str);
96     if (path.isNull())
97     {
98         SG_LOG(SG_SOUND, SG_ALERT,
99                "File not found: '" << path_str);
100         return;
101     }
102     SG_LOG(SG_SOUND, SG_INFO, "Reading sound " << node->getName()
103            << " from " << path.str());
104
105     SGPropertyNode root;
106     try {
107         readProperties(path.str(), &root);
108     } catch (const sg_exception &) {
109         SG_LOG(SG_SOUND, SG_ALERT,
110                "Error reading file '" << path.str() << '\'');
111         return;
112     }
113
114     node = root.getNode("fx");
115     if(node) {
116         for (int i = 0; i < node->nChildren(); ++i) {
117             SGXmlSound *soundfx = new SGXmlSound();
118   
119             try {
120                 soundfx->init( _props, node->getChild(i), this, _avionics,
121                                path.dir() );
122                 _sound.push_back( soundfx );
123             } catch ( sg_exception &e ) {
124                 SG_LOG(SG_SOUND, SG_ALERT, e.getFormattedMessage());
125                 delete soundfx;
126             }
127         }
128     }
129 }
130
131
132 void
133 FGFX::reinit()
134 {
135     for ( unsigned int i = 0; i < _sound.size(); i++ ) {
136         delete _sound[i];
137     }
138     _sound.clear();
139     init();
140 };
141
142
143 void
144 FGFX::update (double dt)
145 {
146     if ( _enabled->getBoolValue() ) {
147         if ( _avionics_enabled->getBoolValue())
148         {
149             if (_avionics_ext->getBoolValue() || _internal->getBoolValue()) {
150                 // avionics sound is enabled
151                 _avionics->resume(); // no-op if already in resumed state
152                 _avionics->set_volume( _avionics_volume->getFloatValue() );
153             }
154             else
155                 _avionics->suspend();
156         }
157
158         set_volume( _volume->getDoubleValue() );
159         resume();
160
161         // update sound effects if not paused
162         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
163             _sound[i]->update(dt);
164         }
165
166         SGSampleGroup::update(dt);
167     }
168     else
169         suspend();
170 }
171
172 // end of fg_fx.cxx