]> git.mxchange.org Git - simgear.git/blobdiff - simgear/sound/sample_openal.hxx
Switch to out own audio format defines
[simgear.git] / simgear / sound / sample_openal.hxx
index 9a4cb06a654dfb5309717cec2e7a74d734fd6d07..91e2b2ac0c2c375ca667b609c8d86df42b43aa46 100644 (file)
 #ifndef _SG_SAMPLE_HXX
 #define _SG_SAMPLE_HXX 1
 
-#ifndef __cplusplus
-# error This library requires C++
-#endif
-
-#include <string>
-
-#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 <simgear/props/props.hxx>
+     
+enum {
+    SG_SAMPLE_MONO = 1,
+    SG_SAMPLE_STEREO = 2,
+
+    SG_SAMPLE_4BITS = 4,
+    SG_SAMPLE_8BITS = 8,
+    SG_SAMPLE_16BITS = 16,
+
+    SG_SAMPLE_UNCOMPRESSED = 0,
+    SG_SAMPLE_COMPRESSED = 256,
+
+    SG_SAMPLE_MONO8    = (SG_SAMPLE_MONO|SG_SAMPLE_8BITS),
+    SG_SAMPLE_MONO16   = (SG_SAMPLE_MONO|SG_SAMPLE_16BITS),
+    SG_SAMPLE_MULAW    = (SG_SAMPLE_MONO|SG_SAMPLE_8BITS|SG_SAMPLE_COMPRESSED),
+    SG_SAMPLE_ADPCM    = (SG_SAMPLE_MONO|SG_SAMPLE_4BITS|SG_SAMPLE_COMPRESSED)
+};
 
-// #include <plib/sg.h>
 
 /**
  * manages everything we need to know for an individual audio sample
  */
 
-class SGSoundSample : public SGReferenced {
+class SGSoundSampleInfo
+{
+public:
+    SGSoundSampleInfo();
+    ~SGSoundSampleInfo() {}
+
+    /**
+     * Returns the format of this audio sample.
+     * @return SimGear format-id
+     */
+    inline unsigned int get_format() { return (_tracks | _bits | _compressed*256); }
+
+    /**
+     * Get the reference name of this audio sample.
+     * @return Sample name
+     */
+    inline std::string get_sample_name() const { return _refname; }
+
+    /**
+     * Returns the frequency (in Herz) of this audio sample.
+     * @return Frequency
+     */
+    inline unsigned int get_frequency() { return _frequency; }
+
+    /**
+     * Get the current pitch value of this audio sample.
+     * @return Pitch
+     */
+    inline float get_pitch() { return _pitch; }
+
+    /**
+     * Get the final volume value of this audio sample.
+     * @return Volume
+     */
+    inline float get_volume() { return _volume * _master_volume; }
+
+    /**
+     * Returns the size (in bytes) of this audio sample.
+     * @return Data size
+     */
+    inline size_t get_size() const {
+        return (_samples * _tracks * _bits)/8;
+    }
+
+
+    /**
+     * 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
+     */
+    inline SGVec3d& get_position() { return _absolute_pos; }
+
+    /**
+     * 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
+     */
+    inline SGVec3f& get_orientation() { return _orivec; }
+
+    /**
+     * Get the inner angle of the audio cone.
+     * @return Inner angle in degrees
+     */
+    inline float get_innerangle() { return _inner_angle; }
+
+    /**
+     * Get the outer angle of the audio cone.
+     * @return Outer angle in degrees
+     */
+    inline float get_outerangle() { return _outer_angle; }
+
+    /**
+     * Get the remaining gain at the edge of the outer cone.
+     * @return Gain
+     */
+    inline float get_outergain() { return _outer_gain; }
+
+    /**
+     * 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
+     */
+    inline SGVec3f& get_velocity() { return _velocity; }
+
+    /**
+     * 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; }
+
+    /**
+     * 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; }
+
+    /**
+     * Test if static data 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;
+    }
+
+protected:
+    // static sound emitter info
+    std::string _refname;
+    unsigned int _bits;
+    unsigned int _tracks;
+    unsigned int _samples;
+    unsigned int _frequency;
+    bool _compressed;
+    bool _loop;
+
+    // dynamic sound emitter info (non 3d)
+    bool _static_changed;
+    bool _playing;
+
+    float _pitch;
+    float _volume;
+    float _master_volume;
+
+    // dynamic sound emitter info (3d)
+    bool _use_pos_props;
+    bool _out_of_range;
+
+    float _inner_angle;
+    float _outer_angle;
+    float _outer_gain;
+
+    float _reference_dist;
+    float _max_dist;
+
+    SGPropertyNode_ptr _pos_prop[3];
+    SGVec3d _absolute_pos;     // absolute position
+    SGVec3d _relative_pos;     // position relative to the base position
+    SGVec3d _direction;                // orientation offset
+    SGVec3f _velocity;         // Velocity of the source sound.
+
+    // The position and orientation of this sound
+    SGQuatd _orientation;      // base orientation
+    SGVec3f _orivec;           // orientation vector
+    SGVec3d _base_pos;         // base position
+
+    SGQuatd _rotation;
+
+private:
+    static std::string random_string();
+};
+
+
+class SGSoundSample : public SGSoundSampleInfo, public SGReferenced {
 public:
 
      /**
@@ -59,36 +221,29 @@ public:
 
     /**
      * Constructor
-     * @param path Path name to sound
      * @param file File name of sound
        Buffer data is freed by the sample group
      */
-    SGSoundSample( const char *path, const char *file );
+    SGSoundSample(const char *file, const SGPath& currentDir);
 
     /**
      * Constructor.
      * @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. This pointer will be
+       will be resident until the class is destroyed. This pointer will be
        set to NULL after calling this function.
      * @param len Byte length of array
      * @param freq Frequency of the provided data (bytes per second)
-     * @param format OpenAL format id of the data
+     * @param format SimGear format id of the data
      */
-    SGSoundSample( void** data, int len, int freq, int format=AL_FORMAT_MONO8 );
+    SGSoundSample( void** data, int len, int freq, int format=SG_SAMPLE_MONO8 );
     SGSoundSample( const unsigned char** data, int len, int freq,
-                   int format = AL_FORMAT_MONO8 );
+                   int format = SG_SAMPLE_MONO8 );
 
     /**
      * Destructor
      */
-    ~SGSoundSample ();
-
-    /**
-     * 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.
-     */
-    inline bool is_file() const { return _is_file; }
+    virtual ~SGSoundSample ();
 
     /**
      * Test if this audio sample configuration has changed since the last call.
@@ -101,22 +256,20 @@ public:
     }
 
     /**
-     * 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.
+     * Detect whether this audio sample holds the information of a sound file.
+     * @return Return true if this sample is to be constructed from a file.
      */
-    bool has_static_data_changed() {
-        bool b = _static_changed; _static_changed = false; return b;
-    }
+    inline bool is_file() const { return _is_file; }
+
+    SGPath file_path() const;
 
     /**
      * 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;
+    void play( bool loop = false ) {
+        _playing = true; _loop = loop; _changed = true; _static_changed = true;
     }
 
     /**
@@ -128,7 +281,7 @@ public:
     /**
      * Schedule this audio sample to stop playing.
      */
-    void stop() {
+    virtual void stop() {
         _playing = false; _changed = true;
     }
 
@@ -150,6 +303,22 @@ public:
      */
     inline bool is_playing() { return _playing; }
 
+
+    /**
+     * Set this sample to out-of-range (or not) and
+     * Schedule this audio sample to stop (or start) playing.
+     */
+    inline void set_out_of_range(bool oor = true) {
+        _out_of_range = oor; _playing = (!oor && _loop); _changed = true;
+    }
+
+    /**
+     * Test if this sample to out-of-range or not.
+     */
+    inline bool test_out_of_range() {
+        return _out_of_range;
+    }
+
     /**
      * Set the data associated with this audio sample
      * @param data Pointer to a memory block containg this audio sample data.
@@ -158,7 +327,7 @@ public:
     inline void set_data( const unsigned char **data ) {
         _data = (unsigned char*)*data; *data = NULL;
     }
-    inline void set_data( void **data ) {
+    inline void set_data( const void **data ) {
         _data = (unsigned char*)*data; *data = NULL;
     }
 
@@ -171,51 +340,49 @@ public:
     /**
      * Free the data associated with this audio sample
      */
-    void free_data() {
-        if ( _data ) free( _data ); _data = NULL;
-    }
+    void free_data();
 
     /**
      * Set the source id of this source
-     * @param sid OpenAL source-id
+     * @param sid source-id
      */
-    void set_source(unsigned int sid) {
+    virtual void set_source(unsigned int sid) {
         _source = sid; _valid_source = true; _changed = true;
     }
 
     /**
-     * Get the OpenAL source id of this source
-     * @return OpenAL source-id
+     * Get the source id of this source
+     * @return source-id
      */
-    inline unsigned int get_source() { return _source; }
+    virtual unsigned int get_source() { return _source; }
 
     /**
-     * Test if the source-id of this audio sample may be passed to OpenAL.
+     * Test if the source-id of this audio sample is usable.
      * @return true if the source-id is valid
      */
-    inline bool is_valid_source() const { return _valid_source; }
+    virtual bool is_valid_source() const { return _valid_source; }
 
     /**
      * Set the source-id of this audio sample to invalid.
      */
-    inline void no_valid_source() { _valid_source = false; }
+    virtual void no_valid_source() { _valid_source = false; }
 
     /**
-     * Set the OpenAL buffer-id of this source
-     * @param bid OpenAL buffer-id
+     * Set the buffer-id of this source
+     * @param bid buffer-id
      */
-    void set_buffer(unsigned int bid) {
+    inline void set_buffer(unsigned int bid) {
         _buffer = bid; _valid_buffer = true; _changed = true;
     } 
 
     /**
-     * Get the OpenAL buffer-id of this source
-     * @return OpenAL buffer-id
+     * Get the buffer-id of this source
+     * @return buffer-id
      */
     inline unsigned int get_buffer() { return _buffer; }
 
     /**
-     * Test if the buffer-id of this audio sample may be passed to OpenAL.
+     * Test if the buffer-id of this audio sample is usable.
      * @return true if the buffer-id is valid
      */
     inline bool is_valid_buffer() const { return _valid_buffer; }
@@ -235,12 +402,6 @@ public:
         _pitch = p; _changed = true;
     }
 
-    /**
-     * 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
@@ -264,46 +425,27 @@ public:
     }
 
     /**
-     * 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
+     * Set the SimGear format of this audio sample.
+     * @param format SimGear 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; }
+    inline void set_format( int fmt ) {
+        _tracks = fmt & 0x3; _bits = fmt & 0x1C; _compressed = fmt & 0x100;
+    }
 
     /**
      * 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; }
+    inline void set_frequency( int freq ) { _frequency = 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; }
+    inline void set_size( size_t size ) {
+        _samples = size*8/(_bits*_tracks);
+    }
+    inline void set_no_samples(size_t samples) { _samples = samples; }
 
     /**
      * Set the position of this sound relative to the base position.
@@ -322,12 +464,11 @@ public:
        _base_pos = pos; _changed = true;
     }
 
-    /**
-     * 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
-     */
-    SGVec3d& get_position() { return _absolute_pos; }
+    inline void set_position_properties(SGPropertyNode_ptr pos[3]) {
+        _pos_prop[0] = pos[0]; _pos_prop[1] = pos[1]; _pos_prop[2] = pos[2];
+        if (pos[0] || pos[1] || pos[2]) _use_pos_props = true;
+        _changed = true;
+    }
 
     /**
      * Set the orientation of this sound.
@@ -337,54 +478,31 @@ public:
         _orientation = ori; _changed = true;
     }
 
+    inline void set_rotation( const SGQuatd& ec2body ) {
+        _rotation = ec2body; _changed = true;
+    }
+
     /**
      * 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
      */
     inline void set_direction( const SGVec3f& dir ) {
-        _direction = toVec3d(dir); _changed = true;
+        _direction = toVec3d(dir); _static_changed = true;
     }
 
     /**
      * Define the audio cone parameters for directional audio.
-     * Note: setting it to 1 degree will result in 0.5 degrees to both sides.
+     * Note: setting it to 2 degree will result in 1 degree 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;
+        _inner_angle = inner; _outer_angle = outer; _outer_gain = gain;
         _static_changed = true;
     }
 
-    /**
-     * 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
-     */
-    SGVec3f& get_orientation() { return _orivec; }
-
-    /**
-     * Get the inner angle of the audio cone.
-     * @return Inner angle in degrees
-     */
-    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 local frame coordinate system; x=north, y=east, z=down
@@ -394,14 +512,6 @@ public:
         _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
-     */
-    SGVec3f& get_velocity() { return _velocity; }
-
-
     /**
      * Set reference distance (in meters) of this sound.
      * This is the distance where the gain will be half.
@@ -411,14 +521,6 @@ public:
         _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 maximum distance (in meters) of this sound.
      * This is the distance where this sound is no longer audible.
@@ -428,71 +530,26 @@ public:
         _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; }
+    inline virtual bool is_queue() const { return false; }
 
     void update_pos_and_orientation();
 
-private:
-
-    // Position of the source sound.
-    SGVec3d _absolute_pos;      // absolute position
-    SGVec3d _relative_pos;      // position relative to the base position
-    SGVec3d _direction;         // orientation offset
-    SGVec3f _velocity;          // Velocity of the source sound.
+protected:
+    bool _is_file;
+    bool _changed;
 
-    // The position and orientation of this sound
-    SGQuatd _orientation;       // base orientation
-    SGVec3f _orivec;           // orientation vector for OpenAL
-    SGVec3d _base_pos;         // base position
+    // Sources are points emitting sound.
+    bool _valid_source;
+    unsigned int _source;
 
-    std::string _refname;      // name or file path
+private:
     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;
-
-    string random_string();
 };
 
-
 #endif // _SG_SAMPLE_HXX