1 // sound.hxx -- Sound class implementation
3 // Started by Erik Hofman, February 2002
5 // Copyright (C) 2002 Erik Hofman - erik@ehofman.com
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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * Provides a class to manage a single sound event including things
26 * like looping, volume and pitch changes.
30 #define _SG_SOUND_HXX 1
33 # error This library requires C++
38 #include <simgear/compiler.h>
40 #include <simgear/props/propsfwd.hxx>
42 #include "sample_group.hxx"
43 #include "sample_openal.hxx"
45 static const double MAX_TRANSIT_TIME = 0.1; // 100 ms.
49 * Class for handling one sound event.
51 * This class handles everything for a particular sound event, by
52 * scanning an a pre-loaded property tree structure for sound
53 * settings, setting up its internal states, and managing sound
54 * playback whenever such an event happens.
62 virtual ~SGXmlSound();
65 * Initialize the sound event.
67 * Prior to initialization of the sound event the program's property root
68 * has to be defined, the sound configuration XML tree has to be loaded
69 * and a sound manager class has to be defined.
71 * A sound configuration file would look like this:
74 * <name/> Define the name of the event. For reference only.
76 * looped: play this sound looped.
77 * in-transit: play looped while the event is happening.
78 * once: play this sound once.
79 * <path/> The relative path to the audio file.
80 * <property/> Take action if this property becomes true.
81 * <condition/> Take action if this condition becomes true.
82 * <delay-sec/> Time after which the sound should be played.
83 * <volume> or <pitch> Define volume or pitch settings.
84 * <property/> Take the value of this property as a reference for the
87 * dt_start: the time elapsed since this sound is playing.
88 * dt_stop: the time elapsed since this sound has stopped.
89 * <offset/> Add this value to the result.
90 * <factor/> Multiply the result by this factor.
91 * <min/> Make sure the value is never less than this value.
92 * <max/> Make sure the value is never larger than this value.
93 * </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 defined
102 * 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 *, SGPropertyNode *, SGSampleGroup *,
108 SGSampleGroup *, const SGPath& currentDir);
111 * Check whether an event has happened and if action has to be taken.
113 virtual void update (double dt);
116 * Stop taking action on the pre-defined events.
123 enum { ONCE=0, LOOPED, IN_TRANSIT };
124 enum { LEVEL=0, INVERTED, FLIPFLOP };
126 // SGXmlSound properties
128 SGPropertyNode_ptr prop;
129 double (*fn)(double);
140 SGSampleGroup * _sgrp;
141 SGSharedPtr<SGSoundSample> _sample;
143 SGSharedPtr<SGCondition> _condition;
144 SGPropertyNode_ptr _property;
152 double _delay; // time after which the sound should be started (default: 0)
153 double _stopping; // time after the sound should have stopped.
154 // This is useful for lost packets in in-transit mode.
157 std::vector<_snd_prop> _volume;
158 std::vector<_snd_prop> _pitch;
161 #endif // _SG_SOUND_HXX