]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
e1f7f4f231c40f89ba60145d3719a69afff75291
[flightgear.git] / src / Sound / fg_fx.cxx
1 // fg_fx.cxx -- Sound effect management class implementation
2 //
3 // Started by David Megginson, October 2001
4 // (Reuses some code from main.cxx, probably by Curtis Olson)
5 //
6 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23
24 #ifdef _MSC_VER
25 #pragma warning (disable: 4786)
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/structure/exception.hxx>
34 #ifdef __BORLANDC__
35 #  define exception c_exception
36 #endif
37 #include <simgear/misc/sg_path.hxx>
38 #include <simgear/props/props.hxx>
39 #include <simgear/sound/xmlsound.hxx>
40
41 #include <Main/fg_props.hxx>
42
43 #include "fg_fx.hxx"
44
45
46 FGFX::FGFX () :
47     last_pause( true ),
48     last_volume( 0.0 ),
49     _pause( fgGetNode("/sim/sound/pause") ),
50     _volume( fgGetNode("/sim/sound/volume") )
51 {
52 }
53
54 FGFX::~FGFX ()
55 {
56     unsigned int i;
57     for ( i = 0; i < _sound.size(); i++ ) {
58         delete _sound[i];
59     }
60     _sound.clear();
61
62     while ( _samplequeue.size() > 0 ) {
63         delete _samplequeue.front();
64         _samplequeue.pop();
65     }
66 }
67
68 void
69 FGFX::init()
70 {
71     SGPropertyNode *node = fgGetNode("/sim/sound", true);
72
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");
77         return;
78     }
79
80     path.append(path_str.c_str());
81     SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
82            << " from " << path.str());
83
84     SGPropertyNode root;
85     try {
86         readProperties(path.str(), &root);
87     } catch (const sg_exception &) {
88         SG_LOG(SG_GENERAL, SG_ALERT,
89                "Error reading file '" << path.str() << '\'');
90         return;
91     }
92
93     node = root.getNode("fx");
94     if(node) {
95         for (int i = 0; i < node->nChildren(); ++i) {
96             SGXmlSound *sound = new SGXmlSound();
97   
98             try {
99                 sound->init(globals->get_props(), node->getChild(i),
100                             globals->get_soundmgr(), globals->get_fg_root());
101   
102                 _sound.push_back(sound);
103             } catch ( sg_io_exception &e ) {
104                 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
105                 delete sound;
106             }
107         }
108     }
109 }
110
111 void
112 FGFX::reinit()
113 {
114    _sound.clear();
115    init();
116 };
117
118 void
119 FGFX::bind ()
120 {
121 }
122
123 void
124 FGFX::unbind ()
125 {
126 }
127
128 void
129 FGFX::update (double dt)
130 {
131     SGSoundMgr *smgr = globals->get_soundmgr();
132
133     if (smgr->is_working() == false) {
134         return;
135     }
136
137     // command sound manger
138     bool pause = _pause->getBoolValue();
139     if ( pause != last_pause ) {
140         if ( pause ) {
141             smgr->pause();
142         } else {
143             smgr->resume();
144         }
145         last_pause = pause;
146     }
147
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
154             is_playing = true;
155         } else {
156             // current message finished, stop and remove
157             smgr->stop( msgid );   // removes source
158             smgr->remove( msgid ); // removes buffer
159         }
160     }
161     if ( !is_playing ) {
162         // message queue idle, add next sound if we have one
163         if ( _samplequeue.size() > 0 ) {
164             smgr->add( _samplequeue.front(), msgid );
165             _samplequeue.pop();
166             smgr->play_once( msgid );
167         }
168     }
169
170     double volume = _volume->getDoubleValue();
171     if ( volume != last_volume ) {
172         smgr->set_volume( volume );        
173         last_volume = volume;
174     }
175
176     if ( !pause ) {
177         // update sound effects if not paused
178         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
179             _sound[i]->update(dt);
180         }
181     }
182 }
183
184 /**
185  * add a sound sample to the message queue which is played sequentially
186  * in order.
187  */
188 void
189 FGFX::play_message( SGSoundSample *_sample )
190 {
191     _samplequeue.push( _sample );
192 }
193 void
194 FGFX::play_message( const string path, const string fname, double volume )
195 {
196     if (globals->get_soundmgr()->is_working() == false) {
197         return;
198     }
199     SGSoundSample *sample;
200     sample = new SGSoundSample( path.c_str(), fname.c_str() );
201     sample->set_volume( volume );
202     play_message( sample );
203 }
204
205
206 // end of fg_fx.cxx