]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
0a5dabb60fb01106ee0b784abb20c581a5be2f7d
[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 "fg_fx.hxx"
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/structure/exception.hxx>
36 #include <simgear/misc/sg_path.hxx>
37 #include <simgear/props/props.hxx>
38 #include <simgear/sound/xmlsound.hxx>
39 #include <simgear/sound/soundmgr_openal.hxx>
40
41 #include <Main/fg_props.hxx>
42
43 #include <simgear/scene/model/placement.hxx>
44 #include <Model/acmodel.hxx>
45 #include <Main/viewer.hxx>
46
47 FGFX::FGFX () :
48     last_visitor_pos(SGVec3d::zeros()),
49     last_model_pos(SGVec3d::zeros()),
50     last_pause( true ),
51     last_volume( 0.0 ),
52     _pause( fgGetNode("/sim/sound/pause") ),
53     _volume( fgGetNode("/sim/sound/volume") )
54 {
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_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 #if 0
141     // moved back to the mainloop to prevent audio problems
142     smgr->update(dt);
143     update_pos_and_orientation(smgr, dt);
144 #endif
145
146     // command sound manger
147     bool pause = _pause->getBoolValue();
148     if ( pause != last_pause ) {
149         if ( pause ) {
150             smgr->pause();
151         } else {
152             smgr->resume();
153         }
154         last_pause = pause;
155     }
156
157     // process mesage queue
158     const string msgid = "Sequential Audio Message";
159     bool is_playing = false;
160     if ( smgr->exists( msgid ) ) {
161         if ( smgr->is_playing( msgid ) ) {
162             // still playing, do nothing
163             is_playing = true;
164         } else {
165             // current message finished, stop and remove
166             smgr->stop( msgid );   // removes source
167             smgr->remove( msgid ); // removes buffer
168         }
169     }
170     if ( !is_playing ) {
171         // message queue idle, add next sound if we have one
172         if ( _samplequeue.size() > 0 ) {
173             smgr->add( _samplequeue.front(), msgid );
174             _samplequeue.pop();
175             smgr->play_once( msgid );
176         }
177     }
178
179     double volume = _volume->getDoubleValue();
180     if ( volume != last_volume ) {
181         smgr->set_volume( volume );        
182         last_volume = volume;
183     }
184
185     if ( !pause ) {
186         // update sound effects if not paused
187         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
188             _sound[i]->update(dt);
189         }
190     }
191 }
192
193 /**
194  * add a sound sample to the message queue which is played sequentially
195  * in order.
196  */
197 void
198 FGFX::play_message( SGSoundSample *_sample )
199 {
200     _samplequeue.push( _sample );
201 }
202 void
203 FGFX::play_message( const std::string& path, const std::string& fname, double volume )
204 {
205     if (globals->get_soundmgr()->is_working() == true) {
206         SGSoundSample *sample;
207         sample = new SGSoundSample( path.c_str(), fname.c_str() );
208         sample->set_volume( volume );
209         play_message( sample );
210     }
211 }
212
213 void
214 FGFX::update_pos_and_orientation(SGSoundMgr *smgr, double dt)
215 {
216     SGModelPlacement *model = globals->get_aircraft_model()->get3DModel();
217     FGViewer *observer = globals->get_current_view();
218
219     // Right now we make a simplifying assumption that the primary
220     // aircraft is the source of all sounds and that all sounds are
221     // positioned in the aircraft base
222     // EMH: Note: this is fine, to hear multiple aircraft simulataniously
223     //      we just have to trigger one instance of the FGFX class for every
224     //      aircraft
225
226     // get the orientation
227     const SGQuatd view_or = observer->getViewOrientation();
228     SGQuatd surf_or = SGQuatd::fromLonLat(observer->getPosition());
229     
230     SGQuatd model_or = SGQuatd::fromYawPitchRollDeg(
231                                 model->getHeadingDeg(),
232                                 model->getPitchDeg(),
233                                 model->getRollDeg());
234
235     // get the up and at vector in the aircraft base
236     // (ok, the up vector is a down vector, but the coordinates
237     // are finally calculated in a left hand system and openal
238     // lives in a right hand system. Therefore we need to pass
239     // the down vector to get correct stereo sound.)
240     SGVec3d sgv_up
241       = model_or.rotateBack(surf_or.rotateBack(view_or.rotate(SGVec3d(0,1,0))));
242     SGVec3d sgv_at
243       = model_or.rotateBack(surf_or.rotateBack(view_or.rotate(SGVec3d(0,0,1))));
244
245     // get the location data for the primary FDM (now hardcoded to ac model)...
246     // EMH: to add multiple sound sources this should be replaced
247     SGVec3d absolute_view_pos = SGVec3d::fromGeod(model->getPosition());
248
249     // calculate speed of visitor and model
250     SGVec3d moved = last_visitor_pos - observer->get_view_pos();
251     last_visitor_pos = observer->get_view_pos();
252     SGVec3f listener_vel(model_or.rotateBack(surf_or.rotateBack(moved)));
253
254     moved = last_model_pos - absolute_view_pos;
255     last_model_pos = absolute_view_pos;
256     SGVec3f model_vel(model_or.rotateBack(surf_or.rotateBack(moved)));
257     
258     if (dt > 0) {
259       model_vel /= dt;
260       listener_vel /= dt;
261     }
262
263     // checking, if the listener pos has moved suddenly
264     if (length(listener_vel) > 1000) {
265         // check if the relative speed model vs listener has moved suddenly, too
266         SGVec3f delta_vel = listener_vel - model_vel;
267         if (length(delta_vel) > 1000)
268             // a sane value
269             smgr->set_listener_vel(model_vel.data());
270         else
271             smgr->set_listener_vel(listener_vel.data());
272     } else {
273       smgr->set_listener_vel( listener_vel.data());
274     }
275     
276     // set positional offset for sources
277     SGVec3d dsource_pos_offset = observer->get_view_pos() - absolute_view_pos;
278     dsource_pos_offset = model_or.rotateBack(surf_or.rotateBack(
279                                    dsource_pos_offset
280                                 ));
281
282     smgr->set_source_pos_all( SGVec3f(dsource_pos_offset).data() );
283     smgr->set_source_vel_all(model_vel.data() );
284
285     float orient[6];
286     for (int i = 0; i < 3; i++) {
287         orient[i] = sgv_at[i];
288         orient[i + 3] = sgv_up[i];
289     }
290     smgr->set_listener_orientation( orient );
291
292     // The listener is always positioned at the origin.
293     smgr->set_listener_pos( SGVec3f::zeros().data() );
294 }
295
296 // end of fg_fx.cxx