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.25);
82 _crank->set_volume(0.175);
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 _flaps->set_volume(0.50);
115 mgr->add(_flaps, "flaps");
118 // Create and add the squeal noise.
120 _squeal = new FGSimpleSound(fgGetString("/sim/sounds/squeal",
121 "Sounds/squeal.wav"));
122 mgr->add(_squeal, "squeal");
125 // Create and add the click noise.
126 _click = new FGSimpleSound(fgGetString("/sim/sounds/click",
127 "Sounds/click.wav"));
128 mgr->add(_click, "click");
131 ////////////////////////////////////////////////////////////////////
132 // Grab some properties.
133 ////////////////////////////////////////////////////////////////////
135 _engine_running_prop = fgGetNode("/engines/engine[0]/running", true);
136 _engine_cranking_prop = fgGetNode("/engines/engine[0]/cranking", true);
137 _stall_warning_prop = fgGetNode("/sim/aircraft/alarms/stall-warning", true);
138 _flaps_prop = fgGetNode("/controls/flaps", true);
154 FGSoundMgr * mgr = globals->get_soundmgr();
157 ////////////////////////////////////////////////////////////////////
158 // Update the engine sound.
159 ////////////////////////////////////////////////////////////////////
161 if (cur_fdm_state->get_num_engines() > 0 && _engine_running_prop->getBoolValue()) {
162 // pitch corresponds to rpm
163 // volume corresponds to manifold pressure
166 if ( cur_fdm_state->get_num_engines() > 0 )
167 rpm_factor = cur_fdm_state->get_engine(0)->get_RPM() / 2500.0;
171 double pitch = 0.3 + rpm_factor * 3.0;
173 // don't run at absurdly slow rates -- not realistic
174 // and sounds bad to boot. :-)
181 if ( cur_fdm_state->get_num_engines() > 0 )
182 mp_factor = cur_fdm_state->get_engine(0)->get_Manifold_Pressure() / 100;
186 double volume = 0.15 + mp_factor / 2.0;
193 _engine->set_pitch( pitch );
194 _engine->set_volume( volume );
195 set_playing("engine", true);
197 set_playing("engine", false);
201 ////////////////////////////////////////////////////////////////////
202 // Update the cranking sound.
203 ////////////////////////////////////////////////////////////////////
206 set_playing("crank", _engine_cranking_prop->getBoolValue());
209 ////////////////////////////////////////////////////////////////////
210 // Update the wind noise.
211 ////////////////////////////////////////////////////////////////////
213 float rel_wind = cur_fdm_state->get_V_rel_wind(); // FPS
214 float airspeed_kt = cur_fdm_state->get_V_equiv_kts();
215 if (rel_wind > 60.0) { // a little off 30kt
216 // float volume = rel_wind/600.0; // FIXME!!!
217 float volume = rel_wind/937.0; // FIXME!!!
218 double pitch = 1.0+(airspeed_kt/113.0);
219 _wind->set_volume(volume);
220 _wind->set_pitch(pitch);
221 set_playing("wind", true);
223 set_playing("wind", false);
227 ////////////////////////////////////////////////////////////////////
228 // Update the stall horn.
229 ////////////////////////////////////////////////////////////////////
231 double stall = _stall_warning_prop->getDoubleValue();
233 _stall->set_volume(stall);
234 set_playing("stall", true);
236 set_playing("stall", false);
240 ////////////////////////////////////////////////////////////////////
241 // Update the rumble.
242 ////////////////////////////////////////////////////////////////////
244 float totalGear = min(cur_fdm_state->get_num_gear(), int(MAX_GEAR));
245 float gearOnGround = 0;
248 // Calculate whether a squeal is
249 // required, and set the volume.
250 // Currently, the squeal volume is the
251 // current local down velocity in feet
252 // per second divided by 10.0, and
253 // will not be played if under 0.1.
255 // FIXME: take rotational velocities
256 // into account as well.
257 for (int i = 0; i < totalGear; i++) {
258 if (cur_fdm_state->get_gear_unit(i)->GetWoW()) {
260 if (!_gear_on_ground[i]) {
261 double squeal_volume = cur_fdm_state->get_V_down() / 5.0;
262 if (squeal_volume > 0.1) {
263 _squeal->set_volume(squeal_volume);
264 mgr->play_once("squeal");
266 _gear_on_ground[i] = true;
269 _gear_on_ground[i] = false;
273 // Now, if any of the gear is in
274 // contact with the ground play the
275 // rumble sound. The volume is the
276 // absolute velocity in knots divided
277 // by 120.0. No rumble will be played
278 // if the velocity is under 6kt.
279 double speed = cur_fdm_state->get_V_equiv_kts();
280 if (gearOnGround > 0 && speed >= 6.0) {
281 double volume = 2.0 * (gearOnGround/totalGear) * (speed/60.0);
282 _rumble->set_volume(volume);
283 set_playing("rumble", true);
285 set_playing("rumble", false);
289 ////////////////////////////////////////////////////////////////////////
290 // Check for flap movement.
291 ////////////////////////////////////////////////////////////////////
293 double flap_position = _flaps_prop->getDoubleValue();
294 if (fabs(flap_position - _old_flap_position) > 0.1) {
295 mgr->play_once("flaps");
296 _old_flap_position = flap_position;
305 FGFX::set_playing (const char * soundName, bool state)
307 FGSoundMgr * mgr = globals->get_soundmgr();
308 bool playing = mgr->is_playing(soundName);
309 if (state && !playing)
310 mgr->play_looped(soundName);
311 else if (!state && playing)
312 mgr->stop(soundName);