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