]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.hxx
Fix final simgear GCC warnings.
[simgear.git] / simgear / sound / sample_openal.hxx
1 // sample_openal.hxx -- Audio sample encapsulation class
2 // 
3 // Written by Curtis Olson, started April 2004.
4 // Modified to match the new SoundSystem by Erik Hofman, October 2009
5 //
6 // Copyright (C) 2004  Curtis L. Olson - http://www.flightgear.org/~curt
7 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software Foundation,
21 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23 // $Id$
24
25 /**
26  * \file audio sample.hxx
27  * Provides a audio sample encapsulation
28  */
29
30 #ifndef _SG_SAMPLE_HXX
31 #define _SG_SAMPLE_HXX 1
32
33 #ifndef __cplusplus
34 # error This library requires C++
35 #endif
36
37 #include <string>
38 #include <cstdlib>
39
40 #include <simgear/compiler.h>
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/structure/SGReferenced.hxx>
43 #include <simgear/structure/SGSharedPtr.hxx>
44 #include <simgear/math/SGMath.hxx>
45
46 class SGPath;
47
48 /**
49  * manages everything we need to know for an individual audio sample
50  */
51
52 class SGSoundSample : public SGReferenced {
53 public:
54
55      /**
56       * Empty constructor, can be used to read data to the systems
57       * memory and not to the driver.
58       */
59     SGSoundSample();
60
61     /**
62      * Constructor
63      * @param file File name of sound
64        Buffer data is freed by the sample group
65      */
66     SGSoundSample(const char *file, const SGPath& currentDir);
67
68     /**
69      * Constructor.
70      * @param data Pointer to a memory buffer containing this audio sample data
71        The application may free the data by calling free_data(), otherwise it
72        will be resident untill the class is destroyed. This pointer will be
73        set to NULL after calling this function.
74      * @param len Byte length of array
75      * @param freq Frequency of the provided data (bytes per second)
76      * @param format OpenAL format id of the data
77      */
78     SGSoundSample( void** data, int len, int freq, int format=AL_FORMAT_MONO8 );
79     SGSoundSample( const unsigned char** data, int len, int freq,
80                    int format = AL_FORMAT_MONO8 );
81
82     /**
83      * Destructor
84      */
85     virtual ~SGSoundSample ();
86
87     /**
88      * Detect whether this audio sample holds the information of a sound file.
89      * @return Return true if this audio sample is to be constructed from a file.
90      */
91     inline bool is_file() const { return _is_file; }
92
93     SGPath file_path() const;
94
95     /**
96      * Test if this audio sample configuration has changed since the last call.
97      * Calling this function will reset the flag so calling it a second
98      * time in a row will return false.
99      * @return Return true is the configuration has changed in the mean time.
100      */
101     bool has_changed() {
102         bool b = _changed; _changed = false; return b;
103     }
104
105     /**
106      * Test if static data of audio sample configuration has changed.
107      * Calling this function will reset the flag so calling it a second
108      * time in a row will return false.
109      * @return Return true is the static data has changed in the mean time.
110      */
111     bool has_static_data_changed() {
112         bool b = _static_changed; _static_changed = false; return b;
113     }
114
115     /**
116      * Schedule this audio sample for playing. Actual playing will only start
117      * at the next call op SoundGroup::update()
118      * @param _loop Define whether this sound should be played in a loop.
119      */
120     void play( bool loop = false ) {
121         _playing = true; _loop = loop; _changed = true; _static_changed = true;
122     }
123
124     /**
125      * Check if this audio sample is set to be continuous looping.
126      * @return Return true if this audio sample is set to looping.
127      */
128     inline bool is_looping() { return _loop; }
129
130     /**
131      * Schedule this audio sample to stop playing.
132      */
133     virtual void stop() {
134         _playing = false; _changed = true;
135     }
136
137     /**
138      * Schedule this audio sample to play once.
139      * @see #play
140      */
141     inline void play_once() { play(false); }
142
143     /** 
144      * Schedule this audio sample to play looped.
145      * @see #play
146      */
147     inline void play_looped() { play(true); }
148
149     /**
150      * Test if a audio sample is scheduled for playing.
151      * @return true if this audio sample is playing, false otherwise.
152      */
153     inline bool is_playing() { return _playing; }
154
155
156     /**
157      * Set this sample to out-of-range (or not) and
158      * Schedule this audio sample to stop (or start) playing.
159      */
160     inline void set_out_of_range(bool oor = true) {
161         _out_of_range = oor; _playing = (!oor && _loop); _changed = true;
162     }
163
164     /**
165      * Test if this sample to out-of-range or not.
166      */
167     inline bool test_out_of_range() {
168         return _out_of_range;
169     }
170
171     /**
172      * Set the data associated with this audio sample
173      * @param data Pointer to a memory block containg this audio sample data.
174        This pointer will be set to NULL after calling this function.
175      */
176     inline void set_data( const unsigned char **data ) {
177         _data = (unsigned char*)*data; *data = NULL;
178     }
179     inline void set_data( const void **data ) {
180         _data = (unsigned char*)*data; *data = NULL;
181     }
182
183     /**
184      * Return the data associated with this audio sample.
185      * @return A pointer to this sound data of this audio sample.
186      */
187     inline void* get_data() const { return _data; }
188
189     /**
190      * Free the data associated with this audio sample
191      */
192     void free_data() {
193         if ( _data != NULL ) free( _data ); _data = NULL;
194     }
195
196     /**
197      * Set the source id of this source
198      * @param sid OpenAL source-id
199      */
200     virtual inline void set_source(unsigned int sid) {
201         _source = sid; _valid_source = true; _changed = true;
202     }
203
204     /**
205      * Get the OpenAL source id of this source
206      * @return OpenAL source-id
207      */
208     virtual inline unsigned int get_source() { return _source; }
209
210     /**
211      * Test if the source-id of this audio sample may be passed to OpenAL.
212      * @return true if the source-id is valid
213      */
214     virtual inline bool is_valid_source() const { return _valid_source; }
215
216     /**
217      * Set the source-id of this audio sample to invalid.
218      */
219     virtual inline void no_valid_source() { _valid_source = false; }
220
221     /**
222      * Set the OpenAL buffer-id of this source
223      * @param bid OpenAL buffer-id
224      */
225     inline void set_buffer(unsigned int bid) {
226         _buffer = bid; _valid_buffer = true; _changed = true;
227     } 
228
229     /**
230      * Get the OpenAL buffer-id of this source
231      * @return OpenAL buffer-id
232      */
233     inline unsigned int get_buffer() { return _buffer; }
234
235     /**
236      * Test if the buffer-id of this audio sample may be passed to OpenAL.
237      * @return true if the buffer-id is valid
238      */
239     inline bool is_valid_buffer() const { return _valid_buffer; }
240
241     /**
242      * Set the buffer-id of this audio sample to invalid.
243      */
244     inline void no_valid_buffer() { _valid_buffer = false; }
245
246     /**
247      * Set the playback pitch of this audio sample. 
248      * Should be between 0.0 and 2.0 for maximum compatibility.
249      * @param p Pitch
250      */
251     inline void set_pitch( float p ) {
252         if (p > 2.0) p = 2.0; else if (p < 0.01) p = 0.01;
253         _pitch = p; _changed = true;
254     }
255
256     /**
257      * Get the current pitch value of this audio sample.
258      * @return Pitch
259      */
260     inline float get_pitch() { return _pitch; }
261
262     /**
263      * Set the master volume of this sample. Should be between 0.0 and 1.0.
264      * The final volume is calculated by multiplying the master and audio sample
265      * volume.
266      * @param v Volume
267      */
268     inline void set_master_volume( float v ) {
269         if (v > 1.0) v = 1.0; else if (v < 0.0) v = 0.0;
270         _master_volume = v; _changed = true;
271     }
272
273     /**
274      * Set the volume of this audio sample. Should be between 0.0 and 1.0.
275      * The final volume is calculated by multiplying the master and audio sample
276      * volume.
277      * @param v Volume
278      */
279     inline void set_volume( float v ) {
280         if (v > 1.0) v = 1.0; else if (v < 0.0) v = 0.0;
281         _volume = v; _changed = true;
282     }
283
284     /**
285      * Get the final volume value of this audio sample.
286      * @return Volume
287      */
288     inline float get_volume() { return _volume * _master_volume; }
289
290     /**
291      * Set the OpenAL format of this audio sample.
292      * @param format OpenAL format-id
293      */
294     inline void set_format( int format ) { _format = format; }
295
296     /**
297      * Returns the format of this audio sample.
298      * @return OpenAL format-id
299      */
300     inline int get_format() { return _format; }
301
302     /**
303      * Set the frequency (in Herz) of this audio sample.
304      * @param freq Frequency
305      */
306     inline void set_frequency( int freq ) { _freq = freq; }
307
308     /**
309      * Returns the frequency (in Herz) of this audio sample.
310      * @return Frequency
311      */
312     inline int get_frequency() { return _freq; }
313
314     /**
315      * Sets the size (in bytes) of this audio sample.
316      * @param size Data size
317      */
318     inline void set_size( size_t size ) { _size = size; }
319
320     /**
321      * Returns the size (in bytes) of this audio sample.
322      * @return Data size
323      */
324     inline size_t get_size() const { return _size; }
325
326     /**
327      * Set the position of this sound relative to the base position.
328      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
329      * @param pos Relative position of this sound
330      */
331     inline void set_relative_position( const SGVec3f& pos ) {
332         _relative_pos = toVec3d(pos); _changed = true;
333     }
334
335     /**
336      * Set the base position in Cartesian coordinates
337      * @param pos position in Cartesian coordinates
338      */
339     inline void set_position( const SGVec3d& pos ) {
340        _base_pos = pos; _changed = true;
341     }
342
343     /**
344      * Get the absolute position of this sound.
345      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
346      * @return Absolute position
347      */
348     SGVec3d& get_position() { return _absolute_pos; }
349
350     /**
351      * Set the orientation of this sound.
352      * @param ori Quaternation containing the orientation information
353      */
354     inline void set_orientation( const SGQuatd& ori ) {
355         _orientation = ori; _changed = true;
356     }
357
358     inline void set_rotation( const SGQuatd& ec2body ) {
359         _rotation = ec2body; _changed = true;
360     }
361
362     /**
363      * Set direction of this sound relative to the orientation.
364      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
365      * @param dir Sound emission direction
366      */
367     inline void set_direction( const SGVec3f& dir ) {
368         _direction = toVec3d(dir); _static_changed = true;
369     }
370
371     /**
372      * Define the audio cone parameters for directional audio.
373      * Note: setting it to 2 degree will result in 1 degree to both sides.
374      * @param inner Inner cone angle (0 - 360 degrees)
375      * @param outer Outer cone angle (0 - 360 degrees)
376      * @param gain Remaining gain at the edge of the outer cone (0.0 - 1.0)
377      */
378     void set_audio_cone( float inner, float outer, float gain ) {
379         _inner_angle = inner; _outer_angle = outer; _outer_gain = gain;
380         _static_changed = true;
381     }
382
383     /**
384      * Get the orientation vector of this sound.
385      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
386      * @return Orientaton vector
387      */
388     SGVec3f& get_orientation() { return _orivec; }
389
390     /**
391      * Get the inner angle of the audio cone.
392      * @return Inner angle in degrees
393      */
394     float get_innerangle() { return _inner_angle; }
395
396     /**
397      * Get the outer angle of the audio cone.
398      * @return Outer angle in degrees
399      */
400     float get_outerangle() { return _outer_angle; }
401
402     /**
403      * Get the remaining gain at the edge of the outer cone.
404      * @return Gain
405      */
406     float get_outergain() { return _outer_gain; }
407
408     /**
409      * Set the velocity vector (in meters per second) of this sound.
410      * This is in the local frame coordinate system; x=north, y=east, z=down
411      * @param Velocity vector
412      */
413     inline void set_velocity( const SGVec3f& vel ) {
414         _velocity = vel; _changed = true;
415     }
416
417     /**
418      * Get velocity vector (in meters per second) of this sound.
419      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
420      * @return Velocity vector
421      */
422     SGVec3f& get_velocity() { return _velocity; }
423
424
425     /**
426      * Set reference distance (in meters) of this sound.
427      * This is the distance where the gain will be half.
428      * @param dist Reference distance
429      */
430     inline void set_reference_dist( float dist ) {
431         _reference_dist = dist; _static_changed = true;
432     }
433
434     /**
435      * Get reference distance ((in meters) of this sound.
436      * This is the distance where the gain will be half.
437      * @return Reference distance
438      */
439     inline float get_reference_dist() { return _reference_dist; }
440
441
442     /**
443      * Set maximum distance (in meters) of this sound.
444      * This is the distance where this sound is no longer audible.
445      * @param dist Maximum distance
446      */
447     inline void set_max_dist( float dist ) {
448         _max_dist = dist; _static_changed = true;
449     }
450
451     /**
452      * Get maximum distance (in meters) of this sound.
453      * This is the distance where this sound is no longer audible.
454      * @return dist Maximum distance
455      */
456     inline float get_max_dist() { return _max_dist; }
457
458     /**
459      * Get the reference name of this audio sample.
460      * @return Sample name
461      */
462     inline std::string get_sample_name() const { return _refname; }
463
464     inline virtual bool is_queue() const { return false; }
465
466     void update_pos_and_orientation();
467
468 private:
469
470     // Position of the source sound.
471     SGVec3d _absolute_pos;      // absolute position
472     SGVec3d _relative_pos;      // position relative to the base position
473     SGVec3d _direction;         // orientation offset
474     SGVec3f _velocity;          // Velocity of the source sound.
475
476     // The position and orientation of this sound
477     SGQuatd _orientation;       // base orientation
478     SGVec3f _orivec;            // orientation vector for OpenAL
479     SGVec3d _base_pos;          // base position
480
481     SGQuatd _rotation;
482
483     std::string _refname;       // name or file path
484     unsigned char* _data;
485
486     // configuration values
487     int _format;
488     size_t _size;
489     int _freq;
490
491     // Buffers hold sound data.
492     bool _valid_buffer;
493     unsigned int _buffer;
494
495     // Sources are points emitting sound.
496     bool _valid_source;
497     unsigned int _source;
498
499     // The orientation of this sound (direction and cut-off angles)
500     float _inner_angle;
501     float _outer_angle;
502     float _outer_gain;
503
504     float _pitch;
505     float _volume;
506     float _master_volume;
507     float _reference_dist;
508     float _max_dist;
509     bool _loop;
510
511     bool _playing;
512     bool _changed;
513     bool _static_changed;
514     bool _out_of_range;
515     bool _is_file;
516
517     string random_string();
518 };
519
520
521 #endif // _SG_SAMPLE_HXX
522
523