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)
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/structure/exception.hxx>
31 # define exception c_exception
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/props/props.hxx>
35 #include <simgear/sound/xmlsound.hxx>
37 #include <Main/fg_props.hxx>
43 _volume( fgGetNode("/sim/sound/volume") ),
44 _pause( fgGetNode("/sim/sound/pause") ),
53 for ( i = 0; i < _sound.size(); i++ ) {
58 while ( _samplequeue.size() > 0 ) {
59 delete _samplequeue.front();
67 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, "Incorrect path in configuration file.");
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 &e) {
85 SG_LOG(SG_GENERAL, SG_ALERT,
86 "Incorrect path specified in configuration file");
90 node = root.getNode("fx");
91 for (i = 0; i < node->nChildren(); i++) {
92 SGXmlSound *sound = new SGXmlSound();
94 sound->init(globals->get_props(), node->getChild(i),
95 globals->get_soundmgr(), globals->get_fg_root());
97 _sound.push_back(sound);
119 FGFX::update (double dt)
121 SGSoundMgr *smgr = globals->get_soundmgr();
123 // command sound manger
124 bool pause = _pause->getBoolValue();
125 if ( pause != last_pause ) {
134 // process mesage queue
135 const string msgid = "Sequential Audio Message";
136 bool is_playing = false;
137 if ( smgr->exists( msgid ) ) {
138 if ( smgr->is_playing( msgid ) ) {
139 // still playing, do nothing
142 // current message finished, remove
143 smgr->remove( msgid );
147 // message queue idle, add next sound if we have one
148 if ( _samplequeue.size() > 0 ) {
149 smgr->add( _samplequeue.front(), msgid );
151 smgr->play_once( msgid );
155 double volume = _volume->getDoubleValue();
156 if ( volume != last_volume ) {
157 smgr->set_volume( volume );
158 last_volume = volume;
162 // update sound effects if not paused
163 for ( unsigned int i = 0; i < _sound.size(); i++ ) {
164 _sound[i]->update(dt);
170 * add a sound sample to the message queue which is played sequentially
174 FGFX::play_message( SGSoundSample *_sample )
176 _samplequeue.push( _sample );
179 FGFX::play_message( const string path, const string fname )
181 SGSoundSample *sample;
182 sample = new SGSoundSample( path.c_str(), fname.c_str() );
183 play_message( sample );