]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.hxx
Expose the ability to specify how the sound volume fades relative to
[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 - curt@flightgear.org
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 #if defined(__APPLE__)
40 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
41 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
42 # include <OpenAL/al.h>
43 # include <OpenAL/alut.h>
44 #else
45 # include <AL/al.h>
46 # include <AL/alut.h>
47 #endif
48
49 #include <simgear/debug/logstream.hxx>
50
51 SG_USING_STD(string);
52
53
54 /**
55  * manages everything we need to know for an individual sound sample
56  */
57
58 class SGSoundSample {
59
60 private:
61
62     string sample_name;
63
64     // Buffers hold sound data.
65     ALuint buffer;
66
67     // Sources are points emitting sound.
68     ALuint source;
69
70     // Position of the source sound.
71     ALfloat source_pos[3];
72
73     // Velocity of the source sound.
74     ALfloat source_vel[3];
75
76     // configuration values
77     ALenum format;
78     ALsizei size;
79     ALvoid* data;
80     ALsizei freq;
81
82     double pitch;
83     double volume;
84     double reference_dist;
85     double max_dist;
86     ALboolean loop;
87
88 public:
89
90     /**
91      * Constructor
92      * @param path Path name to sound
93      * @param file File name of sound
94      * @param cleanup Request clean up the intermediate data (this
95        should usually be true unless you want to manipulate the data
96        later.)
97      */
98     SGSoundSample( const char *path, const char *file, bool cleanup );
99     SGSoundSample( unsigned char *_data, int len, int _freq );
100     ~SGSoundSample();
101
102     /**
103      * Start playing this sample.
104      *
105      * @param _loop Define wether the sound should be played in a loop.
106      */
107     void play( bool _loop );
108
109     /**
110      * Stop playing this sample.
111      *
112      * @param sched A pointer to the appropriate scheduler.
113      */
114     void stop();
115
116     /**
117      * Play this sample once.
118      * @see #play
119      */
120     inline void play_once() { play(false); }
121
122     /** 
123      * Play this sample looped.
124      * @see #play
125      */
126     inline void play_looped() { play(true); }
127
128     /**
129      * Test if a sample is curretnly playing.
130      * @return true if is is playing, false otherwise.
131      */
132     inline bool is_playing( ) {
133         ALint result;
134         alGetSourcei( source, AL_SOURCE_STATE, &result );
135         if ( alGetError() != AL_NO_ERROR) {
136             SG_LOG( SG_GENERAL, SG_ALERT,
137                     "Oops AL error in sample is_playing(): " << sample_name );
138         }
139         return (result == AL_PLAYING) ;
140     }
141
142     /**
143      * Get the current pitch setting of this sample.
144      */
145     inline double get_pitch() const { return pitch; }
146
147     /**
148      * Set the pitch of this sample.
149      */
150     inline void set_pitch( double p ) {
151         // clamp in the range of 0.01 to 2.0
152         if ( p < 0.01 ) { p = 0.01; }
153         if ( p > 2.0 ) { p = 2.0; }
154         pitch = p;
155         alSourcef( source, AL_PITCH, pitch );
156         if ( alGetError() != AL_NO_ERROR) {
157             SG_LOG( SG_GENERAL, SG_ALERT,
158                     "Oops AL error in sample set_pitch()! " << p
159                     << " for " << sample_name );
160         }
161     }
162
163     /**
164      * Get the current volume setting of this sample.
165      */
166     inline double get_volume() const { return volume; }
167
168     /**
169      * Set the volume of this sample.
170      */
171     inline void set_volume( double v ) {
172         volume = v;
173         alSourcef( source, AL_GAIN, volume );
174         if ( alGetError() != AL_NO_ERROR) {
175             SG_LOG( SG_GENERAL, SG_ALERT,
176                     "Oops AL error in sample set_volume()! " << v
177                     << " for " << sample_name  );
178         }
179     }
180
181     /**
182      * Returns the size of the sounds sample
183      */
184     inline int get_size() {
185         return size;
186     }
187
188     /**
189      * Return a pointer to the raw data
190      */
191     inline char *get_data() {
192         return (char *)data;
193     }
194
195     /**
196      * Set position of sound source (uses same coordinate system as opengl)
197      */
198     inline void set_source_pos( ALfloat *pos ) {
199         source_pos[0] = pos[0];
200         source_pos[1] = pos[1];
201         source_pos[2] = pos[2];
202         alSourcefv( source, AL_POSITION, source_pos );
203     }
204
205     /**
206      * Set velocity of sound source (uses same coordinate system as opengl)
207      */
208     inline void set_source_vel( ALfloat *vel ) {
209         source_vel[0] = vel[0];
210         source_vel[1] = vel[1];
211         source_vel[2] = vel[2];
212         alSourcefv( source, AL_VELOCITY, source_vel );
213     }
214
215
216     /**
217      * Set reference distance of sound (the distance where the gain
218      * will be half.)
219      */
220     inline void set_reference_dist( ALfloat dist ) {
221         reference_dist = dist;
222         alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
223     }
224
225
226     /**
227      * Set maximume distance of sound (the distance where the sound is
228      * no longer audible.
229      */
230     inline void set_max_dist( ALfloat dist ) {
231         max_dist = dist;
232         alSourcef( source, AL_MAX_DISTANCE, max_dist );
233     }
234 };
235
236
237 #endif // _SG_SAMPLE_HXX
238
239