1 // fg_fx.cxx -- Sound effect management class implementation
3 // Started by David Megginson, October 2001
4 // (Reuses some code from main.cxx, probably by Curtis Olson)
6 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
25 #pragma warning (disable: 4786)
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>
41 #include <Main/fg_props.hxx>
43 #include <simgear/scene/model/placement.hxx>
44 #include <Model/acmodel.hxx>
45 #include <Main/viewer.hxx>
48 last_visitor_pos(SGVec3d::zeros()),
49 last_model_pos(SGVec3d::zeros()),
52 _pause( fgGetNode("/sim/sound/pause") ),
53 _volume( fgGetNode("/sim/sound/volume") )
60 for ( i = 0; i < _sound.size(); i++ ) {
65 while ( _samplequeue.size() > 0 ) {
66 delete _samplequeue.front();
74 SGPropertyNode *node = fgGetNode("/sim/sound", true);
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");
83 path.append(path_str.c_str());
84 SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
85 << " from " << path.str());
89 readProperties(path.str(), &root);
90 } catch (const sg_exception &) {
91 SG_LOG(SG_GENERAL, SG_ALERT,
92 "Error reading file '" << path.str() << '\'');
96 node = root.getNode("fx");
98 for (int i = 0; i < node->nChildren(); ++i) {
99 SGXmlSound *sound = new SGXmlSound();
102 sound->init(globals->get_props(), node->getChild(i),
103 globals->get_soundmgr(), globals->get_fg_root());
105 _sound.push_back(sound);
106 } catch ( sg_exception &e ) {
107 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
132 FGFX::update (double dt)
134 SGSoundMgr *smgr = globals->get_soundmgr();
136 if (smgr->is_working() == false) {
140 // command sound manger
141 bool pause = _pause->getBoolValue();
142 if ( pause != last_pause ) {
151 // process mesage queue
152 const string msgid = "Sequential Audio Message";
153 bool is_playing = false;
154 if ( smgr->exists( msgid ) ) {
155 if ( smgr->is_playing( msgid ) ) {
156 // still playing, do nothing
159 // current message finished, stop and remove
160 smgr->stop( msgid ); // removes source
161 smgr->remove( msgid ); // removes buffer
165 // message queue idle, add next sound if we have one
166 if ( _samplequeue.size() > 0 ) {
167 smgr->add( _samplequeue.front(), msgid );
169 smgr->play_once( msgid );
173 double volume = _volume->getDoubleValue();
174 if ( volume != last_volume ) {
175 smgr->set_volume( volume );
176 last_volume = volume;
180 // update sound effects if not paused
181 for ( unsigned int i = 0; i < _sound.size(); i++ ) {
182 _sound[i]->update(dt);
188 FGFX::update_fx_late(double dt)
190 SGSoundMgr *smgr = globals->get_soundmgr();
191 if (!smgr->is_working()) {
196 update_pos_and_orientation(smgr, dt);
200 * add a sound sample to the message queue which is played sequentially
204 FGFX::play_message( SGSoundSample *_sample )
206 _samplequeue.push( _sample );
209 FGFX::play_message( const std::string& path, const std::string& fname, double volume )
211 if (globals->get_soundmgr()->is_working() == true) {
212 SGSoundSample *sample;
213 sample = new SGSoundSample( path.c_str(), fname.c_str() );
214 sample->set_volume( volume );
215 play_message( sample );
220 FGFX::update_pos_and_orientation(SGSoundMgr *smgr, double dt)
222 SGModelPlacement *model = globals->get_aircraft_model()->get3DModel();
223 FGViewer *observer = globals->get_current_view();
225 // Right now we make a simplifying assumption that the primary
226 // aircraft is the source of all sounds and that all sounds are
227 // positioned in the aircraft base
228 // EMH: Note: this is fine, to hear multiple aircraft simulataniously
229 // we just have to trigger one instance of the FGFX class for every
232 // get the orientation
233 const SGQuatd view_or = observer->getViewOrientation();
234 SGQuatd surf_or = SGQuatd::fromLonLat(observer->getPosition());
236 SGQuatd model_or = SGQuatd::fromYawPitchRollDeg(
237 model->getHeadingDeg(),
238 model->getPitchDeg(),
239 model->getRollDeg());
241 // get the up and at vector in the aircraft base
242 // (ok, the up vector is a down vector, but the coordinates
243 // are finally calculated in a left hand system and openal
244 // lives in a right hand system. Therefore we need to pass
245 // the down vector to get correct stereo sound.)
247 = model_or.rotateBack(surf_or.rotateBack(view_or.rotate(SGVec3d(0,1,0))));
249 = model_or.rotateBack(surf_or.rotateBack(view_or.rotate(SGVec3d(0,0,1))));
251 // get the location data for the primary FDM (now hardcoded to ac model)...
252 // EMH: to add multiple sound sources this should be replaced
253 SGVec3d absolute_view_pos = SGVec3d::fromGeod(model->getPosition());
255 // calculate speed of visitor and model
256 SGVec3d moved = last_visitor_pos - observer->get_view_pos();
257 last_visitor_pos = observer->get_view_pos();
258 SGVec3f listener_vel(model_or.rotateBack(surf_or.rotateBack(moved)));
260 moved = last_model_pos - absolute_view_pos;
261 last_model_pos = absolute_view_pos;
262 SGVec3f model_vel(model_or.rotateBack(surf_or.rotateBack(moved)));
269 // checking, if the listener pos has moved suddenly
270 if (length(listener_vel) > 1000) {
271 // check if the relative speed model vs listener has moved suddenly, too
272 SGVec3f delta_vel = listener_vel - model_vel;
273 if (length(delta_vel) > 1000)
275 smgr->set_listener_vel(model_vel.data());
277 smgr->set_listener_vel(listener_vel.data());
279 smgr->set_listener_vel( listener_vel.data());
282 // set positional offset for sources
283 SGVec3d dsource_pos_offset = observer->get_view_pos() - absolute_view_pos;
284 dsource_pos_offset = model_or.rotateBack(surf_or.rotateBack(
288 smgr->set_source_pos_all( SGVec3f(dsource_pos_offset).data() );
289 smgr->set_source_vel_all(model_vel.data() );
292 for (int i = 0; i < 3; i++) {
293 orient[i] = sgv_at[i];
294 orient[i + 3] = sgv_up[i];
296 smgr->set_listener_orientation( orient );
298 // The listener is always positioned at the origin.
299 smgr->set_listener_pos( SGVec3f::zeros().data() );