]> git.mxchange.org Git - simgear.git/blob - simgear/sound/soundmgr_openal.hxx
Effects framework
[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
22 // Foundation, 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 <simgear/compiler.h>
41
42 #include <string>
43 #include <map>
44
45 #if defined( __APPLE__ )
46 # include <OpenAL/al.h>
47 # include <OpenAL/alc.h>
48 #else
49 # include <AL/al.h>
50 # include <AL/alc.h>
51 #endif
52
53 #include "sample_openal.hxx"
54
55 using std::map;
56 using std::string;
57
58
59 typedef map < string, SGSharedPtr<SGSoundSample> > sample_map;
60 typedef sample_map::iterator sample_map_iterator;
61 typedef sample_map::const_iterator const_sample_map_iterator;
62
63
64 /**
65  * Manage a collection of SGSoundSample instances
66  */
67 class SGSoundMgr
68 {
69
70     ALCdevice *dev;
71     ALCcontext *context;
72
73     // Position of the listener.
74     ALfloat listener_pos[3];
75
76     // Velocity of the listener.
77     ALfloat listener_vel[3];
78
79     // Orientation of the listener. (first 3 elements are "at", second
80     // 3 are "up")
81     ALfloat listener_ori[6];
82
83     sample_map samples;
84
85     bool working;
86     double safety;
87
88 public:
89
90     SGSoundMgr();
91     ~SGSoundMgr();
92
93
94     /**
95      * (re) initialize the sound manager.
96      */
97     void init();
98
99
100     /**
101      * Bind properties for the sound manager.
102      */
103     void bind();
104
105
106     /**
107      * Unbind properties for the sound manager.
108      */
109     void unbind();
110
111
112     /**
113      * Run the audio scheduler.
114      */
115     void update(double dt);
116
117
118     /**
119      * Pause all sounds.
120      */
121     void pause();
122
123
124     /**
125      * Resume all sounds.
126      */
127     void resume();
128
129
130     /**
131      * is audio working?
132      */
133     inline bool is_working() const { return working; }
134
135     /**
136      * reinitialize the sound manager
137      */
138     inline void reinit() { init(); }
139
140     /**
141      * add a sound effect, return true if successful
142      */
143     bool add( SGSoundSample *sound, const string& refname);
144
145     /** 
146      * remove a sound effect, return true if successful
147      */
148     bool remove( const string& refname );
149
150     /**
151      * return true of the specified sound exists in the sound manager system
152      */
153     bool exists( const string& refname );
154
155     /**
156      * return a pointer to the SGSoundSample if the specified sound
157      * exists in the sound manager system, otherwise return NULL
158      */
159     SGSoundSample *find( const string& refname );
160
161     /**
162      * tell the scheduler to play the indexed sample in a continuous
163      * loop
164      */
165     bool play_looped( const string& refname );
166
167     /**
168      * tell the scheduler to play the indexed sample once
169      */
170     bool play_once( const string& refname );
171
172     /**
173      * return true of the specified sound is currently being played
174      */
175     bool is_playing( const string& refname );
176
177     /**
178      * immediate stop playing the sound
179      */
180     bool stop( const string& refname );
181
182     /**
183      * set overall volume for the application.
184      * @param vol 1.0 is default, must be greater than 0
185      */
186     inline void set_volume( const ALfloat vol ) {
187         if ( vol > 0.0 ) {
188             alListenerf( AL_GAIN, vol );
189         }
190     }
191
192     /**
193      * set the position of the listener (in opengl coordinates)
194      */
195     inline void set_listener_pos( ALfloat *pos ) {
196         listener_pos[0] = pos[0];
197         listener_pos[1] = pos[1];
198         listener_pos[2] = pos[2];
199         alListenerfv( AL_POSITION, listener_pos );
200     }
201
202     /**
203      * set the velocity of the listener (in opengl coordinates)
204      */
205     inline void set_listener_vel( ALfloat *vel ) {
206         listener_vel[0] = vel[0];
207         listener_vel[1] = vel[1];
208         listener_vel[2] = vel[2];
209 #ifdef USE_OPEN_AL_DOPPLER
210         alListenerfv( AL_VELOCITY, listener_vel );
211 #endif
212     }
213
214     /**
215      * set the orientation of the listener (in opengl coordinates)
216      *
217      * Description: ORIENTATION is a pair of 3-tuples representing the
218      * 'at' direction vector and 'up' direction of the Object in
219      * Cartesian space. AL expects two vectors that are orthogonal to
220      * each other. These vectors are not expected to be normalized. If
221      * one or more vectors have zero length, implementation behavior
222      * is undefined. If the two vectors are linearly dependent,
223      * behavior is undefined.
224      */
225     inline void set_listener_orientation( ALfloat *ori ) {
226         listener_ori[0] = ori[0];
227         listener_ori[1] = ori[1];
228         listener_ori[2] = ori[2];
229         listener_ori[3] = ori[3];
230         listener_ori[4] = ori[4];
231         listener_ori[5] = ori[5];
232         alListenerfv( AL_ORIENTATION, listener_ori );
233     }
234
235     /**
236      * set the positions of all managed sound sources 
237      */
238     void set_source_pos_all( ALfloat *pos );
239
240     /**
241      * set the velocities of all managed sound sources 
242      */
243     void set_source_vel_all( ALfloat *pos );
244 };
245
246
247 #endif // _SG_SOUNDMGR_OPENAL_HXX
248
249