]> git.mxchange.org Git - simgear.git/blobdiff - simgear/sound/sample_openal.hxx
Initial work on making OpenAL private to the sound code.
[simgear.git] / simgear / sound / sample_openal.hxx
index 972a57c440a68af529792e5fcb4b473c3ad0d670..706fa8d07383106ccf8bfe13667ac9d03f8180a2 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>
+     
+class SGPath;
 
-// #include <plib/sg.h>
+#ifndef AL_FORMAT_MONO8
+     #define AL_FORMAT_MONO8    0x1100
+#endif
 
 /**
  * manages everything we need to know for an individual audio sample
@@ -59,11 +59,10 @@ 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.
@@ -82,14 +81,16 @@ public:
     /**
      * Destructor
      */
-    ~SGSoundSample ();
+    virtual ~SGSoundSample ();
 
     /**
-     * Detect wheter this audio sample holds the information of a sound file.
+     * Detect whether 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; }
 
+    SGPath file_path() const;
+
     /**
      * Test if this audio sample configuration has changed since the last call.
      * Calling this function will reset the flag so calling it a second
@@ -101,7 +102,7 @@ public:
     }
 
     /**
-     * Test if static dataa of audio sample configuration has changed.
+     * 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.
@@ -115,8 +116,8 @@ public:
      * 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 +129,7 @@ public:
     /**
      * Schedule this audio sample to stop playing.
      */
-    void stop() {
+    virtual void stop() {
         _playing = false; _changed = true;
     }
 
@@ -150,6 +151,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 +175,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;
     }
 
@@ -172,14 +189,14 @@ public:
      * Free the data associated with this audio sample
      */
     void free_data() {
-        if ( _data ) free( _data ); _data = NULL;
+        if ( _data != NULL ) free( _data ); _data = NULL;
     }
 
     /**
      * Set the source id of this source
      * @param sid OpenAL source-id
      */
-    void set_source(unsigned int sid) {
+    virtual inline void set_source(unsigned int sid) {
         _source = sid; _valid_source = true; _changed = true;
     }
 
@@ -187,24 +204,24 @@ public:
      * Get the OpenAL source id of this source
      * @return OpenAL source-id
      */
-    inline unsigned int get_source() { return _source; }
+    virtual inline unsigned int get_source() { return _source; }
 
     /**
      * Test if the source-id of this audio sample may be passed to OpenAL.
      * @return true if the source-id is valid
      */
-    inline bool is_valid_source() const { return _valid_source; }
+    virtual inline 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 inline void no_valid_source() { _valid_source = false; }
 
     /**
      * Set the OpenAL buffer-id of this source
      * @param bid OpenAL buffer-id
      */
-    void set_buffer(unsigned int bid) {
+    inline void set_buffer(unsigned int bid) {
         _buffer = bid; _valid_buffer = true; _changed = true;
     } 
 
@@ -230,7 +247,10 @@ public:
      * Should be between 0.0 and 2.0 for maximum compatibility.
      * @param p Pitch
      */
-    inline void set_pitch( float p ) { _pitch = p; _changed = true; }
+    inline void set_pitch( float p ) {
+        if (p > 2.0) p = 2.0; else if (p < 0.01) p = 0.01;
+        _pitch = p; _changed = true;
+    }
 
     /**
      * Get the current pitch value of this audio sample.
@@ -245,6 +265,7 @@ public:
      * @param v Volume
      */
     inline void set_master_volume( float v ) {
+        if (v > 1.0) v = 1.0; else if (v < 0.0) v = 0.0;
         _master_volume = v; _changed = true;
     }
 
@@ -254,7 +275,10 @@ public:
      * volume.
      * @param v Volume
      */
-    inline void set_volume( float v ) { _volume = v; _changed = true; }
+    inline void set_volume( float v ) {
+        if (v > 1.0) v = 1.0; else if (v < 0.0) v = 0.0;
+        _volume = v; _changed = true;
+    }
 
     /**
      * Get the final volume value of this audio sample.
@@ -278,7 +302,7 @@ public:
      * Set the frequency (in Herz) of this audio sample.
      * @param freq Frequency
      */
-    inline void set_frequency( int freq ) { _freq = freq; _changed = true; }
+    inline void set_frequency( int freq ) { _freq = freq; }
 
     /**
      * Returns the frequency (in Herz) of this audio sample.
@@ -308,11 +332,11 @@ public:
     }
 
     /**
-     * Set the base position of this sound in Geodetic coordinates.
-     * @param pos Geodetic position
+     * Set the base position in Cartesian coordinates
+     * @param pos position in Cartesian coordinates
      */
-    inline void set_position( const SGGeod& pos ) {
-        _base_pos = pos; _changed = true;
+    inline void set_position( const SGVec3d& pos ) {
+       _base_pos = pos; _changed = true;
     }
 
     /**
@@ -320,8 +344,7 @@ public:
      * 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(); }
-    SGVec3f get_position_vec() const { return toVec3f(_absolute_pos); }
+    SGVec3d& get_position() { return _absolute_pos; }
 
     /**
      * Set the orientation of this sound.
@@ -331,26 +354,28 @@ 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 SGVec3d& dir ) {
-        _direction = dir; _changed = true;
+    inline void set_direction( const SGVec3f& dir ) {
+        _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;
     }
 
@@ -359,7 +384,7 @@ public:
      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
      * @return Orientaton vector
      */
-    float *get_orientation() { return _orivec.data(); }
+    SGVec3f& get_orientation() { return _orivec; }
 
     /**
      * Get the inner angle of the audio cone.
@@ -381,10 +406,10 @@ public:
 
     /**
      * 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
+     * This is in the local frame coordinate system; x=north, y=east, z=down
      * @param Velocity vector
      */
-    inline void set_velocity( const SGVec3d& vel ) {
+    inline void set_velocity( const SGVec3f& vel ) {
         _velocity = vel; _changed = true;
     }
 
@@ -393,7 +418,7 @@ public:
      * 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(); }
+    SGVec3f& get_velocity() { return _velocity; }
 
 
     /**
@@ -435,7 +460,9 @@ public:
      */
     inline std::string get_sample_name() const { return _refname; }
 
-    void update_absolute_position();
+    inline virtual bool is_queue() const { return false; }
+
+    void update_pos_and_orientation();
 
 private:
 
@@ -443,12 +470,14 @@ private:
     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.
+    SGVec3f _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
+    SGVec3d _base_pos;         // base position
+
+    SGQuatd _rotation;
 
     std::string _refname;      // name or file path
     unsigned char* _data;
@@ -481,9 +510,10 @@ private:
     bool _playing;
     bool _changed;
     bool _static_changed;
+    bool _out_of_range;
     bool _is_file;
 
-    string random_string();
+    std::string random_string();
 };