]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.hxx
use auto_ptr instead
[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 <map>
46
47 #include <simgear/compiler.h>
48 #include <simgear/math/SGMath.hxx>
49 #include <simgear/structure/SGReferenced.hxx>
50 #include <simgear/structure/SGSharedPtr.hxx>
51 #include <simgear/structure/exception.hxx>
52
53 #include "sample_openal.hxx"
54
55 using std::map;
56 using std::string;
57
58 typedef map < string, SGSharedPtr<SGSoundSample> > sample_map;
59 typedef sample_map::iterator sample_map_iterator;
60 typedef sample_map::const_iterator const_sample_map_iterator;
61
62 class SGSoundMgr;
63
64 class SGSampleGroup : public SGReferenced
65 {
66 public:
67
68     /**
69      * Empty constructor for class encapsulation.
70      * Note: The sample group should still be activated before use
71      */
72     SGSampleGroup ();
73
74     /**
75      * Constructor
76      * Register this sample group at the sound manager using refname
77      * Note: The sample group should still be activated before use
78      * @param smgr Pointer to a pre-initialized sound manager class
79      * @param refname Name of this group for reference purposes.
80      */
81     SGSampleGroup ( SGSoundMgr *smgr, const string &refname );
82
83     /**
84      * Destructor
85      */
86     ~SGSampleGroup ();
87
88     /**
89      * Set the status of this sample group to active.
90      */
91     inline void activate() { _active = true; }
92
93     /**
94      * Update function.
95      * Call this function periodically to update the OpenAL state of all
96      * samples associated with this class. None op the configuration changes
97      * take place without a call to this function.
98      */
99     virtual void update (double dt);
100
101     /**
102      * Register an audio sample to this group.
103      * @param sound Pointer to a pre-initialized audio sample
104      * @param refname Name of this audio sample for reference purposes
105      * @return return true if successful
106      */
107     bool add( SGSoundSample *sound, const string& refname );
108
109     /**
110      * Remove an audio sample from this group.
111      * @param refname Reference name of the audio sample to remove
112      * @return return true if successful
113      */
114     bool remove( const string& refname );
115
116     /**
117      * Test if a specified audio sample is registered at this sample group
118      * @param refname Reference name of the audio sample to test for
119      * @return true if the specified audio sample exists
120      */
121     bool exists( const string& refname );
122
123     /**
124      * Find a specified audio sample in this sample group
125      * @param refname Reference name of the audio sample to find
126      * @return A pointer to the SGSoundSample
127      */
128     SGSoundSample *find( const string& refname );
129
130     /**
131      * Request to stop playing all audio samples until further notice.
132      */
133     void suspend();
134
135     /**
136      * Request to resume playing all audio samples.
137      */
138     void resume();
139
140     /**
141      * Request to start playing the refered audio sample.
142      * @param refname Reference name of the audio sample to start playing
143      * @param looping Define if the sound should loop continuously
144      * @return true if the audio sample exsists and is scheduled for playing
145      */
146     bool play( const string& refname, bool looping );
147     
148     /**
149      * Request to start playing the refered audio sample looping.
150      * @param refname Reference name of the audio sample to start playing
151      * @return true if the audio sample exsists and is scheduled for playing
152      */
153     inline bool play_looped( const string& refname ) {
154         return play( refname, true );
155     }
156
157     /**
158      * Request to start playing the refered audio sample once.
159      * @param refname Reference name of the audio sample to start playing
160      * @return true if the audio sample exsists and is scheduled for playing
161      */
162     inline bool play_once( const string& refname ) {
163         return play( refname, false );
164     }
165
166     /**
167      * Test if a referenced sample is playing or not.
168      * @param refname Reference name of the audio sample to test for
169      * @return True of the specified sound is currently playing
170      */
171     bool is_playing( const string& refname );
172
173     /**
174      * Request to stop playing the refered audio sample.
175      * @param refname Reference name of the audio sample to stop
176      * @return true if the audio sample exsists and is scheduled to stop
177      */
178     bool stop( const string& refname );
179
180     /**
181      * Set the master volume for this sample group.
182      * @param vol Volume (must be between 0.0 and 1.0)
183      */
184     void set_volume( float vol );
185
186     /**
187      * Set the velocity vector of this sample group.
188      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
189      * @param vel Velocity vector 
190      */
191     void set_velocity( const SGVec3d& vel );
192
193     /**
194      * Set the position of this sample group.
195      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
196      * @param pos Base position
197      */
198     void set_position( const SGGeod& pos );
199
200     /**
201      * Set the orientation of this sample group.
202      * @param ori Quaternation containing the orientation information
203      */
204     void set_orientation( const SGQuatd& ori );
205
206     /**
207      * Tie this sample group to the listener position, orientation and velocity
208      */
209     void tie_to_listener() { _tied_to_listener = true; }
210
211 protected:
212     SGSoundMgr *_smgr;
213     string _refname;
214     bool _active;
215
216 private:
217     float _volume;
218     bool _tied_to_listener;
219
220     SGVec3d _velocity;
221     SGQuatd _orientation;
222     SGGeod _position;
223
224     sample_map _samples;
225
226     bool testForALError(string s);
227     bool testForError(void *p, string s);
228
229     void update_sample_config( SGSoundSample *sound );
230 };
231
232 #endif // _SG_SAMPLE_GROUP_OPENAL_HXX
233