]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
ddb58ce05e1ff32c063a89673671e6a2e230a821
[flightgear.git] / src / Sound / fg_fx.cxx
1 // fgfx.cxx -- Sound effect management class implementation
2 //
3 // Started by David Megginson, October 2001
4 // (Reuses some code from main.cxx, probably by Curtis Olson)
5 //
6 // Copyright (C) 2001  Curtis L. Olson - curt@flightgear.org
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23
24 #include "fg_fx.hxx"
25 #include <Main/fg_props.hxx>
26
27 // FIXME: remove direct dependencies
28 #include <FDM/flight.hxx>
29
30
31 FGFX::FGFX ()
32   : _old_flap_position(0),
33     _engine(0),
34     _crank(0),
35     _wind(0),
36     _stall(0),
37     _rumble(0),
38     _flaps(0),
39     _squeal(0),
40     _click(0),
41     _engine_running_prop(0),
42     _engine_cranking_prop(0),
43     _stall_warning_prop(0),
44     _flaps_prop(0)
45 {
46 }
47
48 FGFX::~FGFX ()
49 {
50                                 // FIXME: is this right, or does the
51                                 // sound manager assume pointer ownership?
52   delete _engine;
53   delete _crank;
54   delete _wind;
55   delete _stall;
56   delete _rumble;
57
58   delete _flaps;
59   delete _squeal;
60   delete _click;
61 }
62
63
64 void
65 FGFX::init ()
66 {
67   FGSoundMgr * mgr = globals->get_soundmgr();
68
69   //
70   // Create and add the engine sound
71   //
72   _engine =
73     new FGSimpleSound(fgGetString("/sim/sounds/engine", "Sounds/wasp.wav"));
74   mgr->add(_engine, "engine");
75
76   //
77   // Create and add the cranking sound.
78   //
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");
84
85
86   //
87   // Create and add the wind noise.
88   //
89   _wind = new FGSimpleSound(fgGetString("/sim/sounds/wind",
90                                         "Sounds/wind.wav"));
91   mgr->add(_wind, "wind");
92
93
94   //
95   // Create and add the stall noise.
96   //
97   _stall = new FGSimpleSound(fgGetString("/sim/sounds/stall",
98                                          "Sounds/stall.wav"));
99   mgr->add(_stall, "stall");
100
101   //
102   // Create and add the rumble noise.
103   //
104   _rumble = new FGSimpleSound(fgGetString("/sim/sounds/rumble",
105                                           "Sounds/rumble.wav"));
106   mgr->add(_rumble, "rumble");
107
108
109   //
110   // Create and add the flaps noise
111   //
112   _flaps = new FGSimpleSound(fgGetString("/sim/sounds/flaps",
113                                          "Sounds/flaps.wav"));
114   _flaps->set_volume(0.50);
115   mgr->add(_flaps, "flaps");
116
117   //
118   // Create and add the squeal noise.
119   //
120   _squeal = new FGSimpleSound(fgGetString("/sim/sounds/squeal",
121                                           "Sounds/squeal.wav"));
122   mgr->add(_squeal, "squeal");
123
124   //
125   // Create and add the click noise.
126   _click = new FGSimpleSound(fgGetString("/sim/sounds/click",
127                                          "Sounds/click.wav"));
128   mgr->add(_click, "click");
129
130
131   ////////////////////////////////////////////////////////////////////
132   // Grab some properties.
133   ////////////////////////////////////////////////////////////////////
134
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);
139 }
140
141 void
142 FGFX::bind ()
143 {
144 }
145
146 void
147 FGFX::unbind ()
148 {
149 }
150
151 void
152 FGFX::update ()
153 {
154   FGSoundMgr * mgr = globals->get_soundmgr();
155
156
157   ////////////////////////////////////////////////////////////////////
158   // Update the engine sound.
159   ////////////////////////////////////////////////////////////////////
160
161   if (cur_fdm_state->get_num_engines() > 0 && _engine_running_prop->getBoolValue()) {
162           // pitch corresponds to rpm
163           // volume corresponds to manifold pressure
164
165     double rpm_factor;
166     if ( cur_fdm_state->get_num_engines() > 0 )
167       rpm_factor = cur_fdm_state->get_engine(0)->get_RPM() / 2500.0;
168     else
169       rpm_factor = 1.0;
170
171     double pitch = 0.3 + rpm_factor * 3.0;
172
173     // don't run at absurdly slow rates -- not realistic
174     // and sounds bad to boot.  :-)
175     if (pitch < 0.7)
176       pitch = 0.7;
177     if (pitch > 5.0)
178       pitch = 5.0;
179
180     double mp_factor;
181     if ( cur_fdm_state->get_num_engines() > 0 )
182       mp_factor = cur_fdm_state->get_engine(0)->get_Manifold_Pressure() / 100;
183     else
184       mp_factor = 0.3;
185
186     double volume = 0.15 + mp_factor / 2.0;
187
188     if (volume < 0.15)
189       volume = 0.15;
190     if (volume > 0.5)
191       volume = 0.5;
192
193     _engine->set_pitch( pitch );
194     _engine->set_volume( volume );
195     set_playing("engine", true);
196   } else {
197     set_playing("engine", false);
198   }
199
200
201   ////////////////////////////////////////////////////////////////////
202   // Update the cranking sound.
203   ////////////////////////////////////////////////////////////////////
204
205                                 // FIXME
206   set_playing("crank", _engine_cranking_prop->getBoolValue());
207
208
209   ////////////////////////////////////////////////////////////////////
210   // Update the wind noise.
211   ////////////////////////////////////////////////////////////////////
212
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);
222   } else {
223     set_playing("wind", false);
224   }
225
226
227   ////////////////////////////////////////////////////////////////////
228   // Update the stall horn.
229   ////////////////////////////////////////////////////////////////////
230
231   double stall = _stall_warning_prop->getDoubleValue();
232   if (stall > 0.0) {
233     _stall->set_volume(stall);
234     set_playing("stall", true);
235   } else {
236     set_playing("stall", false);
237   }
238
239
240   ////////////////////////////////////////////////////////////////////
241   // Update the rumble.
242   ////////////////////////////////////////////////////////////////////
243
244   float totalGear = min(cur_fdm_state->get_num_gear(), int(MAX_GEAR));
245   float gearOnGround = 0;
246
247
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.
254
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()) {
259       gearOnGround++;
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");
265         }
266         _gear_on_ground[i] = true;
267       }
268     } else {
269       _gear_on_ground[i] = false;
270     }
271   }
272
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);
284   } else {
285     set_playing("rumble", false);
286   }
287
288
289   ////////////////////////////////////////////////////////////////////////
290   // Check for flap movement.
291   ////////////////////////////////////////////////////////////////////
292
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;
297   }
298
299   // TODO: click
300
301 }
302
303
304 void
305 FGFX::set_playing (const char * soundName, bool state)
306 {
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);
313 }
314
315 // end of fg_fx.cxx