]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
wasn't missing
[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 &) {
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       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 void
111 FGFX::reinit()
112 {
113    _sound.clear();
114    init();
115 };
116
117 void
118 FGFX::bind ()
119 {
120 }
121
122 void
123 FGFX::unbind ()
124 {
125 }
126
127 void
128 FGFX::update (double dt)
129 {
130     SGSoundMgr *smgr = globals->get_soundmgr();
131
132     // command sound manger
133     bool pause = _pause->getBoolValue();
134     if ( pause != last_pause ) {
135         if ( pause ) {
136             smgr->pause();
137         } else {
138             smgr->resume();
139         }
140         last_pause = pause;
141     }
142
143     // process mesage queue
144     const string msgid = "Sequential Audio Message";
145     bool is_playing = false;
146     if ( smgr->exists( msgid ) ) {
147         if ( smgr->is_playing( msgid ) ) {
148             // still playing, do nothing
149             is_playing = true;
150         } else {
151             // current message finished, stop and remove
152             smgr->stop( msgid );   // removes source
153             smgr->remove( msgid ); // removes buffer
154         }
155     }
156     if ( !is_playing ) {
157         // message queue idle, add next sound if we have one
158         if ( _samplequeue.size() > 0 ) {
159             smgr->add( _samplequeue.front(), msgid );
160             _samplequeue.pop();
161             smgr->play_once( msgid );
162         }
163     }
164
165     double volume = _volume->getDoubleValue();
166     if ( volume != last_volume ) {
167         smgr->set_volume( volume );        
168         last_volume = volume;
169     }
170
171     if ( !pause ) {
172         // update sound effects if not paused
173         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
174             _sound[i]->update(dt);
175         }
176     }
177 }
178
179 /**
180  * add a sound sample to the message queue which is played sequentially
181  * in order.
182  */
183 void
184 FGFX::play_message( SGSoundSample *_sample )
185 {
186     _sample->set_volume( 1.0 );
187     _samplequeue.push( _sample );
188 }
189 void
190 FGFX::play_message( const string path, const string fname )
191 {
192     SGSoundSample *sample;
193     sample = new SGSoundSample( path.c_str(), fname.c_str() );
194     play_message( sample );
195 }
196
197
198 // end of fg_fx.cxx