]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.hxx
Prevent a possible memory leak.
[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 - http://www.flightgear.org/~curt
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 STL_STRING
38
39 #include <simgear/debug/logstream.hxx>
40
41 #include <plib/sg.h>
42
43 #if defined(__APPLE__)
44 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
45 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
46 # include <OpenAL/al.h>
47 # include <OpenAL/alut.h>
48 #else
49 # include <AL/al.h>
50 # include <AL/alut.h>
51 #endif
52
53 SG_USING_STD(string);
54
55 /**
56  * manages everything we need to know for an individual sound sample
57  */
58
59 class SGSoundSample {
60
61 private:
62
63     string sample_name;
64
65     // Buffers hold sound data.
66     ALuint buffer;
67
68     // Sources are points emitting sound.
69     ALuint source;
70
71     // Position of the source sound.
72     ALfloat source_pos[3];
73
74     // A constant offset to be applied to the final source_pos
75     ALfloat offset_pos[3];
76
77     // The orientation of the sound (direction and cut-off angles)
78     ALfloat direction[3];
79     ALfloat inner, outer, outergain;
80
81     // Velocity of the source sound.
82     ALfloat source_vel[3];
83
84     // configuration values
85     ALenum format;
86     ALsizei size;
87     ALsizei freq;
88
89     double pitch;
90     double volume;
91     double reference_dist;
92     double max_dist;
93     ALboolean loop;
94
95     bool playing;
96     bool bind_source();
97
98 public:
99
100      /**
101       * Empty constructor, can be used to read data to the systems
102       * memory and not to the driver.
103       */
104     SGSoundSample();
105
106     /**
107      * Constructor
108      * @param path Path name to sound
109      * @param file File name of sound
110        should usually be true unless you want to manipulate the data
111        later.)
112      */
113     SGSoundSample( const char *path, const char *file );
114
115     /**
116      * Constructor.
117      * @param _data Pointer to a memory buffer containing the sample data
118      * @param len Byte length of array
119      * @param _freq Frequency of the provided data (bytes per second)
120        should usually be true unless you want to manipulate the data
121        later.)
122      */
123     SGSoundSample( unsigned char *_data, int len, int _freq );
124
125     ~SGSoundSample();
126
127     /**
128      * Start playing this sample.
129      *
130      * @param _loop Define wether the sound should be played in a loop.
131      */
132     void play( bool _loop );
133
134     /**
135      * Stop playing this sample.
136      *
137      * @param sched A pointer to the appropriate scheduler.
138      */
139     void stop();
140
141     /**
142      * Play this sample once.
143      * @see #play
144      */
145     inline void play_once() { play(false); }
146
147     /** 
148      * Play this sample looped.
149      * @see #play
150      */
151     inline void play_looped() { play(true); }
152
153     /**
154      * Test if a sample is curretnly playing.
155      * @return true if is is playing, false otherwise.
156      */
157     bool is_playing( );
158
159     /**
160      * Get the current pitch setting of this sample.
161      */
162     inline double get_pitch() const { return pitch; }
163
164     /**
165      * Set the pitch of this sample.
166      */
167     void set_pitch( double p );
168
169     /**
170      * Get the current volume setting of this sample.
171      */
172     inline double get_volume() const { return volume; }
173
174     /**
175      * Set the volume of this sample.
176      */
177     void set_volume( double v );
178
179     /**
180      * Returns the size of the sounds sample
181      */
182     inline int get_size() {
183         return size;
184     }
185
186     /**
187      * Set position of sound source (uses same coordinate system as opengl)
188      */
189     void set_source_pos( ALfloat *pos );
190
191     /**
192      * Set "constant" offset position of sound source (uses same
193      * coordinate system as opengl)
194      */
195     void set_offset_pos( ALfloat *pos );
196
197     /**
198      * Set the orientation of the sound source, both for direction
199      * and audio cut-off angles.
200      */
201     void set_orientation( ALfloat *dir, ALfloat inner_angle=360.0,
202                                                ALfloat outer_angle=360.0,
203                                                ALfloat outer_gain=0.0);
204
205     /**
206      * Set velocity of sound source (uses same coordinate system as opengl)
207      */
208     void set_source_vel( ALfloat *vel );
209
210
211     /**
212      * Set reference distance of sound (the distance where the gain
213      * will be half.)
214      */
215     void set_reference_dist( ALfloat dist );
216
217
218     /**
219      * Set maximume distance of sound (the distance where the sound is
220      * no longer audible.
221      */
222     void set_max_dist( ALfloat dist );
223
224     /**
225      * Load a sound file into a memory buffer only.
226      */
227     ALvoid* load_file(const char *path, const char *file);
228 };
229
230
231 #endif // _SG_SAMPLE_HXX
232
233