]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.hxx
Correct (and verrified) position, orientation and velocity vector. Todo: proper sound...
[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 <string>
36
37 #include <simgear/compiler.h>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/structure/SGReferenced.hxx>
40 #include <simgear/structure/SGSharedPtr.hxx>
41 #include <simgear/math/SGMath.hxx>
42
43 #include <plib/sg.h>
44
45 using std::string;
46
47 /**
48  * manages everything we need to know for an individual sound sample
49  */
50
51 class SGSoundSample : public SGReferenced {
52
53 private:
54
55     // Position of the source sound.
56     SGVec3d _absolute_pos;      // absolute position
57     SGVec3f _relative_pos;      // position relative to the base position
58     SGVec3d _base_pos;          // base position
59
60     // The orientation of the sound (direction and cut-off angles)
61     SGVec3f _orientation;       // base orientation
62     SGVec3f _direction;         // orientation offset
63
64     // Velocity of the source sound.
65     SGVec3f _velocity;
66
67     string _sample_name;
68     unsigned char *_data;
69
70     // configuration values
71     int _format;
72     int _size;
73     int _freq;
74
75     // Buffers hold sound data.
76     bool _valid_buffer;
77     unsigned int _buffer;
78
79     // Sources are points emitting sound.
80     bool _valid_source;
81     unsigned int _source;
82
83     // The orientation of the sound (direction and cut-off angles)
84     float _inner_angle;
85     float _outer_angle;
86     float _outer_gain;
87
88     float _pitch;
89     float _volume;
90     float _master_volume;
91     float _reference_dist;
92     float _max_dist;
93     bool _loop;
94
95     bool _playing;
96     bool _changed;
97     bool _static_changed;
98     bool _is_file;
99
100     void update_absolute_position();
101
102 public:
103
104      /**
105       * Empty constructor, can be used to read data to the systems
106       * memory and not to the driver.
107       */
108     SGSoundSample();
109
110     /**
111      * Constructor
112      * @param path Path name to sound
113      * @param file File name of sound
114        should usually be true unless you want to manipulate the data
115        later.)
116      */
117     SGSoundSample( const char *path, const char *file );
118
119     /**
120      * Constructor.
121      * @param _data Pointer to a memory buffer containing the sample data
122        the application is responsible for freeing the buffer data.
123      * @param len Byte length of array
124      * @param _freq Frequency of the provided data (bytes per second)
125        should usually be true unless you want to manipulate the data
126        later.)
127      */
128     SGSoundSample( unsigned char *data, int len, int freq, int format = AL_FORMAT_MONO8 );
129
130     ~SGSoundSample ();
131
132     /**
133      * detect wheter the sample holds the information of a sound file
134      */
135     inline bool is_file() const { return _is_file; }
136
137     /**
138      * Test whether this sample has a changed configuration since the last
139      * call. (Calling this function resets the value).
140      */
141     inline bool has_changed() {
142         bool b = _changed; _changed = false; return b;
143     }
144
145     inline bool has_static_data_changed() {
146         bool b = _static_changed; _static_changed = false; return b;
147     }
148
149
150     /**
151      * Start playing this sample.
152      *
153      * @param _loop Define whether the sound should be played in a loop.
154      */
155     inline void play( bool loop ) {
156         _playing = true; _loop = loop; _changed = true;
157     }
158
159     /**
160      * Return if the sample is looping or not.
161      */
162     inline bool get_looping() { return _loop; }
163
164     /**
165      * Stop playing this sample.
166      *
167      * @param sched A pointer to the appropriate scheduler.
168      */
169     inline void stop() {
170         _playing = false; _changed = true;
171     }
172
173     /**
174      * Play this sample once.
175      * @see #play
176      */
177     inline void play_once() { play(false); }
178
179     /** 
180      * Play this sample looped.
181      * @see #play
182      */
183     inline void play_looped() { play(true); }
184
185     /**
186      * Test if a sample is currently playing.
187      * @return true if is is playing, false otherwise.
188      */
189     inline bool is_playing() { return _playing; }
190
191     /**
192      * set the data associated with this sample
193      */
194     inline void set_data( unsigned char* data ) {
195         _data = data;
196     }
197
198     /**
199      * @return the data associated with this sample
200      */
201     inline void* get_data() const { return _data; }
202
203     /**
204      * free the data associated with this sample
205      */
206     inline void free_data() {
207         if (_data != NULL) { delete[] _data; _data = NULL; }
208     }
209
210     /**
211      * set the source id of this source
212      */
213     inline void set_source(unsigned int s) {
214         _source = s; _valid_source = true; _changed = true;
215     }
216
217     /**
218      * get the source id of this source
219      */
220     inline unsigned int get_source() { return _source; }
221
222     /**
223      * detect wheter the source id of the sample is valid
224      */
225     inline bool is_valid_source() const { return _valid_source; }
226
227     /**
228      * set the source id of the sample to invalid.
229      */
230     inline void no_valid_source() {
231         _valid_source = false;
232     }
233
234     /**
235      * set the buffer id of this source
236      */
237     inline void set_buffer(unsigned int b) {
238         _buffer = b; _valid_buffer = true; _changed = true;
239     } 
240
241     /**
242      * get the buffer id of this source
243      */
244     inline unsigned int get_buffer() { return _buffer; }
245
246     /**
247      * detect wheter the source id of the sample is valid
248      */
249     inline bool is_valid_buffer() const { return _valid_buffer; }
250
251     /**
252      * set the source id of the sample to invalid.
253      */
254     inline void no_valid_buffer() {
255         _valid_buffer = false;
256     }
257
258     /**
259      * Get the current pitch setting of this sample.
260      */
261     inline float get_pitch() { return _pitch; }
262
263     /**
264      * Set the pitch of this sample.
265      */
266     inline void set_pitch( float p ) {
267         _pitch = p; _changed = true;
268     }
269
270     /**
271      * Get the current volume setting of this sample.
272      */
273     inline float get_volume() { return _volume * _master_volume; }
274
275     /**
276      * Set the master (sampel group) volume of this sample.
277      */
278     inline void set_master_volume( float v ) {
279         _master_volume = v; _changed = true;
280     }
281
282     /**
283      * Set the volume of this sample.
284      */
285     inline void set_volume( float v ) {
286         _volume = v; _changed = true;
287     }
288
289     /**
290      * Set the format of the sounds sample
291      */
292     inline void set_format( int format ) {
293         _format = format;
294     }
295
296     /**
297      * Returns the format of the sounds sample
298      */
299     inline int get_format() { return _format; }
300
301
302     /**
303      * Set the frequency of the sounds sample
304      */
305     inline void set_frequency( int freq ) {
306         _freq = freq; _changed = true;
307     }
308
309     /**
310      * Returns the frequency of the sounds sample
311      */
312     inline int get_frequency() { return _freq; }
313
314     /**
315      * Returns the size of the sounds sample
316      */
317     inline void set_size( int size ) {
318         _size = size;
319     }
320
321     /**
322      * Returns the size of the sounds sample
323      */
324     inline int get_size() const { return _size; }
325
326     /**
327      * Set position of the sound source (uses same coordinate system as opengl)
328      */
329     void set_base_position( SGVec3d pos );
330     void set_relative_position( SGVec3f pos );
331
332     /**
333      * Get position of the sound source (uses same coordinate system as opengl)
334      */
335     inline float *get_position() const { return toVec3f(_absolute_pos).data(); }
336
337     /**
338      * Set the orientation of the sound source, both for direction
339      * and audio cut-off angles.
340      */
341     void set_orientation( SGVec3f ori );
342
343     /**
344      * Set the relative direction of the sound source, both for direction
345      * and audio cut-off angles.
346      */
347     void set_direction( SGVec3f dir );
348
349     /**
350      * Define the audio cone parameters for directional audio
351      */
352     inline void set_audio_cone( float inner, float outer, float gain ) {
353         _inner_angle = inner;
354         _outer_angle = outer;
355         _outer_gain = gain;
356         _static_changed = true;
357     }
358
359     /**
360      * Get the orientation of the sound source, the inner or outer angle
361      * or outer gain.
362      */
363     float *get_orientation();
364
365     inline float get_innerangle() { return _inner_angle; }
366     inline float get_outerangle() { return _outer_angle; }
367     inline float get_outergain() { return _outer_gain; }
368
369     /**
370      * Set velocity of the sound source (uses same coordinate system as opengl)
371      */
372     inline void set_velocity( SGVec3f vel ) {
373         _velocity = SGVec3f(vel); _changed = true;
374     }
375
376     /**
377      * Get velocity of the sound source (uses same coordinate system as opengl)
378      */
379     inline float *get_velocity() { return _velocity.data(); }
380
381
382     /**
383      * Set reference distance of sound (the distance where the gain
384      * will be half.)
385      */
386     inline void set_reference_dist( float dist ) {
387         _reference_dist = dist; _static_changed = true;
388     }
389
390     /**
391      * Get reference distance of sound (the distance where the gain
392      * will be half.)
393      */
394     inline float get_reference_dist() { return _reference_dist; }
395
396
397     /**
398      * Set maximum distance of sound (the distance where the sound is
399      * no longer audible.
400      */
401     void set_max_dist( float dist ) {
402         _max_dist = dist; _static_changed = true;
403     }
404
405     /**
406      * Get maximum istance of sound (the distance where the sound is
407      * no longer audible.
408      */
409     inline float get_max_dist() { return _max_dist; }
410
411     /**
412      * Get the name of this sample
413      */
414     inline string get_sample_name() { return _sample_name; }
415 };
416
417
418 #endif // _SG_SAMPLE_HXX
419
420