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),
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();
90 // Create and add engine-related sounds.
92 for (int i = 0; i < MAX_ENGINES; i++) {
95 new FGSimpleSound(fgGetString("/sim/sounds/engine/path",
97 _engine[i]->set_volume(fgGetFloat("/sim/sounds/engine/volume", 1.0));
98 _engine[i]->set_pitch(fgGetFloat("/sim/sounds/engine/pitch", 1.0));
99 mgr->add(_engine[i], engine_names[i]);
102 _crank[i] = new FGSimpleSound(fgGetString("/sim/sounds/cranking/path",
103 "Sounds/cranking.wav"));
104 _crank[i]->set_volume(fgGetFloat("/sim/sounds/cranking/volume", 0.175));
105 _crank[i]->set_pitch(fgGetFloat("/sim/sounds/cranking/pitch", 1.25));
106 mgr->add(_crank[i], crank_names[i]);
111 // Create and add the wind noise.
113 _wind = new FGSimpleSound(fgGetString("/sim/sounds/wind/path",
115 _wind->set_volume(fgGetFloat("/sim/sounds/wind/volume", 1.0));
116 _wind->set_pitch(fgGetFloat("/sim/sounds/wind/pitch", 1.0));
117 mgr->add(_wind, "wind");
121 // Create and add the stall noise.
123 _stall = new FGSimpleSound(fgGetString("/sim/sounds/stall/path",
124 "Sounds/stall.wav"));
125 _stall->set_volume(fgGetFloat("/sim/sounds/stall/volume", 1.0));
126 _stall->set_pitch(fgGetFloat("/sim/sounds/stall/pitch", 1.0));
127 mgr->add(_stall, "stall");
130 // Create and add the rumble noise.
132 _rumble = new FGSimpleSound(fgGetString("/sim/sounds/rumble/path",
133 "Sounds/rumble.wav"));
134 _rumble->set_volume(fgGetFloat("/sim/sounds/rumble/volume", 1.0));
135 _rumble->set_pitch(fgGetFloat("/sim/sounds/rumble/pitch", 1.0));
136 mgr->add(_rumble, "rumble");
140 // Create and add the flaps noise
142 _flaps = new FGSimpleSound(fgGetString("/sim/sounds/flaps/path",
143 "Sounds/flaps.wav"));
144 _flaps->set_volume(fgGetFloat("/sim/sounds/flaps/volume", 0.5));
145 _flaps->set_pitch(fgGetFloat("/sim/sounds/flaps/pitch", 1.0));
146 mgr->add(_flaps, "flaps");
149 // Create and add the gear noises.
151 _gear_up = new FGSimpleSound(fgGetString("/sim/sounds/gear-up/path",
152 "Sounds/gear-up.wav"));
153 _gear_dn = new FGSimpleSound(fgGetString("/sim/sounds/gear-down/path",
154 "Sounds/gear-dn.wav"));
155 _gear_up->set_volume(fgGetFloat("/sim/sounds/gear-up/volume", 1.0));
156 _gear_dn->set_volume(fgGetFloat("/sim/sounds/gear-down/volume", 1.0));
157 _gear_up->set_pitch(fgGetFloat("/sim/sounds/gear-up/pitch", 1.0));
158 _gear_dn->set_pitch(fgGetFloat("/sim/sounds/gear-down/pitch", 1.0));
159 mgr->add(_gear_up, "gear-up");
160 mgr->add(_gear_dn, "gear-down");
163 // Create and add the squeal noise.
165 _squeal = new FGSimpleSound(fgGetString("/sim/sounds/squeal/path",
166 "Sounds/squeal.wav"));
167 _squeal->set_volume(fgGetFloat("/sim/sounds/squeal/volume", 1.0));
168 _squeal->set_pitch(fgGetFloat("/sim/sounds/squeal/pitch", 1.0));
169 mgr->add(_squeal, "squeal");
172 // Create and add the click noise.
173 _click = new FGSimpleSound(fgGetString("/sim/sounds/click/path",
174 "Sounds/click.wav"));
175 _flaps->set_volume(fgGetFloat("/sim/sounds/click/volume", 1.0));
176 _flaps->set_pitch(fgGetFloat("/sim/sounds/click/pitch", 1.0));
177 mgr->add(_click, "click");
180 ////////////////////////////////////////////////////////////////////
181 // Grab some properties.
182 ////////////////////////////////////////////////////////////////////
184 for (int i = 0; i < MAX_ENGINES; i++) {
186 sprintf(buf, "/engines/engine[%d]/running", i);
187 _engine_running_prop[i] = fgGetNode(buf, true);
188 sprintf(buf, "/engines/engine[%d]/cranking", i);
189 _engine_cranking_prop[i] = fgGetNode(buf, true);
191 _stall_warning_prop = fgGetNode("/sim/aircraft/alarms/stall-warning", true);
192 _vc_prop = fgGetNode("/velocities/airspeed-kt", true);
193 _flaps_prop = fgGetNode("/controls/flaps", true);
194 _gear_prop = fgGetNode("/controls/gear-down", true);
210 FGSoundMgr * mgr = globals->get_soundmgr();
213 ////////////////////////////////////////////////////////////////////
214 // Update the engine sound.
215 ////////////////////////////////////////////////////////////////////
217 for (int i = 0; i < MAX_ENGINES; i++) {
219 if (cur_fdm_state->get_num_engines() > 0 &&
220 _engine_running_prop[i]->getBoolValue()) {
221 // pitch corresponds to rpm
222 // volume corresponds to manifold pressure
225 if ( cur_fdm_state->get_num_engines() > 0 )
226 rpm_factor = cur_fdm_state->get_engine(i)->get_RPM() / 2500.0;
230 double pitch = 0.3 + rpm_factor * 3.0;
232 // don't run at absurdly slow rates -- not realistic
233 // and sounds bad to boot. :-)
240 if ( cur_fdm_state->get_num_engines() > 0 )
242 cur_fdm_state->get_engine(i)->get_Manifold_Pressure() / 100;
246 double volume = 0.15 + mp_factor / 2.0;
253 _engine[i]->set_pitch( pitch );
254 _engine[i]->set_volume( volume );
255 set_playing(engine_names[i], true);
257 set_playing(engine_names[i], false);
261 set_playing(crank_names[i], _engine_cranking_prop[i]->getBoolValue());
265 ////////////////////////////////////////////////////////////////////
266 // Update the wind noise.
267 ////////////////////////////////////////////////////////////////////
269 float rel_wind = cur_fdm_state->get_V_rel_wind(); // FPS
270 float airspeed_kt = cur_fdm_state->get_V_equiv_kts();
271 if (rel_wind > 60.0) { // a little off 30kt
272 // float volume = rel_wind/600.0; // FIXME!!!
273 float volume = rel_wind/937.0; // FIXME!!!
274 double pitch = 1.0+(airspeed_kt/113.0);
275 _wind->set_volume(volume);
276 _wind->set_pitch(pitch);
277 set_playing("wind", true);
279 set_playing("wind", false);
283 ////////////////////////////////////////////////////////////////////
284 // Update the stall horn.
285 ////////////////////////////////////////////////////////////////////
287 double stall = _stall_warning_prop->getDoubleValue();
288 double vc = _vc_prop->getDoubleValue();
289 if (stall > 0.0 && vc > 30.0) {
290 _stall->set_volume(stall);
291 set_playing("stall", true);
293 set_playing("stall", false);
297 ////////////////////////////////////////////////////////////////////
298 // Update the rumble.
299 ////////////////////////////////////////////////////////////////////
301 float totalGear = min(cur_fdm_state->get_num_gear(), int(MAX_GEAR));
302 float gearOnGround = 0;
305 // Calculate whether a squeal is
306 // required, and set the volume.
307 // Currently, the squeal volume is the
308 // current local down velocity in feet
309 // per second divided by 10.0, and
310 // will not be played if under 0.1.
312 // FIXME: take rotational velocities
313 // into account as well.
314 for (int i = 0; i < totalGear; i++) {
315 if (cur_fdm_state->get_gear_unit(i)->GetWoW()) {
317 if (!_gear_on_ground[i]) {
318 double squeal_volume = cur_fdm_state->get_V_down() / 5.0;
319 if (squeal_volume > 0.1) {
320 _squeal->set_volume(squeal_volume);
321 mgr->play_once("squeal");
323 _gear_on_ground[i] = true;
326 _gear_on_ground[i] = false;
330 // Now, if any of the gear is in
331 // contact with the ground play the
332 // rumble sound. The volume is the
333 // absolute velocity in knots divided
334 // by 120.0. No rumble will be played
335 // if the velocity is under 6kt.
336 double speed = cur_fdm_state->get_V_equiv_kts();
337 if (gearOnGround > 0 && speed >= 6.0) {
338 double volume = 2.0 * (gearOnGround/totalGear) * (speed/60.0);
339 _rumble->set_volume(volume);
340 set_playing("rumble", true);
342 set_playing("rumble", false);
346 ////////////////////////////////////////////////////////////////////
347 // Check for flap movement.
348 ////////////////////////////////////////////////////////////////////
350 double flap_position = _flaps_prop->getDoubleValue();
351 if (fabs(flap_position - _old_flap_position) > 0.1) {
352 mgr->play_once("flaps");
353 _old_flap_position = flap_position;
357 ////////////////////////////////////////////////////////////////////
358 // Check for gear movement.
359 ////////////////////////////////////////////////////////////////////
361 double gear_position = _gear_prop->getDoubleValue();
362 if (gear_position != _old_gear_position) {
363 if (gear_position < _old_gear_position) {
364 mgr->play_once("gear-up");
366 mgr->play_once("gear-down");
368 _old_gear_position = gear_position;
377 FGFX::set_playing (const char * soundName, bool state)
379 FGSoundMgr * mgr = globals->get_soundmgr();
380 bool playing = mgr->is_playing(soundName);
381 if (state && !playing)
382 mgr->play_looped(soundName);
383 else if (!state && playing)
384 mgr->stop(soundName);