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)
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>
38 #include <Main/fg_props.hxx>
40 #include <simgear/scene/model/placement.hxx>
41 #include <Model/acmodel.hxx>
42 #include <Main/viewer.hxx>
50 _pause( fgGetNode("/sim/sound/pause") ),
51 _volume( fgGetNode("/sim/sound/volume") )
53 sgdSetVec3(last_visitor_pos, 0, 0, 0);
54 sgdSetVec3(last_model_pos, 0, 0, 0);
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) {
141 // moved back to the mainloop to prevent audio problems
143 update_pos_and_orientation(smgr, dt);
146 // command sound manger
147 bool pause = _pause->getBoolValue();
148 if ( pause != last_pause ) {
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
165 // current message finished, stop and remove
166 smgr->stop( msgid ); // removes source
167 smgr->remove( msgid ); // removes buffer
171 // message queue idle, add next sound if we have one
172 if ( _samplequeue.size() > 0 ) {
173 smgr->add( _samplequeue.front(), msgid );
175 smgr->play_once( msgid );
179 double volume = _volume->getDoubleValue();
180 if ( volume != last_volume ) {
181 smgr->set_volume( volume );
182 last_volume = volume;
186 // update sound effects if not paused
187 for ( unsigned int i = 0; i < _sound.size(); i++ ) {
188 _sound[i]->update(dt);
194 * add a sound sample to the message queue which is played sequentially
198 FGFX::play_message( SGSoundSample *_sample )
200 _samplequeue.push( _sample );
203 FGFX::play_message( const string path, const string fname, double volume )
205 if (globals->get_soundmgr()->is_working() == false) {
208 SGSoundSample *sample;
209 sample = new SGSoundSample( path.c_str(), fname.c_str() );
210 sample->set_volume( volume );
211 play_message( sample );
215 FGFX::update_pos_and_orientation(SGSoundMgr *smgr, double dt)
217 SGModelPlacement *model = globals->get_aircraft_model()->get3DModel();
218 FGViewer *observer = globals->get_current_view();
220 // Right now we make a simplifying assumption that the primary
221 // aircraft is the source of all sounds and that all sounds are
222 // positioned in the aircraft base
223 // EMH: Note: this is fine, to hear multiple aircraft simulataniously
224 // we just have to trigger one instance of the FGFX class for every
227 // get the orientation
228 const SGQuatd view_or = observer->getViewOrientation();
229 SGQuatd surf_or = SGQuatd::fromLonLatDeg(
230 observer->getLongitude_deg(),
231 observer->getLatitude_deg());
232 SGQuatd model_or = SGQuatd::fromYawPitchRollDeg(
233 model->getHeadingDeg(),
234 model->getPitchDeg(),
235 model->getRollDeg());
237 // get the up and at vector in the aircraft base
238 // (ok, the up vector is a down vector, but the coordinates
239 // are finally calculated in a left hand system and openal
240 // lives in a right hand system. Therefore we need to pass
241 // the down vector to get correct stereo sound.)
242 SGVec3d sgv_up, sgv_at;
246 = model_or.rotateBack(surf_or.rotateBack(view_or.rotate(SGVec3d(0,1,0))));
247 sgSetVec3(up, sgv_up[0], sgv_up[1], sgv_up[2]);
249 = model_or.rotateBack(surf_or.rotateBack(view_or.rotate(SGVec3d(0,0,1))));
250 sgSetVec3(at, sgv_at[0], sgv_at[1], sgv_at[2]);
252 // get the location data for the primary FDM (now hardcoded to ac model)...
253 // EMH: to add multiple sound sources this should be replaced
254 SGVec3d absolute_view_pos = SGVec3d::fromGeod(model->getPosition());
256 // calculate speed of visitor and model
257 sgVec3 listener_vel, model_vel;
260 sgdVec3 sgdv3_null = {0, 0, 0};
263 sgdSubVec3(sgdv3_help,
264 last_visitor_pos, (double *)&observer->get_view_pos());
265 sgdAddVec3(last_visitor_pos,
266 sgdv3_null, (double *)&observer->get_view_pos());
268 SGV3d_help = model_or.rotateBack(
270 SGVec3d(sgdv3_help[0], sgdv3_help[1], sgdv3_help[2])
272 sgSetVec3(listener_vel, SGV3d_help[0], SGV3d_help[1], SGV3d_help[2]);
274 sgdSubVec3(sgdv3_help, last_model_pos, absolute_view_pos.sg());
275 sgdAddVec3(last_model_pos, sgdv3_null, absolute_view_pos.sg());
277 SGV3d_help = model_or.rotateBack(
279 SGVec3d(sgdv3_help[0], sgdv3_help[1], sgdv3_help[2])
281 sgSetVec3( model_vel, SGV3d_help[0], SGV3d_help[1], SGV3d_help[2]);
284 sgScaleVec3( model_vel, 1 / dt );
285 sgScaleVec3( listener_vel, 1 / dt );
288 // checking, if the listener pos has moved suddenly
289 if (sgLengthVec3(listener_vel) > 1000)
291 // check if the relative speed model vs listener has moved suddenly, too
293 sgSubVec3(delta_vel, listener_vel, model_vel);
294 if (sgLengthVec3(delta_vel) > 1000)
296 sgSetVec3(listener_vel, model_vel[0], model_vel[1], model_vel[2]);
298 smgr->set_listener_vel(listener_vel);
301 smgr->set_listener_vel( listener_vel );
303 // set positional offset for sources
304 sgdVec3 dsource_pos_offset;
305 sgdSubVec3( dsource_pos_offset,
306 (double*) &observer->get_view_pos(),
307 absolute_view_pos.sg() );
308 SGVec3d sgv_dsource_pos_offset;
309 sgv_dsource_pos_offset = model_or.rotateBack(
311 SGVec3d(dsource_pos_offset[0],
312 dsource_pos_offset[1],
313 dsource_pos_offset[2])
315 sgVec3 source_pos_offset;
316 sgSetVec3(source_pos_offset,
317 sgv_dsource_pos_offset[0],
318 sgv_dsource_pos_offset[1],
319 sgv_dsource_pos_offset[2]);
321 smgr->set_source_pos_all( source_pos_offset );
322 smgr->set_source_vel_all( model_vel );
325 for (int i = 0; i < 3; i++) {
326 orient[i] = sgv_at[i];
327 orient[i + 3] = sgv_up[i];
329 smgr->set_listener_orientation( orient );
331 // The listener is always positioned at the origin.
333 sgSetVec3( listener_pos, 0.0, 0.0, 0.0 );
334 smgr->set_listener_pos( listener_pos );