]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
7054e358e2bdd2c5d12f475394e691e70d01032a
[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., 675 Mass Ave, Cambridge, MA 02139, 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    int i;
73
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.");
78       return;
79    }
80
81    path.append(path_str.c_str());
82    SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
83           << " from " << path.str());
84
85    SGPropertyNode root;
86    try {
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");
91       return;
92    }
93
94    node = root.getNode("fx");
95    for (i = 0; i < node->nChildren(); i++) {
96       SGXmlSound *sound = new SGXmlSound();
97
98       sound->init(globals->get_props(), node->getChild(i),
99                   globals->get_soundmgr(), globals->get_fg_root());
100
101       _sound.push_back(sound);
102    }
103 }
104
105 void
106 FGFX::reinit()
107 {
108    _sound.clear();
109    init();
110 };
111
112 void
113 FGFX::bind ()
114 {
115 }
116
117 void
118 FGFX::unbind ()
119 {
120 }
121
122 void
123 FGFX::update (double dt)
124 {
125     SGSoundMgr *smgr = globals->get_soundmgr();
126
127     // command sound manger
128     bool pause = _pause->getBoolValue();
129     if ( pause != last_pause ) {
130         if ( pause ) {
131             smgr->pause();
132         } else {
133             smgr->resume();
134         }
135         last_pause = pause;
136     }
137
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
144             is_playing = true;
145         } else {
146             // current message finished, stop and remove
147             smgr->stop( msgid );   // removes source
148             smgr->remove( msgid ); // removes buffer
149         }
150     }
151     if ( !is_playing ) {
152         // message queue idle, add next sound if we have one
153         if ( _samplequeue.size() > 0 ) {
154             smgr->add( _samplequeue.front(), msgid );
155             _samplequeue.pop();
156             smgr->play_once( msgid );
157         }
158     }
159
160     double volume = _volume->getDoubleValue();
161     if ( volume != last_volume ) {
162         smgr->set_volume( volume );        
163         last_volume = volume;
164     }
165
166     if ( !pause ) {
167         // update sound effects if not paused
168         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
169             _sound[i]->update(dt);
170         }
171     }
172 }
173
174 /**
175  * add a sound sample to the message queue which is played sequentially
176  * in order.
177  */
178 void
179 FGFX::play_message( SGSoundSample *_sample )
180 {
181     _sample->set_volume( 1.0 );
182     _samplequeue.push( _sample );
183 }
184 void
185 FGFX::play_message( const string path, const string fname )
186 {
187     SGSoundSample *sample;
188     sample = new SGSoundSample( path.c_str(), fname.c_str() );
189     play_message( sample );
190 }
191
192
193 // end of fg_fx.cxx