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