-// sample.hxx -- Sound sample encapsulation class
+// sample_openal.hxx -- Audio sample encapsulation class
//
// Written by Curtis Olson, started April 2004.
+// Modified to match the new SoundSystem by Erik Hofman, October 2009
//
-// Copyright (C) 2004 Curtis L. Olson - curt@flightgear.org
+// Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
+// Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// along with this program; if not, write to the Free Software Foundation,
+// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// $Id$
/**
- * \file sample.hxx
- * Provides a sound sample encapsulation
+ * \file audio sample.hxx
+ * Provides a audio sample encapsulation
*/
#ifndef _SG_SAMPLE_HXX
# error This library requires C++
#endif
-#include <simgear/compiler.h>
-
-#include STL_STRING
+#include <string>
+#include <memory>
+#include <simgear/compiler.h>
#include <simgear/debug/logstream.hxx>
+#include <simgear/structure/SGReferenced.hxx>
+#include <simgear/structure/SGSharedPtr.hxx>
+#include <simgear/math/SGMath.hxx>
-#include <plib/sg.h>
-
-#if defined(__APPLE__)
-# define AL_ILLEGAL_ENUM AL_INVALID_ENUM
-# define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
-# include <OpenAL/al.h>
-# include <OpenAL/alut.h>
-#else
-# include <AL/al.h>
-# include <AL/alut.h>
-#endif
-
-SG_USING_STD(string);
+// #include <plib/sg.h>
/**
- * manages everything we need to know for an individual sound sample
+ * manages everything we need to know for an individual audio sample
*/
-class SGSoundSample {
-
-private:
-
- string sample_name;
-
- // Buffers hold sound data.
- ALuint buffer;
-
- // Sources are points emitting sound.
- ALuint source;
-
- // Position of the source sound.
- ALfloat source_pos[3];
-
- // A constant offset to be applied to the final source_pos
- ALfloat offset_pos[3];
-
- // The orientation of the sound (direction and cut-off angles)
- ALfloat direction[3];
- ALfloat inner, outer, outergain;
-
- // Velocity of the source sound.
- ALfloat source_vel[3];
-
- // configuration values
- ALenum format;
- ALsizei size;
- ALvoid* data;
- ALsizei freq;
-
- double pitch;
- double volume;
- double reference_dist;
- double max_dist;
- ALboolean loop;
-
-
+class SGSoundSample : public SGReferenced {
public:
+ /**
+ * Empty constructor, can be used to read data to the systems
+ * memory and not to the driver.
+ */
+ SGSoundSample();
+
/**
* Constructor
* @param path Path name to sound
* @param file File name of sound
- * @param cleanup Request clean up the intermediate data (this
- should usually be true unless you want to manipulate the data
- later.)
+ Buffer data is freed by the sample group
*/
- SGSoundSample( const char *path, const char *file, bool cleanup );
+ SGSoundSample( const char *path, const char *file );
/**
* Constructor.
- * @param _data Pointer to a memory buffer containing the sample data
+ * @param data Pointer to a memory buffer containing this audio sample data
+ The application may free the data by calling free_data(), otherwise it
+ will be resident untill the class is destroyed.
* @param len Byte length of array
- * @param _freq Frequency of the provided data (bytes per second)
- * @param cleanup Request clean up the intermediate data (this
- should usually be true unless you want to manipulate the data
- later.)
+ * @param freq Frequency of the provided data (bytes per second)
+ * @param format OpenAL format id of the data
*/
- SGSoundSample( unsigned char *_data, int len, int _freq, bool cleanup );
+ SGSoundSample( std::auto_ptr<unsigned char>& data, int len, int freq,
+ int format = AL_FORMAT_MONO8 );
- ~SGSoundSample();
+ /**
+ * Destructor
+ */
+ ~SGSoundSample ();
/**
- * Start playing this sample.
- *
- * @param _loop Define wether the sound should be played in a loop.
+ * Detect wheter this audio sample holds the information of a sound file.
+ * @return Return true if this audio sample is to be constructed from a file.
*/
- void play( bool _loop );
+ inline bool is_file() const { return _is_file; }
/**
- * Stop playing this sample.
- *
- * @param sched A pointer to the appropriate scheduler.
+ * Test if this audio sample configuration has changed since the last call.
+ * Calling this function will reset the flag so calling it a second
+ * time in a row will return false.
+ * @return Return true is the configuration has changed in the mean time.
*/
- void stop();
+ bool has_changed() {
+ bool b = _changed; _changed = false; return b;
+ }
+
+ /**
+ * Test if static dataa of audio sample configuration has changed.
+ * Calling this function will reset the flag so calling it a second
+ * time in a row will return false.
+ * @return Return true is the static data has changed in the mean time.
+ */
+ bool has_static_data_changed() {
+ bool b = _static_changed; _static_changed = false; return b;
+ }
+
+ /**
+ * Schedule this audio sample for playing. Actual playing will only start
+ * at the next call op SoundGroup::update()
+ * @param _loop Define whether this sound should be played in a loop.
+ */
+ void play( bool loop ) {
+ _playing = true; _loop = loop; _changed = true;
+ }
/**
- * Play this sample once.
+ * Check if this audio sample is set to be continuous looping.
+ * @return Return true if this audio sample is set to looping.
+ */
+ inline bool get_looping() { return _loop; }
+
+ /**
+ * Schedule this audio sample to stop playing.
+ */
+ void stop() {
+ _playing = false; _changed = true;
+ }
+
+ /**
+ * Schedule this audio sample to play once.
* @see #play
*/
inline void play_once() { play(false); }
/**
- * Play this sample looped.
+ * Schedule this audio sample to play looped.
* @see #play
*/
inline void play_looped() { play(true); }
/**
- * Test if a sample is curretnly playing.
- * @return true if is is playing, false otherwise.
+ * Test if a audio sample is scheduled for playing.
+ * @return true if this audio sample is playing, false otherwise.
*/
- inline bool is_playing( ) {
- ALint result;
- alGetSourcei( source, AL_SOURCE_STATE, &result );
- if ( alGetError() != AL_NO_ERROR) {
- SG_LOG( SG_GENERAL, SG_ALERT,
- "Oops AL error in sample is_playing(): " << sample_name );
- }
- return (result == AL_PLAYING) ;
+ inline bool is_playing() { return _playing; }
+
+ /**
+ * sSt the data associated with this audio sample
+ * @param data Pointer to a memory block containg this audio sample data.
+ */
+ inline void set_data( std::auto_ptr<unsigned char>& data ) {
+ _data = data;
}
/**
- * Get the current pitch setting of this sample.
+ * Return the data associated with this audio sample.
+ * @return A pointer to this sound data of this audio sample.
*/
- inline double get_pitch() const { return pitch; }
+ inline void* get_data() const { return _data.get(); }
/**
- * Set the pitch of this sample.
+ * Free the data associated with this audio sample
*/
- inline void set_pitch( double p ) {
- // clamp in the range of 0.01 to 2.0
- if ( p < 0.01 ) { p = 0.01; }
- if ( p > 2.0 ) { p = 2.0; }
- pitch = p;
- alSourcef( source, AL_PITCH, pitch );
- if ( alGetError() != AL_NO_ERROR) {
- SG_LOG( SG_GENERAL, SG_ALERT,
- "Oops AL error in sample set_pitch()! " << p
- << " for " << sample_name );
- }
+ void free_data() {
+ free( _data.release() );
}
/**
- * Get the current volume setting of this sample.
+ * Set the source id of this source
+ * @param sid OpenAL source-id
*/
- inline double get_volume() const { return volume; }
+ void set_source(unsigned int sid) {
+ _source = sid; _valid_source = true; _changed = true;
+ }
/**
- * Set the volume of this sample.
+ * Get the OpenAL source id of this source
+ * @return OpenAL source-id
*/
- inline void set_volume( double v ) {
- volume = v;
- alSourcef( source, AL_GAIN, volume );
- if ( alGetError() != AL_NO_ERROR) {
- SG_LOG( SG_GENERAL, SG_ALERT,
- "Oops AL error in sample set_volume()! " << v
- << " for " << sample_name );
- }
- }
+ inline unsigned int get_source() { return _source; }
/**
- * Returns the size of the sounds sample
+ * Test if the source-id of this audio sample may be passed to OpenAL.
+ * @return true if the source-id is valid
*/
- inline int get_size() {
- return size;
- }
+ inline bool is_valid_source() const { return _valid_source; }
/**
- * Return a pointer to the raw data
+ * Set the source-id of this audio sample to invalid.
*/
- inline char *get_data() {
- return (char *)data;
- }
+ inline void no_valid_source() { _valid_source = false; }
/**
- * Set position of sound source (uses same coordinate system as opengl)
+ * Set the OpenAL buffer-id of this source
+ * @param bid OpenAL buffer-id
*/
- inline void set_source_pos( ALfloat *pos ) {
- source_pos[0] = pos[0];
- source_pos[1] = pos[1];
- source_pos[2] = pos[2];
+ void set_buffer(unsigned int bid) {
+ _buffer = bid; _valid_buffer = true; _changed = true;
+ }
- sgVec3 final_pos;
- sgAddVec3( final_pos, source_pos, offset_pos );
+ /**
+ * Get the OpenAL buffer-id of this source
+ * @return OpenAL buffer-id
+ */
+ inline unsigned int get_buffer() { return _buffer; }
- alSourcefv( source, AL_POSITION, final_pos );
- }
+ /**
+ * Test if the buffer-id of this audio sample may be passed to OpenAL.
+ * @return true if the buffer-id is valid
+ */
+ inline bool is_valid_buffer() const { return _valid_buffer; }
/**
- * Set "constant" offset position of sound source (uses same
- * coordinate system as opengl)
+ * Set the buffer-id of this audio sample to invalid.
*/
- inline void set_offset_pos( ALfloat *pos ) {
- offset_pos[0] = pos[0];
- offset_pos[1] = pos[1];
- offset_pos[2] = pos[2];
+ inline void no_valid_buffer() { _valid_buffer = false; }
- sgVec3 final_pos;
- sgAddVec3( final_pos, source_pos, offset_pos );
+ /**
+ * Set the playback pitch of this audio sample.
+ * Should be between 0.0 and 2.0 for maximum compatibility.
+ * @param p Pitch
+ */
+ inline void set_pitch( float p ) { _pitch = p; _changed = true; }
- alSourcefv( source, AL_POSITION, final_pos );
+ /**
+ * Get the current pitch value of this audio sample.
+ * @return Pitch
+ */
+ inline float get_pitch() { return _pitch; }
+
+ /**
+ * Set the master volume of this sample. Should be between 0.0 and 1.0.
+ * The final volume is calculated by multiplying the master and audio sample
+ * volume.
+ * @param v Volume
+ */
+ inline void set_master_volume( float v ) {
+ _master_volume = v; _changed = true;
}
/**
- * Set the orientation of the sound source, both for direction
- * and audio cut-off angles.
- */
- inline void set_orientation( ALfloat *dir, ALfloat inner_angle=360.0,
- ALfloat outer_angle=360.0,
- ALfloat outer_gain=0.0)
- {
- inner = inner_angle;
- outer = outer_angle;
- outergain = outer_gain;
- alSourcefv( source, AL_DIRECTION, dir);
- alSourcef( source, AL_CONE_INNER_ANGLE, inner );
- alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
- alSourcef( source, AL_CONE_OUTER_GAIN, outergain );
+ * Set the volume of this audio sample. Should be between 0.0 and 1.0.
+ * The final volume is calculated by multiplying the master and audio sample
+ * volume.
+ * @param v Volume
+ */
+ inline void set_volume( float v ) { _volume = v; _changed = true; }
+
+ /**
+ * Get the final volume value of this audio sample.
+ * @return Volume
+ */
+ inline float get_volume() { return _volume * _master_volume; }
+
+ /**
+ * Set the OpenAL format of this audio sample.
+ * @param format OpenAL format-id
+ */
+ inline void set_format( int format ) { _format = format; }
+
+ /**
+ * Returns the format of this audio sample.
+ * @return OpenAL format-id
+ */
+ inline int get_format() { return _format; }
+
+ /**
+ * Set the frequency (in Herz) of this audio sample.
+ * @param freq Frequency
+ */
+ inline void set_frequency( int freq ) { _freq = freq; _changed = true; }
+
+ /**
+ * Returns the frequency (in Herz) of this audio sample.
+ * @return Frequency
+ */
+ inline int get_frequency() { return _freq; }
+
+ /**
+ * Sets the size (in bytes) of this audio sample.
+ * @param size Data size
+ */
+ inline void set_size( size_t size ) { _size = size; }
+
+ /**
+ * Returns the size (in bytes) of this audio sample.
+ * @return Data size
+ */
+ inline size_t get_size() const { return _size; }
+
+ /**
+ * Set the position of this sound relative to the base position.
+ * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
+ * @param pos Relative position of this sound
+ */
+ void set_relative_position( const SGVec3f& pos );
+
+ /**
+ * Set the base position of this sound in Geodetic coordinates.
+ * @param pos Geodetic position
+ */
+ void set_position( const SGGeod& pos );
+
+ /**
+ * Get the absolute position of this sound.
+ * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
+ * @return Absolute position
+ */
+ float *get_position() const { return toVec3f(_absolute_pos).data(); }
+
+ /**
+ * Set the orientation of this sound.
+ * @param ori Quaternation containing the orientation information
+ */
+ void set_orientation( const SGQuatd& ori );
+
+ /**
+ * Set direction of this sound relative to the orientation.
+ * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
+ * @param dir Sound emission direction
+ */
+ void set_direction( const SGVec3d& dir );
+
+ /**
+ * Define the audio cone parameters for directional audio.
+ * Note: setting it to 1 degree will result in 0.5 degrees to both sides.
+ * @param inner Inner cone angle (0 - 360 degrees)
+ * @param outer Outer cone angle (0 - 360 degrees)
+ * @param gain Remaining gain at the edge of the outer cone (0.0 - 1.0)
+ */
+ void set_audio_cone( float inner, float outer, float gain ) {
+ _inner_angle = inner;
+ _outer_angle = outer;
+ _outer_gain = gain;
+ _static_changed = true;
}
/**
- * Set velocity of sound source (uses same coordinate system as opengl)
+ * Get the orientation vector of this sound.
+ * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
+ * @return Orientaton vector
+ */
+ float *get_orientation() { return _orivec.data(); }
+
+ /**
+ * Get the inner angle of the audio cone.
+ * @return Inner angle in degrees
*/
- inline void set_source_vel( ALfloat *vel ) {
- source_vel[0] = vel[0];
- source_vel[1] = vel[1];
- source_vel[2] = vel[2];
- alSourcefv( source, AL_VELOCITY, source_vel );
+ float get_innerangle() { return _inner_angle; }
+
+ /**
+ * Get the outer angle of the audio cone.
+ * @return Outer angle in degrees
+ */
+ float get_outerangle() { return _outer_angle; }
+
+ /**
+ * Get the remaining gain at the edge of the outer cone.
+ * @return Gain
+ */
+ float get_outergain() { return _outer_gain; }
+
+ /**
+ * Set the velocity vector (in meters per second) of this sound.
+ * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
+ * @param Velocity vector
+ */
+ inline void set_velocity( const SGVec3d& vel ) {
+ _velocity = vel; _changed = true;
}
+ /**
+ * Get velocity vector (in meters per second) of this sound.
+ * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
+ * @return Velocity vector
+ */
+ float *get_velocity() { return toVec3f(_velocity).data(); }
+
/**
- * Set reference distance of sound (the distance where the gain
- * will be half.)
+ * Set reference distance (in meters) of this sound.
+ * This is the distance where the gain will be half.
+ * @param dist Reference distance
*/
- inline void set_reference_dist( ALfloat dist ) {
- reference_dist = dist;
- alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
+ inline void set_reference_dist( float dist ) {
+ _reference_dist = dist; _static_changed = true;
}
+ /**
+ * Get reference distance ((in meters) of this sound.
+ * This is the distance where the gain will be half.
+ * @return Reference distance
+ */
+ inline float get_reference_dist() { return _reference_dist; }
+
/**
- * Set maximume distance of sound (the distance where the sound is
- * no longer audible.
+ * Set maximum distance (in meters) of this sound.
+ * This is the distance where this sound is no longer audible.
+ * @param dist Maximum distance
*/
- inline void set_max_dist( ALfloat dist ) {
- max_dist = dist;
- alSourcef( source, AL_MAX_DISTANCE, max_dist );
+ inline void set_max_dist( float dist ) {
+ _max_dist = dist; _static_changed = true;
}
+
+ /**
+ * Get maximum distance (in meters) of this sound.
+ * This is the distance where this sound is no longer audible.
+ * @return dist Maximum distance
+ */
+ inline float get_max_dist() { return _max_dist; }
+
+ /**
+ * Get the reference name of this audio sample.
+ * @return Sample name
+ */
+ inline std::string get_sample_name() const { return _refname; }
+
+private:
+
+ // Position of the source sound.
+ SGVec3d _absolute_pos; // absolute position
+ SGVec3d _relative_pos; // position relative to the base position
+ SGVec3d _direction; // orientation offset
+ SGVec3d _velocity; // Velocity of the source sound.
+
+ // The position and orientation of this sound
+ SGQuatd _orientation; // base orientation
+ SGVec3f _orivec; // orientation vector for OpenAL
+ SGGeod _base_pos; // base position
+
+ std::string _refname; // name or file path
+ std::auto_ptr<unsigned char> _data;
+
+ // configuration values
+ int _format;
+ size_t _size;
+ int _freq;
+
+ // Buffers hold sound data.
+ bool _valid_buffer;
+ unsigned int _buffer;
+
+ // Sources are points emitting sound.
+ bool _valid_source;
+ unsigned int _source;
+
+ // The orientation of this sound (direction and cut-off angles)
+ float _inner_angle;
+ float _outer_angle;
+ float _outer_gain;
+
+ float _pitch;
+ float _volume;
+ float _master_volume;
+ float _reference_dist;
+ float _max_dist;
+ bool _loop;
+
+ bool _playing;
+ bool _changed;
+ bool _static_changed;
+ bool _is_file;
+
+ void update_absolute_position();
+ string random_string();
};