]> git.mxchange.org Git - simgear.git/blob - simgear/sound/soundmgr_openal.hxx
4baad69b0805e45548f3c1cfb7d6823b405236ca
[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 //
8 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software Foundation,
22 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26 /**
27  * \file soundmgr.hxx
28  * Provides a sound manager class to keep track of
29  * multiple sounds and manage playing them with different effects and
30  * timings.
31  */
32
33 #ifndef _SG_SOUNDMGR_OPENAL_HXX
34 #define _SG_SOUNDMGR_OPENAL_HXX 1
35
36 #ifndef __cplusplus
37 # error This library requires C++
38 #endif
39
40 #include <string>
41 #include <vector>
42 #include <map>
43
44 #if defined(__APPLE__)
45 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
46 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
47 # include <OpenAL/al.h>
48 # include <OpenAL/alc.h>
49 # include <OpenAL/alut.h>
50 #elif defined(_WIN32)
51 # include <al.h>
52 # include <alc.h>
53 # include <AL/alut.h>
54 #else
55 # include <AL/al.h>
56 # include <AL/alc.h>
57 # include <AL/alut.h>
58 #endif
59
60 #include <simgear/compiler.h>
61 #include <simgear/structure/subsystem_mgr.hxx>
62 #include <simgear/math/SGMathFwd.hxx>
63
64 #include "sample_group.hxx"
65 #include "sample_openal.hxx"
66
67 using std::string;
68
69 struct refUint {
70     unsigned int refctr;
71     ALuint id;
72
73     refUint() { refctr = 0; id = (ALuint)-1; };
74     refUint(ALuint i) { refctr = 1; id = i; };
75     ~refUint() {};
76 };
77
78 typedef std::map < string, refUint > buffer_map;
79 typedef buffer_map::iterator buffer_map_iterator;
80 typedef buffer_map::const_iterator  const_buffer_map_iterator;
81
82 typedef std::map < string, SGSharedPtr<SGSampleGroup> > sample_group_map;
83 typedef sample_group_map::iterator sample_group_map_iterator;
84 typedef sample_group_map::const_iterator const_sample_group_map_iterator;
85
86 /**
87  * Manage a collection of SGSampleGroup instances
88  */
89 class SGSoundMgr : public SGSubsystem
90 {
91 public:
92
93     SGSoundMgr();
94     ~SGSoundMgr();
95
96     void init();
97     void bind();
98     void unbind();
99     void update(double dt);
100     void update_late(double dt);
101     
102     void suspend();
103     void resume();
104     void stop();
105
106     inline void reinit() { stop(); init(); }
107
108     /**
109      * is audio working?
110      */
111     inline bool is_working() const { return _working; }
112
113     /**
114      * add a sample group, return true if successful
115      */
116     bool add( SGSampleGroup *sgrp, const string& refname );
117
118     /** 
119      * remove a sample group, return true if successful
120      */
121     bool remove( const string& refname );
122
123     /**
124      * return true of the specified sound exists in the sound manager system
125      */
126     bool exists( const string& refname );
127
128     /**
129      * return a pointer to the SGSampleGroup if the specified sound
130      * exists in the sound manager system, otherwise return NULL
131      */
132     SGSampleGroup *find( const string& refname, bool create = false );
133
134     /**
135      * set the position of the listener (in opengl coordinates)
136      */
137     void set_position( const SGVec3d& pos ) {
138         _position = -pos;
139         _changed = true;
140     }
141
142     inline SGVec3f get_position() { return toVec3f(_position); }
143
144     /**
145      * set the velocity direction of the listener (in opengl coordinates)
146      */
147     void set_velocity( SGVec3d& dir ) {
148         _velocity = dir;
149         _changed = true;
150     }
151
152     inline SGVec3f get_velocity() { return toVec3f(_velocity); }
153
154     /**
155      * set the orientation of the listener (in opengl coordinates)
156      */
157     void set_orientation( SGQuatd ori );
158
159     inline const SGQuatd& get_orientation() { return _orientation; }
160
161     inline SGVec3f get_direction() {
162         return SGVec3f(_at_up_vec[0], _at_up_vec[1], _at_up_vec[2]);
163     }
164
165     enum {
166         NO_SOURCE = (unsigned int)-1,
167         NO_BUFFER = (unsigned int)-1
168     };
169
170     void set_volume( float v );
171     inline float get_volume() { return _volume; }
172
173     /**
174      * get a new OpenAL source id
175      * returns NO_SOURCE if no source is available
176      */
177     unsigned int request_source();
178
179     /**
180      * give back an OpenAL source id for further use.
181      */
182     void release_source( unsigned int source );
183
184     /**
185      * get a new OpenAL buffer id
186      * returns NO_BUFFER if loading of the buffer failed.
187      */
188     unsigned int request_buffer(SGSoundSample *sample);
189
190     /**
191      * give back an OpenAL source id for further use.
192      */
193     void release_buffer( SGSoundSample *sample );
194
195
196
197     /**
198      * returns true if the position has changed
199      */
200     inline bool has_changed() { return _changed; }
201
202     static bool load(string &samplepath, void **data, int *format,
203                                          unsigned int*size, int *freq );
204
205
206
207 private:
208     static int _alut_init;
209
210     bool _working;
211     bool _changed;
212     float _volume;
213
214     ALCdevice *_device;
215     ALCcontext *_context;
216
217     // Position of the listener.
218     SGVec3d _position;
219
220     // Velocity of the listener.
221     SGVec3d _velocity;
222
223     // Orientation of the listener. 
224     // first 3 elements are "at" vector, second 3 are "up" vector
225     SGQuatd _orientation;
226     ALfloat _at_up_vec[6];
227
228     sample_group_map _sample_groups;
229     buffer_map _buffers;
230
231     vector<ALuint> _free_sources;
232     vector<ALuint> _sources_in_use;
233
234     char *_devname;
235
236     bool testForALError(string s);
237     bool testForALCError(string s);
238     bool testForALUTError(string s);
239     bool testForError(void *p, string s);
240     void update_sample_config( SGSampleGroup *sound );
241 };
242
243
244 #endif // _SG_SOUNDMGR_OPENAL_HXX
245
246