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