]> git.mxchange.org Git - simgear.git/blob - simgear/sound/soundmgr_openal.hxx
OpenAL buffer management; add a buffer cache to prevent loading the same sample in...
[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 #else
51 # include <AL/al.h>
52 # include <AL/alc.h>
53 # include <AL/alut.h>
54 #endif
55
56 #include <simgear/compiler.h>
57 #include <simgear/structure/subsystem_mgr.hxx>
58 #include <simgear/math/SGMathFwd.hxx>
59
60 #include "sample_group.hxx"
61 #include "sample_openal.hxx"
62
63 using std::string;
64
65 struct refUint {
66     unsigned int refctr;
67     ALuint id;
68
69     refUint() { refctr = 0; id = (ALuint)-1; };
70     refUint(ALuint i) { refctr = 1; id = i; };
71     ~refUint() {};
72 };
73
74 typedef std::map < string, refUint > buffer_map;
75 typedef buffer_map::iterator buffer_map_iterator;
76 typedef buffer_map::const_iterator  const_buffer_map_iterator;
77
78 typedef std::map < string, SGSharedPtr<SGSampleGroup> > sample_group_map;
79 typedef sample_group_map::iterator sample_group_map_iterator;
80 typedef sample_group_map::const_iterator const_sample_group_map_iterator;
81
82 /**
83  * Manage a collection of SGSampleGroup instances
84  */
85 class SGSoundMgr : public SGSubsystem
86 {
87 public:
88
89     SGSoundMgr();
90     ~SGSoundMgr();
91
92     void init();
93     void bind();
94     void unbind();
95     void update(double dt);
96     void update_late(double dt);
97     
98     void suspend();
99     void resume();
100     void stop();
101
102     inline void reinit() { stop(); init(); }
103
104     /**
105      * is audio working?
106      */
107     inline bool is_working() const { return _working; }
108
109     /**
110      * add a sample group, return true if successful
111      */
112     bool add( SGSampleGroup *sgrp, const string& refname );
113
114     /** 
115      * remove a sample group, return true if successful
116      */
117     bool remove( const string& refname );
118
119     /**
120      * return true of the specified sound exists in the sound manager system
121      */
122     bool exists( const string& refname );
123
124     /**
125      * return a pointer to the SGSampleGroup if the specified sound
126      * exists in the sound manager system, otherwise return NULL
127      */
128     SGSampleGroup *find( const string& refname, bool create = false );
129
130     /**
131      * set the position of the listener (in opengl coordinates)
132      */
133     inline void set_position( SGVec3d pos ) {
134         _listener_pos = pos;
135         _changed = true;
136     }
137
138     inline double *get_position() { return _listener_pos.data(); }
139     inline SGVec3d get_position_vec() { return _listener_pos; };
140
141     /**
142      * set the velocity of the listener (in opengl coordinates)
143      */
144     inline void set_velocity( SGVec3f vel ) {
145         _listener_vel = vel;
146         _changed = true;
147     }
148
149     inline SGVec3f get_velocity() { return _listener_vel; }
150
151     /**
152      * set the orientation of the listener (in opengl coordinates)
153      */
154     void set_orientation( SGQuatd ori );
155
156     inline SGVec3f get_direction() {
157         return SGVec3f(_listener_ori[0], _listener_ori[1], _listener_ori[2]);
158     }
159
160     enum {
161         NO_SOURCE = (unsigned int)-1,
162         NO_BUFFER = (unsigned int)-1
163     };
164
165     void set_volume( float v );
166     inline float get_volume() { return _volume; }
167
168     /**
169      * get a new OpenAL source id
170      * returns NO_SOURCE if no source is available
171      */
172     unsigned int request_source();
173
174     /**
175      * give back an OpenAL source id for further use.
176      */
177     void release_source( unsigned int source );
178
179     /**
180      * get a new OpenAL buffer id
181      * returns NO_BUFFER if loading of the buffer failed.
182      */
183     unsigned int request_buffer(SGSoundSample *sample);
184
185     /**
186      * give back an OpenAL source id for further use.
187      */
188     void release_buffer( SGSoundSample *sample );
189
190
191
192     /**
193      * returns true if the position has changed
194      */
195     inline bool has_changed() { return _changed; }
196
197     static bool load(string &samplepath, void **data, int *format,
198                                          unsigned int*size, int *freq );
199
200
201
202 private:
203     static int _alut_init;
204
205     bool _working;
206     bool _changed;
207     float _volume;
208
209     ALCdevice *_device;
210     ALCcontext *_context;
211
212     // Position of the listener.
213     SGVec3d _listener_pos;
214
215     // Velocity of the listener.
216     SGVec3f _listener_vel;
217
218     // Orientation of the listener. 
219     // first 3 elements are "at" vector, second 3 are "up" vector
220     ALfloat _listener_ori[6];
221
222     sample_group_map _sample_groups;
223     buffer_map _buffers;
224
225     vector<ALuint> _free_sources;
226     vector<ALuint> _sources_in_use;
227
228     char *_devname;
229
230     bool testForALError(string s);
231     bool testForALCError(string s);
232     bool testForALUTError(string s);
233     bool testForError(void *p, string s);
234     void update_sample_config( SGSampleGroup *sound );
235 };
236
237
238 #endif // _SG_SOUNDMGR_OPENAL_HXX
239
240