Panda3D

milesAudioManager.h

00001 // Filename: milesAudioManager.h
00002 // Created by:  skyler (June 6, 2001)
00003 // Prior system by: cary
00004 //
00005 ////////////////////////////////////////////////////////////////////
00006 //
00007 // PANDA 3D SOFTWARE
00008 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00009 //
00010 // All use of this software is subject to the terms of the revised BSD
00011 // license.  You should have received a copy of this license along
00012 // with this source code in a file named "LICENSE."
00013 //
00014 ////////////////////////////////////////////////////////////////////
00015 
00016 #ifndef __MILES_AUDIO_MANAGER_H__ //[
00017 #define __MILES_AUDIO_MANAGER_H__
00018 
00019 #include "pandabase.h"
00020 #ifdef HAVE_RAD_MSS //[
00021 
00022 #include "audioManager.h"
00023 #include "mss.h"
00024 #include "pset.h"
00025 #include "pmap.h"
00026 #include "pdeque.h"
00027 #include "pvector.h"
00028 #include "thread.h"
00029 #include "pmutex.h"
00030 #include "lightReMutex.h"
00031 #include "conditionVar.h"
00032 
00033 class MilesAudioSound;
00034 
00035 class EXPCL_MILES_AUDIO MilesAudioManager: public AudioManager {
00036 public:
00037   // See AudioManager.h for documentation.
00038   
00039   MilesAudioManager();
00040   virtual ~MilesAudioManager();
00041   
00042   virtual void shutdown();
00043 
00044   virtual bool is_valid();
00045   
00046   virtual PT(AudioSound) get_sound(const string &file_name, bool positional = false, int mode=SM_heuristic);
00047   virtual PT(AudioSound) get_sound(MovieAudio *sound, bool positional = false, int mode=SM_heuristic);
00048   virtual void uncache_sound(const string &file_name);
00049   virtual void clear_cache();
00050   virtual void set_cache_limit(unsigned int count);
00051   virtual unsigned int get_cache_limit() const;
00052 
00053   virtual void set_volume(PN_stdfloat volume);
00054   virtual PN_stdfloat get_volume() const;
00055 
00056   void set_play_rate(PN_stdfloat play_rate);
00057   PN_stdfloat get_play_rate() const;
00058   
00059   virtual void set_active(bool active);
00060   virtual bool get_active() const;
00061 
00062   virtual void set_concurrent_sound_limit(unsigned int limit = 0);
00063   virtual unsigned int get_concurrent_sound_limit() const;
00064 
00065   virtual void reduce_sounds_playing_to(unsigned int count);
00066   virtual void stop_all_sounds();
00067 
00068   virtual void update();
00069 
00070   // Tell the manager that the sound dtor was called.
00071   void release_sound(MilesAudioSound *audioSound);
00072   void cleanup();
00073 
00074   // 3D spatialized sound support.
00075   // Spatialized sound was originally added for FMOD, so there are parts of the
00076   // interface in the Miles implementation that are a little more awkward than
00077   // they would be otherwise.
00078   virtual void audio_3d_set_listener_attributes(PN_stdfloat px, PN_stdfloat py, PN_stdfloat pz, PN_stdfloat vx, PN_stdfloat xy, PN_stdfloat xz, PN_stdfloat fx, PN_stdfloat fy, PN_stdfloat fz, PN_stdfloat ux, PN_stdfloat uy, PN_stdfloat uz);
00079   virtual void audio_3d_get_listener_attributes(PN_stdfloat *px, PN_stdfloat *py, PN_stdfloat *pz, PN_stdfloat *vx, PN_stdfloat *vy, PN_stdfloat *vz, PN_stdfloat *fx, PN_stdfloat *fy, PN_stdfloat *fz, PN_stdfloat *ux, PN_stdfloat *uy, PN_stdfloat *uz);
00080   virtual void audio_3d_set_distance_factor(PN_stdfloat factor);
00081   virtual PN_stdfloat audio_3d_get_distance_factor() const;
00082   virtual void audio_3d_set_doppler_factor(PN_stdfloat factor);
00083   virtual PN_stdfloat audio_3d_get_doppler_factor() const;
00084   virtual void audio_3d_set_drop_off_factor(PN_stdfloat factor);
00085   virtual PN_stdfloat audio_3d_get_drop_off_factor() const;
00086   virtual void set_speaker_configuration(LVecBase3 *speaker1, LVecBase3 *speaker2=NULL, LVecBase3 *speaker3=NULL, LVecBase3 *speaker4=NULL, LVecBase3 *speaker5=NULL, LVecBase3 *speaker6=NULL, LVecBase3 *speaker7=NULL, LVecBase3 *speaker8=NULL, LVecBase3 *speaker9=NULL);
00087 
00088   virtual void output(ostream &out) const;
00089   virtual void write(ostream &out) const;
00090 
00091 private:
00092   bool do_is_valid();
00093   void do_reduce_sounds_playing_to(unsigned int count);
00094   void do_clear_cache();
00095 
00096   void start_service_stream(HSTREAM stream);
00097   void stop_service_stream(HSTREAM stream);
00098   
00099   void most_recently_used(const string &path);
00100   void uncache_a_sound();
00101 
00102   void starting_sound(MilesAudioSound *audio);
00103   void stopping_sound(MilesAudioSound *audio);
00104 
00105   class SoundData;
00106   PT(SoundData) load(const Filename &file_name);
00107 
00108   void thread_main(volatile bool &keep_running);
00109   void do_service_streams();
00110 
00111 private:
00112   class StreamThread : public Thread {
00113   public:
00114     StreamThread(MilesAudioManager *mgr);
00115     virtual void thread_main();
00116 
00117     MilesAudioManager *_mgr;
00118     volatile bool _keep_running;
00119   };
00120 
00121   // The sound cache:
00122   class SoundData : public ReferenceCount {
00123   public:
00124     SoundData();
00125     ~SoundData();
00126     PN_stdfloat get_length();
00127     void set_length(PN_stdfloat length);
00128 
00129     Filename _basename;
00130     S32 _file_type;
00131     pvector<unsigned char> _raw_data;
00132     bool _has_length;
00133     PN_stdfloat _length;  // in seconds.
00134   };
00135   typedef pmap<string, PT(SoundData) > SoundMap;
00136   SoundMap _sounds;
00137 
00138   typedef pset<MilesAudioSound *> AudioSet;
00139   // The offspring of this manager:
00140   AudioSet _sounds_on_loan;
00141 
00142   typedef pset<MilesAudioSound *> SoundsPlaying;
00143   // The sounds from this manager that are currently playing:
00144   SoundsPlaying _sounds_playing;
00145 
00146   // The Least Recently Used mechanism:
00147   typedef pdeque<const string *> LRU;
00148   LRU _lru;
00149   // State:
00150   PN_stdfloat _volume;
00151   PN_stdfloat _play_rate;
00152   bool _active;
00153   int _cache_limit;
00154   bool _cleanup_required;
00155   unsigned int _concurrent_sound_limit;
00156   
00157   bool _is_valid;
00158   bool _hasMidiSounds;
00159 
00160   // This mutex protects everything above.
00161   LightReMutex _lock;
00162   bool _sounds_finished;
00163 
00164   typedef pvector<HSTREAM> Streams;
00165   PT(StreamThread) _stream_thread;
00166   Streams _streams;
00167   Mutex _streams_lock;
00168   ConditionVar _streams_cvar;
00169 
00170 public:
00171   static TypeHandle get_class_type() {
00172     return _type_handle;
00173   }
00174   static void init_type() {
00175     AudioManager::init_type();
00176     register_type(_type_handle, "MilesAudioManager",
00177                   AudioManager::get_class_type());
00178   }
00179   virtual TypeHandle get_type() const {
00180     return get_class_type();
00181   }
00182   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00183 
00184 private:
00185   static TypeHandle _type_handle;
00186 
00187   friend class MilesAudioSound;
00188   friend class MilesAudioSample;
00189   friend class MilesAudioSequence;
00190   friend class MilesAudioStream;
00191 };
00192 
00193 EXPCL_MILES_AUDIO AudioManager *Create_MilesAudioManager();
00194 
00195 
00196 #endif //]
00197 
00198 #endif //]
00199 
00200 
 All Classes Functions Variables Enumerations