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>
35 # define exception c_exception
37 #include <simgear/misc/sg_path.hxx>
38 #include <simgear/props/props.hxx>
39 #include <simgear/sound/xmlsound.hxx>
41 #include <Main/fg_props.hxx>
49 _pause( fgGetNode("/sim/sound/pause") ),
50 _volume( fgGetNode("/sim/sound/volume") )
57 for ( i = 0; i < _sound.size(); i++ ) {
62 while ( _samplequeue.size() > 0 ) {
63 delete _samplequeue.front();
71 SGPropertyNode *node = fgGetNode("/sim/sound", true);
74 string path_str = node->getStringValue("path");
75 SGPath path( globals->get_fg_root() );
76 if (path_str.empty()) {
77 SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path in configuration file.");
81 path.append(path_str.c_str());
82 SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
83 << " from " << path.str());
87 readProperties(path.str(), &root);
88 } catch (const sg_exception &) {
89 SG_LOG(SG_GENERAL, SG_ALERT,
90 "Incorrect path specified in configuration file");
94 node = root.getNode("fx");
95 for (i = 0; i < node->nChildren(); i++) {
96 SGXmlSound *sound = new SGXmlSound();
99 sound->init(globals->get_props(), node->getChild(i),
100 globals->get_soundmgr(), globals->get_fg_root());
102 _sound.push_back(sound);
103 } catch ( sg_io_exception &e ) {
104 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
128 FGFX::update (double dt)
130 SGSoundMgr *smgr = globals->get_soundmgr();
132 // command sound manger
133 bool pause = _pause->getBoolValue();
134 if ( pause != last_pause ) {
143 // process mesage queue
144 const string msgid = "Sequential Audio Message";
145 bool is_playing = false;
146 if ( smgr->exists( msgid ) ) {
147 if ( smgr->is_playing( msgid ) ) {
148 // still playing, do nothing
151 // current message finished, stop and remove
152 smgr->stop( msgid ); // removes source
153 smgr->remove( msgid ); // removes buffer
157 // message queue idle, add next sound if we have one
158 if ( _samplequeue.size() > 0 ) {
159 smgr->add( _samplequeue.front(), msgid );
161 smgr->play_once( msgid );
165 double volume = _volume->getDoubleValue();
166 if ( volume != last_volume ) {
167 smgr->set_volume( volume );
168 last_volume = volume;
172 // update sound effects if not paused
173 for ( unsigned int i = 0; i < _sound.size(); i++ ) {
174 _sound[i]->update(dt);
180 * add a sound sample to the message queue which is played sequentially
184 FGFX::play_message( SGSoundSample *_sample )
186 _sample->set_volume( 1.0 );
187 _samplequeue.push( _sample );
190 FGFX::play_message( const string path, const string fname )
192 SGSoundSample *sample;
193 sample = new SGSoundSample( path.c_str(), fname.c_str() );
194 play_message( sample );