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