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>
49 #include <simgear/debug/logstream.hxx>
55 * manages everything we need to know for an individual sound sample
64 // Buffers hold sound data.
67 // Sources are points emitting sound.
70 // Position of the source sound.
71 ALfloat source_pos[3];
73 // Velocity of the source sound.
74 ALfloat source_vel[3];
76 // configuration values
90 * @param path Path name to sound
91 * @param file File name of sound
92 * @param cleanup Request clean up the intermediate data (this
93 should usually be true unless you want to manipulate the data
96 SGSoundSample( const char *path, const char *file, bool cleanup );
97 SGSoundSample( unsigned char *_data, int len, int _freq );
101 * Start playing this sample.
103 * @param _loop Define wether the sound should be played in a loop.
105 void play( bool _loop );
108 * Stop playing this sample.
110 * @param sched A pointer to the appropriate scheduler.
115 * Play this sample once.
118 inline void play_once() { play(false); }
121 * Play this sample looped.
124 inline void play_looped() { play(true); }
127 * Test if a sample is curretnly playing.
128 * @return true if is is playing, false otherwise.
130 inline bool is_playing( ) {
132 alGetSourcei( source, AL_SOURCE_STATE, &result );
133 if ( alGetError() != AL_NO_ERROR) {
134 SG_LOG( SG_GENERAL, SG_ALERT,
135 "Oops AL error in sample is_playing(): " << sample_name );
137 return (result == AL_PLAYING) ;
141 * Get the current pitch setting of this sample.
143 inline double get_pitch() const { return pitch; }
146 * Set the pitch of this sample.
148 inline void set_pitch( double p ) {
149 // clamp in the range of 0.01 to 2.0
150 if ( p < 0.01 ) { p = 0.01; }
151 if ( p > 2.0 ) { p = 2.0; }
153 alSourcef( source, AL_PITCH, pitch );
154 if ( alGetError() != AL_NO_ERROR) {
155 SG_LOG( SG_GENERAL, SG_ALERT,
156 "Oops AL error in sample set_pitch()! " << p
157 << " for " << sample_name );
162 * Get the current volume setting of this sample.
164 inline double get_volume() const { return volume; }
167 * Set the volume of this sample.
169 inline void set_volume( double v ) {
171 alSourcef( source, AL_GAIN, volume );
172 if ( alGetError() != AL_NO_ERROR) {
173 SG_LOG( SG_GENERAL, SG_ALERT,
174 "Oops AL error in sample set_volume()! " << v
175 << " for " << sample_name );
180 * Returns the size of the sounds sample
182 inline int get_size() {
187 * Return a pointer to the raw data
189 inline char *get_data() {
194 * Set position of sound source (uses same coordinate system as opengl)
196 inline void set_source_pos( ALfloat *pos ) {
197 source_pos[0] = pos[0];
198 source_pos[1] = pos[1];
199 source_pos[2] = pos[2];
200 alSourcefv( source, AL_POSITION, source_pos );
204 * Set velocity of sound source (uses same coordinate system as opengl)
206 inline void set_source_vel( ALfloat *vel ) {
207 source_vel[0] = vel[0];
208 source_vel[1] = vel[1];
209 source_vel[2] = vel[2];
210 alSourcefv( source, AL_VELOCITY, source_vel );
216 #endif // _SG_SAMPLE_HXX