]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_environmentfx.cxx
First implementation of an environment audio fx module
[flightgear.git] / src / Sound / fg_environmentfx.cxx
1 // fg_environmentfx.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_environmentfx.hxx"
33
34 #include <Main/fg_props.hxx>
35 #include <Main/globals.hxx>
36
37 // #include <simgear/props/props.hxx>
38 // #include <simgear/props/props_io.hxx>
39 #include <simgear/misc/sg_path.hxx>
40 #include <simgear/sound/soundmgr_openal.hxx>
41
42
43 static std::string _refname = "EnvironmentFX";
44
45
46 FGEnvironmentFX::FGEnvironmentFX()
47 {
48     _enabled = fgGetNode("/sim/sound/environment/enabled", true);
49     _volume = fgGetNode("/sim/sound/environment/volume", true);
50     _smgr->add(this, _refname);
51 }
52
53 FGEnvironmentFX::~FGEnvironmentFX()
54 {
55 }
56
57 void FGEnvironmentFX::unbind()
58 {
59     if (_smgr) {
60         _smgr->remove(_refname);
61     }
62 }
63
64 void FGEnvironmentFX::init()
65 {
66     if (!_smgr) {
67         return;
68     }
69 }
70
71 void FGEnvironmentFX::reinit()
72 {
73     init();
74 }
75
76 void FGEnvironmentFX::update (double dt)
77 {
78     if (!_smgr) {
79         return;
80     }
81
82     if ( _enabled->getBoolValue() ) {
83         set_volume( _volume->getDoubleValue() );
84         resume();
85
86         SGSampleGroup::update(dt);
87     }
88     else {
89         suspend();
90     }
91 }
92
93 void FGEnvironmentFX::add(std::string& path_str, const std::string& refname)
94 {
95     if (!_smgr) {
96         return;
97     }
98
99     SGPath path = globals->resolve_resource_path(path_str);
100     if (path.isNull())
101     {
102         SG_LOG(SG_SOUND, SG_ALERT, "File not found: '" << path_str);
103         return;
104     }
105     SG_LOG(SG_SOUND, SG_INFO, "Reading sound from " << path.str());
106
107     SGSharedPtr<SGSoundSample> sample;
108     sample = new SGSoundSample("", path);
109     if (sample->file_path().exists()) {
110         SGSampleGroup::add( sample, refname );
111     }
112     else
113     {
114         throw sg_io_exception("Environment FX: couldn't find file: '" + path.str() + "'");
115         delete sample;
116     }
117 }
118
119 void FGEnvironmentFX::position(const std::string& refname, const SGVec3d& pos)
120 {
121     SGSoundSample* sample = SGSampleGroup::find(refname);
122     if (sample)
123     {
124         sample->set_position( pos );
125     }
126 }
127
128 void FGEnvironmentFX::pitch(const std::string& refname, float pitch)
129 {
130     SGSoundSample* sample = SGSampleGroup::find(refname);
131     if (sample)
132     {
133         sample->set_volume( pitch );
134     }
135 }
136
137 void FGEnvironmentFX::volume(const std::string& refname, float volume)
138 {
139     SGSoundSample* sample = SGSampleGroup::find(refname);
140     if (sample)
141     {
142         sample->set_volume( volume );
143     }
144 }
145
146 void FGEnvironmentFX::properties(const std::string& refname, float reference_dist, float max_dist )
147 {
148     SGSoundSample* sample = SGSampleGroup::find(refname);
149     if (sample)
150     {
151         sample->set_reference_dist( reference_dist );
152         if (max_dist > 0) {
153             sample->set_max_dist( max_dist );
154         }
155     }
156 }
157
158 // end of fg_environmentfx.cxx