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);
73 string path_str = node->getStringValue("path");
74 SGPath path( globals->get_fg_root() );
75 if (path_str.empty()) {
76 SG_LOG(SG_GENERAL, SG_ALERT, "No path in /sim/sound/path");
80 path.append(path_str.c_str());
81 SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
82 << " from " << path.str());
86 readProperties(path.str(), &root);
87 } catch (const sg_exception &) {
88 SG_LOG(SG_GENERAL, SG_ALERT,
89 "Error reading file '" << path.str() << '\'');
93 node = root.getNode("fx");
95 for (int 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());
129 FGFX::update (double dt)
131 SGSoundMgr *smgr = globals->get_soundmgr();
133 if (smgr->is_working() == false) {
137 // command sound manger
138 bool pause = _pause->getBoolValue();
139 if ( pause != last_pause ) {
148 // process mesage queue
149 const string msgid = "Sequential Audio Message";
150 bool is_playing = false;
151 if ( smgr->exists( msgid ) ) {
152 if ( smgr->is_playing( msgid ) ) {
153 // still playing, do nothing
156 // current message finished, stop and remove
157 smgr->stop( msgid ); // removes source
158 smgr->remove( msgid ); // removes buffer
162 // message queue idle, add next sound if we have one
163 if ( _samplequeue.size() > 0 ) {
164 smgr->add( _samplequeue.front(), msgid );
166 smgr->play_once( msgid );
170 double volume = _volume->getDoubleValue();
171 if ( volume != last_volume ) {
172 smgr->set_volume( volume );
173 last_volume = volume;
177 // update sound effects if not paused
178 for ( unsigned int i = 0; i < _sound.size(); i++ ) {
179 _sound[i]->update(dt);
185 * add a sound sample to the message queue which is played sequentially
189 FGFX::play_message( SGSoundSample *_sample )
191 _samplequeue.push( _sample );
194 FGFX::play_message( const string path, const string fname, double volume )
196 if (globals->get_soundmgr()->is_working() == false) {
199 SGSoundSample *sample;
200 sample = new SGSoundSample( path.c_str(), fname.c_str() );
201 sample->set_volume( volume );
202 play_message( sample );