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