]> git.mxchange.org Git - flightgear.git/blob - src/Sound/fg_fx.cxx
b2196b7a5c0f4bfb8a3243bc9b0da201aff14a27
[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     _volume( fgGetNode("/sim/sound/volume") ),
44     _pause( fgGetNode("/sim/sound/pause") ),
45     last_pause( true ),
46     last_volume( 0.0 )
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, remove
143             smgr->remove( msgid );
144         }
145     }
146     if ( !is_playing ) {
147         // message queue idle, add next sound if we have one
148         if ( _samplequeue.size() > 0 ) {
149             smgr->add( _samplequeue.front(), msgid );
150             _samplequeue.pop();
151             smgr->play_once( msgid );
152         }
153     }
154
155     double volume = _volume->getDoubleValue();
156     if ( volume != last_volume ) {
157         smgr->set_volume( volume );        
158         last_volume = volume;
159     }
160
161     if ( !pause ) {
162         // update sound effects if not paused
163         for ( unsigned int i = 0; i < _sound.size(); i++ ) {
164             _sound[i]->update(dt);
165         }
166     }
167 }
168
169 /**
170  * add a sound sample to the message queue which is played sequentially
171  * in order.
172  */
173 void
174 FGFX::play_message( SGSoundSample *_sample )
175 {
176     _samplequeue.push( _sample );
177 }
178 void
179 FGFX::play_message( const string path, const string fname )
180 {
181     SGSoundSample *sample;
182     sample = new SGSoundSample( path.c_str(), fname.c_str() );
183     play_message( sample );
184 }
185
186
187 // end of fg_fx.cxx