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>
46 _pause( fgGetNode("/sim/sound/pause") ),
47 _volume( fgGetNode("/sim/sound/volume") )
54 for ( i = 0; i < _sound.size(); i++ ) {
59 while ( _samplequeue.size() > 0 ) {
60 delete _samplequeue.front();
68 SGPropertyNode *node = fgGetNode("/sim/sound", true);
70 string path_str = node->getStringValue("path");
71 SGPath path( globals->get_fg_root() );
72 if (path_str.empty()) {
73 SG_LOG(SG_GENERAL, SG_ALERT, "No path in /sim/sound/path");
77 path.append(path_str.c_str());
78 SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
79 << " from " << path.str());
83 readProperties(path.str(), &root);
84 } catch (const sg_exception &) {
85 SG_LOG(SG_GENERAL, SG_ALERT,
86 "Error reading file '" << path.str() << '\'');
90 node = root.getNode("fx");
92 for (int i = 0; i < node->nChildren(); ++i) {
93 SGXmlSound *sound = new SGXmlSound();
96 sound->init(globals->get_props(), node->getChild(i),
97 globals->get_soundmgr(), globals->get_fg_root());
99 _sound.push_back(sound);
100 } catch ( sg_io_exception &e ) {
101 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
126 FGFX::update (double dt)
128 SGSoundMgr *smgr = globals->get_soundmgr();
130 if (smgr->is_working() == false) {
134 // command sound manger
135 bool pause = _pause->getBoolValue();
136 if ( pause != last_pause ) {
145 // process mesage queue
146 const string msgid = "Sequential Audio Message";
147 bool is_playing = false;
148 if ( smgr->exists( msgid ) ) {
149 if ( smgr->is_playing( msgid ) ) {
150 // still playing, do nothing
153 // current message finished, stop and remove
154 smgr->stop( msgid ); // removes source
155 smgr->remove( msgid ); // removes buffer
159 // message queue idle, add next sound if we have one
160 if ( _samplequeue.size() > 0 ) {
161 smgr->add( _samplequeue.front(), msgid );
163 smgr->play_once( msgid );
167 double volume = _volume->getDoubleValue();
168 if ( volume != last_volume ) {
169 smgr->set_volume( volume );
170 last_volume = volume;
174 // update sound effects if not paused
175 for ( unsigned int i = 0; i < _sound.size(); i++ ) {
176 _sound[i]->update(dt);
182 * add a sound sample to the message queue which is played sequentially
186 FGFX::play_message( SGSoundSample *_sample )
188 _samplequeue.push( _sample );
191 FGFX::play_message( const string path, const string fname, double volume )
193 if (globals->get_soundmgr()->is_working() == false) {
196 SGSoundSample *sample;
197 sample = new SGSoundSample( path.c_str(), fname.c_str() );
198 sample->set_volume( volume );
199 play_message( sample );