1 // fg_fx.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.
24 #include <FDM/flight.hxx> // FIXME: remove direct dependencies
25 #include <Main/fg_props.hxx>
30 static const char * engine_names[FGFX::MAX_ENGINES] = {
35 static const char * crank_names[FGFX::MAX_ENGINES] = {
42 : _old_flap_position(0),
43 _old_gear_position(0),
52 _stall_warning_prop(0),
56 for (int i = 0; i < MAX_ENGINES; i++) {
59 _engine_running_prop[i] = 0;
60 _engine_cranking_prop[i] = 0;
66 // FIXME: is this right, or does the
67 // sound manager assume pointer ownership?
68 for (int i = 0; i < MAX_ENGINES; i++) {
87 FGSoundMgr * mgr = globals->get_soundmgr();
91 // Create and add engine-related sounds.
93 for (i = 0; i < MAX_ENGINES; i++) {
96 new FGSimpleSound(fgGetString("/sim/sounds/engine/path",
98 _engine[i]->set_volume(fgGetFloat("/sim/sounds/engine/volume", 1.0));
99 _engine[i]->set_pitch(fgGetFloat("/sim/sounds/engine/pitch", 1.0));
100 mgr->add(_engine[i], engine_names[i]);
103 _crank[i] = new FGSimpleSound(fgGetString("/sim/sounds/cranking/path",
104 "Sounds/cranking.wav"));
105 _crank[i]->set_volume(fgGetFloat("/sim/sounds/cranking/volume", 0.5));
106 _crank[i]->set_pitch(fgGetFloat("/sim/sounds/cranking/pitch", 1.00));
107 mgr->add(_crank[i], crank_names[i]);
112 // Create and add the wind noise.
114 _wind = new FGSimpleSound(fgGetString("/sim/sounds/wind/path",
116 _wind->set_volume(fgGetFloat("/sim/sounds/wind/volume", 1.0));
117 _wind->set_pitch(fgGetFloat("/sim/sounds/wind/pitch", 1.0));
118 mgr->add(_wind, "wind");
122 // Create and add the stall noise.
124 _stall = new FGSimpleSound(fgGetString("/sim/sounds/stall/path",
125 "Sounds/stall.wav"));
126 _stall->set_volume(fgGetFloat("/sim/sounds/stall/volume", 1.0));
127 _stall->set_pitch(fgGetFloat("/sim/sounds/stall/pitch", 1.0));
128 mgr->add(_stall, "stall");
131 // Create and add the rumble noise.
133 _rumble = new FGSimpleSound(fgGetString("/sim/sounds/rumble/path",
134 "Sounds/rumble.wav"));
135 _rumble->set_volume(fgGetFloat("/sim/sounds/rumble/volume", 1.0));
136 _rumble->set_pitch(fgGetFloat("/sim/sounds/rumble/pitch", 1.0));
137 mgr->add(_rumble, "rumble");
141 // Create and add the flaps noise
143 _flaps = new FGSimpleSound(fgGetString("/sim/sounds/flaps/path",
144 "Sounds/flaps.wav"));
145 _flaps->set_volume(fgGetFloat("/sim/sounds/flaps/volume", 0.25));
146 _flaps->set_pitch(fgGetFloat("/sim/sounds/flaps/pitch", 1.0));
147 mgr->add(_flaps, "flaps");
150 // Create and add the gear noises.
152 _gear_up = new FGSimpleSound(fgGetString("/sim/sounds/gear-up/path",
153 "Sounds/gear-up.wav"));
154 _gear_dn = new FGSimpleSound(fgGetString("/sim/sounds/gear-down/path",
155 "Sounds/gear-dn.wav"));
156 _gear_up->set_volume(fgGetFloat("/sim/sounds/gear-up/volume", 1.0));
157 _gear_dn->set_volume(fgGetFloat("/sim/sounds/gear-down/volume", 1.0));
158 _gear_up->set_pitch(fgGetFloat("/sim/sounds/gear-up/pitch", 1.0));
159 _gear_dn->set_pitch(fgGetFloat("/sim/sounds/gear-down/pitch", 1.0));
160 mgr->add(_gear_up, "gear-up");
161 mgr->add(_gear_dn, "gear-down");
164 // Create and add the squeal noise.
166 _squeal = new FGSimpleSound(fgGetString("/sim/sounds/squeal/path",
167 "Sounds/squeal.wav"));
168 _squeal->set_volume(fgGetFloat("/sim/sounds/squeal/volume", 1.0));
169 _squeal->set_pitch(fgGetFloat("/sim/sounds/squeal/pitch", 1.0));
170 mgr->add(_squeal, "squeal");
173 // Simplistic wheel spin model for audio effect purposes only. We
174 // don't want to play a full squeel, if the wheel has only departed
175 // from the ground for a split second.
177 for (i = 0; i < MAX_GEAR; i++) {
178 _wheel_spin[i] = 0.0;
182 // Create and add the click noise.
183 _click = new FGSimpleSound(fgGetString("/sim/sounds/click/path",
184 "Sounds/click.wav"));
185 _flaps->set_volume(fgGetFloat("/sim/sounds/click/volume", 1.0));
186 _flaps->set_pitch(fgGetFloat("/sim/sounds/click/pitch", 1.0));
187 mgr->add(_click, "click");
190 ////////////////////////////////////////////////////////////////////
191 // Grab some properties.
192 ////////////////////////////////////////////////////////////////////
194 for (i = 0; i < MAX_ENGINES; i++) {
196 sprintf(buf, "/engines/engine[%d]/running", i);
197 _engine_running_prop[i] = fgGetNode(buf, true);
198 sprintf(buf, "/engines/engine[%d]/cranking", i);
199 _engine_cranking_prop[i] = fgGetNode(buf, true);
201 _stall_warning_prop = fgGetNode("/sim/aero/alarms/stall-warning", true);
202 _vc_prop = fgGetNode("/velocities/airspeed-kt", true);
203 _flaps_prop = fgGetNode("/controls/flaps", true);
204 _gear_prop = fgGetNode("/controls/gear-down", true);
218 FGFX::update (int dt)
220 FGSoundMgr * mgr = globals->get_soundmgr();
223 ////////////////////////////////////////////////////////////////////
224 // Update the engine sound.
225 ////////////////////////////////////////////////////////////////////
227 for (i = 0; i < MAX_ENGINES; i++) {
229 if (cur_fdm_state->get_num_engines() > 0 &&
230 _engine_running_prop[i]->getBoolValue()) {
231 // pitch corresponds to rpm
232 // volume corresponds to manifold pressure
235 if ( cur_fdm_state->get_num_engines() > 0 )
236 rpm_factor = cur_fdm_state->get_engine(i)->get_RPM() / 2500.0;
240 double pitch = 0.3 + rpm_factor * 3.0;
242 // don't run at absurdly slow rates -- not realistic
243 // and sounds bad to boot. :-)
250 if ( cur_fdm_state->get_num_engines() > 0 )
252 cur_fdm_state->get_engine(i)->get_Manifold_Pressure() / 100;
256 double volume = 0.15 + mp_factor / 2.0;
263 _engine[i]->set_pitch( pitch );
264 _engine[i]->set_volume( volume );
265 set_playing(engine_names[i], true);
267 set_playing(engine_names[i], false);
271 set_playing(crank_names[i], _engine_cranking_prop[i]->getBoolValue());
275 ////////////////////////////////////////////////////////////////////
276 // Update the wind noise.
277 ////////////////////////////////////////////////////////////////////
279 // The wind sound is airspeed and altitude
280 // dependent. The wind noise should become
281 // silent at about 65000 feet, and is based
282 // on a logarithmic scale.
283 float rel_wind = cur_fdm_state->get_V_rel_wind(); // FPS
284 float alt_vol = 1-log(cur_fdm_state->get_Altitude()/65000.0)/4.81;
285 if ((rel_wind > 2.0) && (alt_vol > 0.2)) {
286 float volume = rel_wind/937;
287 double pitch = 1.0+(rel_wind/53.0);
288 _wind->set_volume(volume*alt_vol/2);
289 _wind->set_pitch(pitch);
290 set_playing("wind", true);
292 set_playing("wind", false);
296 ////////////////////////////////////////////////////////////////////
297 // Update the stall horn.
298 ////////////////////////////////////////////////////////////////////
300 double stall = _stall_warning_prop->getDoubleValue();
301 double vc = _vc_prop->getDoubleValue();
302 if (stall > 0.0 && vc > 30.0) {
303 _stall->set_volume(stall);
304 set_playing("stall", true);
306 set_playing("stall", false);
310 ////////////////////////////////////////////////////////////////////
311 // Update the rumble.
312 ////////////////////////////////////////////////////////////////////
314 float totalGear = min(cur_fdm_state->get_num_gear(), int(MAX_GEAR));
315 float gearOnGround = 0;
318 // Calculate whether a squeal is
319 // required, and set the volume.
320 // Currently, the squeal volume is the
321 // current local down velocity in feet
322 // per second divided by 10.0, and
323 // will not be played if under 0.1.
325 // FIXME: take rotational velocities
326 // into account as well.
327 for (i = 0; i < totalGear; i++) {
328 // cout << "air speed = " << cur_fdm_state->get_V_equiv_kts();
329 // cout << " wheel " << i << " speed = " << _wheel_spin[i];
330 if (cur_fdm_state->get_gear_unit(i)->GetWoW()) {
332 if (!_gear_on_ground[i]) {
333 // wheel just touched down
334 // 3 parts horizontal velocity + 1 part vertical velocity
335 double squeal_volume = 0.75 * cur_fdm_state->get_V_equiv_kts() / 90.0
336 + 0.25 * cur_fdm_state->get_V_down() / 5.0;
338 // scale volume by difference between wheel spin speed and
340 double diff = fabs( cur_fdm_state->get_V_equiv_kts()
342 // cout << " speed diff = " << diff;
343 double scale_factor = 0.0;
345 scale_factor = ( diff - 10.0 ) / 30.0f;
346 if ( scale_factor > 1.0 ) { scale_factor = 1.0; }
348 // cout << " scale_factor = " << scale_factor;
349 squeal_volume *= scale_factor;
351 if (squeal_volume > 0.1) {
352 _squeal->set_volume(squeal_volume);
353 _squeal->set_pitch(1.25);
354 mgr->play_once("squeal");
356 _gear_on_ground[i] = true;
359 _wheel_spin[i] = cur_fdm_state->get_V_equiv_kts();
361 // cout << " airborn";
362 _gear_on_ground[i] = false;
363 /* fix me: wheel spindown is currently frame rate dependent which
365 _wheel_spin[i] -= 0.2;
366 if ( _wheel_spin[i] < 0.0 ) { _wheel_spin[i] = 0.0; }
371 // Now, if any of the gear is in
372 // contact with the ground play the
373 // rumble sound. The volume is a
374 // logarthmic scale of the absolute
375 // velocity in knots divided by 12.0.
376 // No rumble will be played if the
377 // velocity is under ~0.25 kt.
378 double speed = cur_fdm_state->get_V_equiv_kts();
379 if (gearOnGround > 0 && speed > 0.5) {
380 double volume = 2.0 * (gearOnGround/totalGear) * log(speed)/12; //(speed/60.0);
381 _rumble->set_volume(volume);
382 set_playing("rumble", true);
384 set_playing("rumble", false);
388 ////////////////////////////////////////////////////////////////////
389 // Check for flap movement.
390 ////////////////////////////////////////////////////////////////////
392 double flap_position = _flaps_prop->getDoubleValue();
393 if (fabs(flap_position - _old_flap_position) > 0.1) {
394 mgr->play_once("flaps");
395 _old_flap_position = flap_position;
399 ////////////////////////////////////////////////////////////////////
400 // Check for gear movement.
401 ////////////////////////////////////////////////////////////////////
403 double gear_position = _gear_prop->getDoubleValue();
404 if (gear_position != _old_gear_position) {
405 if (gear_position < _old_gear_position) {
406 mgr->play_once("gear-up");
408 mgr->play_once("gear-down");
410 _old_gear_position = gear_position;
419 FGFX::set_playing (const char * soundName, bool state)
421 FGSoundMgr * mgr = globals->get_soundmgr();
422 bool playing = mgr->is_playing(soundName);
423 if (state && !playing)
424 mgr->play_looped(soundName);
425 else if (!state && playing)
426 mgr->stop(soundName);