]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.hxx
Maik JUSTUS: Doppler fixes (add option to turn off Doppler for sounds that
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 #include <simgear/structure/SGReferenced.hxx>
41 #include <simgear/structure/SGSharedPtr.hxx>
42
43 #include <plib/sg.h>
44
45 #if defined(__APPLE__)
46 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
47 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
48 # include <OpenAL/al.h>
49 # include <OpenAL/alut.h>
50 #else
51 # include <AL/al.h>
52 # include <AL/alut.h>
53 #endif
54
55
56 SG_USING_STD(string);
57
58 /**
59  * manages everything we need to know for an individual sound sample
60  */
61
62 class SGSoundSample : public SGReferenced {
63
64 private:
65
66     string sample_name;
67
68     // Buffers hold sound data.
69     ALuint buffer;
70
71     // Sources are points emitting sound.
72     ALuint source;
73
74     // Position of the source sound.
75     ALfloat source_pos[3];
76
77     // A constant offset to be applied to the final source_pos
78     ALfloat offset_pos[3];
79
80     // The orientation of the sound (direction and cut-off angles)
81     ALfloat direction[3];
82     ALfloat inner, outer, outergain;
83
84     // Velocity of the source sound.
85     ALfloat source_vel[3];
86
87     // configuration values
88     ALenum format;
89     ALsizei size;
90     ALsizei freq;
91
92     double pitch;
93     double volume;
94     double reference_dist;
95     double max_dist;
96     ALboolean loop;
97
98     bool playing;
99     bool bind_source();
100     bool no_Doppler_effect;
101
102 public:
103
104      /**
105       * Empty constructor, can be used to read data to the systems
106       * memory and not to the driver.
107       */
108     SGSoundSample();
109
110     /**
111      * Constructor
112      * @param path Path name to sound
113      * @param file File name of sound
114        should usually be true unless you want to manipulate the data
115        later.)
116      */
117     SGSoundSample( const char *path, const char *file , bool no_Doppler_effect = true );
118
119     /**
120      * Constructor.
121      * @param _data Pointer to a memory buffer containing the sample data
122        the application is responsible for freeing the buffer data.
123      * @param len Byte length of array
124      * @param _freq Frequency of the provided data (bytes per second)
125        should usually be true unless you want to manipulate the data
126        later.)
127      */
128     SGSoundSample( unsigned char *_data, int len, int _freq , bool no_Doppler_effect = true );
129
130     ~SGSoundSample();
131
132     /**
133      * Start playing this sample.
134      *
135      * @param _loop Define wether the sound should be played in a loop.
136      */
137     void play( bool _loop );
138
139     /**
140      * Stop playing this sample.
141      *
142      * @param sched A pointer to the appropriate scheduler.
143      */
144     void stop();
145
146     /**
147      * Play this sample once.
148      * @see #play
149      */
150     inline void play_once() { play(false); }
151
152     /** 
153      * Play this sample looped.
154      * @see #play
155      */
156     inline void play_looped() { play(true); }
157
158     /**
159      * Test if a sample is curretnly playing.
160      * @return true if is is playing, false otherwise.
161      */
162     bool is_playing( );
163
164     /**
165      * Get the current pitch setting of this sample.
166      */
167     inline double get_pitch() const { return pitch; }
168
169     /**
170      * Set the pitch of this sample.
171      */
172     void set_pitch( double p );
173
174     /**
175      * Get the current volume setting of this sample.
176      */
177     inline double get_volume() const { return volume; }
178
179     /**
180      * Set the volume of this sample.
181      */
182     void set_volume( double v );
183
184     /**
185      * Returns the size of the sounds sample
186      */
187     inline int get_size() {
188         return size;
189     }
190
191     /**
192      * Set position of sound source (uses same coordinate system as opengl)
193      */
194     void set_source_pos( ALfloat *pos );
195
196     /**
197      * Set "constant" offset position of sound source (uses same
198      * coordinate system as opengl)
199      */
200     void set_offset_pos( ALfloat *pos );
201
202     /**
203      * Set the orientation of the sound source, both for direction
204      * and audio cut-off angles.
205      */
206     void set_orientation( ALfloat *dir, ALfloat inner_angle=360.0,
207                                                ALfloat outer_angle=360.0,
208                                                ALfloat outer_gain=0.0);
209
210     /**
211      * Set velocity of sound source (uses same coordinate system as opengl)
212      */
213     void set_source_vel( ALfloat *vel , ALfloat *listener_vel );
214
215
216     /**
217      * Set reference distance of sound (the distance where the gain
218      * will be half.)
219      */
220     void set_reference_dist( ALfloat dist );
221
222
223     /**
224      * Set maximume distance of sound (the distance where the sound is
225      * no longer audible.
226      */
227     void set_max_dist( ALfloat dist );
228
229     /**
230      * Load a sound file into a memory buffer only.
231      */
232     ALvoid* load_file(const char *path, const char *file);
233 };
234
235
236 #endif // _SG_SAMPLE_HXX
237
238