]> git.mxchange.org Git - simgear.git/blob - simgear/sound/soundmgr_openal.hxx
bc3851f962dda3325805766884fc1b49bd662d36
[simgear.git] / simgear / sound / soundmgr_openal.hxx
1 // soundmgr.hxx -- Sound effect management class
2 //
3 // Sound manager initially written by David Findlay
4 // <david_j_findlay@yahoo.com.au> 2001
5 //
6 // C++-ified by Curtis Olson, started March 2001.
7 // Modified for the new SoundSystem by Erik Hofman, October 2009
8 //
9 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
10 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
11 //
12 // This program is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of the
15 // License, or (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 // General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software Foundation,
24 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
25 //
26 // $Id$
27
28 /**
29  * \file soundmgr.hxx
30  * Provides a sound manager class to keep track of
31  * multiple sounds and manage playing them with different effects and
32  * timings.
33  */
34
35 #ifndef _SG_SOUNDMGR_OPENAL_HXX
36 #define _SG_SOUNDMGR_OPENAL_HXX 1
37
38 #ifndef __cplusplus
39 # error This library requires C++
40 #endif
41
42 #include <string>
43 #include <vector>
44 #include <map>
45
46 #if defined(__APPLE__)
47 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
48 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
49 # include <OpenAL/al.h>
50 # include <OpenAL/alc.h>
51 # include <OpenAL/alut.h>
52 #elif defined(OPENALSDK)
53 # include <al.h>
54 # include <alc.h>
55 # include <AL/alut.h> 
56 #else
57 # include <AL/al.h>
58 # include <AL/alc.h>
59 # include <AL/alut.h>
60 #endif
61
62 #include <simgear/compiler.h>
63 #include <simgear/structure/subsystem_mgr.hxx>
64 #include <simgear/math/SGMathFwd.hxx>
65
66 #include "sample_group.hxx"
67 #include "sample_openal.hxx"
68
69 using std::string;
70
71 struct refUint {
72     unsigned int refctr;
73     ALuint id;
74
75     refUint() { refctr = 0; id = (ALuint)-1; };
76     refUint(ALuint i) { refctr = 1; id = i; };
77     ~refUint() {};
78 };
79
80 typedef std::map < string, refUint > buffer_map;
81 typedef buffer_map::iterator buffer_map_iterator;
82 typedef buffer_map::const_iterator  const_buffer_map_iterator;
83
84 typedef std::map < string, SGSharedPtr<SGSampleGroup> > sample_group_map;
85 typedef sample_group_map::iterator sample_group_map_iterator;
86 typedef sample_group_map::const_iterator const_sample_group_map_iterator;
87
88 /**
89  * Manage a collection of SGSampleGroup instances
90  */
91 class SGSoundMgr : public SGSubsystem
92 {
93 public:
94
95     SGSoundMgr();
96     ~SGSoundMgr();
97
98     void init();
99     void bind();
100     void unbind();
101     void update(double dt);
102     
103     void suspend();
104     void resume();
105     void stop();
106
107     inline void reinit() { stop(); init(); }
108
109     /**
110      * Test is the sound manager is in a working condition.
111      * @return true is the sound manager is working
112      */
113     inline bool is_working() const { return _working; }
114
115     /**
116      * Set the sound manager to a  working condition.
117      */
118     void activate();
119
120     /**
121      * Test is the sound manager is in an active and working condition.
122      * @return true is the sound manager is active
123      */
124     inline bool is_active() const { return _active; }
125
126     /**
127      * Register a sample group to the sound manager.
128      * @para sgrp Pointer to a sample group to add
129      * @param refname Reference name of the sample group
130      * @return true if successful, false otherwise
131      */
132     bool add( SGSampleGroup *sgrp, const string& refname );
133
134     /** 
135      * Remove a sample group from the sound manager.
136      * @param refname Reference name of the sample group to remove
137      * @return true if successful, false otherwise
138      */
139     bool remove( const string& refname );
140
141     /**
142      * Test if a specified sample group is registered at the sound manager
143      * @param refname Reference name of the sample group test for
144      * @return true if the specified sample group exists
145      */
146     bool exists( const string& refname );
147
148     /**
149      * Find a specified sample group in the sound manager
150      * @param refname Reference name of the sample group to find
151      * @return A pointer to the SGSampleGroup
152      */
153     SGSampleGroup *find( const string& refname, bool create = false );
154
155     /**
156      * Set the position of the sound manager.
157      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
158      * @param pos OpenAL listener position
159      */
160     void set_position( const SGVec3d& pos ) {
161         _position = pos;
162         _changed = true;
163     }
164
165     /**
166      * Get the position of the sound manager.
167      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
168      * @return OpenAL listener position
169      */
170     SGVec3f get_position() { return toVec3f(_position); }
171
172     /**
173      * Set the velocity vector of the sound manager
174      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
175      * @param vel Velocity vector of the OpenAL listener
176      */
177     void set_velocity( SGVec3d& vel ) {
178         _velocity = vel; _changed = true;
179     }
180
181     /**
182      * Get the velocity vector of the sound manager
183      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
184      * @return Velocity vector of the OpenAL listener
185      */
186     inline SGVec3f get_velocity() { return toVec3f(_velocity); }
187
188     /**
189      * Set the orientation of the sound manager
190      * @param ori Quaternation containing the orientation information
191      */
192     void set_orientation( const SGQuatd& ori, const SGQuatd& offs );
193
194     /**
195      * Get the orientation of the sound manager
196      * @return Quaternation containing the orientation information
197      */
198     inline const SGQuatd& get_orientation() { return _orientation; }
199     inline const SGQuatd& get_orientation_offset() { return _orient_offs; }
200
201     /**
202      * Get the direction vector of the sound manager
203      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
204      * @return Look-at direction of the OpenAL listener
205      */
206     SGVec3f get_direction() {
207         return SGVec3f(_at_up_vec[0], _at_up_vec[1], _at_up_vec[2]);
208     }
209
210     enum {
211         NO_SOURCE = (unsigned int)-1,
212         NO_BUFFER = (unsigned int)-1
213     };
214
215     /**
216      * Set the master volume.
217      * @param vol Volume (must be between 0.0 and 1.0)
218      */
219     void set_volume( float vol );
220
221     /**
222      * Get the master volume.
223      * @return Volume (must be between 0.0 and 1.0)
224      */
225     inline float get_volume() { return _volume; }
226
227     /**
228      * Get a free OpenAL source-id
229      * @return NO_SOURCE if no source is available
230      */
231     unsigned int request_source();
232
233     /**
234      * Free an OpenAL source-id for future use
235      * @param source OpenAL source-id to free
236      */
237     void release_source( unsigned int source );
238
239     /**
240      * Get a free OpenAL buffer-id
241      * The buffer-id will be asigned to the sample by calling this function.
242      * @param sample Pointer to an audio sample to assign the buffer-id to
243      * @return NO_BUFFER if loading of the buffer failed.
244      */
245     unsigned int request_buffer(SGSoundSample *sample);
246
247     /**
248      * Free an OpenAL buffer-id for this sample
249      * @param sample Pointer to an audio sample for which to free the buffer
250      */
251     void release_buffer( SGSoundSample *sample );
252
253     /**
254      * Test if the position of the sound manager has changed.
255      * The value will be set to false upon the next call to update_late()
256      * @return true if the position has changed
257      */
258     inline bool has_changed() { return _changed; }
259
260     /**
261      * Load a sample file and return it's configuration and data.
262      * @param samplepath Path to the file to load
263      * @param data Pointer to a variable that points to the allocated data
264      * @param format Pointer to a vairable that gets the OpenAL format
265      * @param size Pointer to a vairable that gets the sample size in bytes
266      * @param freq Pointer to a vairable that gets the sample frequency in Herz
267      * @return true if succesful, false on error
268      */
269     bool load(string &samplepath, void **data, int *format,
270                                          size_t *size, int *freq );
271
272 private:
273     static int _alut_init;
274
275     bool _working;
276     bool _active;
277     bool _changed;
278     float _volume;
279
280     ALCdevice *_device;
281     ALCcontext *_context;
282
283     // Position of the listener.
284     SGVec3d _position;
285
286     // Velocity of the listener.
287     SGVec3d _velocity;
288
289     // Orientation of the listener. 
290     // first 3 elements are "at" vector, second 3 are "up" vector
291     SGQuatd _orientation;
292     SGQuatd _orient_offs;
293     ALfloat _at_up_vec[6];
294
295     sample_group_map _sample_groups;
296     buffer_map _buffers;
297
298     vector<ALuint> _free_sources;
299     vector<ALuint> _sources_in_use;
300
301     char *_devname;
302
303     bool testForALError(string s);
304     bool testForALCError(string s);
305     bool testForALUTError(string s);
306     bool testForError(void *p, string s);
307
308     void update_sample_config( SGSampleGroup *sound );
309 };
310
311
312 #endif // _SG_SOUNDMGR_OPENAL_HXX
313
314