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