]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
e5a0f22e20aa147957bb2cf7f21aa225fe30cede
[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 static const char * engine_names[FGFX::MAX_ENGINES] = {
31   "engine0",
32   "engine1"
33 };
34
35 static const char * crank_names[FGFX::MAX_ENGINES] = {
36   "crank0",
37   "crank1"
38 };
39
40
41 FGFX::FGFX ()
42   : _old_flap_position(0),
43     _wind(0),
44     _stall(0),
45     _rumble(0),
46     _flaps(0),
47     _squeal(0),
48     _click(0),
49     _stall_warning_prop(0),
50     _flaps_prop(0)
51 {
52   for (int i = 0; i < MAX_ENGINES; i++) {
53     _engine[i] = 0;
54     _crank[i] = 0;
55     _engine_running_prop[i] = 0;
56     _engine_cranking_prop[i] = 0;
57   }
58 }
59
60 FGFX::~FGFX ()
61 {
62                                 // FIXME: is this right, or does the
63                                 // sound manager assume pointer ownership?
64   for (int i = 0; i < MAX_ENGINES; i++) {
65     delete _engine[i];
66     delete _crank[i];
67   }
68   delete _wind;
69   delete _stall;
70   delete _rumble;
71
72   delete _flaps;
73   delete _squeal;
74   delete _click;
75 }
76
77
78 void
79 FGFX::init ()
80 {
81   FGSoundMgr * mgr = globals->get_soundmgr();
82
83   //
84   // Create and add engine-related sounds.
85   //
86   for (int i = 0; i < MAX_ENGINES; i++) {
87                                 // Engine
88     _engine[i] =
89       new FGSimpleSound(fgGetString("/sim/sounds/engine", "Sounds/wasp.wav"));
90     mgr->add(_engine[i], engine_names[i]);
91
92                                 // Starter
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]);
98   }
99
100
101   //
102   // Create and add the wind noise.
103   //
104   _wind = new FGSimpleSound(fgGetString("/sim/sounds/wind",
105                                         "Sounds/wind.wav"));
106   mgr->add(_wind, "wind");
107
108
109   //
110   // Create and add the stall noise.
111   //
112   _stall = new FGSimpleSound(fgGetString("/sim/sounds/stall",
113                                          "Sounds/stall.wav"));
114   mgr->add(_stall, "stall");
115
116   //
117   // Create and add the rumble noise.
118   //
119   _rumble = new FGSimpleSound(fgGetString("/sim/sounds/rumble",
120                                           "Sounds/rumble.wav"));
121   mgr->add(_rumble, "rumble");
122
123
124   //
125   // Create and add the flaps noise
126   //
127   _flaps = new FGSimpleSound(fgGetString("/sim/sounds/flaps",
128                                          "Sounds/flaps.wav"));
129   _flaps->set_volume(0.50);
130   mgr->add(_flaps, "flaps");
131
132   //
133   // Create and add the squeal noise.
134   //
135   _squeal = new FGSimpleSound(fgGetString("/sim/sounds/squeal",
136                                           "Sounds/squeal.wav"));
137   mgr->add(_squeal, "squeal");
138
139   //
140   // Create and add the click noise.
141   _click = new FGSimpleSound(fgGetString("/sim/sounds/click",
142                                          "Sounds/click.wav"));
143   mgr->add(_click, "click");
144
145
146   ////////////////////////////////////////////////////////////////////
147   // Grab some properties.
148   ////////////////////////////////////////////////////////////////////
149
150   for (int i = 0; i < MAX_ENGINES; i++) {
151     char buf[100];
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);
156   }
157   _stall_warning_prop = fgGetNode("/sim/aircraft/alarms/stall-warning", true);
158   _flaps_prop = fgGetNode("/controls/flaps", true);
159 }
160
161 void
162 FGFX::bind ()
163 {
164 }
165
166 void
167 FGFX::unbind ()
168 {
169 }
170
171 void
172 FGFX::update ()
173 {
174   FGSoundMgr * mgr = globals->get_soundmgr();
175
176
177   ////////////////////////////////////////////////////////////////////
178   // Update the engine sound.
179   ////////////////////////////////////////////////////////////////////
180   
181   for (int i = 0; i < MAX_ENGINES; i++) {
182
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
187
188       double rpm_factor;
189       if ( cur_fdm_state->get_num_engines() > 0 )
190         rpm_factor = cur_fdm_state->get_engine(i)->get_RPM() / 2500.0;
191       else
192         rpm_factor = 1.0;
193
194       double pitch = 0.3 + rpm_factor * 3.0;
195
196       // don't run at absurdly slow rates -- not realistic
197       // and sounds bad to boot.  :-)
198       if (pitch < 0.7)
199         pitch = 0.7;
200       if (pitch > 5.0)
201         pitch = 5.0;
202
203       double mp_factor;
204       if ( cur_fdm_state->get_num_engines() > 0 )
205         mp_factor =
206           cur_fdm_state->get_engine(i)->get_Manifold_Pressure() / 100;
207       else
208         mp_factor = 0.3;
209
210       double volume = 0.15 + mp_factor / 2.0;
211
212       if (volume < 0.15)
213         volume = 0.15;
214       if (volume > 0.5)
215         volume = 0.5;
216
217       _engine[i]->set_pitch( pitch );
218       _engine[i]->set_volume( volume );
219       set_playing(engine_names[i], true);
220     } else {
221       set_playing(engine_names[i], false);
222     }
223
224                                 // FIXME
225     set_playing(crank_names[i], _engine_cranking_prop[i]->getBoolValue());
226   }
227
228
229   ////////////////////////////////////////////////////////////////////
230   // Update the wind noise.
231   ////////////////////////////////////////////////////////////////////
232
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);
242   } else {
243     set_playing("wind", false);
244   }
245
246
247   ////////////////////////////////////////////////////////////////////
248   // Update the stall horn.
249   ////////////////////////////////////////////////////////////////////
250
251   double stall = _stall_warning_prop->getDoubleValue();
252   if (stall > 0.0) {
253     _stall->set_volume(stall);
254     set_playing("stall", true);
255   } else {
256     set_playing("stall", false);
257   }
258
259
260   ////////////////////////////////////////////////////////////////////
261   // Update the rumble.
262   ////////////////////////////////////////////////////////////////////
263
264   float totalGear = min(cur_fdm_state->get_num_gear(), int(MAX_GEAR));
265   float gearOnGround = 0;
266
267
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.
274
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()) {
279       gearOnGround++;
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");
285         }
286         _gear_on_ground[i] = true;
287       }
288     } else {
289       _gear_on_ground[i] = false;
290     }
291   }
292
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);
304   } else {
305     set_playing("rumble", false);
306   }
307
308
309   ////////////////////////////////////////////////////////////////////////
310   // Check for flap movement.
311   ////////////////////////////////////////////////////////////////////
312
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;
317   }
318
319   // TODO: click
320
321 }
322
323
324 void
325 FGFX::set_playing (const char * soundName, bool state)
326 {
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);
333 }
334
335 // end of fg_fx.cxx