1 // sample.hxx -- Sound sample encapsulation class
3 // Written by Curtis Olson, started April 2004.
5 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
25 * Provides a sound sample encapsulation
28 #ifndef _SG_SAMPLE_HXX
29 #define _SG_SAMPLE_HXX 1
32 # error This library requires C++
35 #include <simgear/compiler.h>
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/structure/SGReferenced.hxx>
41 #include <simgear/structure/SGSharedPtr.hxx>
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>
55 #ifndef HAVE_WINDOWS_H
57 #define USE_OPEN_AL_DOPPLER should work
59 #define USE_OPEN_AL_DOPPLER_WITH_FIXED_LISTENER better than nothing
62 // the Open_AL Doppler calculation seems to be buggy on windows
63 #define USE_SOFTWARE_DOPPLER seem to be necessary
69 * manages everything we need to know for an individual sound sample
72 class SGSoundSample : public SGReferenced {
78 // Buffers hold sound data.
81 // Sources are points emitting sound.
84 // Position of the source sound.
85 ALfloat source_pos[3];
87 // A constant offset to be applied to the final source_pos
88 ALfloat offset_pos[3];
90 // The orientation of the sound (direction and cut-off angles)
92 ALfloat inner, outer, outergain;
94 // Velocity of the source sound.
95 ALfloat source_vel[3];
97 // configuration values
104 #ifdef USE_SOFTWARE_DOPPLER
105 double doppler_pitch_factor;
106 double doppler_volume_factor;
108 double reference_dist;
114 bool no_Doppler_effect;
119 * Empty constructor, can be used to read data to the systems
120 * memory and not to the driver.
126 * @param path Path name to sound
127 * @param file File name of sound
128 should usually be true unless you want to manipulate the data
131 SGSoundSample( const char *path, const char *file, bool no_Doppler_effect = true );
135 * @param _data Pointer to a memory buffer containing the sample data
136 the application is responsible for freeing the buffer data.
137 * @param len Byte length of array
138 * @param _freq Frequency of the provided data (bytes per second)
139 should usually be true unless you want to manipulate the data
142 SGSoundSample( unsigned char *_data, int len, int _freq, bool no_Doppler_effect = true );
147 * Start playing this sample.
149 * @param _loop Define wether the sound should be played in a loop.
151 void play( bool _loop );
154 * Stop playing this sample.
156 * @param sched A pointer to the appropriate scheduler.
161 * Play this sample once.
164 inline void play_once() { play(false); }
167 * Play this sample looped.
170 inline void play_looped() { play(true); }
173 * Test if a sample is curretnly playing.
174 * @return true if is is playing, false otherwise.
179 * Get the current pitch setting of this sample.
181 inline double get_pitch() const { return pitch; }
184 * Set the pitch of this sample.
186 void set_pitch( double p );
189 * Get the current volume setting of this sample.
191 inline double get_volume() const { return volume; }
194 * Set the volume of this sample.
196 void set_volume( double v );
199 * Returns the size of the sounds sample
201 inline int get_size() {
206 * Set position of sound source (uses same coordinate system as opengl)
208 void set_source_pos( ALfloat *pos );
211 * Set "constant" offset position of sound source (uses same
212 * coordinate system as opengl)
214 void set_offset_pos( ALfloat *pos );
217 * Set the orientation of the sound source, both for direction
218 * and audio cut-off angles.
220 void set_orientation( ALfloat *dir, ALfloat inner_angle=360.0,
221 ALfloat outer_angle=360.0,
222 ALfloat outer_gain=0.0);
225 * Set velocity of sound source (uses same coordinate system as opengl)
227 void set_source_vel( ALfloat *vel, ALfloat *listener_vel );
231 * Set reference distance of sound (the distance where the gain
234 void set_reference_dist( ALfloat dist );
238 * Set maximume distance of sound (the distance where the sound is
241 void set_max_dist( ALfloat dist );
244 * Load a sound file into a memory buffer only.
246 ALvoid* load_file(const char *path, const char *file);
250 #endif // _SG_SAMPLE_HXX