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>
37 #if defined(__APPLE__)
38 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
39 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
40 # include <OpenAL/al.h>
41 # include <OpenAL/alut.h>
47 #include <simgear/debug/logstream.hxx>
51 * manages everything we need to know for an individual sound sample
58 // Buffers hold sound data.
61 // Sources are points emitting sound.
64 // Position of the source sound.
65 ALfloat source_pos[3];
67 // Velocity of the source sound.
68 ALfloat source_vel[3];
70 // configuration values
82 SGSoundSample( const char *path, const char *file );
83 SGSoundSample( unsigned char *_data, int len, int _freq );
87 * Start playing this sample.
89 * @param looped Define wether the sound should be played in a loop.
91 void play( bool _loop );
94 * Stop playing this sample.
96 * @param sched A pointer to the appropriate scheduler.
101 * Play this sample once.
104 inline void play_once() { play(false); }
107 * Play this sample looped.
110 inline void play_looped() { play(true); }
113 * Test if a sample is curretnly playing.
114 * @return true if is is playing, false otherwise.
116 inline bool is_playing( ) {
118 alGetSourcei( source, AL_SOURCE_STATE, &result );
119 if ( alGetError() != AL_NO_ERROR) {
120 SG_LOG( SG_GENERAL, SG_ALERT,
121 "Oops AL error in sample is_playing()!" );
123 return (result == AL_PLAYING) ;
127 * Get the current pitch setting of this sample.
129 inline double get_pitch() const { return pitch; }
132 * Set the pitch of this sample.
134 inline void set_pitch( double p ) {
135 // clamp in the range of 0.01 to 2.0
136 if ( p < 0.01 ) { p = 0.01; }
137 if ( p > 2.0 ) { p = 2.0; }
139 alSourcef( source, AL_PITCH, pitch );
140 if ( alGetError() != AL_NO_ERROR) {
141 SG_LOG( SG_GENERAL, SG_ALERT,
142 "Oops AL error in sample set_pitch()! " << p );
147 * Get the current volume setting of this sample.
149 inline double get_volume() const { return volume; }
152 * Set the volume of this sample.
154 inline void set_volume( double v ) {
156 alSourcef( source, AL_GAIN, volume );
157 if ( alGetError() != AL_NO_ERROR) {
158 SG_LOG( SG_GENERAL, SG_ALERT,
159 "Oops AL error in sample set_volume()!" );
164 * Returns the size of the sounds sample
166 inline int get_size() {
171 * Return a pointer to the raw data
173 inline char *get_data() {
179 #endif // _SG_SAMPLE_HXX