]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
- added support for new sounds: flaps, wheel rumble, squeal
[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.5);
82   _crank->set_volume(0.25);
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   mgr->add(_flaps, "flaps");
115
116   //
117   // Create and add the squeal noise.
118   //
119   _squeal = new FGSimpleSound(fgGetString("/sim/sounds/squeal",
120                                           "Sounds/squeal.wav"));
121   mgr->add(_squeal, "squeal");
122
123   //
124   // Create and add the click noise.
125   _click = new FGSimpleSound(fgGetString("/sim/sounds/click",
126                                          "Sounds/click.wav"));
127   mgr->add(_click, "click");
128
129
130   ////////////////////////////////////////////////////////////////////
131   // Grab some properties.
132   ////////////////////////////////////////////////////////////////////
133
134   _engine_running_prop = fgGetNode("/engines/engine[0]/running", true);
135   _engine_cranking_prop = fgGetNode("/engines/engine[0]/cranking", true);
136   _stall_warning_prop = fgGetNode("/sim/aircraft/alarms/stall-warning", true);
137   _flaps_prop = fgGetNode("/controls/flaps", true);
138 }
139
140 void
141 FGFX::bind ()
142 {
143 }
144
145 void
146 FGFX::unbind ()
147 {
148 }
149
150 void
151 FGFX::update ()
152 {
153   FGSoundMgr * mgr = globals->get_soundmgr();
154
155
156   ////////////////////////////////////////////////////////////////////
157   // Update the engine sound.
158   ////////////////////////////////////////////////////////////////////
159
160   if (_engine_running_prop->getBoolValue()) {
161           // pitch corresponds to rpm
162           // volume corresponds to manifold pressure
163
164     double rpm_factor;
165     if ( cur_fdm_state->get_num_engines() > 0 )
166       rpm_factor = cur_fdm_state->get_engine(0)->get_RPM() / 2500.0;
167     else
168       rpm_factor = 1.0;
169
170     double pitch = 0.3 + rpm_factor * 3.0;
171
172     // don't run at absurdly slow rates -- not realistic
173     // and sounds bad to boot.  :-)
174     if (pitch < 0.7)
175       pitch = 0.7;
176     if (pitch > 5.0)
177       pitch = 5.0;
178
179     double mp_factor;
180     if ( cur_fdm_state->get_num_engines() > 0 )
181       mp_factor = cur_fdm_state->get_engine(0)->get_Manifold_Pressure() / 100;
182     else
183       mp_factor = 0.3;
184
185     double volume = 0.15 + mp_factor / 2.0;
186
187     if (volume < 0.15)
188       volume = 0.15;
189     if (volume > 0.5)
190       volume = 0.5;
191
192     _engine->set_pitch( pitch );
193     _engine->set_volume( volume );
194     set_playing("engine", true);
195   } else {
196     set_playing("engine", false);
197   }
198
199
200   ////////////////////////////////////////////////////////////////////
201   // Update the cranking sound.
202   ////////////////////////////////////////////////////////////////////
203
204                                 // FIXME
205   set_playing("crank", _engine_cranking_prop->getBoolValue());
206
207
208   ////////////////////////////////////////////////////////////////////
209   // Update the wind noise.
210   ////////////////////////////////////////////////////////////////////
211
212   float rel_wind = cur_fdm_state->get_V_rel_wind(); // FPS
213   if (rel_wind > 60.0) {        // a little off 30kt
214     float volume = rel_wind/1200.0;     // FIXME!!!
215     _wind->set_volume(volume);
216     set_playing("wind", true);
217   } else {
218     set_playing("wind", false);
219   }
220
221
222   ////////////////////////////////////////////////////////////////////
223   // Update the stall horn.
224   ////////////////////////////////////////////////////////////////////
225
226   double stall = _stall_warning_prop->getDoubleValue();
227   if (stall > 0.0) {
228     _stall->set_volume(stall);
229     set_playing("stall", true);
230   } else {
231     set_playing("stall", false);
232   }
233
234
235   ////////////////////////////////////////////////////////////////////
236   // Update the rumble.
237   ////////////////////////////////////////////////////////////////////
238
239   float totalGear = min(cur_fdm_state->get_num_gear(), MAX_GEAR);
240   float gearOnGround = 0;
241
242
243                                 // Calculate whether a squeal is
244                                 // required, and set the volume.
245                                 // Currently, the squeal volume is the
246                                 // current local down velocity in feet
247                                 // per second divided by 10.0, and
248                                 // will not be played if under 0.1.
249
250                                 // FIXME: take rotational velocities
251                                 // into account as well.
252   for (int i = 0; i < totalGear; i++) {
253     if (cur_fdm_state->get_gear_unit(i)->GetWoW()) {
254       gearOnGround++;
255       if (!_gear_on_ground[i]) {
256         double squeal_volume = cur_fdm_state->get_V_down() / 5.0;
257         if (squeal_volume > 0.1) {
258           _squeal->set_volume(squeal_volume);
259           mgr->play_once("squeal");
260         }
261         _gear_on_ground[i] = true;
262       }
263     } else {
264       _gear_on_ground[i] = false;
265     }
266   }
267
268                                 // Now, if any of the gear is in
269                                 // contact with the ground play the
270                                 // rumble sound.  The volume is the
271                                 // absolute velocity in knots divided
272                                 // by 120.0.  No rumble will be played
273                                 // if the velocity is under 6kt.
274   double speed = cur_fdm_state->get_V_equiv_kts();
275   if (gearOnGround > 0 && speed >= 6.0) {
276     double volume = (gearOnGround/totalGear) * (speed/60.0);
277     _rumble->set_volume(volume);
278     set_playing("rumble", true);
279   } else {
280     set_playing("rumble", false);
281   }
282
283
284   ////////////////////////////////////////////////////////////////////////
285   // Check for flap movement.
286   ////////////////////////////////////////////////////////////////////
287
288   double flap_position = _flaps_prop->getDoubleValue();
289   if (fabs(flap_position - _old_flap_position) > 0.1) {
290     mgr->play_once("flaps");
291     _old_flap_position = flap_position;
292   }
293
294   // TODO: click
295
296 }
297
298
299 void
300 FGFX::set_playing (const char * soundName, bool state)
301 {
302   FGSoundMgr * mgr = globals->get_soundmgr();
303   bool playing = mgr->is_playing(soundName);
304   if (state && !playing)
305     mgr->play_looped(soundName);
306   else if (!state && playing)
307     mgr->stop(soundName);
308 }
309
310 // end of fg_fx.cxx