]> git.mxchange.org Git - simgear.git/blob - simgear/sound/xmlsound.hxx
add <delay-sec> parameter that defines how many seconds after triggering
[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 #ifndef __cplusplus
33 # error This library requires C++
34 #endif
35
36 #include <simgear/compiler.h>
37 #include <simgear/props/condition.hxx>
38
39 #include "sample_openal.hxx"
40 #include "soundmgr_openal.hxx"
41
42 static const double MAX_TRANSIT_TIME = 0.1;     // 100 ms.
43
44
45 /**
46  * Class for handling one sound event.
47  *
48  * This class handles everything for a particular sound event, by
49  * scanning an a pre-loaded property tree structure for sound
50  * settings, setting up its internal states, and managing sound
51  * playback whenever such an event happens.
52  */
53 class SGXmlSound
54 {
55
56 public:
57
58   SGXmlSound();
59   virtual ~SGXmlSound();
60
61   /**
62    * Initialize the sound event.
63    *
64    * Prior to initialization of the sound event the propgrams property root
65    * has to be defined, the sound configuration XML tree has to be loaded 
66    * and a sound manager class has to be defined.
67    *
68    * A sound configuration file would look like this:
69    *  <fx>
70    *   <event_name>
71    *    <name/> Define the name of the event. For refference only.
72    *    <mode/> Either:
73    *              looped: play this sound looped.
74    *              in-transit: play looped while the event is happening.
75    *              once: play this sound once.
76    *    <path/> The relative path to the audio file.
77    *    <property/> Take action if this property becomes true.
78    *    <condition/> Take action if this condition becomes true.
79    *    <delay-sec/> Time after which the sound should be played.
80    *    <volume> or <pitch> Define volume or pitch settings.
81    *     <property/> Take the value of this property as a refference for the
82    *                 result.
83    *     <internal/> Either:
84    *                   dt_start: the time elapsed since this sound is playing.
85    *                   dt_stop: the time elapsed since this sound has stopped.
86    *     <offset/> Add this value to the result.
87    *     <factor/> Multiply the result by this factor.
88    *     <min/> Make sure the value is never less than this value.
89    *     <max/> Make sure the value is never larger than this value.
90    *    </volume> or </pitch>
91    *   </event_name>
92    *
93    *   <event_name>
94    *   </event_name>
95    *  </fx>
96    *
97    * @param root The root node of the programs property tree.
98    * @param child A pointer to the location of the current event as defined
99    * in the configuration file.
100    * @param sndmgr A pointer to a pre-initialized sound manager class.
101    * @param path The path where the audio files remain.
102    */
103   virtual void init (SGPropertyNode *, SGPropertyNode *, SGSoundMgr *,
104                      const string &);
105
106   /**
107    * Check wheter an event has happened and if action has to be taken.
108    */
109   virtual void update (double dt);
110
111   /**
112    * Stop taking action on the pre-defined events.
113    */
114   void stop();
115
116 protected:
117
118   enum { MAXPROP=5 };
119   enum { ONCE=0, LOOPED, IN_TRANSIT };
120   enum { LEVEL=0, INVERTED, FLIPFLOP };
121
122   // SGXmlSound properties
123   typedef struct {
124         SGPropertyNode_ptr prop;
125         double (*fn)(double);
126         double *intern;
127         double factor;
128         double offset;
129         double min;
130         double max;
131         bool subtract;
132   } _snd_prop;
133
134 private:
135
136   SGSoundMgr * _mgr;
137   SGSharedPtr<SGSoundSample> _sample;
138
139   SGCondition * _condition;
140   SGPropertyNode_ptr _property;
141
142   bool _active;
143   string _name;
144   int _mode;
145   double _prev_value;
146   double _dt_play;
147   double _dt_stop;
148   double _delay;        // time after which the sound should be started (default: 0)
149   double _stopping;     // time after the sound should have stopped.
150                         // This is usefull for lost packets in in-trasit mode.
151
152   vector<_snd_prop> _volume;
153   vector<_snd_prop> _pitch;
154
155 };
156
157 #endif // _SG_SOUND_HXX