2 /// Manage a group of samples relative to a base position
4 /// Sample groups contain all sounds related to one specific object and
5 /// have to be added to the sound manager, otherwise they won't get processed.
7 // Written for the new SoundSystem by Erik Hofman, October 2009
9 // Copyright (C) 2009 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.
25 #ifndef _SG_SAMPLE_GROUP_OPENAL_HXX
26 #define _SG_SAMPLE_GROUP_OPENAL_HXX 1
33 #include <simgear/compiler.h>
34 #include <simgear/math/SGMath.hxx>
35 #include <simgear/structure/SGReferenced.hxx>
36 #include <simgear/structure/SGSharedPtr.hxx>
37 #include <simgear/structure/exception.hxx>
39 #include "sample_openal.hxx"
40 #include "sample_queue.hxx"
43 typedef std::map < std::string, SGSharedPtr<SGSoundSample> > sample_map;
44 typedef sample_map::iterator sample_map_iterator;
45 typedef sample_map::const_iterator const_sample_map_iterator;
49 class SGSampleGroup : public SGReferenced
54 * Empty constructor for class encapsulation.
55 * Note: The sample group should still be activated before use
61 * Register this sample group at the sound manager using refname
62 * Note: The sample group should still be activated before use
63 * @param smgr Pointer to a pre-initialized sound manager class
64 * @param refname Name of this group for reference purposes.
66 SGSampleGroup ( SGSoundMgr *smgr, const std::string &refname );
71 virtual ~SGSampleGroup ();
74 * Set the status of this sample group to active.
76 inline void activate() { _active = true; }
80 * Call this function periodically to update the OpenAL state of all
81 * samples associated with this class. None op the configuration changes
82 * take place without a call to this function.
84 virtual void update (double dt);
87 * Register an audio sample to this group.
88 * @param sound Pointer to a pre-initialized audio sample
89 * @param refname Name of this audio sample for reference purposes
90 * @return return true if successful
92 bool add( SGSharedPtr<SGSoundSample> sound, const std::string& refname );
95 * Remove an audio sample from this group.
96 * @param refname Reference name of the audio sample to remove
97 * @return return true if successful
99 bool remove( const std::string& refname );
102 * Test if a specified audio sample is registered at this sample group
103 * @param refname Reference name of the audio sample to test for
104 * @return true if the specified audio sample exists
106 bool exists( const std::string& refname );
109 * Find a specified audio sample in this sample group
110 * @param refname Reference name of the audio sample to find
111 * @return A pointer to the SGSoundSample
113 SGSoundSample *find( const std::string& refname );
116 * Stop all playing samples and set the source id to invalid.
121 * Request to stop playing all audio samples until further notice.
126 * Request to resume playing all audio samples.
131 * Request to start playing the referred audio sample.
132 * @param refname Reference name of the audio sample to start playing
133 * @param looping Define if the sound should loop continuously
134 * @return true if the audio sample exsists and is scheduled for playing
136 bool play( const std::string& refname, bool looping = false );
139 * Request to start playing the referred audio sample looping.
140 * @param refname Reference name of the audio sample to start playing
141 * @return true if the audio sample exsists and is scheduled for playing
143 inline bool play_looped( const std::string& refname ) {
144 return play( refname, true );
148 * Request to start playing the referred audio sample once.
149 * @param refname Reference name of the audio sample to start playing
150 * @return true if the audio sample exists and is scheduled for playing
152 inline bool play_once( const std::string& refname ) {
153 return play( refname, false );
157 * Test if a referenced sample is playing or not.
158 * @param refname Reference name of the audio sample to test for
159 * @return True of the specified sound is currently playing
161 bool is_playing( const std::string& refname );
164 * Request to stop playing the referred audio sample.
165 * @param refname Reference name of the audio sample to stop
166 * @return true if the audio sample exists and is scheduled to stop
168 bool stop( const std::string& refname );
171 * Set the master volume for this sample group.
172 * @param vol Volume (must be between 0.0 and 1.0)
174 void set_volume( float vol );
177 * Set the velocity vector of this sample group.
178 * This is in the local frame coordinate system; x=north, y=east, z=down
179 * @param vel Velocity vector
181 void set_velocity( const SGVec3d& vel ) {
182 _velocity = vel; _changed = true;
186 * Set the position of this sample group.
187 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
188 * @param pos Base position
190 void set_position_geod( const SGGeod& pos ) {
191 _base_pos = pos; _changed = true;
195 * Set the orientation of this sample group.
196 * @param ori Quaternation containing the orientation information
198 void set_orientation( const SGQuatd& ori ) {
199 _orientation = ori; _changed = true;
203 * Tie this sample group to the listener position, orientation and velocity
205 void tie_to_listener() { _tied_to_listener = true; }
209 std::string _refname;
213 void cleanup_removed_samples();
214 void start_playing_sample(SGSoundSample *sample);
215 void check_playing_sample(SGSoundSample *sample);
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