]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.hxx
My old email address is no longer valid ... point to my web page.
[simgear.git] / simgear / sound / sample_openal.hxx
1 // sample.hxx -- Sound sample encapsulation class
2 // 
3 // Written by Curtis Olson, started April 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 /**
24  * \file sample.hxx
25  * Provides a sound sample encapsulation
26  */
27
28 #ifndef _SG_SAMPLE_HXX
29 #define _SG_SAMPLE_HXX 1
30
31 #ifndef __cplusplus
32 # error This library requires C++
33 #endif
34
35 #include <simgear/compiler.h>
36
37 #include STL_STRING
38
39 #include <simgear/debug/logstream.hxx>
40
41 #include <plib/sg.h>
42
43 #if defined(__APPLE__)
44 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
45 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
46 # include <OpenAL/al.h>
47 # include <OpenAL/alut.h>
48 #else
49 # include <AL/al.h>
50 # include <AL/alut.h>
51 #endif
52
53 SG_USING_STD(string);
54
55 /**
56  * manages everything we need to know for an individual sound sample
57  */
58
59 class SGSoundSample {
60
61 private:
62
63     string sample_name;
64
65     // Buffers hold sound data.
66     ALuint buffer;
67
68     // Sources are points emitting sound.
69     ALuint source;
70
71     // Position of the source sound.
72     ALfloat source_pos[3];
73
74     // A constant offset to be applied to the final source_pos
75     ALfloat offset_pos[3];
76
77     // The orientation of the sound (direction and cut-off angles)
78     ALfloat direction[3];
79     ALfloat inner, outer, outergain;
80
81     // Velocity of the source sound.
82     ALfloat source_vel[3];
83
84     // configuration values
85     ALenum format;
86     ALsizei size;
87     ALvoid* data;
88     ALsizei freq;
89
90     double pitch;
91     double volume;
92     double reference_dist;
93     double max_dist;
94     ALboolean loop;
95
96
97 public:
98
99     /**
100      * Constructor
101      * @param path Path name to sound
102      * @param file File name of sound
103      * @param cleanup Request clean up the intermediate data (this
104        should usually be true unless you want to manipulate the data
105        later.)
106      */
107     SGSoundSample( const char *path, const char *file, bool cleanup );
108
109     /**
110      * Constructor.
111      * @param _data Pointer to a memory buffer containing the sample data
112      * @param len Byte length of array
113      * @param _freq Frequency of the provided data (bytes per second)
114      * @param cleanup Request clean up the intermediate data (this
115        should usually be true unless you want to manipulate the data
116        later.)
117      */
118     SGSoundSample( unsigned char *_data, int len, int _freq, bool cleanup );
119
120     ~SGSoundSample();
121
122     /**
123      * Start playing this sample.
124      *
125      * @param _loop Define wether the sound should be played in a loop.
126      */
127     void play( bool _loop );
128
129     /**
130      * Stop playing this sample.
131      *
132      * @param sched A pointer to the appropriate scheduler.
133      */
134     void stop();
135
136     /**
137      * Play this sample once.
138      * @see #play
139      */
140     inline void play_once() { play(false); }
141
142     /** 
143      * Play this sample looped.
144      * @see #play
145      */
146     inline void play_looped() { play(true); }
147
148     /**
149      * Test if a sample is curretnly playing.
150      * @return true if is is playing, false otherwise.
151      */
152     inline bool is_playing( ) {
153         ALint result;
154         alGetSourcei( source, AL_SOURCE_STATE, &result );
155         if ( alGetError() != AL_NO_ERROR) {
156             SG_LOG( SG_GENERAL, SG_ALERT,
157                     "Oops AL error in sample is_playing(): " << sample_name );
158         }
159         return (result == AL_PLAYING) ;
160     }
161
162     /**
163      * Get the current pitch setting of this sample.
164      */
165     inline double get_pitch() const { return pitch; }
166
167     /**
168      * Set the pitch of this sample.
169      */
170     inline void set_pitch( double p ) {
171         // clamp in the range of 0.01 to 2.0
172         if ( p < 0.01 ) { p = 0.01; }
173         if ( p > 2.0 ) { p = 2.0; }
174         pitch = p;
175         alSourcef( source, AL_PITCH, pitch );
176         if ( alGetError() != AL_NO_ERROR) {
177             SG_LOG( SG_GENERAL, SG_ALERT,
178                     "Oops AL error in sample set_pitch()! " << p
179                     << " for " << sample_name );
180         }
181     }
182
183     /**
184      * Get the current volume setting of this sample.
185      */
186     inline double get_volume() const { return volume; }
187
188     /**
189      * Set the volume of this sample.
190      */
191     inline void set_volume( double v ) {
192         volume = v;
193         alSourcef( source, AL_GAIN, volume );
194         if ( alGetError() != AL_NO_ERROR) {
195             SG_LOG( SG_GENERAL, SG_ALERT,
196                     "Oops AL error in sample set_volume()! " << v
197                     << " for " << sample_name  );
198         }
199     }
200
201     /**
202      * Returns the size of the sounds sample
203      */
204     inline int get_size() {
205         return size;
206     }
207
208     /**
209      * Return a pointer to the raw data
210      */
211     inline char *get_data() {
212         return (char *)data;
213     }
214
215     /**
216      * Set position of sound source (uses same coordinate system as opengl)
217      */
218     inline void set_source_pos( ALfloat *pos ) {
219         source_pos[0] = pos[0];
220         source_pos[1] = pos[1];
221         source_pos[2] = pos[2];
222
223         sgVec3 final_pos;
224         sgAddVec3( final_pos, source_pos, offset_pos );
225
226         alSourcefv( source, AL_POSITION, final_pos );
227     }
228
229     /**
230      * Set "constant" offset position of sound source (uses same
231      * coordinate system as opengl)
232      */
233     inline void set_offset_pos( ALfloat *pos ) {
234         offset_pos[0] = pos[0];
235         offset_pos[1] = pos[1];
236         offset_pos[2] = pos[2];
237
238         sgVec3 final_pos;
239         sgAddVec3( final_pos, source_pos, offset_pos );
240
241         alSourcefv( source, AL_POSITION, final_pos );
242     }
243
244     /**
245      * Set the orientation of the sound source, both for direction
246      * and audio cut-off angles.
247      */
248     inline void set_orientation( ALfloat *dir, ALfloat inner_angle=360.0,
249                                                ALfloat outer_angle=360.0,
250                                                ALfloat outer_gain=0.0)
251     {
252         inner = inner_angle;
253         outer = outer_angle;
254         outergain = outer_gain;
255         alSourcefv( source, AL_DIRECTION, dir);
256         alSourcef( source, AL_CONE_INNER_ANGLE, inner );
257         alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
258         alSourcef( source, AL_CONE_OUTER_GAIN, outergain );
259     }
260
261     /**
262      * Set velocity of sound source (uses same coordinate system as opengl)
263      */
264     inline void set_source_vel( ALfloat *vel ) {
265         source_vel[0] = vel[0];
266         source_vel[1] = vel[1];
267         source_vel[2] = vel[2];
268         alSourcefv( source, AL_VELOCITY, source_vel );
269     }
270
271
272     /**
273      * Set reference distance of sound (the distance where the gain
274      * will be half.)
275      */
276     inline void set_reference_dist( ALfloat dist ) {
277         reference_dist = dist;
278         alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
279     }
280
281
282     /**
283      * Set maximume distance of sound (the distance where the sound is
284      * no longer audible.
285      */
286     inline void set_max_dist( ALfloat dist ) {
287         max_dist = dist;
288         alSourcef( source, AL_MAX_DISTANCE, max_dist );
289     }
290 };
291
292
293 #endif // _SG_SAMPLE_HXX
294
295