2 /// Sound class implementation
4 /// Provides a class to manage a single sound event including things like
5 /// looping, volume and pitch changes.
7 // Started by Erik Hofman, February 2002
9 // Copyright (C) 2002 Erik Hofman - erik@ehofman.com
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Library General Public
13 // License as published by the Free Software Foundation; either
14 // version 2 of the License, or (at your option) any later version.
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Library General Public License for more details.
21 // You should have received a copy of the GNU Library General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
26 #define _SG_SOUND_HXX 1
32 #include <simgear/compiler.h>
34 #include <simgear/props/propsfwd.hxx>
35 #include <simgear/structure/SGSharedPtr.hxx>
43 static const double MAX_TRANSIT_TIME = 0.1; // 100 ms.
47 * Class for handling one sound event.
49 * This class handles everything for a particular sound event, by
50 * scanning an a pre-loaded property tree structure for sound
51 * settings, setting up its internal states, and managing sound
52 * playback whenever such an event happens.
60 virtual ~SGXmlSound();
63 * Initialize the sound event.
65 * Prior to initialization of the sound event the program's property root
66 * has to be defined, the sound configuration XML tree has to be loaded
67 * and a sound manager class has to be defined.
69 * A sound configuration file would look like this:
73 * <name/> Define the name of the event. For reference only.
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
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>
100 * @param root The root node of the programs property tree.
101 * @param child A pointer to the location of the current event as
102 * defined in the configuration file.
103 * @param sgrp A pointer to a pre-initialized sample group class.
104 * @param avionics A pointer to the pre-initialized avionics sample group.
105 * @param path The path where the audio files remain.
107 virtual void init( SGPropertyNode *root,
108 SGPropertyNode *child,
110 SGSampleGroup *avionics,
111 const SGPath& path );
114 * Check whether an event has happened and if action has to be taken.
116 virtual void update (double dt);
119 * Stop taking action on the pre-defined events.
126 enum { ONCE=0, LOOPED, IN_TRANSIT };
127 enum { LEVEL=0, INVERTED, FLIPFLOP };
129 // SGXmlSound properties
131 SGPropertyNode_ptr prop;
132 double (*fn)(double);
143 SGSampleGroup * _sgrp;
144 SGSharedPtr<SGSoundSample> _sample;
146 SGSharedPtr<SGCondition> _condition;
147 SGPropertyNode_ptr _property;
155 double _delay; // time after which the sound should be started (default: 0)
156 double _stopping; // time after the sound should have stopped.
157 // This is useful for lost packets in in-transit mode.
160 std::vector<_snd_prop> _volume;
161 std::vector<_snd_prop> _pitch;
164 #endif // _SG_SOUND_HXX