00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00035 #ifndef _SG_SOUNDMGR_OPENAL_HXX
00036 #define _SG_SOUNDMGR_OPENAL_HXX 1
00037
00038 #ifndef __cplusplus
00039 # error This library requires C++
00040 #endif
00041
00042 #include <string>
00043 #include <vector>
00044 #include <map>
00045
00046 #if defined(__APPLE__)
00047 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
00048 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
00049 # include <OpenAL/al.h>
00050 # include <OpenAL/alc.h>
00051 # include <OpenAL/alut.h>
00052 #elif defined(OPENALSDK)
00053 # include <al.h>
00054 # include <alc.h>
00055 # include <AL/alut.h>
00056 #else
00057 # include <AL/al.h>
00058 # include <AL/alc.h>
00059 # include <AL/alut.h>
00060 #endif
00061
00062 #ifndef ALC_ALL_DEVICES_SPECIFIER
00063 # define ALC_ALL_DEVICES_SPECIFIER 0x1013
00064 #endif
00065
00066 #include <simgear/compiler.h>
00067 #include <simgear/structure/subsystem_mgr.hxx>
00068 #include <simgear/math/SGMathFwd.hxx>
00069
00070 #include "sample_group.hxx"
00071 #include "sample_openal.hxx"
00072
00073 using std::string;
00074
00075 struct refUint {
00076 unsigned int refctr;
00077 ALuint id;
00078
00079 refUint() { refctr = 0; id = (ALuint)-1; };
00080 refUint(ALuint i) { refctr = 1; id = i; };
00081 ~refUint() {};
00082 };
00083
00084 typedef std::map < string, refUint > buffer_map;
00085 typedef buffer_map::iterator buffer_map_iterator;
00086 typedef buffer_map::const_iterator const_buffer_map_iterator;
00087
00088 typedef std::map < string, SGSharedPtr<SGSampleGroup> > sample_group_map;
00089 typedef sample_group_map::iterator sample_group_map_iterator;
00090 typedef sample_group_map::const_iterator const_sample_group_map_iterator;
00091
00095 class SGSoundMgr : public SGSubsystem
00096 {
00097 public:
00098
00099 SGSoundMgr();
00100 ~SGSoundMgr();
00101
00102 void init(const char *devname = NULL);
00103 void bind();
00104 void unbind();
00105 void update(double dt);
00106
00107 void suspend();
00108 void resume();
00109 void stop();
00110
00111 inline void reinit() { stop(); init(); }
00112
00117 inline bool is_working() const { return _working; }
00118
00122 void activate();
00123
00128 inline bool is_active() const { return _active; }
00129
00136 bool add( SGSampleGroup *sgrp, const string& refname );
00137
00143 bool remove( const string& refname );
00144
00150 bool exists( const string& refname );
00151
00157 SGSampleGroup *find( const string& refname, bool create = false );
00158
00163 void set_position( const SGVec3d& pos, const SGGeod& pos_geod ) {
00164 _base_pos = pos; _geod_pos = pos_geod; _changed = true;
00165 }
00166
00167 void set_position_offset( const SGVec3d& pos ) {
00168 _offset_pos = pos; _changed = true;
00169 }
00170
00176 SGVec3d& get_position() { return _absolute_pos; }
00177
00183 void set_velocity( const SGVec3d& vel ) {
00184 _velocity = vel; _changed = true;
00185 }
00186
00192 inline SGVec3f get_velocity() { return toVec3f(_velocity); }
00193
00198 void set_orientation( const SGQuatd& ori ) {
00199 _orientation = ori; _changed = true;
00200 }
00201
00206 inline const SGQuatd& get_orientation() { return _orientation; }
00207
00213 SGVec3f get_direction() {
00214 return SGVec3f(_at_up_vec[0], _at_up_vec[1], _at_up_vec[2]);
00215 }
00216
00217 enum {
00218 NO_SOURCE = (unsigned int)-1,
00219 NO_BUFFER = (unsigned int)-1
00220 };
00221
00226 void set_volume( float vol );
00227
00232 inline float get_volume() { return _volume; }
00233
00238 unsigned int request_source();
00239
00244 void release_source( unsigned int source );
00245
00252 unsigned int request_buffer(SGSoundSample *sample);
00253
00258 void release_buffer( SGSoundSample *sample );
00259
00265 inline bool has_changed() { return _changed; }
00266
00273 inline bool bad_doppler_effect() { return _bad_doppler; }
00274
00284 bool load(string &samplepath, void **data, int *format,
00285 size_t *size, int *freq );
00286
00290 vector<const char*> get_available_devices();
00291
00295 const string& get_vendor() { return _vendor; }
00296 const string& get_renderer() { return _renderer; }
00297
00298 private:
00299 static int _alut_init;
00300
00301 bool _working;
00302 bool _active;
00303 bool _changed;
00304 float _volume;
00305
00306 ALCdevice *_device;
00307 ALCcontext *_context;
00308
00309
00310 SGVec3d _absolute_pos;
00311 SGVec3d _offset_pos;
00312 SGVec3d _base_pos;
00313 SGGeod _geod_pos;
00314
00315
00316 SGVec3d _velocity;
00317
00318
00319
00320 SGQuatd _orientation;
00321 ALfloat _at_up_vec[6];
00322
00323 sample_group_map _sample_groups;
00324 buffer_map _buffers;
00325
00326 vector<ALuint> _free_sources;
00327 vector<ALuint> _sources_in_use;
00328
00329 bool _bad_doppler;
00330 string _renderer;
00331 string _vendor;
00332
00333 bool testForALError(string s);
00334 bool testForALCError(string s);
00335 bool testForALUTError(string s);
00336 bool testForError(void *p, string s);
00337
00338 void update_pos_and_orientation();
00339 void update_sample_config( SGSampleGroup *sound );
00340 };
00341
00342
00343 #endif // _SG_SOUNDMGR_OPENAL_HXX
00344
00345