]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.hxx
_data is not an array of pointer
[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     SGVec3d _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     SGVec3f _orivec;
97     void update_absolute_position();
98
99 public:
100
101      /**
102       * Empty constructor, can be used to read data to the systems
103       * memory and not to the driver.
104       */
105     SGSoundSample();
106
107     /**
108      * Constructor
109      * @param path Path name to sound
110      * @param file File name of sound
111        should usually be true unless you want to manipulate the data
112        later.)
113      */
114     SGSoundSample( const char *path, const char *file );
115
116     /**
117      * Constructor.
118      * @param _data Pointer to a memory buffer containing the sample data
119        the application is responsible for freeing the buffer data.
120      * @param len Byte length of array
121      * @param _freq Frequency of the provided data (bytes per second)
122        should usually be true unless you want to manipulate the data
123        later.)
124      */
125     SGSoundSample( unsigned char *data, int len, int freq, int format = AL_FORMAT_MONO8 );
126
127     ~SGSoundSample ();
128
129     /**
130      * detect wheter the sample holds the information of a sound file
131      */
132     inline bool is_file() const { return _is_file; }
133
134     /**
135      * Test whether this sample has a changed configuration since the last
136      * call. (Calling this function resets the value).
137      */
138     inline bool has_changed() {
139         bool b = _changed; _changed = false; return b;
140     }
141
142     inline bool has_static_data_changed() {
143         bool b = _static_changed; _static_changed = false; return b;
144     }
145
146
147     /**
148      * Start playing this sample.
149      *
150      * @param _loop Define whether the sound should be played in a loop.
151      */
152     inline void play( bool loop ) {
153         _playing = true; _loop = loop; _changed = true;
154     }
155
156     /**
157      * Return if the sample is looping or not.
158      */
159     inline bool get_looping() { return _loop; }
160
161     /**
162      * Stop playing this sample.
163      *
164      * @param sched A pointer to the appropriate scheduler.
165      */
166     inline void stop() {
167         _playing = false; _changed = true;
168     }
169
170     /**
171      * Play this sample once.
172      * @see #play
173      */
174     inline void play_once() { play(false); }
175
176     /** 
177      * Play this sample looped.
178      * @see #play
179      */
180     inline void play_looped() { play(true); }
181
182     /**
183      * Test if a sample is currently playing.
184      * @return true if is is playing, false otherwise.
185      */
186     inline bool is_playing() { return _playing; }
187
188     /**
189      * set the data associated with this sample
190      */
191     inline void set_data( unsigned char* data ) {
192         _data = data;
193     }
194
195     /**
196      * @return the data associated with this sample
197      */
198     inline void* get_data() const { return _data; }
199
200     /**
201      * free the data associated with this sample
202      */
203     inline void free_data() {
204         if (_data != NULL) { delete _data; _data = NULL; }
205     }
206
207     /**
208      * set the source id of this source
209      */
210     inline void set_source(unsigned int s) {
211         _source = s; _valid_source = true; _changed = true;
212     }
213
214     /**
215      * get the source id of this source
216      */
217     inline unsigned int get_source() { return _source; }
218
219     /**
220      * detect wheter the source id of the sample is valid
221      */
222     inline bool is_valid_source() const { return _valid_source; }
223
224     /**
225      * set the source id of the sample to invalid.
226      */
227     inline void no_valid_source() {
228         _valid_source = false;
229     }
230
231     /**
232      * set the buffer id of this source
233      */
234     inline void set_buffer(unsigned int b) {
235         _buffer = b; _valid_buffer = true; _changed = true;
236     } 
237
238     /**
239      * get the buffer id of this source
240      */
241     inline unsigned int get_buffer() { return _buffer; }
242
243     /**
244      * detect wheter the source id of the sample is valid
245      */
246     inline bool is_valid_buffer() const { return _valid_buffer; }
247
248     /**
249      * set the source id of the sample to invalid.
250      */
251     inline void no_valid_buffer() {
252         _valid_buffer = false;
253     }
254
255     /**
256      * Get the current pitch setting of this sample.
257      */
258     inline float get_pitch() { return _pitch; }
259
260     /**
261      * Set the pitch of this sample.
262      */
263     inline void set_pitch( float p ) {
264         _pitch = p; _changed = true;
265     }
266
267     /**
268      * Get the current volume setting of this sample.
269      */
270     inline float get_volume() { return _volume * _master_volume; }
271
272     /**
273      * Set the master (sampel group) volume of this sample.
274      */
275     inline void set_master_volume( float v ) {
276         _master_volume = v; _changed = true;
277     }
278
279     /**
280      * Set the volume of this sample.
281      */
282     inline void set_volume( float v ) {
283         _volume = v; _changed = true;
284     }
285
286     /**
287      * Set the format of the sounds sample
288      */
289     inline void set_format( int format ) {
290         _format = format;
291     }
292
293     /**
294      * Returns the format of the sounds sample
295      */
296     inline int get_format() { return _format; }
297
298
299     /**
300      * Set the frequency of the sounds sample
301      */
302     inline void set_frequency( int freq ) {
303         _freq = freq; _changed = true;
304     }
305
306     /**
307      * Returns the frequency of the sounds sample
308      */
309     inline int get_frequency() { return _freq; }
310
311     /**
312      * Returns the size of the sounds sample
313      */
314     inline void set_size( int size ) {
315         _size = size;
316     }
317
318     /**
319      * Returns the size of the sounds sample
320      */
321     inline int get_size() const { return _size; }
322
323     /**
324      * Set position of the sound source (uses same coordinate system as opengl)
325      */
326     void set_relative_position( SGVec3f pos );
327
328     /**
329      * Get position of the sound source (uses same coordinate system as opengl)
330      */
331     inline float *get_position() const { return toVec3f(_absolute_pos).data(); }
332
333     /**
334      * Set the position of the sound source
335      */
336     void set_position( SGGeod pos );
337
338     /**
339      * Set the orientation of the sound source
340      */
341     void set_orientation( SGQuatd 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( SGVec3d 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() { return _orivec.data(); }
364     float get_innerangle() { return _inner_angle; }
365     float get_outerangle() { return _outer_angle; }
366     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