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