]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.hxx
Rewrite the entire audio support library on top of OpenAL rather than plib's
[simgear.git] / simgear / sound / sample_openal.hxx
1 // sample.hxx -- Sound sample encapsulation class
2 // 
3 // Written by Curtis Olson, started April 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 /**
24  * \file sample.hxx
25  * Provides a sound sample encapsulation
26  */
27
28 #ifndef _SG_SAMPLE_HXX
29 #define _SG_SAMPLE_HXX 1
30
31 #ifndef __cplusplus
32 # error This library requires C++
33 #endif
34
35 #include <simgear/compiler.h>
36
37 #include <AL/al.h>
38
39 #include <simgear/debug/logstream.hxx>
40
41
42 /**
43  * manages everything we need to know for an individual sound sample
44  */
45
46 class SGSoundSample {
47
48 private:
49
50     // Buffers hold sound data.
51     ALuint buffer;
52
53     // Sources are points emitting sound.
54     ALuint source;
55
56     // Position of the source sound.
57     ALfloat source_pos[3];
58
59     // Velocity of the source sound.
60     ALfloat source_vel[3];
61
62     // configuration values
63     ALenum format;
64     ALsizei size;
65     ALvoid* data;
66     ALsizei freq;
67
68     double pitch;
69     double volume;
70     ALboolean loop;
71
72 public:
73
74     SGSoundSample( const char *path, const char *file );
75     SGSoundSample( unsigned char *_data, int len, int _freq );
76     ~SGSoundSample();
77
78     /**
79      * Start playing this sample.
80      *
81      * @param looped Define wether the sound should be played in a loop.
82      */
83     void play( bool _loop );
84
85     /**
86      * Stop playing this sample.
87      *
88      * @param sched A pointer to the appropriate scheduler.
89      */
90     void stop();
91
92     /**
93      * Play this sample once.
94      * @see #play
95      */
96     inline void play_once() { play(false); }
97
98     /** 
99      * Play this sample looped.
100      * @see #play
101      */
102     inline void play_looped() { play(true); }
103
104     /**
105      * Test if a sample is curretnly playing.
106      * @return true if is is playing, false otherwise.
107      */
108     inline bool is_playing( ) {
109         ALint result;
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()!" );
114         }
115         return (result == AL_PLAYING) ;
116     }
117
118     /**
119      * Get the current pitch setting of this sample.
120      */
121     inline double get_pitch() const { return pitch; }
122
123     /**
124      * Set the pitch of this sample.
125      */
126     inline void set_pitch( double p ) {
127         pitch = 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 );
132         }
133     }
134
135     /**
136      * Get the current volume setting of this sample.
137      */
138     inline double get_volume() const { return volume; }
139
140     /**
141      * Set the volume of this sample.
142      */
143     inline void set_volume( double v ) {
144         volume = 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()!" );
149         }
150     }
151
152     /**
153      * Returns the size of the sounds sample
154      */
155     inline int get_size() {
156         return size;
157     }
158
159     /**
160      * Return a pointer to the raw data
161      */
162     inline char *get_data() {
163         return (char *)data;
164     }
165 };
166
167
168 #endif // _SG_SAMPLE_HXX
169
170