1 // voiceplayer.hxx -- voice/sound sample player
3 // Written by Jean-Yves Lefort, started September 2005.
5 // Copyright (C) 2005, 2006 Jean-Yves Lefort - jylefort@FreeBSD.org
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifndef __SOUND_VOICEPLAYER_HXX
23 #define __SOUND_VOICEPLAYER_HXX
31 #include <simgear/props/props.hxx>
32 #include <simgear/props/tiedpropertylist.hxx>
37 #include <Main/globals.hxx>
40 # pragma warning( push )
41 # pragma warning( disable: 4355 )
44 /////////////////////////////////////////////////////////////////////////////
45 // FGVoicePlayer /////////////////////////////////////////////////////
46 /////////////////////////////////////////////////////////////////////////////
52 /////////////////////////////////////////////////////////////////////////////
53 // MK::RawValueMethodsData /////////////////////////////////////////////
54 /////////////////////////////////////////////////////////////////////////////
56 template <class C, class VT, class DT>
57 class RawValueMethodsData : public SGRawValue<VT>
60 typedef VT (C::*getter_t) (DT) const;
61 typedef void (C::*setter_t) (DT, VT);
63 RawValueMethodsData (C &obj, DT data, getter_t getter = 0, setter_t setter = 0)
64 : _obj(obj), _data(data), _getter(getter), _setter(setter) {}
66 virtual VT getValue () const
69 return (_obj.*_getter)(_data);
71 return SGRawValue<VT>::DefaultValue();
73 virtual bool setValue (VT value)
77 (_obj.*_setter)(_data, value);
83 virtual SGRawValue<VT> *clone () const
85 return new RawValueMethodsData<C,VT,DT>(_obj, _data, _getter, _setter);
95 class PropertiesHandler : public simgear::TiedPropertyList
100 inline void tie (SGPropertyNode *node, const SGRawValue<T> &raw_value)
106 inline void tie (SGPropertyNode *node,
107 const char *relative_path,
108 const SGRawValue<T> &raw_value)
110 Tie(node->getNode(relative_path, true),raw_value);
113 PropertiesHandler() {};
115 void unbind () {Untie();}
118 ///////////////////////////////////////////////////////////////////////////
119 // FGVoicePlayer::Voice ////////////////////////////////////////////
120 ///////////////////////////////////////////////////////////////////////////
126 /////////////////////////////////////////////////////////////////////////
127 // FGVoicePlayer::Voice::Element ////////////////////////////////////////
128 /////////////////////////////////////////////////////////////////////////
135 virtual ~Element() {}
136 virtual inline void play (float volume) {}
137 virtual inline void stop () {}
138 virtual bool is_playing () = 0;
139 virtual inline void set_volume (float volume) {}
142 /////////////////////////////////////////////////////////////////////////
143 // FGVoicePlayer::Voice::SampleElement ///////////////////////////
144 /////////////////////////////////////////////////////////////////////////
146 class SampleElement : public Element
148 SGSharedPtr<SGSoundSample> _sample;
152 SampleElement (SGSharedPtr<SGSoundSample> sample, float volume = 1.0);
154 virtual void play (float volume);
155 virtual void stop ();
156 virtual bool is_playing ();
157 virtual void set_volume (float volume);
160 /////////////////////////////////////////////////////////////////////////
161 // FGVoicePlayer::Voice::SilenceElement //////////////////////////
162 /////////////////////////////////////////////////////////////////////////
164 class SilenceElement : public Element
170 inline SilenceElement (double duration)
171 : _duration(duration) { silence = true; }
173 virtual inline void play (float volume) { start_time = globals->get_sim_time_sec(); }
174 virtual inline bool is_playing () { return globals->get_sim_time_sec() - start_time < _duration; }
177 /////////////////////////////////////////////////////////////////////////
178 // FGVoicePlayer::Voice (continued) //////////////////////////////
179 /////////////////////////////////////////////////////////////////////////
183 inline Voice (FGVoicePlayer *_player)
184 : element(NULL), player(_player), volume(1.0) {}
188 inline void append (Element *_element) { elements.push_back(_element); }
191 void stop (bool now);
192 void set_volume (float _volume);
193 void volume_changed ();
197 FGVoicePlayer *player;
201 std::vector<Element *> elements;
202 std::vector<Element *>::iterator iter;
204 inline float get_volume () const { return player->volume * player->speaker.volume * volume; }
207 ///////////////////////////////////////////////////////////////////////////
208 // FGVoicePlayer (continued) ///////////////////////////////////////
209 ///////////////////////////////////////////////////////////////////////////
221 std::string dev_name;
222 std::string dir_prefix;
224 FGVoicePlayer (PropertiesHandler* properties_handler, std::string _dev_name);
226 virtual ~FGVoicePlayer ();
231 bool is_playing() { return (voice!=NULL);}
238 void play (Voice *_voice, unsigned int flags = 0);
244 void stop (unsigned int flags = 0);
246 void set_volume (float _volume);
249 void bind (SGPropertyNode *node, const char* default_dir_prefix);
253 ///////////////////////////////////////////////////////////////////////////
254 // FGVoicePlayer::Speaker //////////////////////////////////////////
255 ///////////////////////////////////////////////////////////////////////////
259 FGVoicePlayer *player;
260 PropertiesHandler* properties_handler;
265 inline void tie (SGPropertyNode *node, const char *name, T *ptr)
267 properties_handler->tie
268 (node, (std::string("speaker/") + name).c_str(),
269 RawValueMethodsData<FGVoicePlayer::Speaker,T,T*>
271 &FGVoicePlayer::Speaker::get_property,
272 &FGVoicePlayer::Speaker::set_property));
277 inline void set_property (T *ptr, T value) { *ptr = value; update_configuration(); }
280 inline T get_property (T *ptr) const { return *ptr; }
284 inline Speaker (FGVoicePlayer *_player,PropertiesHandler* _properties_handler)
286 properties_handler(_properties_handler),
292 void bind (SGPropertyNode *node);
293 void update_configuration ();
297 ///////////////////////////////////////////////////////////////////////////
298 // FGVoicePlayer (continued) ///////////////////////////////////////
299 ///////////////////////////////////////////////////////////////////////////
301 SGSharedPtr<SGSampleGroup> _sgr;
304 std::map< std::string, SGSharedPtr<SGSoundSample> > samples;
305 std::vector<Voice *> _voices;
310 SGSoundSample *get_sample (const char *name);
312 inline void append (Voice *voice, Voice::Element *element) { voice->append(element); }
313 void append (Voice *voice, const char *sample_name);
314 inline void append (Voice *voice, double silence) { voice->append(new Voice::SilenceElement(silence)); }
316 inline void make_voice (Voice **voice) { *voice = new Voice(this); _voices.push_back(*voice); }
319 inline void make_voice (Voice **voice, T1 e1) { make_voice(voice); append(*voice, e1); }
320 template <class T1, class T2>
321 inline void make_voice (Voice **voice, T1 e1, T2 e2) { make_voice(voice, e1); append(*voice, e2); }
322 template <class T1, class T2, class T3>
323 inline void make_voice (Voice **voice, T1 e1, T2 e2, T3 e3) { make_voice(voice, e1, e2); append(*voice, e3); }
324 template <class T1, class T2, class T3, class T4>
325 inline void make_voice (Voice **voice, T1 e1, T2 e2, T3 e3, T4 e4) { make_voice(voice, e1, e2, e3); append(*voice, e4); }
328 #endif // __SOUND_VOICEPLAYER_HXX