]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
ad9c4ed47ad35d57bef8976d1c19777a7bf4d0a1
[flightgear.git] / src / Sound / fg_fx.cxx
1 // fg_fx.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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifdef _MSC_VER
25 #pragma warning (disable: 4786)
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/structure/exception.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/props/props.hxx>
36 #include <simgear/sound/xmlsound.hxx>
37
38 #include <Main/fg_props.hxx>
39
40 #include <simgear/scene/model/placement.hxx>
41 #include <Model/acmodel.hxx>
42 #include <Main/viewer.hxx>
43
44 #include "fg_fx.hxx"
45
46
47 FGFX::FGFX () :
48     last_pause( true ),
49     last_volume( 0.0 ),
50     _pause( fgGetNode("/sim/sound/pause") ),
51     _volume( fgGetNode("/sim/sound/volume") )
52 {
53     sgdSetVec3(last_visitor_pos, 0, 0, 0);
54     sgdSetVec3(last_model_pos, 0, 0, 0);
55 }
56
57 FGFX::~FGFX ()
58 {
59     unsigned int i;
60     for ( i = 0; i < _sound.size(); i++ ) {
61         delete _sound[i];
62     }
63     _sound.clear();
64
65     while ( _samplequeue.size() > 0 ) {
66         delete _samplequeue.front();
67         _samplequeue.pop();
68     }
69 }
70
71 void
72 FGFX::init()
73 {
74     SGPropertyNode *node = fgGetNode("/sim/sound", true);
75
76     string path_str = node->getStringValue("path");
77     SGPath path( globals->get_fg_root() );
78     if (path_str.empty()) {
79         SG_LOG(SG_GENERAL, SG_ALERT, "No path in /sim/sound/path");
80         return;
81     }
82
83     path.append(path_str.c_str());
84     SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
85            << " from " << path.str());
86
87     SGPropertyNode root;
88     try {
89         readProperties(path.str(), &root);
90     } catch (const sg_exception &) {
91         SG_LOG(SG_GENERAL, SG_ALERT,
92                "Error reading file '" << path.str() << '\'');
93         return;
94     }
95
96     node = root.getNode("fx");
97     if(node) {
98         for (int i = 0; i < node->nChildren(); ++i) {
99             SGXmlSound *sound = new SGXmlSound();
100   
101             try {
102                 sound->init(globals->get_props(), node->getChild(i),
103                             globals->get_soundmgr(), globals->get_fg_root());
104   
105                 _sound.push_back(sound);
106             } catch ( sg_io_exception &e ) {
107                 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
108                 delete sound;
109             }
110         }
111     }
112 }
113
114 void
115 FGFX::reinit()
116 {
117    _sound.clear();
118    init();
119 };
120
121 void
122 FGFX::bind ()
123 {
124 }
125
126 void
127 FGFX::unbind ()
128 {
129 }
130
131 void
132 FGFX::update (double dt)
133 {
134     SGSoundMgr *smgr = globals->get_soundmgr();
135
136     if (smgr->is_working() == false) {
137         return;
138     }
139
140     smgr->update(dt);
141     update_pos_and_orientation(smgr, dt);
142
143     // command sound manger
144     bool pause = _pause->getBoolValue();
145     if ( pause != last_pause ) {
146         if ( pause ) {
147             smgr->pause();
148         } else {
149             smgr->resume();
150         }
151         last_pause = pause;
152     }
153
154     // process mesage queue
155     const string msgid = "Sequential Audio Message";
156     bool is_playing = false;
157     if ( smgr->exists( msgid ) ) {
158         if ( smgr->is_playing( msgid ) ) {
159             // still playing, do nothing
160             is_playing = true;
161         } else {
162             // current message finished, stop and remove
163             smgr->stop( msgid );   // removes source
164             smgr->remove( msgid ); // removes buffer
165         }
166     }
167     if ( !is_playing ) {
168         // message queue idle, add next sound if we have one
169         if ( _samplequeue.size() > 0 ) {
170             smgr->add( _samplequeue.front(), msgid );
171             _samplequeue.pop();
172             smgr->play_once( msgid );
173         }
174     }
175
176     double volume = _volume->getDoubleValue();
177     if ( volume != last_volume ) {
178         smgr->set_volume( volume );        
179         last_volume = volume;
180     }
181
182     if ( !pause ) {
183         // update sound effects if not paused
184         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
185             _sound[i]->update(dt);
186         }
187     }
188 }
189
190 /**
191  * add a sound sample to the message queue which is played sequentially
192  * in order.
193  */
194 void
195 FGFX::play_message( SGSoundSample *_sample )
196 {
197     _samplequeue.push( _sample );
198 }
199 void
200 FGFX::play_message( const string path, const string fname, double volume )
201 {
202     if (globals->get_soundmgr()->is_working() == false) {
203         return;
204     }
205     SGSoundSample *sample;
206     sample = new SGSoundSample( path.c_str(), fname.c_str() );
207     sample->set_volume( volume );
208     play_message( sample );
209 }
210
211 void
212 FGFX::update_pos_and_orientation(SGSoundMgr *smgr, double dt)
213 {
214     SGModelPlacement *model = globals->get_aircraft_model()->get3DModel();
215     FGViewer *observer = globals->get_current_view();
216
217     // Right now we make a simplifying assumption that the primary
218     // aircraft is the source of all sounds and that all sounds are
219     // positioned in the aircraft base
220     // EMH: Note: this is fine, to hear multiple aircraft simulataniously
221     //      we just have to trigger one instance of the FGFX class for every
222     //      aircraft
223
224     // get the orientation
225     const SGQuatd view_or = observer->getViewOrientation();
226     SGQuatd surf_or = SGQuatd::fromLonLatDeg(
227                                 observer->getLongitude_deg(),
228                                 observer->getLatitude_deg());
229     SGQuatd model_or = SGQuatd::fromYawPitchRollDeg(
230                                 model->getHeadingDeg(),
231                                 model->getPitchDeg(),
232                                 model->getRollDeg());
233
234     // get the up and at vector in the aircraft base
235     // (ok, the up vector is a down vector, but the coordinates
236     // are finally calculated in a left hand system and openal
237     // lives in a right hand system. Therefore we need to pass
238     // the down vector to get correct stereo sound.)
239     SGVec3d sgv_up, sgv_at;
240     sgVec3 up, at;
241
242     sgv_up
243       = model_or.rotateBack(surf_or.rotateBack(view_or.rotate(SGVec3d(0,1,0))));
244     sgSetVec3(up, sgv_up[0], sgv_up[1], sgv_up[2]);
245     sgv_at
246       = model_or.rotateBack(surf_or.rotateBack(view_or.rotate(SGVec3d(0,0,1))));
247     sgSetVec3(at, sgv_at[0], sgv_at[1], sgv_at[2]);
248
249     // get the location data for the primary FDM (now hardcoded to ac model)...
250     // EMH: to add multiple sound sources this should be replaced
251     SGLocation *acmodel_loc = (SGLocation *)model->getSGLocation();
252
253     // calculate speed of visitor and model
254     sgVec3 listener_vel, model_vel;
255     SGVec3d SGV3d_help;
256     sgdVec3 sgdv3_help;
257     sgdVec3 sgdv3_null = {0, 0, 0};
258     
259
260     sgdSubVec3(sgdv3_help,
261                 last_visitor_pos, (double *)&observer->get_view_pos());
262     sgdAddVec3(last_visitor_pos,
263                 sgdv3_null, (double *)&observer->get_view_pos());
264
265     SGV3d_help = model_or.rotateBack(
266                     surf_or.rotateBack(
267                        SGVec3d(sgdv3_help[0], sgdv3_help[1], sgdv3_help[2])
268                     ));
269     sgSetVec3(listener_vel, SGV3d_help[0], SGV3d_help[1], SGV3d_help[2]);
270
271     sgdSubVec3(sgdv3_help, last_model_pos,acmodel_loc->get_absolute_view_pos());
272     sgdAddVec3(last_model_pos, sgdv3_null,acmodel_loc->get_absolute_view_pos());
273
274     SGV3d_help = model_or.rotateBack(
275                     surf_or.rotateBack(
276                        SGVec3d(sgdv3_help[0], sgdv3_help[1], sgdv3_help[2])
277                     ));
278     sgSetVec3( model_vel, SGV3d_help[0], SGV3d_help[1], SGV3d_help[2]);
279
280     if (dt > 0) {
281         sgScaleVec3( model_vel, 1 / dt );
282         sgScaleVec3( listener_vel, 1 / dt );
283     }
284
285     // checking, if the listener pos has moved suddenly
286     if (sgLengthVec3(listener_vel) > 1000)
287     {
288         // check if the relative speed model vs listener has moved suddenly, too
289         sgVec3 delta_vel;
290         sgSubVec3(delta_vel, listener_vel, model_vel);
291         if (sgLengthVec3(delta_vel) > 1000)
292             // a sane value
293             sgSetVec3(listener_vel, model_vel[0], model_vel[1], model_vel[2]);
294         else
295             smgr->set_listener_vel(listener_vel);
296     }
297     else
298         smgr->set_listener_vel( listener_vel );
299
300     // set positional offset for sources
301     sgdVec3 dsource_pos_offset;
302     sgdSubVec3( dsource_pos_offset,
303                 (double*) &observer->get_view_pos(),
304                 acmodel_loc->get_absolute_view_pos() );
305     SGVec3d sgv_dsource_pos_offset;
306     sgv_dsource_pos_offset = model_or.rotateBack(
307                                 surf_or.rotateBack(
308                                    SGVec3d(dsource_pos_offset[0],
309                                           dsource_pos_offset[1],
310                                           dsource_pos_offset[2])
311                                 ));
312     sgVec3 source_pos_offset;
313     sgSetVec3(source_pos_offset,
314                  sgv_dsource_pos_offset[0],
315                  sgv_dsource_pos_offset[1],
316                  sgv_dsource_pos_offset[2]);
317
318     smgr->set_source_pos_all( source_pos_offset );
319     smgr->set_source_vel_all( model_vel );
320
321     float orient[6];
322     for (int i = 0; i < 3; i++) {
323         orient[i] = sgv_at[i];
324         orient[i + 3] = sgv_up[i];
325     }
326     smgr->set_listener_orientation( orient );
327
328     // The listener is always positioned at the origin.
329     sgVec3 listener_pos;
330     sgSetVec3( listener_pos, 0.0, 0.0, 0.0 );
331     smgr->set_listener_pos( listener_pos );
332 }
333
334 // end of fg_fx.cxx