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