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)
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/structure/exception.hxx>
36 #include <simgear/misc/sg_path.hxx>
37 #include <simgear/props/props.hxx>
38 #include <simgear/sound/soundmgr_openal.hxx>
39 #include <simgear/sound/xmlsound.hxx>
41 #include <Main/fg_props.hxx>
43 #include <simgear/scene/model/placement.hxx>
44 #include <Model/acmodel.hxx>
45 #include <Main/viewer.hxx>
47 FGFX::FGFX ( SGSoundMgr *smgr, const string &refname ) :
48 last_visitor_pos(SGVec3d::zeros()),
49 last_model_pos(SGVec3d::zeros()),
52 _pause( fgGetNode("/sim/sound/pause") ),
53 _volume( fgGetNode("/sim/sound/volume") )
55 SGSampleGroup::_smgr = smgr;
56 SGSampleGroup::_smgr->add(this, refname);
57 SGSampleGroup::_active = _smgr->is_working();
64 for ( i = 0; i < _sound.size(); i++ ) {
69 while ( _samplequeue.size() > 0 ) {
70 delete _samplequeue.front();
79 SGPropertyNode *node = fgGetNode("/sim/sound", true);
81 string path_str = node->getStringValue("path");
82 SGPath path( globals->get_fg_root() );
83 if (path_str.empty()) {
84 SG_LOG(SG_GENERAL, SG_ALERT, "No path in /sim/sound/path");
88 path.append(path_str.c_str());
89 SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
90 << " from " << path.str());
94 readProperties(path.str(), &root);
95 } catch (const sg_exception &) {
96 SG_LOG(SG_GENERAL, SG_ALERT,
97 "Error reading file '" << path.str() << '\'');
101 node = root.getNode("fx");
103 for (int i = 0; i < node->nChildren(); ++i) {
104 SGXmlSound *sound = new SGXmlSound();
107 sound->init(globals->get_props(), node->getChild(i), this,
108 globals->get_fg_root());
110 _sound.push_back(sound);
111 } catch ( sg_exception &e ) {
112 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
129 FGFX::update (double dt)
131 // command sound manger
132 bool new_pause = _pause->getBoolValue();
133 if ( new_pause != last_pause ) {
139 last_pause = new_pause;
142 // process mesage queue
143 const string msgid = "Sequential Audio Message";
144 bool now_playing = false;
145 if ( exists( msgid ) ) {
146 if ( is_playing( msgid ) ) {
147 // still playing, do nothing
150 // current message finished, stop and remove
151 stop( msgid ); // removes source
152 remove( msgid ); // removes buffer
155 if ( !now_playing ) {
156 // message queue idle, add next sound if we have one
157 if ( _samplequeue.size() > 0 ) {
158 add( _samplequeue.front(), msgid );
164 double volume = _volume->getDoubleValue();
165 if ( volume != last_volume ) {
166 set_volume( volume );
167 last_volume = volume;
171 // update sound effects if not paused
172 for ( unsigned int i = 0; i < _sound.size(); i++ ) {
173 _sound[i]->update(dt);
177 SGSampleGroup::update(dt);
181 * add a sound sample to the message queue which is played sequentially
185 FGFX::play_message( SGSoundSample *_sample )
187 _samplequeue.push( _sample );
190 FGFX::play_message( const std::string& path, const std::string& fname, double volume )
192 SGSoundSample *sample = new SGSoundSample( path.c_str(), fname.c_str() );
193 sample->set_volume( volume );
194 play_message( sample );