]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.hxx
Second attempt to fixed OpenAL tests.
[simgear.git] / simgear / sound / sample_group.hxx
1 // sample_group.hxx -- Manage a group of samples relative to a base position
2 //
3 // Written for the new SoundSystem by Erik Hofman, October 2009
4 //
5 // Copyright (C) 2009 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 Foundation,
19 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 /**
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.
27  */
28
29 #ifndef _SG_SAMPLE_GROUP_OPENAL_HXX
30 #define _SG_SAMPLE_GROUP_OPENAL_HXX 1
31
32 #ifndef __cplusplus
33 # error This library requires C++
34 #endif
35
36 #if defined(__APPLE__)
37 # include <OpenAL/al.h>
38 #elif defined(OPENALSDK)
39 # include <al.h>
40 #else
41 # include <AL/al.h>
42 #endif
43
44 #include <string>
45 #include <vector>
46 #include <map>
47
48 #include <simgear/compiler.h>
49 #include <simgear/math/SGMath.hxx>
50 #include <simgear/structure/SGReferenced.hxx>
51 #include <simgear/structure/SGSharedPtr.hxx>
52 #include <simgear/structure/exception.hxx>
53
54 #include "sample_openal.hxx"
55 #include "sample_queue.hxx"
56
57 using std::map;
58 using std::string;
59
60 typedef map < string, SGSharedPtr<SGSoundSample> > sample_map;
61 typedef sample_map::iterator sample_map_iterator;
62 typedef sample_map::const_iterator const_sample_map_iterator;
63
64 class SGSoundMgr;
65
66 class SGSampleGroup : public SGReferenced
67 {
68 public:
69
70     /**
71      * Empty constructor for class encapsulation.
72      * Note: The sample group should still be activated before use
73      */
74     SGSampleGroup ();
75
76     /**
77      * Constructor
78      * Register this sample group at the sound manager using refname
79      * Note: The sample group should still be activated before use
80      * @param smgr Pointer to a pre-initialized sound manager class
81      * @param refname Name of this group for reference purposes.
82      */
83     SGSampleGroup ( SGSoundMgr *smgr, const string &refname );
84
85     /**
86      * Destructor
87      */
88     ~SGSampleGroup ();
89
90     /**
91      * Set the status of this sample group to active.
92      */
93     inline void activate() { _active = true; }
94
95     /**
96      * Update function.
97      * Call this function periodically to update the OpenAL state of all
98      * samples associated with this class. None op the configuration changes
99      * take place without a call to this function.
100      */
101     virtual void update (double dt);
102
103     /**
104      * Register an audio sample to this group.
105      * @param sound Pointer to a pre-initialized audio sample
106      * @param refname Name of this audio sample for reference purposes
107      * @return return true if successful
108      */
109     bool add( SGSharedPtr<SGSoundSample> sound, const string& refname );
110
111     /**
112      * Remove an audio sample from this group.
113      * @param refname Reference name of the audio sample to remove
114      * @return return true if successful
115      */
116     bool remove( const string& refname );
117
118     /**
119      * Test if a specified audio sample is registered at this sample group
120      * @param refname Reference name of the audio sample to test for
121      * @return true if the specified audio sample exists
122      */
123     bool exists( const string& refname );
124
125     /**
126      * Find a specified audio sample in this sample group
127      * @param refname Reference name of the audio sample to find
128      * @return A pointer to the SGSoundSample
129      */
130     SGSoundSample *find( const string& refname );
131
132     /**
133      * Stop all playing samples and set the source id to invalid.
134      */
135     void stop();
136
137     /**
138      * Request to stop playing all audio samples until further notice.
139      */
140     void suspend();
141
142     /**
143      * Request to resume playing all audio samples.
144      */
145     void resume();
146
147     /**
148      * Request to start playing the refered audio sample.
149      * @param refname Reference name of the audio sample to start playing
150      * @param looping Define if the sound should loop continuously
151      * @return true if the audio sample exsists and is scheduled for playing
152      */
153     bool play( const string& refname, bool looping );
154     
155     /**
156      * Request to start playing the refered audio sample looping.
157      * @param refname Reference name of the audio sample to start playing
158      * @return true if the audio sample exsists and is scheduled for playing
159      */
160     inline bool play_looped( const string& refname ) {
161         return play( refname, true );
162     }
163
164     /**
165      * Request to start playing the refered audio sample once.
166      * @param refname Reference name of the audio sample to start playing
167      * @return true if the audio sample exsists and is scheduled for playing
168      */
169     inline bool play_once( const string& refname ) {
170         return play( refname, false );
171     }
172
173     /**
174      * Test if a referenced sample is playing or not.
175      * @param refname Reference name of the audio sample to test for
176      * @return True of the specified sound is currently playing
177      */
178     bool is_playing( const string& refname );
179
180     /**
181      * Request to stop playing the refered audio sample.
182      * @param refname Reference name of the audio sample to stop
183      * @return true if the audio sample exsists and is scheduled to stop
184      */
185     bool stop( const string& refname );
186
187     /**
188      * Set the master volume for this sample group.
189      * @param vol Volume (must be between 0.0 and 1.0)
190      */
191     void set_volume( float vol );
192
193     /**
194      * Set the velocity vector of this sample group.
195      * This is in the local frame coordinate system; x=north, y=east, z=down
196      * @param vel Velocity vector 
197      */
198     void set_velocity( const SGVec3d& vel ) {
199        _velocity = vel; _changed = true;
200     }
201
202     /**
203      * Set the position of this sample group.
204      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
205      * @param pos Base position
206      */
207     void set_position_geod( const SGGeod& pos ) {
208         _base_pos = pos; _changed = true;
209     }
210
211     /**
212      * Set the orientation of this sample group.
213      * @param ori Quaternation containing the orientation information
214      */
215     void set_orientation( const SGQuatd& ori ) {
216         _orientation = ori; _changed = true;
217     }
218
219     /**
220      * Tie this sample group to the listener position, orientation and velocity
221      */
222     void tie_to_listener() { _tied_to_listener = true; }
223
224 protected:
225     SGSoundMgr *_smgr;
226     string _refname;
227     bool _active;
228
229 private:
230     bool _changed;
231     bool _pause;
232     float _volume;
233     bool _tied_to_listener;
234
235     SGVec3d _velocity;
236     SGGeod _base_pos;
237     SGQuatd _orientation;
238
239     sample_map _samples;
240     std::vector< SGSharedPtr<SGSoundSample> > _removed_samples;
241
242     bool testForALError(string s);
243     bool testForError(void *p, string s);
244
245     void update_pos_and_orientation();
246     void update_sample_config( SGSoundSample *sound );
247 };
248
249 #endif // _SG_SAMPLE_GROUP_OPENAL_HXX
250