1 // sample_openal.hxx -- Audio sample encapsulation class
3 // Written by Curtis Olson, started April 2004.
4 // Modified to match the new SoundSystem by Erik Hofman, October 2009
6 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
7 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
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.
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.
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.
26 * \file audio sample.hxx
27 * Provides a audio sample encapsulation
30 #ifndef _SG_SAMPLE_HXX
31 #define _SG_SAMPLE_HXX 1
34 # error This library requires C++
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>
49 * manages everything we need to know for an individual audio sample
52 class SGSoundSample : public SGReferenced {
56 * Empty constructor, can be used to read data to the systems
57 * memory and not to the driver.
63 * @param file File name of sound
64 Buffer data is freed by the sample group
66 SGSoundSample(const char *file, const SGPath& currentDir);
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
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 );
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.
91 inline bool is_file() const { return _is_file; }
93 SGPath file_path() const;
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.
102 bool b = _changed; _changed = false; return b;
106 * Test if static dataa 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.
111 bool has_static_data_changed() {
112 bool b = _static_changed; _static_changed = false; return b;
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.
120 void play( bool loop = false ) {
121 _playing = true; _loop = loop; _changed = true;
125 * Check if this audio sample is set to be continuous looping.
126 * @return Return true if this audio sample is set to looping.
128 inline bool is_looping() { return _loop; }
131 * Schedule this audio sample to stop playing.
133 virtual void stop() {
134 _playing = false; _changed = true;
138 * Schedule this audio sample to play once.
141 inline void play_once() { play(false); }
144 * Schedule this audio sample to play looped.
147 inline void play_looped() { play(true); }
150 * Test if a audio sample is scheduled for playing.
151 * @return true if this audio sample is playing, false otherwise.
153 inline bool is_playing() { return _playing; }
156 * Set the data associated with this audio sample
157 * @param data Pointer to a memory block containg this audio sample data.
158 This pointer will be set to NULL after calling this function.
160 inline void set_data( const unsigned char **data ) {
161 _data = (unsigned char*)*data; *data = NULL;
163 inline void set_data( const void **data ) {
164 _data = (unsigned char*)*data; *data = NULL;
168 * Return the data associated with this audio sample.
169 * @return A pointer to this sound data of this audio sample.
171 inline void* get_data() const { return _data; }
174 * Free the data associated with this audio sample
177 if ( _data != NULL ) free( _data ); _data = NULL;
181 * Set the source id of this source
182 * @param sid OpenAL source-id
184 virtual inline void set_source(unsigned int sid) {
185 _source = sid; _valid_source = true; _changed = true;
189 * Get the OpenAL source id of this source
190 * @return OpenAL source-id
192 virtual inline unsigned int get_source() { return _source; }
195 * Test if the source-id of this audio sample may be passed to OpenAL.
196 * @return true if the source-id is valid
198 virtual inline bool is_valid_source() const { return _valid_source; }
201 * Set the source-id of this audio sample to invalid.
203 virtual inline void no_valid_source() { _valid_source = false; }
206 * Set the OpenAL buffer-id of this source
207 * @param bid OpenAL buffer-id
209 inline void set_buffer(unsigned int bid) {
210 _buffer = bid; _valid_buffer = true; _changed = true;
214 * Get the OpenAL buffer-id of this source
215 * @return OpenAL buffer-id
217 inline unsigned int get_buffer() { return _buffer; }
220 * Test if the buffer-id of this audio sample may be passed to OpenAL.
221 * @return true if the buffer-id is valid
223 inline bool is_valid_buffer() const { return _valid_buffer; }
226 * Set the buffer-id of this audio sample to invalid.
228 inline void no_valid_buffer() { _valid_buffer = false; }
231 * Set the playback pitch of this audio sample.
232 * Should be between 0.0 and 2.0 for maximum compatibility.
235 inline void set_pitch( float p ) {
236 if (p > 2.0) p = 2.0; else if (p < 0.01) p = 0.01;
237 _pitch = p; _changed = true;
241 * Get the current pitch value of this audio sample.
244 inline float get_pitch() { return _pitch; }
247 * Set the master volume of this sample. Should be between 0.0 and 1.0.
248 * The final volume is calculated by multiplying the master and audio sample
252 inline void set_master_volume( float v ) {
253 if (v > 1.0) v = 1.0; else if (v < 0.0) v = 0.0;
254 _master_volume = v; _changed = true;
258 * Set the volume of this audio sample. Should be between 0.0 and 1.0.
259 * The final volume is calculated by multiplying the master and audio sample
263 inline void set_volume( float v ) {
264 if (v > 1.0) v = 1.0; else if (v < 0.0) v = 0.0;
265 _volume = v; _changed = true;
269 * Get the final volume value of this audio sample.
272 inline float get_volume() { return _volume * _master_volume; }
275 * Set the OpenAL format of this audio sample.
276 * @param format OpenAL format-id
278 inline void set_format( int format ) { _format = format; }
281 * Returns the format of this audio sample.
282 * @return OpenAL format-id
284 inline int get_format() { return _format; }
287 * Set the frequency (in Herz) of this audio sample.
288 * @param freq Frequency
290 inline void set_frequency( int freq ) { _freq = freq; }
293 * Returns the frequency (in Herz) of this audio sample.
296 inline int get_frequency() { return _freq; }
299 * Sets the size (in bytes) of this audio sample.
300 * @param size Data size
302 inline void set_size( size_t size ) { _size = size; }
305 * Returns the size (in bytes) of this audio sample.
308 inline size_t get_size() const { return _size; }
311 * Set the position of this sound relative to the base position.
312 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
313 * @param pos Relative position of this sound
315 inline void set_relative_position( const SGVec3f& pos ) {
316 _relative_pos = toVec3d(pos); _changed = true;
320 * Set the base position in Cartesian coordinates
321 * @param pos position in Cartesian coordinates
323 inline void set_position( const SGVec3d& pos ) {
324 _base_pos = pos; _changed = true;
328 * Get the absolute position of this sound.
329 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
330 * @return Absolute position
332 SGVec3d& get_position() { return _absolute_pos; }
335 * Set the orientation of this sound.
336 * @param ori Quaternation containing the orientation information
338 inline void set_orientation( const SGQuatd& ori ) {
339 _orientation = ori; _changed = true;
342 inline void set_rotation( const SGQuatd& ec2body ) {
343 _rotation = ec2body; _changed = true;
347 * Set direction of this sound relative to the orientation.
348 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
349 * @param dir Sound emission direction
351 inline void set_direction( const SGVec3f& dir ) {
352 _direction = toVec3d(dir); _static_changed = true;
356 * Define the audio cone parameters for directional audio.
357 * Note: setting it to 2 degree will result in 1 degree to both sides.
358 * @param inner Inner cone angle (0 - 360 degrees)
359 * @param outer Outer cone angle (0 - 360 degrees)
360 * @param gain Remaining gain at the edge of the outer cone (0.0 - 1.0)
362 void set_audio_cone( float inner, float outer, float gain ) {
363 _inner_angle = inner; _outer_angle = outer; _outer_gain = gain;
364 _static_changed = true;
368 * Get the orientation vector of this sound.
369 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
370 * @return Orientaton vector
372 SGVec3f& get_orientation() { return _orivec; }
375 * Get the inner angle of the audio cone.
376 * @return Inner angle in degrees
378 float get_innerangle() { return _inner_angle; }
381 * Get the outer angle of the audio cone.
382 * @return Outer angle in degrees
384 float get_outerangle() { return _outer_angle; }
387 * Get the remaining gain at the edge of the outer cone.
390 float get_outergain() { return _outer_gain; }
393 * Set the velocity vector (in meters per second) of this sound.
394 * This is in the local frame coordinate system; x=north, y=east, z=down
395 * @param Velocity vector
397 inline void set_velocity( const SGVec3f& vel ) {
398 _velocity = vel; _changed = true;
402 * Get velocity vector (in meters per second) of this sound.
403 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
404 * @return Velocity vector
406 SGVec3f& get_velocity() { return _velocity; }
410 * Set reference distance (in meters) of this sound.
411 * This is the distance where the gain will be half.
412 * @param dist Reference distance
414 inline void set_reference_dist( float dist ) {
415 _reference_dist = dist; _static_changed = true;
419 * Get reference distance ((in meters) of this sound.
420 * This is the distance where the gain will be half.
421 * @return Reference distance
423 inline float get_reference_dist() { return _reference_dist; }
427 * Set maximum distance (in meters) of this sound.
428 * This is the distance where this sound is no longer audible.
429 * @param dist Maximum distance
431 inline void set_max_dist( float dist ) {
432 _max_dist = dist; _static_changed = true;
436 * Get maximum distance (in meters) of this sound.
437 * This is the distance where this sound is no longer audible.
438 * @return dist Maximum distance
440 inline float get_max_dist() { return _max_dist; }
443 * Get the reference name of this audio sample.
444 * @return Sample name
446 inline std::string get_sample_name() const { return _refname; }
448 inline virtual bool is_queue() const { return false; }
450 void update_pos_and_orientation();
454 // Position of the source sound.
455 SGVec3d _absolute_pos; // absolute position
456 SGVec3d _relative_pos; // position relative to the base position
457 SGVec3d _direction; // orientation offset
458 SGVec3f _velocity; // Velocity of the source sound.
460 // The position and orientation of this sound
461 SGQuatd _orientation; // base orientation
462 SGVec3f _orivec; // orientation vector for OpenAL
463 SGVec3d _base_pos; // base position
467 std::string _refname; // name or file path
468 unsigned char* _data;
470 // configuration values
475 // Buffers hold sound data.
477 unsigned int _buffer;
479 // Sources are points emitting sound.
481 unsigned int _source;
483 // The orientation of this sound (direction and cut-off angles)
490 float _master_volume;
491 float _reference_dist;
497 bool _static_changed;
500 string random_string();
504 #endif // _SG_SAMPLE_HXX