1 // sample.hxx -- Sound sample encapsulation class
3 // Written by Curtis Olson, started April 2004.
5 // Copyright (C) 2004 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, 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 #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>
51 #include <simgear/debug/logstream.hxx>
57 * manages everything we need to know for an individual sound sample
66 // Buffers hold sound data.
69 // Sources are points emitting sound.
72 // Position of the source sound.
73 ALfloat source_pos[3];
75 // A constant offset to be applied to the final source_pos
76 ALfloat offset_pos[3];
78 // Velocity of the source sound.
79 ALfloat source_vel[3];
81 // configuration values
89 double reference_dist;
98 * @param path Path name to sound
99 * @param file File name of sound
100 * @param cleanup Request clean up the intermediate data (this
101 should usually be true unless you want to manipulate the data
104 SGSoundSample( const char *path, const char *file, bool cleanup );
105 SGSoundSample( unsigned char *_data, int len, int _freq );
109 * Start playing this sample.
111 * @param _loop Define wether the sound should be played in a loop.
113 void play( bool _loop );
116 * Stop playing this sample.
118 * @param sched A pointer to the appropriate scheduler.
123 * Play this sample once.
126 inline void play_once() { play(false); }
129 * Play this sample looped.
132 inline void play_looped() { play(true); }
135 * Test if a sample is curretnly playing.
136 * @return true if is is playing, false otherwise.
138 inline bool is_playing( ) {
140 alGetSourcei( source, AL_SOURCE_STATE, &result );
141 if ( alGetError() != AL_NO_ERROR) {
142 SG_LOG( SG_GENERAL, SG_ALERT,
143 "Oops AL error in sample is_playing(): " << sample_name );
145 return (result == AL_PLAYING) ;
149 * Get the current pitch setting of this sample.
151 inline double get_pitch() const { return pitch; }
154 * Set the pitch of this sample.
156 inline void set_pitch( double p ) {
157 // clamp in the range of 0.01 to 2.0
158 if ( p < 0.01 ) { p = 0.01; }
159 if ( p > 2.0 ) { p = 2.0; }
161 alSourcef( source, AL_PITCH, pitch );
162 if ( alGetError() != AL_NO_ERROR) {
163 SG_LOG( SG_GENERAL, SG_ALERT,
164 "Oops AL error in sample set_pitch()! " << p
165 << " for " << sample_name );
170 * Get the current volume setting of this sample.
172 inline double get_volume() const { return volume; }
175 * Set the volume of this sample.
177 inline void set_volume( double v ) {
179 alSourcef( source, AL_GAIN, volume );
180 if ( alGetError() != AL_NO_ERROR) {
181 SG_LOG( SG_GENERAL, SG_ALERT,
182 "Oops AL error in sample set_volume()! " << v
183 << " for " << sample_name );
188 * Returns the size of the sounds sample
190 inline int get_size() {
195 * Return a pointer to the raw data
197 inline char *get_data() {
202 * Set position of sound source (uses same coordinate system as opengl)
204 inline void set_source_pos( ALfloat *pos ) {
205 source_pos[0] = pos[0];
206 source_pos[1] = pos[1];
207 source_pos[2] = pos[2];
210 sgAddVec3( final_pos, source_pos, offset_pos );
212 alSourcefv( source, AL_POSITION, final_pos );
216 * Set "constant" offset position of sound source (uses same
217 * coordinate system as opengl)
219 inline void set_offset_pos( ALfloat *pos ) {
220 offset_pos[0] = pos[0];
221 offset_pos[1] = pos[1];
222 offset_pos[2] = pos[2];
225 sgAddVec3( final_pos, source_pos, offset_pos );
227 alSourcefv( source, AL_POSITION, final_pos );
231 * Set velocity of sound source (uses same coordinate system as opengl)
233 inline void set_source_vel( ALfloat *vel ) {
234 source_vel[0] = vel[0];
235 source_vel[1] = vel[1];
236 source_vel[2] = vel[2];
237 alSourcefv( source, AL_VELOCITY, source_vel );
242 * Set reference distance of sound (the distance where the gain
245 inline void set_reference_dist( ALfloat dist ) {
246 reference_dist = dist;
247 alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
252 * Set maximume distance of sound (the distance where the sound is
255 inline void set_max_dist( ALfloat dist ) {
257 alSourcef( source, AL_MAX_DISTANCE, max_dist );
262 #endif // _SG_SAMPLE_HXX