]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
Merge branch 'attenuation' into navaids-radio
[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 string &refname, SGPropertyNode *props ) :
43     _props( props ),
44     _enabled( fgGetNode("/sim/sound/effects/enabled", true) ),
45     _volume( fgGetNode("/sim/sound/effects/volume", true) )
46 {
47     if (!props) _props = globals->get_props();
48
49     _avionics_enabled = _props->getNode("sim/sound/avionics/enabled", true);
50     _avionics_volume = _props->getNode("sim/sound/avionics/volume", true);
51     _avionics_ext = _props->getNode("sim/sound/avionics/external-view", true);
52     _internal = _props->getNode("sim/current-view/internal", true);
53
54     SGSampleGroup::_smgr = smgr;
55     SGSampleGroup::_refname = refname;
56     SGSampleGroup::_smgr->add(this, refname);
57
58     if (_avionics_enabled->getBoolValue())
59     {
60         _avionics = _smgr->find("avionics", true);
61         _avionics->tie_to_listener();
62     }
63 }
64
65
66 FGFX::~FGFX ()
67 {
68     for (unsigned int i = 0; i < _sound.size(); i++ ) {
69         delete _sound[i];
70     }
71     _sound.clear();
72 }
73
74
75 void
76 FGFX::init()
77 {
78     SGPropertyNode *node = _props->getNode("sim/sound", true);
79
80     string path_str = node->getStringValue("path");
81     if (path_str.empty()) {
82         SG_LOG(SG_SOUND, SG_ALERT, "No path in sim/sound/path");
83         return;
84     }
85     
86     SGPath path = globals->resolve_aircraft_path(path_str);
87     SG_LOG(SG_SOUND, SG_INFO, "Reading sound " << node->getName()
88            << " from " << path.str());
89
90     SGPropertyNode root;
91     try {
92         readProperties(path.str(), &root);
93     } catch (const sg_exception &) {
94         SG_LOG(SG_SOUND, SG_ALERT,
95                "Error reading file '" << path.str() << '\'');
96         return;
97     }
98
99     node = root.getNode("fx");
100     if(node) {
101         for (int i = 0; i < node->nChildren(); ++i) {
102             SGXmlSound *sound = new SGXmlSound();
103   
104             try {
105                 sound->init(globals->get_props(), node->getChild(i), this,
106                             _avionics, path.dir());
107   
108                 _sound.push_back(sound);
109             } catch ( sg_exception &e ) {
110                 SG_LOG(SG_SOUND, SG_ALERT, e.getFormattedMessage());
111                 delete sound;
112             }
113         }
114     }
115 }
116
117
118 void
119 FGFX::reinit()
120 {
121     for ( unsigned int i = 0; i < _sound.size(); i++ ) {
122         delete _sound[i];
123     }
124     _sound.clear();
125     init();
126 };
127
128
129 void
130 FGFX::update (double dt)
131 {
132     bool active = _avionics_ext->getBoolValue() ||
133                   _internal->getBoolValue();
134
135     if (_avionics_enabled->getBoolValue()) {
136         if (!_avionics) {
137             _avionics = _smgr->find("avionics", true);
138             _avionics->tie_to_listener();
139         }
140
141         if ( active )
142             _avionics->resume(); // no-op if already in resumed state
143         else
144             _avionics->suspend();
145         _avionics->set_volume( _avionics_volume->getFloatValue() );
146     }
147
148     if ( _enabled->getBoolValue() ) {
149         set_volume( _volume->getDoubleValue() );
150         resume();
151
152         // update sound effects if not paused
153         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
154             _sound[i]->update(dt);
155         }
156
157         SGSampleGroup::update(dt);
158     }
159     else
160         suspend();
161 }
162
163 // end of fg_fx.cxx