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 #include <simgear/debug/logstream.hxx>
43 * manages everything we need to know for an individual sound sample
50 // Buffers hold sound data.
53 // Sources are points emitting sound.
56 // Position of the source sound.
57 ALfloat source_pos[3];
59 // Velocity of the source sound.
60 ALfloat source_vel[3];
62 // configuration values
74 SGSoundSample( const char *path, const char *file );
75 SGSoundSample( unsigned char *_data, int len, int _freq );
79 * Start playing this sample.
81 * @param looped Define wether the sound should be played in a loop.
83 void play( bool _loop );
86 * Stop playing this sample.
88 * @param sched A pointer to the appropriate scheduler.
93 * Play this sample once.
96 inline void play_once() { play(false); }
99 * Play this sample looped.
102 inline void play_looped() { play(true); }
105 * Test if a sample is curretnly playing.
106 * @return true if is is playing, false otherwise.
108 inline bool is_playing( ) {
110 alGetSourcei( source, AL_SOURCE_STATE, &result );
111 if ( alGetError() != AL_NO_ERROR) {
112 SG_LOG( SG_GENERAL, SG_ALERT,
113 "Oops AL error in sample is_playing()!" );
115 return (result == AL_PLAYING) ;
119 * Get the current pitch setting of this sample.
121 inline double get_pitch() const { return pitch; }
124 * Set the pitch of this sample.
126 inline void set_pitch( double p ) {
128 alSourcef( source, AL_PITCH, pitch );
129 if ( alGetError() != AL_NO_ERROR) {
130 SG_LOG( SG_GENERAL, SG_ALERT,
131 "Oops AL error in sample set_pitch()! " << p );
136 * Get the current volume setting of this sample.
138 inline double get_volume() const { return volume; }
141 * Set the volume of this sample.
143 inline void set_volume( double v ) {
145 alSourcef( source, AL_GAIN, volume );
146 if ( alGetError() != AL_NO_ERROR) {
147 SG_LOG( SG_GENERAL, SG_ALERT,
148 "Oops AL error in sample set_volume()!" );
153 * Returns the size of the sounds sample
155 inline int get_size() {
160 * Return a pointer to the raw data
162 inline char *get_data() {
168 #endif // _SG_SAMPLE_HXX