]> git.mxchange.org Git - simgear.git/blobdiff - simgear/sound/sample_openal.hxx
Use quadtree to improve culling of STG objects
[simgear.git] / simgear / sound / sample_openal.hxx
index b5267bb10ca1963d8ddcf2e4bf8e759168f79326..fd9216d803ad1b9ea2da6abed0b89c608f3b3845 100644 (file)
@@ -1,4 +1,5 @@
-// sample_openal.hxx -- Audio sample encapsulation class
+///@file
+/// Provides an audio sample encapsulation class.
 // 
 // Written by Curtis Olson, started April 2004.
 // Modified to match the new SoundSystem by Erik Hofman, October 2009
@@ -6,47 +7,41 @@
 // 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
-// published by the Free Software Foundation; either version 2 of the
-// License, or (at your option) any later version.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
 //
-// This program is distributed in the hope that it will be useful, but
-// WITHOUT ANY WARRANTY; without even the implied warranty of
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-// General Public License for more details.
+// Library 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-//
-// $Id$
-
-/**
- * \file audio sample.hxx
- * Provides a audio sample encapsulation
- */
+// You should have received a copy of the GNU Library General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
 
 #ifndef _SG_SAMPLE_HXX
 #define _SG_SAMPLE_HXX 1
 
-#ifndef __cplusplus
-# error This library requires C++
-#endif
-
 #include <string>
-#include <cstdlib>
 
 #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;
 
+#ifndef AL_FORMAT_MONO8
+     #define AL_FORMAT_MONO8    0x1100
+#endif
+
 /**
- * manages everything we need to know for an individual audio sample
+ * Encapsulate and audio sample.
+ *
+ * Manages everything we need to know for an individual audio sample.
  */
 
 class SGSoundSample : public SGReferenced {
@@ -69,7 +64,7 @@ public:
      * 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)
@@ -82,10 +77,10 @@ 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; }
@@ -96,6 +91,7 @@ public:
      * 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.
      */
     bool has_changed() {
@@ -103,9 +99,10 @@ 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.
      */
     bool has_static_data_changed() {
@@ -115,7 +112,8 @@ public:
     /**
      * 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.
+     *
+     * @param loop Whether this sound should be played in a loop.
      */
     void play( bool loop = false ) {
         _playing = true; _loop = loop; _changed = true; _static_changed = true;
@@ -123,6 +121,7 @@ public:
 
     /**
      * Check if this audio sample is set to be continuous looping.
+     *
      * @return Return true if this audio sample is set to looping.
      */
     inline bool is_looping() { return _loop; }
@@ -152,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.
@@ -173,15 +188,13 @@ public:
     /**
      * Free the data associated with this audio sample
      */
-    void free_data() {
-        if ( _data != NULL ) free( _data ); _data = NULL;
-    }
+    void free_data();
 
     /**
      * Set the source id of this source
      * @param sid OpenAL source-id
      */
-    virtual inline void set_source(unsigned int sid) {
+    virtual void set_source(unsigned int sid) {
         _source = sid; _valid_source = true; _changed = true;
     }
 
@@ -189,18 +202,18 @@ public:
      * Get the OpenAL source id of this source
      * @return OpenAL source-id
      */
-    virtual 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.
      * @return true if the source-id is valid
      */
-    virtual 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.
      */
-    virtual inline void no_valid_source() { _valid_source = false; }
+    virtual void no_valid_source() { _valid_source = false; }
 
     /**
      * Set the OpenAL buffer-id of this source
@@ -392,7 +405,8 @@ public:
     /**
      * 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
-     * @param Velocity vector
+     *
+     * @param vel Velocity vector
      */
     inline void set_velocity( const SGVec3f& vel ) {
         _velocity = vel; _changed = true;
@@ -449,6 +463,16 @@ public:
 
     void update_pos_and_orientation();
 
+protected:
+    int _format;
+    size_t _size;
+    int _freq;
+    bool _changed;
+    
+    // Sources are points emitting sound.
+    bool _valid_source;
+    unsigned int _source;
+
 private:
 
     // Position of the source sound.
@@ -468,18 +492,11 @@ 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;
@@ -493,11 +510,11 @@ private:
     bool _loop;
 
     bool _playing;
-    bool _changed;
     bool _static_changed;
+    bool _out_of_range;
     bool _is_file;
 
-    string random_string();
+    std::string random_string();
 };