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>
30 static const char * engine_names[FGFX::MAX_ENGINES] = {
35 static const char * crank_names[FGFX::MAX_ENGINES] = {
42 : _old_flap_position(0),
49 _stall_warning_prop(0),
52 for (int i = 0; i < MAX_ENGINES; i++) {
55 _engine_running_prop[i] = 0;
56 _engine_cranking_prop[i] = 0;
62 // FIXME: is this right, or does the
63 // sound manager assume pointer ownership?
64 for (int i = 0; i < MAX_ENGINES; i++) {
81 FGSoundMgr * mgr = globals->get_soundmgr();
84 // Create and add engine-related sounds.
86 for (int i = 0; i < MAX_ENGINES; i++) {
89 new FGSimpleSound(fgGetString("/sim/sounds/engine", "Sounds/wasp.wav"));
90 mgr->add(_engine[i], engine_names[i]);
93 _crank[i] = new FGSimpleSound(fgGetString("/sim/sounds/cranking",
94 "Sounds/cranking.wav"));
95 _crank[i]->set_pitch(1.25);
96 _crank[i]->set_volume(0.175);
97 mgr->add(_crank[i], crank_names[i]);
102 // Create and add the wind noise.
104 _wind = new FGSimpleSound(fgGetString("/sim/sounds/wind",
106 mgr->add(_wind, "wind");
110 // Create and add the stall noise.
112 _stall = new FGSimpleSound(fgGetString("/sim/sounds/stall",
113 "Sounds/stall.wav"));
114 mgr->add(_stall, "stall");
117 // Create and add the rumble noise.
119 _rumble = new FGSimpleSound(fgGetString("/sim/sounds/rumble",
120 "Sounds/rumble.wav"));
121 mgr->add(_rumble, "rumble");
125 // Create and add the flaps noise
127 _flaps = new FGSimpleSound(fgGetString("/sim/sounds/flaps",
128 "Sounds/flaps.wav"));
129 _flaps->set_volume(0.50);
130 mgr->add(_flaps, "flaps");
133 // Create and add the squeal noise.
135 _squeal = new FGSimpleSound(fgGetString("/sim/sounds/squeal",
136 "Sounds/squeal.wav"));
137 mgr->add(_squeal, "squeal");
140 // Create and add the click noise.
141 _click = new FGSimpleSound(fgGetString("/sim/sounds/click",
142 "Sounds/click.wav"));
143 mgr->add(_click, "click");
146 ////////////////////////////////////////////////////////////////////
147 // Grab some properties.
148 ////////////////////////////////////////////////////////////////////
150 for (int i = 0; i < MAX_ENGINES; i++) {
152 sprintf(buf, "/engines/engine[%d]/running", i);
153 _engine_running_prop[i] = fgGetNode(buf, true);
154 sprintf(buf, "/engines/engine[%d]/cranking", i);
155 _engine_cranking_prop[i] = fgGetNode(buf, true);
157 _stall_warning_prop = fgGetNode("/sim/aircraft/alarms/stall-warning", true);
158 _flaps_prop = fgGetNode("/controls/flaps", true);
174 FGSoundMgr * mgr = globals->get_soundmgr();
177 ////////////////////////////////////////////////////////////////////
178 // Update the engine sound.
179 ////////////////////////////////////////////////////////////////////
181 for (int i = 0; i < MAX_ENGINES; i++) {
183 if (cur_fdm_state->get_num_engines() > 0 &&
184 _engine_running_prop[i]->getBoolValue()) {
185 // pitch corresponds to rpm
186 // volume corresponds to manifold pressure
189 if ( cur_fdm_state->get_num_engines() > 0 )
190 rpm_factor = cur_fdm_state->get_engine(i)->get_RPM() / 2500.0;
194 double pitch = 0.3 + rpm_factor * 3.0;
196 // don't run at absurdly slow rates -- not realistic
197 // and sounds bad to boot. :-)
204 if ( cur_fdm_state->get_num_engines() > 0 )
206 cur_fdm_state->get_engine(i)->get_Manifold_Pressure() / 100;
210 double volume = 0.15 + mp_factor / 2.0;
217 _engine[i]->set_pitch( pitch );
218 _engine[i]->set_volume( volume );
219 set_playing(engine_names[i], true);
221 set_playing(engine_names[i], false);
225 set_playing(crank_names[i], _engine_cranking_prop[i]->getBoolValue());
229 ////////////////////////////////////////////////////////////////////
230 // Update the wind noise.
231 ////////////////////////////////////////////////////////////////////
233 float rel_wind = cur_fdm_state->get_V_rel_wind(); // FPS
234 float airspeed_kt = cur_fdm_state->get_V_equiv_kts();
235 if (rel_wind > 60.0) { // a little off 30kt
236 // float volume = rel_wind/600.0; // FIXME!!!
237 float volume = rel_wind/937.0; // FIXME!!!
238 double pitch = 1.0+(airspeed_kt/113.0);
239 _wind->set_volume(volume);
240 _wind->set_pitch(pitch);
241 set_playing("wind", true);
243 set_playing("wind", false);
247 ////////////////////////////////////////////////////////////////////
248 // Update the stall horn.
249 ////////////////////////////////////////////////////////////////////
251 double stall = _stall_warning_prop->getDoubleValue();
253 _stall->set_volume(stall);
254 set_playing("stall", true);
256 set_playing("stall", false);
260 ////////////////////////////////////////////////////////////////////
261 // Update the rumble.
262 ////////////////////////////////////////////////////////////////////
264 float totalGear = min(cur_fdm_state->get_num_gear(), int(MAX_GEAR));
265 float gearOnGround = 0;
268 // Calculate whether a squeal is
269 // required, and set the volume.
270 // Currently, the squeal volume is the
271 // current local down velocity in feet
272 // per second divided by 10.0, and
273 // will not be played if under 0.1.
275 // FIXME: take rotational velocities
276 // into account as well.
277 for (int i = 0; i < totalGear; i++) {
278 if (cur_fdm_state->get_gear_unit(i)->GetWoW()) {
280 if (!_gear_on_ground[i]) {
281 double squeal_volume = cur_fdm_state->get_V_down() / 5.0;
282 if (squeal_volume > 0.1) {
283 _squeal->set_volume(squeal_volume);
284 mgr->play_once("squeal");
286 _gear_on_ground[i] = true;
289 _gear_on_ground[i] = false;
293 // Now, if any of the gear is in
294 // contact with the ground play the
295 // rumble sound. The volume is the
296 // absolute velocity in knots divided
297 // by 120.0. No rumble will be played
298 // if the velocity is under 6kt.
299 double speed = cur_fdm_state->get_V_equiv_kts();
300 if (gearOnGround > 0 && speed >= 6.0) {
301 double volume = 2.0 * (gearOnGround/totalGear) * (speed/60.0);
302 _rumble->set_volume(volume);
303 set_playing("rumble", true);
305 set_playing("rumble", false);
309 ////////////////////////////////////////////////////////////////////////
310 // Check for flap movement.
311 ////////////////////////////////////////////////////////////////////
313 double flap_position = _flaps_prop->getDoubleValue();
314 if (fabs(flap_position - _old_flap_position) > 0.1) {
315 mgr->play_once("flaps");
316 _old_flap_position = flap_position;
325 FGFX::set_playing (const char * soundName, bool state)
327 FGSoundMgr * mgr = globals->get_soundmgr();
328 bool playing = mgr->is_playing(soundName);
329 if (state && !playing)
330 mgr->play_looped(soundName);
331 else if (!state && playing)
332 mgr->stop(soundName);