]> git.mxchange.org Git - simgear.git/blob - simgear/sound/soundmgr_openal.hxx
Compile even if OSG_USE_REF_PTR_IMPLICIT_OUTPUT_CONVERSION is not set.
[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 #ifndef ALC_ALL_DEVICES_SPECIFIER
63 # define ALC_ALL_DEVICES_SPECIFIER      0x1013
64 #endif
65
66 #include <simgear/compiler.h>
67 #include <simgear/structure/subsystem_mgr.hxx>
68 #include <simgear/math/SGMathFwd.hxx>
69
70 #include "sample_group.hxx"
71 #include "sample_openal.hxx"
72
73 using std::string;
74
75 struct refUint {
76     unsigned int refctr;
77     ALuint id;
78
79     refUint() { refctr = 0; id = (ALuint)-1; };
80     refUint(ALuint i) { refctr = 1; id = i; };
81     ~refUint() {};
82 };
83
84 typedef std::map < string, refUint > buffer_map;
85 typedef buffer_map::iterator buffer_map_iterator;
86 typedef buffer_map::const_iterator  const_buffer_map_iterator;
87
88 typedef std::map < string, SGSharedPtr<SGSampleGroup> > sample_group_map;
89 typedef sample_group_map::iterator sample_group_map_iterator;
90 typedef sample_group_map::const_iterator const_sample_group_map_iterator;
91
92 /**
93  * Manage a collection of SGSampleGroup instances
94  */
95 class SGSoundMgr : public SGSubsystem
96 {
97 public:
98
99     SGSoundMgr();
100     ~SGSoundMgr();
101
102     void init(const char *devname = NULL);
103     void bind();
104     void unbind();
105     void update(double dt);
106     
107     void suspend();
108     void resume();
109     void stop();
110
111     inline void reinit() { stop(); init(); }
112
113     /**
114      * Test is the sound manager is in a working condition.
115      * @return true is the sound manager is working
116      */
117     inline bool is_working() const { return _working; }
118
119     /**
120      * Set the sound manager to a  working condition.
121      */
122     void activate();
123
124     /**
125      * Test is the sound manager is in an active and working condition.
126      * @return true is the sound manager is active
127      */
128     inline bool is_active() const { return _active; }
129
130     /**
131      * Register a sample group to the sound manager.
132      * @para sgrp Pointer to a sample group to add
133      * @param refname Reference name of the sample group
134      * @return true if successful, false otherwise
135      */
136     bool add( SGSampleGroup *sgrp, const string& refname );
137
138     /** 
139      * Remove a sample group from the sound manager.
140      * @param refname Reference name of the sample group to remove
141      * @return true if successful, false otherwise
142      */
143     bool remove( const string& refname );
144
145     /**
146      * Test if a specified sample group is registered at the sound manager
147      * @param refname Reference name of the sample group test for
148      * @return true if the specified sample group exists
149      */
150     bool exists( const string& refname );
151
152     /**
153      * Find a specified sample group in the sound manager
154      * @param refname Reference name of the sample group to find
155      * @return A pointer to the SGSampleGroup
156      */
157     SGSampleGroup *find( const string& refname, bool create = false );
158
159     /**
160      * Set the Cartesian position of the sound manager.
161      * @param pos OpenAL listener position
162      */
163     void set_position( const SGVec3d& pos, const SGGeod& pos_geod ) {
164         _base_pos = pos; _geod_pos = pos_geod; _changed = true;
165     }
166
167     void set_position_offset( const SGVec3d& pos ) {
168         _offset_pos = pos; _changed = true;
169     }
170
171     /**
172      * Get the position of the sound manager.
173      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
174      * @return OpenAL listener position
175      */
176     SGVec3d& get_position() { return _absolute_pos; }
177
178     /**
179      * Set the velocity vector (in meters per second) of the sound manager
180      * This is the horizontal local frame; x=north, y=east, z=down
181      * @param Velocity vector
182      */
183     void set_velocity( const SGVec3d& vel ) {
184         _velocity = vel; _changed = true;
185     }
186
187     /**
188      * Get the velocity vector of the sound manager
189      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
190      * @return Velocity vector of the OpenAL listener
191      */
192     inline SGVec3f get_velocity() { return toVec3f(_velocity); }
193
194     /**
195      * Set the orientation of the sound manager
196      * @param ori Quaternation containing the orientation information
197      */
198     void set_orientation( const SGQuatd& ori ) {
199         _orientation = ori; _changed = true;
200     }
201
202     /**
203      * Get the orientation of the sound manager
204      * @return Quaternation containing the orientation information
205      */
206     inline const SGQuatd& get_orientation() { return _orientation; }
207
208     /**
209      * Get the direction vector of the sound manager
210      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
211      * @return Look-at direction of the OpenAL listener
212      */
213     SGVec3f get_direction() {
214         return SGVec3f(_at_up_vec[0], _at_up_vec[1], _at_up_vec[2]);
215     }
216
217     enum {
218         NO_SOURCE = (unsigned int)-1,
219         NO_BUFFER = (unsigned int)-1
220     };
221
222     /**
223      * Set the master volume.
224      * @param vol Volume (must be between 0.0 and 1.0)
225      */
226     void set_volume( float vol );
227
228     /**
229      * Get the master volume.
230      * @return Volume (must be between 0.0 and 1.0)
231      */
232     inline float get_volume() { return _volume; }
233
234     /**
235      * Get a free OpenAL source-id
236      * @return NO_SOURCE if no source is available
237      */
238     unsigned int request_source();
239
240     /**
241      * Free an OpenAL source-id for future use
242      * @param source OpenAL source-id to free
243      */
244     void release_source( unsigned int source );
245
246     /**
247      * Get a free OpenAL buffer-id
248      * The buffer-id will be asigned to the sample by calling this function.
249      * @param sample Pointer to an audio sample to assign the buffer-id to
250      * @return NO_BUFFER if loading of the buffer failed.
251      */
252     unsigned int request_buffer(SGSoundSample *sample);
253
254     /**
255      * Free an OpenAL buffer-id for this sample
256      * @param sample Pointer to an audio sample for which to free the buffer
257      */
258     void release_buffer( SGSoundSample *sample );
259
260     /**
261      * Test if the position of the sound manager has changed.
262      * The value will be set to false upon the next call to update_late()
263      * @return true if the position has changed
264      */
265     inline bool has_changed() { return _changed; }
266
267     /**
268      * Some implementations seem to need the velocity miltyplied by a
269      * factor of 100 to make them distinct. I've not found if this is
270      * a problem in the implementation or in out code. Until then
271      * this function is used to detect the problematic implementations.
272      */
273     inline bool bad_doppler_effect() { return _bad_doppler; }
274
275     /**
276      * Load a sample file and return it's configuration and data.
277      * @param samplepath Path to the file to load
278      * @param data Pointer to a variable that points to the allocated data
279      * @param format Pointer to a vairable that gets the OpenAL format
280      * @param size Pointer to a vairable that gets the sample size in bytes
281      * @param freq Pointer to a vairable that gets the sample frequency in Herz
282      * @return true if succesful, false on error
283      */
284     bool load(string &samplepath, void **data, int *format,
285                                          size_t *size, int *freq );
286
287     /**
288      * Get a list of available playback devices.
289      */
290     vector<const char*> get_available_devices();
291
292     /**
293      * Get the current OpenAL vendor or rendering backend.
294      */
295     const string& get_vendor() { return _vendor; }
296     const string& get_renderer() { return _renderer; }
297
298 private:
299     static int _alut_init;
300
301     bool _working;
302     bool _active;
303     bool _changed;
304     float _volume;
305
306     ALCdevice *_device;
307     ALCcontext *_context;
308
309     // Position of the listener.
310     SGVec3d _absolute_pos;
311     SGVec3d _offset_pos;
312     SGVec3d _base_pos;
313     SGGeod _geod_pos;
314
315     // Velocity of the listener.
316     SGVec3d _velocity;
317
318     // Orientation of the listener. 
319     // first 3 elements are "at" vector, second 3 are "up" vector
320     SGQuatd _orientation;
321     ALfloat _at_up_vec[6];
322
323     sample_group_map _sample_groups;
324     buffer_map _buffers;
325
326     vector<ALuint> _free_sources;
327     vector<ALuint> _sources_in_use;
328
329     bool _bad_doppler;
330     string _renderer;
331     string _vendor;
332
333     bool testForALError(string s);
334     bool testForALCError(string s);
335     bool testForALUTError(string s);
336     bool testForError(void *p, string s);
337
338     void update_pos_and_orientation();
339     void update_sample_config( SGSampleGroup *sound );
340 };
341
342
343 #endif // _SG_SOUNDMGR_OPENAL_HXX
344
345