]> git.mxchange.org Git - simgear.git/blob - simgear/sound/soundmgr_openal.hxx
d60294dd236af65737617e49d88a9b444baa71d1
[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 #include <string>
39 #include <vector>
40 #include <map>
41 #include <memory> // for std::auto_ptr
42      
43 #include <simgear/compiler.h>
44 #include <simgear/structure/subsystem_mgr.hxx>
45 #include <simgear/math/SGMath.hxx>
46
47 // forward decls
48 class SGSampleGroup;
49 class SGSoundSample;
50
51 /**
52  * Manage a collection of SGSampleGroup instances
53  */
54 class SGSoundMgr : public SGSubsystem
55 {
56 public:
57
58     SGSoundMgr();
59     ~SGSoundMgr();
60
61     void init();
62     void update(double dt);
63     
64     void suspend();
65     void resume();
66     void stop();
67
68     void reinit();
69
70     /**
71      * Select a specific sound device.
72      * Requires a init/reinit call before sound is actually switched.
73      */
74     inline void select_device(const char* devname) {_device_name = devname;}
75
76     /**
77      * Test is the sound manager is in a working condition.
78      * @return true is the sound manager is working
79      */
80     bool is_working() const;
81
82     /**
83      * Set the sound manager to a  working condition.
84      */
85     void activate();
86
87     /**
88      * Test is the sound manager is in an active and working condition.
89      * @return true is the sound manager is active
90      */
91     inline bool is_active() const { return _active; }
92
93     /**
94      * Register a sample group to the sound manager.
95      * @param sgrp Pointer to a sample group to add
96      * @param refname Reference name of the sample group
97      * @return true if successful, false otherwise
98      */
99     bool add( SGSampleGroup *sgrp, const std::string& refname );
100
101     /** 
102      * Remove a sample group from the sound manager.
103      * @param refname Reference name of the sample group to remove
104      * @return true if successful, false otherwise
105      */
106     bool remove( const std::string& refname );
107
108     /**
109      * Test if a specified sample group is registered at the sound manager
110      * @param refname Reference name of the sample group test for
111      * @return true if the specified sample group exists
112      */
113     bool exists( const std::string& refname );
114
115     /**
116      * Find a specified sample group in the sound manager
117      * @param refname Reference name of the sample group to find
118      * @return A pointer to the SGSampleGroup
119      */
120     SGSampleGroup *find( const std::string& refname, bool create = false );
121
122     /**
123      * Set the Cartesian position of the sound manager.
124      * @param pos OpenAL listener position
125      */
126     void set_position( const SGVec3d& pos, const SGGeod& pos_geod );
127
128     void set_position_offset( const SGVec3d& pos ) {
129         _offset_pos = pos; _changed = true;
130     }
131
132     /**
133      * Get the position of the sound manager.
134      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
135      * @return OpenAL listener position
136      */
137     const SGVec3d& get_position() const;
138
139     /**
140      * Set the velocity vector (in meters per second) of the sound manager
141      * This is the horizontal local frame; x=north, y=east, z=down
142      * @param Velocity vector
143      */
144     void set_velocity( const SGVec3d& vel ) {
145         _velocity = vel; _changed = true;
146     }
147
148     /**
149      * Get the velocity vector of the sound manager
150      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
151      * @return Velocity vector of the OpenAL listener
152      */
153     inline SGVec3f get_velocity() { return toVec3f(_velocity); }
154
155     /**
156      * Set the orientation of the sound manager
157      * @param ori Quaternation containing the orientation information
158      */
159     void set_orientation( const SGQuatd& ori );
160
161     /**
162      * Get the orientation of the sound manager
163      * @return Quaternation containing the orientation information
164      */
165     const SGQuatd& get_orientation() const;
166
167     /**
168      * Get the direction vector of the sound manager
169      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
170      * @return Look-at direction of the OpenAL listener
171      */
172     SGVec3f get_direction() const;
173
174     enum {
175         NO_SOURCE = (unsigned int)-1,
176         NO_BUFFER = (unsigned int)-1,
177         FAILED_BUFFER = (unsigned int)-2
178     };
179
180     /**
181      * Set the master volume.
182      * @param vol Volume (must be between 0.0 and 1.0)
183      */
184     void set_volume( float vol );
185
186     /**
187      * Get the master volume.
188      * @return Volume (must be between 0.0 and 1.0)
189      */
190     inline float get_volume() { return _volume; }
191
192     /**
193      * Get a free OpenAL source-id
194      * @return NO_SOURCE if no source is available
195      */
196     unsigned int request_source();
197
198     /**
199      * Free an OpenAL source-id for future use
200      * @param source OpenAL source-id to free
201      */
202     void release_source( unsigned int source );
203
204     /**
205      * Get a free OpenAL buffer-id
206      * The buffer-id will be asigned to the sample by calling this function.
207      * @param sample Pointer to an audio sample to assign the buffer-id to
208      * @return NO_BUFFER if loading of the buffer failed.
209      */
210     unsigned int request_buffer(SGSoundSample *sample);
211
212     /**
213      * Free an OpenAL buffer-id for this sample
214      * @param sample Pointer to an audio sample for which to free the buffer
215      */
216     void release_buffer( SGSoundSample *sample );
217
218     /**
219      * Test if the position of the sound manager has changed.
220      * The value will be set to false upon the next call to update_late()
221      * @return true if the position has changed
222      */
223     inline bool has_changed() { return _changed; }
224
225     /**
226      * Some implementations seem to need the velocity multiplied by a
227      * factor of 100 to make them distinct. I've not found if this is
228      * a problem in the implementation or in out code. Until then
229      * this function is used to detect the problematic implementations.
230      */
231     inline bool bad_doppler_effect() { return _bad_doppler; }
232
233     /**
234      * Load a sample file and return it's configuration and data.
235      * @param samplepath Path to the file to load
236      * @param data Pointer to a variable that points to the allocated data
237      * @param format Pointer to a vairable that gets the OpenAL format
238      * @param size Pointer to a vairable that gets the sample size in bytes
239      * @param freq Pointer to a vairable that gets the sample frequency in Herz
240      * @return true if succesful, false on error
241      */
242     bool load(const std::string &samplepath, void **data, int *format,
243                                          size_t *size, int *freq );
244
245     /**
246      * Get a list of available playback devices.
247      */
248     std::vector<const char*> get_available_devices();
249
250     /**
251      * Get the current OpenAL vendor or rendering backend.
252      */
253     const std::string& get_vendor() { return _vendor; }
254     const std::string& get_renderer() { return _renderer; }
255
256 private:
257     class SoundManagerPrivate;
258     /// private implementation object
259     std::auto_ptr<SoundManagerPrivate> d;
260         
261     bool _active;
262     bool _changed;
263     float _volume;
264
265     // Position of the listener.
266     SGVec3d _offset_pos;
267     SGGeod _geod_pos;
268
269     // Velocity of the listener.
270     SGVec3d _velocity;
271
272     bool _bad_doppler;
273     std::string _renderer;
274     std::string _vendor;
275     std::string _device_name;
276
277     bool testForALError(std::string s);
278     bool testForALCError(std::string s);
279     bool testForError(void *p, std::string s);
280
281     void update_sample_config( SGSampleGroup *sound );
282 };
283
284
285 #endif // _SG_SOUNDMGR_OPENAL_HXX
286
287