-// soundmgr.hxx -- Sound effect management class
+///@file
+/// Sound effect management class
+///
+/// Provides a sound manager class to keep track of multiple sounds and manage
+/// playing them with different effects and timings.
//
// Sound manager initially written by David Findlay
// <david_j_findlay@yahoo.com.au> 2001
//
// C++-ified by Curtis Olson, started March 2001.
+// Modified for the new SoundSystem by Erik Hofman, October 2009
//
// Copyright (C) 2001 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 soundmgr.hxx
- * Provides a sound manager class to keep track of
- * multiple sounds and manage playing them with different effects and
- * timings.
- */
+// 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_SOUNDMGR_OPENAL_HXX
#define _SG_SOUNDMGR_OPENAL_HXX 1
-#ifndef __cplusplus
-# error This library requires C++
-#endif
-
-#include <simgear/compiler.h>
-
-#include STL_STRING
+#include <string>
+#include <vector>
#include <map>
+#include <memory> // for std::auto_ptr
+
+#include <simgear/compiler.h>
+#include <simgear/structure/subsystem_mgr.hxx>
+#include <simgear/math/SGMath.hxx>
-#if defined( __APPLE__ )
-# include <OpenAL/al.h>
-# include <OpenAL/alc.h>
-#else
-# include <AL/al.h>
-# include <AL/alc.h>
-#endif
-
-#include "sample_openal.hxx"
-
-SG_USING_STD(map);
-SG_USING_STD(string);
-
-
-typedef map < string, SGSharedPtr<SGSoundSample> > sample_map;
-typedef sample_map::iterator sample_map_iterator;
-typedef sample_map::const_iterator const_sample_map_iterator;
-
+// forward decls
+class SGSampleGroup;
+class SGSoundSample;
/**
- * Manage a collection of SGSoundSample instances
+ * Manage a collection of SGSampleGroup instances
*/
-class SGSoundMgr
+class SGSoundMgr : public SGSubsystem
{
-
- ALCdevice *dev;
- ALCcontext *context;
-
- // Position of the listener.
- ALfloat listener_pos[3];
-
- // Velocity of the listener.
- ALfloat listener_vel[3];
-
- // Orientation of the listener. (first 3 elements are "at", second
- // 3 are "up")
- ALfloat listener_ori[6];
-
- sample_map samples;
-
- bool working;
- double safety;
-
public:
SGSoundMgr();
~SGSoundMgr();
+ void init();
+ void update(double dt);
+
+ void suspend();
+ void resume();
+ void stop();
+
+ void reinit();
/**
- * (re) initialize the sound manager.
+ * Select a specific sound device.
+ * Requires a init/reinit call before sound is actually switched.
*/
- void init();
+ inline void select_device(const char* devname) {_device_name = devname;}
+ /**
+ * Test is the sound manager is in a working condition.
+ * @return true is the sound manager is working
+ */
+ bool is_working() const;
/**
- * Bind properties for the sound manager.
+ * Set the sound manager to a working condition.
*/
- void bind();
+ void activate();
+ /**
+ * Test is the sound manager is in an active and working condition.
+ * @return true is the sound manager is active
+ */
+ inline bool is_active() const { return _active; }
/**
- * Unbind properties for the sound manager.
+ * Register a sample group to the sound manager.
+ * @param sgrp Pointer to a sample group to add
+ * @param refname Reference name of the sample group
+ * @return true if successful, false otherwise
*/
- void unbind();
+ bool add( SGSampleGroup *sgrp, const std::string& refname );
+ /**
+ * Remove a sample group from the sound manager.
+ * @param refname Reference name of the sample group to remove
+ * @return true if successful, false otherwise
+ */
+ bool remove( const std::string& refname );
/**
- * Run the audio scheduler.
+ * Test if a specified sample group is registered at the sound manager
+ * @param refname Reference name of the sample group test for
+ * @return true if the specified sample group exists
*/
- void update(double dt);
+ bool exists( const std::string& refname );
+ /**
+ * Find a specified sample group in the sound manager
+ *
+ * @param refname Reference name of the sample group to find
+ * @param create If the group should be create if it does not exist
+ * @return A pointer to the SGSampleGroup
+ */
+ SGSampleGroup *find( const std::string& refname, bool create = false );
/**
- * Pause all sounds.
+ * Set the Cartesian position of the sound manager.
+ *
+ * @param pos OpenAL listener position
*/
- void pause();
+ void set_position( const SGVec3d& pos, const SGGeod& pos_geod );
+ void set_position_offset( const SGVec3d& pos ) {
+ _offset_pos = pos; _changed = true;
+ }
/**
- * Resume all sounds.
+ * Get the position of the sound manager.
+ * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
+ *
+ * @return OpenAL listener position
*/
- void resume();
-
+ const SGVec3d& get_position() const;
/**
- * is audio working?
+ * Set the velocity vector (in meters per second) of the sound manager
+ * This is the horizontal local frame; x=north, y=east, z=down
+ *
+ * @param vel Velocity vector
*/
- inline bool is_working() const { return working; }
+ void set_velocity( const SGVec3d& vel ) {
+ _velocity = vel; _changed = true;
+ }
/**
- * reinitialize the sound manager
+ * Get the velocity vector of the sound manager
+ * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
+ *
+ * @return Velocity vector of the OpenAL listener
*/
- inline void reinit() { init(); }
+ inline SGVec3f get_velocity() { return toVec3f(_velocity); }
/**
- * add a sound effect, return true if successful
+ * Set the orientation of the sound manager
+ *
+ * @param ori Quaternation containing the orientation information
*/
- bool add( SGSoundSample *sound, const string& refname);
+ void set_orientation( const SGQuatd& ori );
- /**
- * remove a sound effect, return true if successful
+ /**
+ * Get the orientation of the sound manager
+ *
+ * @return Quaternation containing the orientation information
*/
- bool remove( const string& refname );
+ const SGQuatd& get_orientation() const;
/**
- * return true of the specified sound exists in the sound manager system
+ * Get the direction vector of the sound manager
+ * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
+ *
+ * @return Look-at direction of the OpenAL listener
*/
- bool exists( const string& refname );
+ SGVec3f get_direction() const;
+
+ enum {
+ NO_SOURCE = (unsigned int)-1,
+ NO_BUFFER = (unsigned int)-1,
+ FAILED_BUFFER = (unsigned int)-2
+ };
/**
- * return a pointer to the SGSoundSample if the specified sound
- * exists in the sound manager system, otherwise return NULL
+ * Set the master volume.
+ *
+ * @param vol Volume (must be between 0.0 and 1.0)
*/
- SGSoundSample *find( const string& refname );
+ void set_volume( float vol );
/**
- * tell the scheduler to play the indexed sample in a continuous
- * loop
+ * Get the master volume.
+ *
+ * @return Volume (must be between 0.0 and 1.0)
*/
- bool play_looped( const string& refname );
+ inline float get_volume() { return _volume; }
/**
- * tell the scheduler to play the indexed sample once
+ * Get a free OpenAL source-id
+ *
+ * @return NO_SOURCE if no source is available
*/
- bool play_once( const string& refname );
+ unsigned int request_source();
/**
- * return true of the specified sound is currently being played
+ * Free an OpenAL source-id for future use
+ *
+ * @param source OpenAL source-id to free
*/
- bool is_playing( const string& refname );
+ void release_source( unsigned int source );
/**
- * immediate stop playing the sound
+ * Get a free OpenAL buffer-id
+ * The buffer-id will be assigned to the sample by calling this function.
+ *
+ * @param sample Pointer to an audio sample to assign the buffer-id to
+ * @return NO_BUFFER if loading of the buffer failed.
*/
- bool stop( const string& refname );
+ unsigned int request_buffer(SGSoundSample *sample);
/**
- * set overall volume for the application.
- * @param vol 1.0 is default, must be greater than 0
+ * Free an OpenAL buffer-id for this sample
+ *
+ * @param sample Pointer to an audio sample for which to free the buffer
*/
- inline void set_volume( const ALfloat vol ) {
- if ( vol > 0.0 ) {
- alListenerf( AL_GAIN, vol );
- }
- }
+ void release_buffer( SGSoundSample *sample );
/**
- * set the position of the listener (in opengl coordinates)
+ * Test if the position of the sound manager has changed.
+ * The value will be set to false upon the next call to update_late()
+ *
+ * @return true if the position has changed
*/
- inline void set_listener_pos( ALfloat *pos ) {
- listener_pos[0] = pos[0];
- listener_pos[1] = pos[1];
- listener_pos[2] = pos[2];
- alListenerfv( AL_POSITION, listener_pos );
- }
+ inline bool has_changed() { return _changed; }
/**
- * set the velocity of the listener (in opengl coordinates)
+ * Some implementations seem to need the velocity multiplied by a
+ * factor of 100 to make them distinct. I've not found if this is
+ * a problem in the implementation or in out code. Until then
+ * this function is used to detect the problematic implementations.
*/
- inline void set_listener_vel( ALfloat *vel ) {
- listener_vel[0] = vel[0];
- listener_vel[1] = vel[1];
- listener_vel[2] = vel[2];
- alListenerfv( AL_VELOCITY, listener_vel );
- }
+ inline bool bad_doppler_effect() { return _bad_doppler; }
/**
- * set the orientation of the listener (in opengl coordinates)
- *
- * Description: ORIENTATION is a pair of 3-tuples representing the
- * 'at' direction vector and 'up' direction of the Object in
- * Cartesian space. AL expects two vectors that are orthogonal to
- * each other. These vectors are not expected to be normalized. If
- * one or more vectors have zero length, implementation behavior
- * is undefined. If the two vectors are linearly dependent,
- * behavior is undefined.
- */
- inline void set_listener_orientation( ALfloat *ori ) {
- listener_ori[0] = ori[0];
- listener_ori[1] = ori[1];
- listener_ori[2] = ori[2];
- listener_ori[3] = ori[3];
- listener_ori[4] = ori[4];
- listener_ori[5] = ori[5];
- alListenerfv( AL_ORIENTATION, listener_ori );
- }
+ * Load a sample file and return it's configuration and data.
+ *
+ * @param samplepath Path to the file to load
+ * @param data Pointer to a variable that points to the allocated data
+ * @param format Pointer to a vairable that gets the OpenAL format
+ * @param size Pointer to a vairable that gets the sample size in bytes
+ * @param freq Pointer to a vairable that gets the sample frequency in Herz
+ * @return true if succesful, false on error
+ */
+ bool load( const std::string &samplepath,
+ void **data,
+ int *format,
+ size_t *size,
+ int *freq );
/**
- * set the positions of all managaged sound sources
+ * Get a list of available playback devices.
*/
- void set_source_pos_all( ALfloat *pos );
+ std::vector<const char*> get_available_devices();
/**
- * set the velocities of all managaged sound sources
+ * Get the current OpenAL vendor or rendering backend.
*/
- void set_source_vel_all( ALfloat *pos );
+ const std::string& get_vendor() { return _vendor; }
+ const std::string& get_renderer() { return _renderer; }
+
+private:
+ class SoundManagerPrivate;
+ /// private implementation object
+ std::auto_ptr<SoundManagerPrivate> d;
+
+ bool _active;
+ bool _changed;
+ float _volume;
+
+ // Position of the listener.
+ SGVec3d _offset_pos;
+ SGGeod _geod_pos;
+
+ // Velocity of the listener.
+ SGVec3d _velocity;
+
+ bool _bad_doppler;
+ std::string _renderer;
+ std::string _vendor;
+ std::string _device_name;
+
+ bool testForALError(std::string s);
+ bool testForALCError(std::string s);
+ bool testForError(void *p, std::string s);
+
+ void update_sample_config( SGSampleGroup *sound );
};