1 // soundmgr.hxx -- Sound effect management class
3 // Sound manager initially written by David Findlay
4 // <david_j_findlay@yahoo.com.au> 2001
6 // C++-ified by Curtis Olson, started March 2001.
8 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
28 * Provides a sound manager class to keep track of
29 * multiple sounds and manage playing them with different effects and
33 #ifndef _SG_SOUNDMGR_OPENAL_HXX
34 #define _SG_SOUNDMGR_OPENAL_HXX 1
37 # error This library requires C++
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>
56 #include <simgear/compiler.h>
57 #include <simgear/structure/subsystem_mgr.hxx>
58 #include <simgear/math/SGMathFwd.hxx>
60 #include "sample_group.hxx"
61 #include "sample_openal.hxx"
66 typedef map < string, SGSharedPtr<SGSampleGroup> > sample_group_map;
67 typedef sample_group_map::iterator sample_group_map_iterator;
68 typedef sample_group_map::const_iterator const_sample_group_map_iterator;
72 * Manage a collection of SGSampleGroup instances
74 class SGSoundMgr : public SGSubsystem
84 void update(double dt);
85 void update_late(double dt);
91 inline void reinit() { stop(); init(); }
96 inline bool is_working() const { return _working; }
99 * add a sample group, return true if successful
101 bool add( SGSampleGroup *sgrp, const string& refname );
104 * remove a sample group, return true if successful
106 bool remove( const string& refname );
109 * return true of the specified sound exists in the sound manager system
111 bool exists( const string& refname );
114 * return a pointer to the SGSampleGroup if the specified sound
115 * exists in the sound manager system, otherwise return NULL
117 SGSampleGroup *find( const string& refname, bool create = false );
120 * set the position of the listener (in opengl coordinates)
122 inline void set_position( SGVec3d pos ) {
127 inline double *get_position() { return _listener_pos.data(); }
128 inline SGVec3d get_position_vec() { return _listener_pos; };
131 * set the velocity of the listener (in opengl coordinates)
133 inline void set_velocity( SGVec3f vel ) {
138 inline SGVec3f get_velocity() { return _listener_vel; }
141 * set the orientation of the listener (in opengl coordinates)
143 void set_orientation( SGQuatd ori );
145 inline SGVec3f get_direction() {
146 return SGVec3f(_listener_ori[0], _listener_ori[1], _listener_ori[2]);
150 NO_SOURCE = (unsigned int)-1,
151 NO_BUFFER = (unsigned int)-1
154 void set_volume( float v );
155 inline float get_volume() { return _volume; }
158 * get a new OpenAL source id
159 * returns NO_SOURCE is no source is available
161 unsigned int request_source();
164 * give back an OpenAL source id for further use.
166 void release_source( unsigned int source );
170 * returns true if the position has changed
172 inline bool has_changed() { return _changed; }
174 static bool load(string &samplepath, void **data, int *format,
175 unsigned int*size, int *freq );
180 static int _alut_init;
187 ALCcontext *_context;
189 // Position of the listener.
190 SGVec3d _listener_pos;
192 // Velocity of the listener.
193 SGVec3f _listener_vel;
195 // Orientation of the listener.
196 // first 3 elements are "at" vector, second 3 are "up" vector
197 ALfloat _listener_ori[6];
199 sample_group_map _sample_groups;
201 vector<ALuint> _free_sources;
202 vector<ALuint> _sources_in_use;
206 bool testForALError(string s);
207 bool testForALCError(string s);
208 bool testForALUTError(string s);
209 bool testForError(void *p, string s);
210 void update_sample_config( SGSampleGroup *sound );
214 #endif // _SG_SOUNDMGR_OPENAL_HXX