]> git.mxchange.org Git - flightgear.git/blob - src/Sound/voiceplayer.hxx
Avoid nuisance sound effects after sim-reset.
[flightgear.git] / src / Sound / voiceplayer.hxx
1 // voiceplayer.hxx -- voice/sound sample player
2 //
3 // Written by Jean-Yves Lefort, started September 2005.
4 //
5 // Copyright (C) 2005, 2006  Jean-Yves Lefort - jylefort@FreeBSD.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21
22 #ifndef __SOUND_VOICEPLAYER_HXX
23 #define __SOUND_VOICEPLAYER_HXX
24
25 #include <assert.h>
26
27 #include <vector>
28 #include <deque>
29 #include <map>
30
31 #include <simgear/props/props.hxx>
32 #include <simgear/props/tiedpropertylist.hxx>
33 #include <simgear/sound/sample_openal.hxx>
34 using std::vector;
35 using std::deque;
36 using std::map;
37
38 class SGSampleGroup;
39
40 #include <Main/globals.hxx>
41
42 #ifdef _MSC_VER
43 #  pragma warning( push )
44 #  pragma warning( disable: 4355 )
45 #endif
46
47 /////////////////////////////////////////////////////////////////////////////
48 // FGVoicePlayer /////////////////////////////////////////////////////
49 /////////////////////////////////////////////////////////////////////////////
50
51 class FGVoicePlayer
52 {
53 public:
54
55     /////////////////////////////////////////////////////////////////////////////
56     // MK::RawValueMethodsData /////////////////////////////////////////////
57     /////////////////////////////////////////////////////////////////////////////
58
59     template <class C, class VT, class DT>
60     class RawValueMethodsData : public SGRawValue<VT>
61     {
62     public:
63       typedef VT (C::*getter_t) (DT) const;
64       typedef void (C::*setter_t) (DT, VT);
65
66       RawValueMethodsData (C &obj, DT data, getter_t getter = 0, setter_t setter = 0)
67         : _obj(obj), _data(data), _getter(getter), _setter(setter) {}
68
69       virtual VT getValue () const
70       {
71         if (_getter)
72       return (_obj.*_getter)(_data);
73         else
74       return SGRawValue<VT>::DefaultValue();
75       }
76       virtual bool setValue (VT value)
77       {
78         if (_setter)
79       {
80         (_obj.*_setter)(_data, value);
81         return true;
82       }
83         else
84       return false;
85       }
86       virtual SGRawValue<VT> *clone () const 
87       {
88         return new RawValueMethodsData<C,VT,DT>(_obj, _data, _getter, _setter);
89       }
90
91     private:
92       C       &_obj;
93       DT      _data;
94       getter_t    _getter;
95       setter_t    _setter;
96     };
97     
98     class PropertiesHandler : public simgear::TiedPropertyList
99     {
100     public:
101
102       template <class T>
103       inline void tie (SGPropertyNode *node, const SGRawValue<T> &raw_value)
104       {
105           Tie(node,raw_value);
106       }
107
108       template <class T>
109       inline void tie (SGPropertyNode *node,
110                const char *relative_path,
111                const SGRawValue<T> &raw_value)
112       {
113         Tie(node->getNode(relative_path, true),raw_value);
114       }
115
116       PropertiesHandler() {};
117
118       void unbind () {Untie();}
119     };
120
121   ///////////////////////////////////////////////////////////////////////////
122   // FGVoicePlayer::Voice ////////////////////////////////////////////
123   ///////////////////////////////////////////////////////////////////////////
124
125   class Voice
126   {
127   public:
128
129     /////////////////////////////////////////////////////////////////////////
130     // FGVoicePlayer::Voice::Element ////////////////////////////////////////
131     /////////////////////////////////////////////////////////////////////////
132
133     class Element
134     {
135     public:
136         bool silence;
137
138         virtual inline void play (float volume) {}
139         virtual inline void stop () {}
140         virtual bool is_playing () = 0;
141         virtual inline void set_volume (float volume) {}
142     };
143
144     /////////////////////////////////////////////////////////////////////////
145     // FGVoicePlayer::Voice::SampleElement ///////////////////////////
146     /////////////////////////////////////////////////////////////////////////
147
148     class SampleElement : public Element
149     {
150         SGSharedPtr<SGSoundSample>  _sample;
151         float               _volume;
152
153     public:
154         inline SampleElement (SGSharedPtr<SGSoundSample> sample, float volume = 1.0)
155           : _sample(sample), _volume(volume) { silence = false; }
156
157         virtual inline void play (float volume) { if (_sample && (volume > 0.05)) { set_volume(volume); _sample->play_once(); } }
158         virtual inline void stop () { if (_sample) _sample->stop(); }
159         virtual inline bool is_playing () { return _sample ? _sample->is_playing() : false; }
160         virtual inline void set_volume (float volume) { if (_sample) _sample->set_volume(volume * _volume); }
161     };
162
163     /////////////////////////////////////////////////////////////////////////
164     // FGVoicePlayer::Voice::SilenceElement //////////////////////////
165     /////////////////////////////////////////////////////////////////////////
166
167     class SilenceElement : public Element
168     {
169         double _duration;
170         double start_time;
171
172     public:
173         inline SilenceElement (double duration)
174           : _duration(duration) { silence = true; }
175
176         virtual inline void play (float volume) { start_time = globals->get_sim_time_sec(); }
177         virtual inline bool is_playing () { return globals->get_sim_time_sec() - start_time < _duration; }
178     };
179
180     /////////////////////////////////////////////////////////////////////////
181     // FGVoicePlayer::Voice (continued) //////////////////////////////
182     /////////////////////////////////////////////////////////////////////////
183
184     Element *element;
185
186     inline Voice (FGVoicePlayer *_player)
187       : element(NULL), player(_player), volume(1.0) {}
188
189     ~Voice ();
190
191     inline void append (Element *_element) { elements.push_back(_element); }
192
193     void play ();
194     void stop (bool now);
195     void set_volume (float _volume);
196     void volume_changed ();
197     void update ();
198
199   private:
200       FGVoicePlayer *player;
201
202       float volume;
203
204       vector<Element *>         elements;
205       vector<Element *>::iterator   iter;
206
207       inline float get_volume () const { return player->volume * player->speaker.volume * volume; }
208   };
209
210   ///////////////////////////////////////////////////////////////////////////
211   // FGVoicePlayer (continued) ///////////////////////////////////////
212   ///////////////////////////////////////////////////////////////////////////
213
214   struct
215   {
216     float volume;
217   } conf;
218
219   float volume;
220
221   Voice *voice;
222   Voice *next_voice;
223   bool paused;
224   string dev_name;
225   string dir_prefix;
226
227   inline FGVoicePlayer (PropertiesHandler* properties_handler, string _dev_name)
228     : volume(1.0), voice(NULL), next_voice(NULL), paused(false),
229       dev_name(_dev_name), dir_prefix(""),
230       speaker(this,properties_handler) {}
231
232   ~FGVoicePlayer ();
233
234   void init ();
235   void pause();
236   void resume();
237   bool is_playing() { return (voice!=NULL);}
238
239   enum
240   {
241     PLAY_NOW      = 1 << 0,
242     PLAY_LOOPED   = 1 << 1
243   };
244   void play (Voice *_voice, unsigned int flags = 0);
245
246   enum
247   {
248     STOP_NOW      = 1 << 0
249   };
250   void stop (unsigned int flags = 0);
251
252   void set_volume (float _volume);
253   void update ();
254
255   void bind (SGPropertyNode *node, const char* default_dir_prefix);
256
257 public:
258
259   ///////////////////////////////////////////////////////////////////////////
260   // FGVoicePlayer::Speaker //////////////////////////////////////////
261   ///////////////////////////////////////////////////////////////////////////
262
263   class Speaker
264   {
265     FGVoicePlayer *player;
266     PropertiesHandler* properties_handler;
267
268     double    pitch;
269
270     template <class T>
271     inline void tie (SGPropertyNode *node, const char *name, T *ptr)
272     {
273     properties_handler->tie
274     (node, (string("speaker/") + name).c_str(),
275      RawValueMethodsData<FGVoicePlayer::Speaker,T,T*>
276      (*this, ptr,
277       &FGVoicePlayer::Speaker::get_property,
278       &FGVoicePlayer::Speaker::set_property));
279     }
280
281   public:
282     template <class T>
283     inline void set_property (T *ptr, T value) { *ptr = value; update_configuration(); }
284
285     template <class T>
286     inline T get_property (T *ptr) const { return *ptr; }
287
288     float volume;
289
290     inline Speaker (FGVoicePlayer *_player,PropertiesHandler* _properties_handler)
291   : player(_player),
292     properties_handler(_properties_handler),
293     pitch(1),
294     volume(1)
295     {
296     }
297
298     void bind (SGPropertyNode *node);
299     void update_configuration ();
300   };
301
302 protected:
303   ///////////////////////////////////////////////////////////////////////////
304   // FGVoicePlayer (continued) ///////////////////////////////////////
305   ///////////////////////////////////////////////////////////////////////////
306
307   SGSharedPtr<SGSampleGroup> _sgr;
308   Speaker speaker;
309
310   map< string, SGSharedPtr<SGSoundSample> >   samples;
311   vector<Voice *>         _voices;
312
313   bool looped;
314   bool next_looped;
315
316   SGSoundSample *get_sample (const char *name);
317
318   inline void append (Voice *voice, Voice::Element *element) { voice->append(element); }
319   inline void append (Voice *voice, const char *sample_name) { voice->append(new Voice::SampleElement(get_sample(sample_name))); }
320   inline void append (Voice *voice, double silence) { voice->append(new Voice::SilenceElement(silence)); }
321
322   inline void make_voice (Voice **voice) { *voice = new Voice(this); _voices.push_back(*voice); }
323
324   template <class T1>
325   inline void make_voice (Voice **voice, T1 e1) { make_voice(voice); append(*voice, e1); }
326   template <class T1, class T2>
327   inline void make_voice (Voice **voice, T1 e1, T2 e2) { make_voice(voice, e1); append(*voice, e2); }
328   template <class T1, class T2, class T3>
329   inline void make_voice (Voice **voice, T1 e1, T2 e2, T3 e3) { make_voice(voice, e1, e2); append(*voice, e3); }
330   template <class T1, class T2, class T3, class T4>
331   inline void make_voice (Voice **voice, T1 e1, T2 e2, T3 e3, T4 e4) { make_voice(voice, e1, e2, e3); append(*voice, e4); }
332 };
333
334 #endif // __SOUND_VOICEPLAYER_HXX