]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
9a3f85ecf04e085a9e94ef55166d763ca3141722
[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 <FDM/flight.hxx>       // FIXME: remove direct dependencies
25 #include <Main/fg_props.hxx>
26
27 #include "fg_fx.hxx"
28
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     _old_gear_position(0),
44     _wind(0),
45     _stall(0),
46     _rumble(0),
47     _flaps(0),
48     _gear_up(0),
49     _gear_dn(0),
50     _squeal(0),
51     _click(0),
52     _stall_warning_prop(0),
53     _flaps_prop(0),
54     _gear_prop(0)
55 {
56   for (int i = 0; i < MAX_ENGINES; i++) {
57     _engine[i] = 0;
58     _crank[i] = 0;
59     _engine_running_prop[i] = 0;
60     _engine_cranking_prop[i] = 0;
61   }
62 }
63
64 FGFX::~FGFX ()
65 {
66                                 // FIXME: is this right, or does the
67                                 // sound manager assume pointer ownership?
68   for (int i = 0; i < MAX_ENGINES; i++) {
69     delete _engine[i];
70     delete _crank[i];
71   }
72   delete _wind;
73   delete _stall;
74   delete _rumble;
75
76   delete _flaps;
77   delete _gear_up;
78   delete _gear_dn;
79   delete _squeal;
80   delete _click;
81 }
82
83
84 void
85 FGFX::init ()
86 {
87   FGSoundMgr * mgr = globals->get_soundmgr();
88   int i;
89
90   //
91   // Create and add engine-related sounds.
92   //
93   for (i = 0; i < MAX_ENGINES; i++) {
94                                 // Engine
95     _engine[i] =
96       new FGSimpleSound(fgGetString("/sim/sounds/engine/path",
97                                     "Sounds/wasp.wav"));
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]);
101
102                                 // Starter
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.175));
106     _crank[i]->set_pitch(fgGetFloat("/sim/sounds/cranking/pitch", 1.25));
107     mgr->add(_crank[i], crank_names[i]);
108   }
109
110
111   //
112   // Create and add the wind noise.
113   //
114   _wind = new FGSimpleSound(fgGetString("/sim/sounds/wind/path",
115                                         "Sounds/wind.wav"));
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");
119
120
121   //
122   // Create and add the stall noise.
123   //
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");
129
130   //
131   // Create and add the rumble noise.
132   //
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");
138
139
140   //
141   // Create and add the flaps noise
142   //
143   _flaps = new FGSimpleSound(fgGetString("/sim/sounds/flaps/path",
144                                          "Sounds/flaps.wav"));
145   _flaps->set_volume(fgGetFloat("/sim/sounds/flaps/volume", 0.5));
146   _flaps->set_pitch(fgGetFloat("/sim/sounds/flaps/pitch", 1.0));
147   mgr->add(_flaps, "flaps");
148
149   //
150   // Create and add the gear noises.
151   //
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");
162
163   //
164   // Create and add the squeal noise.
165   //
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");
171
172   //
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.
176   //
177   for (i = 0; i < MAX_GEAR; i++) {
178       _wheel_spin[i] = 0.0;
179   }
180
181   //
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");
188
189
190   ////////////////////////////////////////////////////////////////////
191   // Grab some properties.
192   ////////////////////////////////////////////////////////////////////
193
194   for (i = 0; i < MAX_ENGINES; i++) {
195     char buf[100];
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);
200   }
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);
205 }
206
207 void
208 FGFX::bind ()
209 {
210 }
211
212 void
213 FGFX::unbind ()
214 {
215 }
216
217 void
218 FGFX::update (int dt)
219 {
220   FGSoundMgr * mgr = globals->get_soundmgr();
221   int i;
222
223   ////////////////////////////////////////////////////////////////////
224   // Update the engine sound.
225   ////////////////////////////////////////////////////////////////////
226   
227   for (i = 0; i < MAX_ENGINES; i++) {
228
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
233
234       double rpm_factor;
235       if ( cur_fdm_state->get_num_engines() > 0 )
236         rpm_factor = cur_fdm_state->get_engine(i)->get_RPM() / 2500.0;
237       else
238         rpm_factor = 1.0;
239
240       double pitch = 0.3 + rpm_factor * 3.0;
241
242       // don't run at absurdly slow rates -- not realistic
243       // and sounds bad to boot.  :-)
244       if (pitch < 0.7)
245         pitch = 0.7;
246       if (pitch > 5.0)
247         pitch = 5.0;
248
249       double mp_factor;
250       if ( cur_fdm_state->get_num_engines() > 0 )
251         mp_factor =
252           cur_fdm_state->get_engine(i)->get_Manifold_Pressure() / 100;
253       else
254         mp_factor = 0.3;
255
256       double volume = 0.15 + mp_factor / 2.0;
257
258       if (volume < 0.15)
259         volume = 0.15;
260       if (volume > 0.5)
261         volume = 0.5;
262
263       _engine[i]->set_pitch( pitch );
264       _engine[i]->set_volume( volume );
265       set_playing(engine_names[i], true);
266     } else {
267       set_playing(engine_names[i], false);
268     }
269
270                                 // FIXME
271     set_playing(crank_names[i], _engine_cranking_prop[i]->getBoolValue());
272   }
273
274
275   ////////////////////////////////////////////////////////////////////
276   // Update the wind noise.
277   ////////////////////////////////////////////////////////////////////
278
279   float rel_wind = cur_fdm_state->get_V_rel_wind(); // FPS
280   float airspeed_kt = cur_fdm_state->get_V_equiv_kts();
281   if (rel_wind > 60.0) {        // a little off 30kt
282     // float volume = rel_wind/600.0;   // FIXME!!!
283     float volume = rel_wind/937.0;      // FIXME!!!
284     double pitch = 1.0+(airspeed_kt/113.0);
285     _wind->set_volume(volume);
286     _wind->set_pitch(pitch);
287     set_playing("wind", true);
288   } else {
289     set_playing("wind", false);
290   }
291
292
293   ////////////////////////////////////////////////////////////////////
294   // Update the stall horn.
295   ////////////////////////////////////////////////////////////////////
296
297   double stall = _stall_warning_prop->getDoubleValue();
298   double vc = _vc_prop->getDoubleValue();
299   if (stall > 0.0 && vc > 30.0) {
300     _stall->set_volume(stall);
301     set_playing("stall", true);
302   } else {
303     set_playing("stall", false);
304   }
305
306
307   ////////////////////////////////////////////////////////////////////
308   // Update the rumble.
309   ////////////////////////////////////////////////////////////////////
310
311   float totalGear = min(cur_fdm_state->get_num_gear(), int(MAX_GEAR));
312   float gearOnGround = 0;
313
314
315                                 // Calculate whether a squeal is
316                                 // required, and set the volume.
317                                 // Currently, the squeal volume is the
318                                 // current local down velocity in feet
319                                 // per second divided by 10.0, and
320                                 // will not be played if under 0.1.
321
322                                 // FIXME: take rotational velocities
323                                 // into account as well.
324   for (i = 0; i < totalGear; i++) {
325     // cout << "air speed = " << cur_fdm_state->get_V_equiv_kts();
326     // cout << "  wheel " << i << " speed = " << _wheel_spin[i];
327     if (cur_fdm_state->get_gear_unit(i)->GetWoW()) {
328       gearOnGround++;
329       if (!_gear_on_ground[i]) {
330           // wheel just touched down
331           // 3 parts horizontal velocity + 1 part vertical velocity
332           double squeal_volume = 0.75 * cur_fdm_state->get_V_equiv_kts() / 90.0
333               + 0.25 * cur_fdm_state->get_V_down() / 5.0;
334
335           // scale volume by difference between wheel spin speed and
336           // ground speed
337           double diff = fabs( cur_fdm_state->get_V_equiv_kts()
338                               - _wheel_spin[i] );
339           // cout << " speed diff = " << diff;
340           double scale_factor = 0.0;
341           if ( diff > 10 ) {
342               scale_factor = ( diff - 10.0 ) / 30.0f;
343               if ( scale_factor > 1.0 ) { scale_factor = 1.0; }
344           }
345           // cout << " scale_factor = " << scale_factor;
346           squeal_volume *= scale_factor;
347
348           if (squeal_volume > 0.1) {
349               _squeal->set_volume(squeal_volume);
350               _squeal->set_pitch(1.25);
351               mgr->play_once("squeal");
352           }
353           _gear_on_ground[i] = true;
354       }
355       // cout << " wow";
356       _wheel_spin[i] = cur_fdm_state->get_V_equiv_kts();
357     } else {
358         // cout << " airborn";
359         _gear_on_ground[i] = false;
360         /* fix me: wheel spindown is currently frame rate dependent which
361            it shouldn't be */
362         _wheel_spin[i] -= 0.2;
363         if ( _wheel_spin[i] < 0.0 ) { _wheel_spin[i] = 0.0; }
364     }
365     // cout << endl;
366   }
367
368                                 // Now, if any of the gear is in
369                                 // contact with the ground play the
370                                 // rumble sound.  The volume is the
371                                 // absolute velocity in knots divided
372                                 // by 120.0.  No rumble will be played
373                                 // if the velocity is under 6kt.
374   double speed = cur_fdm_state->get_V_equiv_kts();
375   if (gearOnGround > 0 && speed >= 6.0) {
376     double volume = 2.0 * (gearOnGround/totalGear) * (speed/60.0);
377     _rumble->set_volume(volume);
378     set_playing("rumble", true);
379   } else {
380     set_playing("rumble", false);
381   }
382
383
384   ////////////////////////////////////////////////////////////////////
385   // Check for flap movement.
386   ////////////////////////////////////////////////////////////////////
387
388   double flap_position = _flaps_prop->getDoubleValue();
389   if (fabs(flap_position - _old_flap_position) > 0.1) {
390     mgr->play_once("flaps");
391     _old_flap_position = flap_position;
392   }
393
394
395   ////////////////////////////////////////////////////////////////////
396   // Check for gear movement.
397   ////////////////////////////////////////////////////////////////////
398   
399   double gear_position = _gear_prop->getDoubleValue();
400   if (gear_position != _old_gear_position) {
401     if (gear_position < _old_gear_position) {
402       mgr->play_once("gear-up");
403     } else {
404       mgr->play_once("gear-down");
405     }
406     _old_gear_position = gear_position;
407   }
408
409   // TODO: click
410
411 }
412
413
414 void
415 FGFX::set_playing (const char * soundName, bool state)
416 {
417   FGSoundMgr * mgr = globals->get_soundmgr();
418   bool playing = mgr->is_playing(soundName);
419   if (state && !playing)
420     mgr->play_looped(soundName);
421   else if (!state && playing)
422     mgr->stop(soundName);
423 }
424
425 // end of fg_fx.cxx