]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
675a6c0571617dd53f6cb2903e1075143274326f
[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
26 // FIXME: remove direct dependencies
27 #include <Controls/controls.hxx>
28 #include <FDM/flight.hxx>
29 #include <Main/fg_props.hxx>
30
31
32 FGFX::FGFX ()
33   : _is_cranking(false),
34     _is_stalling(false),
35     _is_rumbling(false),
36     _engine(0),
37     _crank(0),
38     _wind(0),
39     _stall(0),
40     _rumble(0),
41     _flaps(0),
42     _squeal(0),
43     _click(0)
44 {
45 }
46
47 FGFX::~FGFX ()
48 {
49                                 // FIXME: is this right, or does the
50                                 // sound manager assume pointer ownership?
51   delete _engine;
52   delete _crank;
53   delete _wind;
54   delete _stall;
55   delete _rumble;
56
57   delete _flaps;
58   delete _squeal;
59   delete _click;
60 }
61
62
63 void
64 FGFX::init ()
65 {
66   FGSoundMgr * mgr = globals->get_soundmgr();
67
68   //
69   // Create and add the engine sound
70   //
71   _engine =
72     new FGSimpleSound(fgGetString("/sim/sounds/engine", "Sounds/wasp.wav"));
73   mgr->add(_engine, "engine loop");
74   mgr->play_looped("engine loop");
75
76   SG_LOG( SG_GENERAL, SG_INFO,
77           "Rate = " << _engine->get_sample()->getRate()
78           << "  Bps = " << _engine->get_sample()->getBps()
79           << "  Stereo = " << _engine->get_sample()->getStereo() );
80
81
82   //
83   // Create and add the cranking sound.
84   //
85   _crank =
86     new FGSimpleSound(fgGetString("/sim/sounds/cranking",
87                                   "Sounds/cranking.wav"));
88   mgr->add(_crank, "crank");
89   _crank->set_pitch(1.5);
90   _crank->set_volume(0.25);
91
92
93   //
94   // Create and add the wind noise.
95   //
96   _wind =
97     new FGSimpleSound(fgGetString("/sim/sounds/wind", "Sounds/wind.wav"));
98   mgr->add(_wind, "wind");
99   mgr->play_looped("wind");
100
101
102   //
103   // Create and add the stall noise.
104   //
105   _stall = new FGSimpleSound(fgGetString("/sim/sounds/stall",
106                                          "Sounds/stall.wav"));
107   mgr->add(_stall, "stall");
108
109   //
110   // Create and add the rumble noise.
111   //
112   _rumble = new FGSimpleSound(fgGetString("/sim/sounds/rumble",
113                                           "Sounds/rumble.wav"));
114   mgr->add(_rumble, "rumble");
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 void
131 FGFX::bind ()
132 {
133 }
134
135 void
136 FGFX::unbind ()
137 {
138 }
139
140 void
141 FGFX::update ()
142 {
143   // FGSoundMgr * mgr = globals->get_soundmgr();
144
145
146   ////////////////////////////////////////////////////////////////////
147   // Update the engine sound.
148   ////////////////////////////////////////////////////////////////////
149
150   if (fgGetBool("/engines/engine[0]/running")) { // FIXME
151           // pitch corresponds to rpm
152           // volume corresponds to manifold pressure
153
154     double rpm_factor;
155     if (cur_fdm_state->get_engine(0) != NULL)
156       rpm_factor = cur_fdm_state->get_engine(0)->get_RPM() / 2500.0;
157     else
158       rpm_factor = 1.0;
159
160     double pitch = 0.3 + rpm_factor * 3.0;
161
162     // don't run at absurdly slow rates -- not realistic
163     // and sounds bad to boot.  :-)
164     if (pitch < 0.7)
165       pitch = 0.7;
166     if (pitch > 5.0)
167       pitch = 5.0;
168
169     double mp_factor;
170     if (cur_fdm_state->get_engine(0) != NULL)
171       mp_factor = cur_fdm_state->get_engine(0)->get_Manifold_Pressure() / 100;
172     else
173       mp_factor = 0.3;
174
175     double volume = 0.15 + mp_factor / 2.0;
176
177     if (volume < 0.15)
178       volume = 0.15;
179     if (volume > 0.5)
180       volume = 0.5;
181
182     _engine->set_pitch( pitch );
183     _engine->set_volume( volume );
184   } else {
185     _engine->set_pitch(0.0);
186     _engine->set_volume(0.0);
187   }
188
189
190   ////////////////////////////////////////////////////////////////////
191   // Update the cranking sound.
192   ////////////////////////////////////////////////////////////////////
193
194   if (fgGetBool("/engines/engine[0]/cranking")) { // FIXME
195     if(!_is_cranking) {
196       globals->get_soundmgr()->play_looped("crank");
197       _is_cranking = true;
198     }
199   } else {
200     if(_is_cranking) {
201       globals->get_soundmgr()->stop("crank");
202       _is_cranking = false;
203     }
204   }
205
206
207   ////////////////////////////////////////////////////////////////////
208   // Update the wind noise.
209   ////////////////////////////////////////////////////////////////////
210
211   float rel_wind = cur_fdm_state->get_V_rel_wind();
212   float volume = rel_wind/300.0;        // FIXME!!!
213   _wind->set_volume(volume);
214
215
216   // TODO: stall
217
218   // TODO: rumble
219
220   // TODO: flaps
221
222   // TODO: squeal
223
224   // TODO: click
225
226 }
227
228 // end of fg_fx.cxx