]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
Attached patches remove BORLANDC, and hence SG_MATH_EXCEPTION_CLASH and SG_INCOM
[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 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/props/props.hxx>
36 #include <simgear/sound/xmlsound.hxx>
37
38 #include <Main/fg_props.hxx>
39
40 #include "fg_fx.hxx"
41
42
43 FGFX::FGFX () :
44     last_pause( true ),
45     last_volume( 0.0 ),
46     _pause( fgGetNode("/sim/sound/pause") ),
47     _volume( fgGetNode("/sim/sound/volume") )
48 {
49 }
50
51 FGFX::~FGFX ()
52 {
53     unsigned int i;
54     for ( i = 0; i < _sound.size(); i++ ) {
55         delete _sound[i];
56     }
57     _sound.clear();
58
59     while ( _samplequeue.size() > 0 ) {
60         delete _samplequeue.front();
61         _samplequeue.pop();
62     }
63 }
64
65 void
66 FGFX::init()
67 {
68     SGPropertyNode *node = fgGetNode("/sim/sound", true);
69
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, "No path in /sim/sound/path");
74         return;
75     }
76
77     path.append(path_str.c_str());
78     SG_LOG(SG_GENERAL, SG_INFO, "Reading sound " << node->getName()
79            << " from " << path.str());
80
81     SGPropertyNode root;
82     try {
83         readProperties(path.str(), &root);
84     } catch (const sg_exception &) {
85         SG_LOG(SG_GENERAL, SG_ALERT,
86                "Error reading file '" << path.str() << '\'');
87         return;
88     }
89
90     node = root.getNode("fx");
91     if(node) {
92         for (int i = 0; i < node->nChildren(); ++i) {
93             SGXmlSound *sound = new SGXmlSound();
94   
95             try {
96                 sound->init(globals->get_props(), node->getChild(i),
97                             globals->get_soundmgr(), globals->get_fg_root());
98   
99                 _sound.push_back(sound);
100             } catch ( sg_io_exception &e ) {
101                 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
102                 delete sound;
103             }
104         }
105     }
106 }
107
108 void
109 FGFX::reinit()
110 {
111    _sound.clear();
112    init();
113 };
114
115 void
116 FGFX::bind ()
117 {
118 }
119
120 void
121 FGFX::unbind ()
122 {
123 }
124
125 void
126 FGFX::update (double dt)
127 {
128     SGSoundMgr *smgr = globals->get_soundmgr();
129
130     if (smgr->is_working() == false) {
131         return;
132     }
133
134     // command sound manger
135     bool pause = _pause->getBoolValue();
136     if ( pause != last_pause ) {
137         if ( pause ) {
138             smgr->pause();
139         } else {
140             smgr->resume();
141         }
142         last_pause = pause;
143     }
144
145     // process mesage queue
146     const string msgid = "Sequential Audio Message";
147     bool is_playing = false;
148     if ( smgr->exists( msgid ) ) {
149         if ( smgr->is_playing( msgid ) ) {
150             // still playing, do nothing
151             is_playing = true;
152         } else {
153             // current message finished, stop and remove
154             smgr->stop( msgid );   // removes source
155             smgr->remove( msgid ); // removes buffer
156         }
157     }
158     if ( !is_playing ) {
159         // message queue idle, add next sound if we have one
160         if ( _samplequeue.size() > 0 ) {
161             smgr->add( _samplequeue.front(), msgid );
162             _samplequeue.pop();
163             smgr->play_once( msgid );
164         }
165     }
166
167     double volume = _volume->getDoubleValue();
168     if ( volume != last_volume ) {
169         smgr->set_volume( volume );        
170         last_volume = volume;
171     }
172
173     if ( !pause ) {
174         // update sound effects if not paused
175         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
176             _sound[i]->update(dt);
177         }
178     }
179 }
180
181 /**
182  * add a sound sample to the message queue which is played sequentially
183  * in order.
184  */
185 void
186 FGFX::play_message( SGSoundSample *_sample )
187 {
188     _samplequeue.push( _sample );
189 }
190 void
191 FGFX::play_message( const string path, const string fname, double volume )
192 {
193     if (globals->get_soundmgr()->is_working() == false) {
194         return;
195     }
196     SGSoundSample *sample;
197     sample = new SGSoundSample( path.c_str(), fname.c_str() );
198     sample->set_volume( volume );
199     play_message( sample );
200 }
201
202
203 // end of fg_fx.cxx