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