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., 675 Mass Ave, Cambridge, MA 02139, 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 &e) {
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();
98 sound->init(globals->get_props(), node->getChild(i),
99 globals->get_soundmgr(), globals->get_fg_root());
101 _sound.push_back(sound);
123 FGFX::update (double dt)
125 SGSoundMgr *smgr = globals->get_soundmgr();
127 // command sound manger
128 bool pause = _pause->getBoolValue();
129 if ( pause != last_pause ) {
138 // process mesage queue
139 const string msgid = "Sequential Audio Message";
140 bool is_playing = false;
141 if ( smgr->exists( msgid ) ) {
142 if ( smgr->is_playing( msgid ) ) {
143 // still playing, do nothing
146 // current message finished, stop and remove
147 smgr->stop( msgid ); // removes source
148 smgr->remove( msgid ); // removes buffer
152 // message queue idle, add next sound if we have one
153 if ( _samplequeue.size() > 0 ) {
154 smgr->add( _samplequeue.front(), msgid );
156 smgr->play_once( msgid );
160 double volume = _volume->getDoubleValue();
161 if ( volume != last_volume ) {
162 smgr->set_volume( volume );
163 last_volume = volume;
167 // update sound effects if not paused
168 for ( unsigned int i = 0; i < _sound.size(); i++ ) {
169 _sound[i]->update(dt);
175 * add a sound sample to the message queue which is played sequentially
179 FGFX::play_message( SGSoundSample *_sample )
181 _sample->set_volume( 1.0 );
182 _samplequeue.push( _sample );
185 FGFX::play_message( const string path, const string fname )
187 SGSoundSample *sample;
188 sample = new SGSoundSample( path.c_str(), fname.c_str() );
189 play_message( sample );