1 // fgfx.cxx -- Sound effect management class implementation
3 // Started by David Megginson, October 2001
4 // (Reuses some code from main.cxx, probably by Curtis Olson)
6 // Copyright (C) 2001 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.
25 #include <Main/fg_props.hxx>
27 // FIXME: remove direct dependencies
28 #include <FDM/flight.hxx>
32 : _old_flap_position(0),
41 _engine_running_prop(0),
42 _engine_cranking_prop(0),
43 _stall_warning_prop(0),
50 // FIXME: is this right, or does the
51 // sound manager assume pointer ownership?
67 FGSoundMgr * mgr = globals->get_soundmgr();
70 // Create and add the engine sound
73 new FGSimpleSound(fgGetString("/sim/sounds/engine", "Sounds/wasp.wav"));
74 mgr->add(_engine, "engine");
77 // Create and add the cranking sound.
79 _crank = new FGSimpleSound(fgGetString("/sim/sounds/cranking",
80 "Sounds/cranking.wav"));
81 _crank->set_pitch(1.5);
82 _crank->set_volume(0.25);
83 mgr->add(_crank, "crank");
87 // Create and add the wind noise.
89 _wind = new FGSimpleSound(fgGetString("/sim/sounds/wind",
91 mgr->add(_wind, "wind");
95 // Create and add the stall noise.
97 _stall = new FGSimpleSound(fgGetString("/sim/sounds/stall",
99 mgr->add(_stall, "stall");
102 // Create and add the rumble noise.
104 _rumble = new FGSimpleSound(fgGetString("/sim/sounds/rumble",
105 "Sounds/rumble.wav"));
106 mgr->add(_rumble, "rumble");
110 // Create and add the flaps noise
112 _flaps = new FGSimpleSound(fgGetString("/sim/sounds/flaps",
113 "Sounds/flaps.wav"));
114 mgr->add(_flaps, "flaps");
117 // Create and add the squeal noise.
119 _squeal = new FGSimpleSound(fgGetString("/sim/sounds/squeal",
120 "Sounds/squeal.wav"));
121 mgr->add(_squeal, "squeal");
124 // Create and add the click noise.
125 _click = new FGSimpleSound(fgGetString("/sim/sounds/click",
126 "Sounds/click.wav"));
127 mgr->add(_click, "click");
130 ////////////////////////////////////////////////////////////////////
131 // Grab some properties.
132 ////////////////////////////////////////////////////////////////////
134 _engine_running_prop = fgGetNode("/engines/engine[0]/running", true);
135 _engine_cranking_prop = fgGetNode("/engines/engine[0]/cranking", true);
136 _stall_warning_prop = fgGetNode("/sim/aircraft/alarms/stall-warning", true);
137 _flaps_prop = fgGetNode("/controls/flaps", true);
153 FGSoundMgr * mgr = globals->get_soundmgr();
156 ////////////////////////////////////////////////////////////////////
157 // Update the engine sound.
158 ////////////////////////////////////////////////////////////////////
160 if (_engine_running_prop->getBoolValue()) {
161 // pitch corresponds to rpm
162 // volume corresponds to manifold pressure
165 if ( cur_fdm_state->get_num_engines() > 0 )
166 rpm_factor = cur_fdm_state->get_engine(0)->get_RPM() / 2500.0;
170 double pitch = 0.3 + rpm_factor * 3.0;
172 // don't run at absurdly slow rates -- not realistic
173 // and sounds bad to boot. :-)
180 if ( cur_fdm_state->get_num_engines() > 0 )
181 mp_factor = cur_fdm_state->get_engine(0)->get_Manifold_Pressure() / 100;
185 double volume = 0.15 + mp_factor / 2.0;
192 _engine->set_pitch( pitch );
193 _engine->set_volume( volume );
194 set_playing("engine", true);
196 set_playing("engine", false);
200 ////////////////////////////////////////////////////////////////////
201 // Update the cranking sound.
202 ////////////////////////////////////////////////////////////////////
205 set_playing("crank", _engine_cranking_prop->getBoolValue());
208 ////////////////////////////////////////////////////////////////////
209 // Update the wind noise.
210 ////////////////////////////////////////////////////////////////////
212 float rel_wind = cur_fdm_state->get_V_rel_wind(); // FPS
213 if (rel_wind > 60.0) { // a little off 30kt
214 float volume = rel_wind/1200.0; // FIXME!!!
215 _wind->set_volume(volume);
216 set_playing("wind", true);
218 set_playing("wind", false);
222 ////////////////////////////////////////////////////////////////////
223 // Update the stall horn.
224 ////////////////////////////////////////////////////////////////////
226 double stall = _stall_warning_prop->getDoubleValue();
228 _stall->set_volume(stall);
229 set_playing("stall", true);
231 set_playing("stall", false);
235 ////////////////////////////////////////////////////////////////////
236 // Update the rumble.
237 ////////////////////////////////////////////////////////////////////
239 float totalGear = min(cur_fdm_state->get_num_gear(), MAX_GEAR);
240 float gearOnGround = 0;
243 // Calculate whether a squeal is
244 // required, and set the volume.
245 // Currently, the squeal volume is the
246 // current local down velocity in feet
247 // per second divided by 10.0, and
248 // will not be played if under 0.1.
250 // FIXME: take rotational velocities
251 // into account as well.
252 for (int i = 0; i < totalGear; i++) {
253 if (cur_fdm_state->get_gear_unit(i)->GetWoW()) {
255 if (!_gear_on_ground[i]) {
256 double squeal_volume = cur_fdm_state->get_V_down() / 5.0;
257 if (squeal_volume > 0.1) {
258 _squeal->set_volume(squeal_volume);
259 mgr->play_once("squeal");
261 _gear_on_ground[i] = true;
264 _gear_on_ground[i] = false;
268 // Now, if any of the gear is in
269 // contact with the ground play the
270 // rumble sound. The volume is the
271 // absolute velocity in knots divided
272 // by 120.0. No rumble will be played
273 // if the velocity is under 6kt.
274 double speed = cur_fdm_state->get_V_equiv_kts();
275 if (gearOnGround > 0 && speed >= 6.0) {
276 double volume = (gearOnGround/totalGear) * (speed/60.0);
277 _rumble->set_volume(volume);
278 set_playing("rumble", true);
280 set_playing("rumble", false);
284 ////////////////////////////////////////////////////////////////////////
285 // Check for flap movement.
286 ////////////////////////////////////////////////////////////////////
288 double flap_position = _flaps_prop->getDoubleValue();
289 if (fabs(flap_position - _old_flap_position) > 0.1) {
290 mgr->play_once("flaps");
291 _old_flap_position = flap_position;
300 FGFX::set_playing (const char * soundName, bool state)
302 FGSoundMgr * mgr = globals->get_soundmgr();
303 bool playing = mgr->is_playing(soundName);
304 if (state && !playing)
305 mgr->play_looped(soundName);
306 else if (!state && playing)
307 mgr->stop(soundName);