]> git.mxchange.org Git - simgear.git/blob - simgear/sound/xmlsound.hxx
cppbind.Ghost: clean up a bit
[simgear.git] / simgear / sound / xmlsound.hxx
1 // sound.hxx -- Sound class implementation
2 //
3 // Started by Erik Hofman, February 2002
4 //
5 // Copyright (C) 2002  Erik Hofman - erik@ehofman.com
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 /** 
24  * \file sound.hxx 
25  * Provides a class to manage a single sound event including things
26  * like looping, volume and pitch changes.
27  */
28
29 #ifndef _SG_SOUND_HXX
30 #define _SG_SOUND_HXX 1
31
32
33 #include <vector>
34 #include <string>
35      
36 #include <simgear/compiler.h>
37
38 #include <simgear/props/propsfwd.hxx>
39 #include <simgear/structure/SGSharedPtr.hxx>
40
41 // forward decls
42 class SGSampleGroup;
43 class SGSoundSample;
44 class SGCondition;
45 class SGPath;
46
47 static const double MAX_TRANSIT_TIME = 0.1;     // 100 ms.
48
49
50 /**
51  * Class for handling one sound event.
52  *
53  * This class handles everything for a particular sound event, by
54  * scanning an a pre-loaded property tree structure for sound
55  * settings, setting up its internal states, and managing sound
56  * playback whenever such an event happens.
57  */
58 class SGXmlSound
59 {
60
61 public:
62
63   SGXmlSound();
64   virtual ~SGXmlSound();
65
66   /**
67    * Initialize the sound event.
68    *
69    * Prior to initialization of the sound event the program's property root
70    * has to be defined, the sound configuration XML tree has to be loaded 
71    * and a sound manager class has to be defined.
72    *
73    * A sound configuration file would look like this:
74    *  <fx>
75    *   <event_name>
76    *    <name/> Define the name of the event. For reference only.
77    *    <mode/> Either:
78    *              looped: play this sound looped.
79    *              in-transit: play looped while the event is happening.
80    *              once: play this sound once.
81    *    <path/> The relative path to the audio file.
82    *    <property/> Take action if this property becomes true.
83    *    <condition/> Take action if this condition becomes true.
84    *    <delay-sec/> Time after which the sound should be played.
85    *    <volume> or <pitch> Define volume or pitch settings.
86    *     <property/> Take the value of this property as a reference for the
87    *                 result.
88    *     <internal/> Either:
89    *                   dt_start: the time elapsed since this sound is playing.
90    *                   dt_stop: the time elapsed since this sound has stopped.
91    *     <offset/> Add this value to the result.
92    *     <factor/> Multiply the result by this factor.
93    *     <min/> Make sure the value is never less than this value.
94    *     <max/> Make sure the value is never larger than this value.
95    *    </volume> or </pitch>
96    *   </event_name>
97    *
98    *   <event_name>
99    *   </event_name>
100    *  </fx>
101    *
102    * @param root The root node of the programs property tree.
103    * @param child A pointer to the location of the current event as defined
104    * in the configuration file.
105    * @param sgrp A pointer to a pre-initialized sample group class.
106    * @param avionics A pointer to the pre-initialized avionics sample group.
107    * @param path The path where the audio files remain.
108    */
109   virtual void init (SGPropertyNode *, SGPropertyNode *, SGSampleGroup *,
110                      SGSampleGroup *, const SGPath& currentDir);
111
112   /**
113    * Check whether an event has happened and if action has to be taken.
114    */
115   virtual void update (double dt);
116
117   /**
118    * Stop taking action on the pre-defined events.
119    */
120   void stop();
121
122 protected:
123
124   enum { MAXPROP=5 };
125   enum { ONCE=0, LOOPED, IN_TRANSIT };
126   enum { LEVEL=0, INVERTED, FLIPFLOP };
127
128   // SGXmlSound properties
129   typedef struct {
130         SGPropertyNode_ptr prop;
131         double (*fn)(double);
132         double *intern;
133         double factor;
134         double offset;
135         double min;
136         double max;
137         bool subtract;
138   } _snd_prop;
139
140 private:
141
142   SGSampleGroup * _sgrp;
143   SGSharedPtr<SGSoundSample> _sample;
144
145   SGSharedPtr<SGCondition> _condition;
146   SGPropertyNode_ptr _property;
147
148   bool _active;
149   std::string _name;
150   int _mode;
151   double _prev_value;
152   double _dt_play;
153   double _dt_stop;
154   double _delay;        // time after which the sound should be started (default: 0)
155   double _stopping;     // time after the sound should have stopped.
156                         // This is useful for lost packets in in-transit mode.
157   bool _initialized;
158
159   std::vector<_snd_prop> _volume;
160   std::vector<_snd_prop> _pitch;
161 };
162
163 #endif // _SG_SOUND_HXX