1 // sample_group.hxx -- Manage a group of samples relative to a base position
3 // Written for the new SoundSystem by Erik Hofman, October 2009
5 // Copyright (C) 2009 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 Foundation,
19 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * \file sample_group.hxx
25 * sample groups contain all sounds related to one specific object and
26 * have to be added to the sound manager, otherwise they won't get processed.
29 #ifndef _SG_SAMPLE_GROUP_OPENAL_HXX
30 #define _SG_SAMPLE_GROUP_OPENAL_HXX 1
37 #include <simgear/compiler.h>
38 #include <simgear/math/SGMath.hxx>
39 #include <simgear/structure/SGReferenced.hxx>
40 #include <simgear/structure/SGSharedPtr.hxx>
41 #include <simgear/structure/exception.hxx>
43 #include "sample_openal.hxx"
44 #include "sample_queue.hxx"
47 typedef std::map < std::string, SGSharedPtr<SGSoundSample> > sample_map;
48 typedef sample_map::iterator sample_map_iterator;
49 typedef sample_map::const_iterator const_sample_map_iterator;
53 class SGSampleGroup : public SGReferenced
58 * Empty constructor for class encapsulation.
59 * Note: The sample group should still be activated before use
65 * Register this sample group at the sound manager using refname
66 * Note: The sample group should still be activated before use
67 * @param smgr Pointer to a pre-initialized sound manager class
68 * @param refname Name of this group for reference purposes.
70 SGSampleGroup ( SGSoundMgr *smgr, const std::string &refname );
75 virtual ~SGSampleGroup ();
78 * Set the status of this sample group to active.
80 inline void activate() { _active = true; }
84 * Call this function periodically to update the OpenAL state of all
85 * samples associated with this class. None op the configuration changes
86 * take place without a call to this function.
88 virtual void update (double dt);
91 * Register an audio sample to this group.
92 * @param sound Pointer to a pre-initialized audio sample
93 * @param refname Name of this audio sample for reference purposes
94 * @return return true if successful
96 bool add( SGSharedPtr<SGSoundSample> sound, const std::string& refname );
99 * Remove an audio sample from this group.
100 * @param refname Reference name of the audio sample to remove
101 * @return return true if successful
103 bool remove( const std::string& refname );
106 * Test if a specified audio sample is registered at this sample group
107 * @param refname Reference name of the audio sample to test for
108 * @return true if the specified audio sample exists
110 bool exists( const std::string& refname );
113 * Find a specified audio sample in this sample group
114 * @param refname Reference name of the audio sample to find
115 * @return A pointer to the SGSoundSample
117 SGSoundSample *find( const std::string& refname );
120 * Stop all playing samples and set the source id to invalid.
125 * Request to stop playing all audio samples until further notice.
130 * Request to resume playing all audio samples.
135 * Request to start playing the referred audio sample.
136 * @param refname Reference name of the audio sample to start playing
137 * @param looping Define if the sound should loop continuously
138 * @return true if the audio sample exsists and is scheduled for playing
140 bool play( const std::string& refname, bool looping );
143 * Request to start playing the referred audio sample looping.
144 * @param refname Reference name of the audio sample to start playing
145 * @return true if the audio sample exsists and is scheduled for playing
147 inline bool play_looped( const std::string& refname ) {
148 return play( refname, true );
152 * Request to start playing the referred audio sample once.
153 * @param refname Reference name of the audio sample to start playing
154 * @return true if the audio sample exists and is scheduled for playing
156 inline bool play_once( const std::string& refname ) {
157 return play( refname, false );
161 * Test if a referenced sample is playing or not.
162 * @param refname Reference name of the audio sample to test for
163 * @return True of the specified sound is currently playing
165 bool is_playing( const std::string& refname );
168 * Request to stop playing the referred audio sample.
169 * @param refname Reference name of the audio sample to stop
170 * @return true if the audio sample exists and is scheduled to stop
172 bool stop( const std::string& refname );
175 * Set the master volume for this sample group.
176 * @param vol Volume (must be between 0.0 and 1.0)
178 void set_volume( float vol );
181 * Set the velocity vector of this sample group.
182 * This is in the local frame coordinate system; x=north, y=east, z=down
183 * @param vel Velocity vector
185 void set_velocity( const SGVec3d& vel ) {
186 _velocity = vel; _changed = true;
190 * Set the position of this sample group.
191 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
192 * @param pos Base position
194 void set_position_geod( const SGGeod& pos ) {
195 _base_pos = pos; _changed = true;
199 * Set the orientation of this sample group.
200 * @param ori Quaternation containing the orientation information
202 void set_orientation( const SGQuatd& ori ) {
203 _orientation = ori; _changed = true;
207 * Tie this sample group to the listener position, orientation and velocity
209 void tie_to_listener() { _tied_to_listener = true; }
213 std::string _refname;
220 bool _tied_to_listener;
224 SGQuatd _orientation;
227 std::vector< SGSharedPtr<SGSoundSample> > _removed_samples;
229 bool testForALError(std::string s);
230 bool testForError(void *p, std::string s);
232 void update_pos_and_orientation();
233 void update_sample_config( SGSoundSample *sound );
236 #endif // _SG_SAMPLE_GROUP_OPENAL_HXX