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