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