Jelajahi Sumber

changed audio caching

Dave Schuyler 24 tahun lalu
induk
melakukan
b8a7fd60fd

+ 5 - 1
panda/src/audio/audioManager.h

@@ -42,12 +42,16 @@ PUBLISHED:
   
   
   // Get a sound:
   // Get a sound:
   virtual PT(AudioSound) get_sound(const string& file_name) = 0;
   virtual PT(AudioSound) get_sound(const string& file_name) = 0;
+
   // Tell the AudioManager there is no need to keep this one cached.
   // Tell the AudioManager there is no need to keep this one cached.
   // This doesn't break any connection between AudioSounds that have
   // This doesn't break any connection between AudioSounds that have
   // already given by get_sound() from this manager.  It's
   // already given by get_sound() from this manager.  It's
   // only affecting whether the AudioManager keeps a copy of the sound
   // only affecting whether the AudioManager keeps a copy of the sound
   // in its pool/cache.
   // in its pool/cache.
-  virtual void drop_sound(const string& file_name) = 0;
+  virtual void uncache_sound(const string& file_name) = 0;
+  virtual void clear_cache() = 0;
+  virtual void set_cache_limit(int count) = 0;
+  virtual int get_cache_limit() = 0;
 
 
   // Control volume:
   // Control volume:
   // FYI:
   // FYI:

+ 3 - 0
panda/src/audio/config_audio.cxx

@@ -26,6 +26,9 @@ NotifyCategoryDef(audio, "");
 bool audio_active 
 bool audio_active 
     =config_audio.GetBool("audio-active", true);
     =config_audio.GetBool("audio-active", true);
 
 
+int audio_cache_limit 
+    =config_audio.GetInt("audio-cache-limit", 15);
+
 float audio_volume 
 float audio_volume 
     =config_audio.GetFloat("audio-volume", 1.0f);
     =config_audio.GetFloat("audio-volume", 1.0f);
 
 

+ 1 - 0
panda/src/audio/config_audio.h

@@ -29,6 +29,7 @@
 NotifyCategoryDecl(audio, EXPCL_PANDA, EXPTP_PANDA);
 NotifyCategoryDecl(audio, EXPCL_PANDA, EXPTP_PANDA);
 
 
 extern EXPCL_PANDA bool audio_active;
 extern EXPCL_PANDA bool audio_active;
+extern EXPCL_PANDA int audio_cache_limit;
 extern EXPCL_PANDA float audio_volume;
 extern EXPCL_PANDA float audio_volume;
 
 
 extern EXPCL_PANDA bool audio_software_midi;
 extern EXPCL_PANDA bool audio_software_midi;

+ 33 - 2
panda/src/audio/nullAudioManager.cxx

@@ -61,15 +61,46 @@ get_sound(const string&) {
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: NullAudioManager::drop_sound
+//     Function: NullAudioManager::uncache_sound
 //       Access: Public
 //       Access: Public
 //  Description: 
 //  Description: 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void NullAudioManager::
 void NullAudioManager::
-drop_sound(const string&) {
+uncache_sound(const string&) {
   // intentionally blank.
   // intentionally blank.
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: NullAudioManager::uncache_all_sounds
+//       Access: Public
+//  Description: 
+////////////////////////////////////////////////////////////////////
+void NullAudioManager::
+clear_cache() {
+  // intentionally blank.
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: NullAudioManager::set_cache_limit
+//       Access: Public
+//  Description: 
+////////////////////////////////////////////////////////////////////
+void NullAudioManager::
+set_cache_limit(int) {
+  // intentionally blank.
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: NullAudioManager::get_cache_limit
+//       Access: Public
+//  Description: 
+////////////////////////////////////////////////////////////////////
+int NullAudioManager::
+get_cache_limit() {
+  // intentionally blank.
+  return 0;
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: NullAudioManager::set_volume
 //     Function: NullAudioManager::set_volume
 //       Access: Public
 //       Access: Public

+ 4 - 1
panda/src/audio/nullAudioManager.h

@@ -35,7 +35,10 @@ public:
   virtual bool is_valid();
   virtual bool is_valid();
   
   
   virtual PT(AudioSound) get_sound(const string&);
   virtual PT(AudioSound) get_sound(const string&);
-  virtual void drop_sound(const string&);
+  virtual void uncache_sound(const string&);
+  virtual void clear_cache();
+  virtual void set_cache_limit(int);
+  virtual int get_cache_limit();
 
 
   virtual void set_volume(float);
   virtual void set_volume(float);
   virtual float get_volume();
   virtual float get_volume();

+ 68 - 10
panda/src/audiotraits/milesAudioManager.cxx

@@ -46,6 +46,7 @@ MilesAudioManager() {
   audio_debug("  audio_volume="<<audio_volume);
   audio_debug("  audio_volume="<<audio_volume);
   _active = audio_active;
   _active = audio_active;
   _volume = audio_volume;
   _volume = audio_volume;
+  _cache_limit = audio_cache_limit;
   _is_valid = true;
   _is_valid = true;
   if (!_active_managers) {
   if (!_active_managers) {
     S32 use_digital=(audio_play_wave || audio_play_mp3)?1:0;
     S32 use_digital=(audio_play_wave || audio_play_mp3)?1:0;
@@ -114,11 +115,7 @@ MilesAudioManager::
   audio_debug("MilesAudioManager::~MilesAudioManager()");
   audio_debug("MilesAudioManager::~MilesAudioManager()");
   // Be sure to delete associated sounds before deleting the manager:
   // Be sure to delete associated sounds before deleting the manager:
   nassertv(_soundsOnLoan.empty());
   nassertv(_soundsOnLoan.empty());
-  SoundMap::iterator i=_sounds.begin();
-  for (; i!=_sounds.end(); ++i) {
-    AIL_quick_unload(i->second);
-  }
-  _sounds.clear();
+  clear_cache();
   --_active_managers;
   --_active_managers;
   audio_debug("  _active_managers="<<_active_managers);
   audio_debug("  _active_managers="<<_active_managers);
   if (!_active_managers) {
   if (!_active_managers) {
@@ -185,6 +182,9 @@ get_sound(const string& file_name) {
     // ...the sound was not found in the cache/pool.
     // ...the sound was not found in the cache/pool.
     audio=load(path);
     audio=load(path);
     if (audio) {
     if (audio) {
+      while (_sounds.size() >= _cache_limit) {
+        uncache_a_sound();
+      }
       // Put it in the pool:
       // Put it in the pool:
       // The following is roughly like: _sounds[path] = audio;
       // The following is roughly like: _sounds[path] = audio;
       // But, it gives us an iterator into the map.
       // But, it gives us an iterator into the map.
@@ -215,13 +215,13 @@ get_sound(const string& file_name) {
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: MilesAudioManager::drop_sound
+//     Function: MilesAudioManager::uncache_sound
 //       Access: Public
 //       Access: Public
 //  Description: 
 //  Description: 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void MilesAudioManager::
 void MilesAudioManager::
-drop_sound(const string& file_name) {
-  audio_debug("MilesAudioManager::drop_sound(file_name=\""
+uncache_sound(const string& file_name) {
+  audio_debug("MilesAudioManager::uncache_sound(file_name=\""
       <<file_name<<"\")");
       <<file_name<<"\")");
   Filename path = file_name;
   Filename path = file_name;
   path.resolve_filename(get_sound_path());
   path.resolve_filename(get_sound_path());
@@ -233,6 +233,64 @@ drop_sound(const string& file_name) {
   }
   }
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: MilesAudioManager::uncache_a_sound
+//       Access: Public
+//  Description: 
+////////////////////////////////////////////////////////////////////
+void MilesAudioManager::
+uncache_a_sound() {
+  audio_debug("MilesAudioManager::uncache_a_sound()");
+  SoundMap::iterator i = _sounds.begin();
+  if (i != _sounds.end()) {
+    audio_debug("  uncaching \""<<i->first<<"\"");
+    _sounds.erase(i);
+  }
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: MilesAudioManager::clear_cache
+//       Access: Public
+//  Description: 
+////////////////////////////////////////////////////////////////////
+void MilesAudioManager::
+clear_cache() {
+  audio_debug("MilesAudioManager::clear_cache()");
+  SoundMap::iterator i=_sounds.begin();
+  for (; i!=_sounds.end(); ++i) {
+    AIL_quick_unload(i->second);
+  }
+  _sounds.clear();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: MilesAudioManager::set_cache_limit
+//       Access: Public
+//  Description: 
+////////////////////////////////////////////////////////////////////
+void MilesAudioManager::
+set_cache_limit(int count) {
+  audio_debug("MilesAudioManager::set_cache_limit(count="
+      <<count<<")");
+  while (count < _cache_limit) {
+    uncache_a_sound();
+    --_cache_limit;
+  }
+  _cache_limit=count;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: MilesAudioManager::get_cache_limit
+//       Access: Public
+//  Description: 
+////////////////////////////////////////////////////////////////////
+int MilesAudioManager::
+get_cache_limit() {
+  audio_debug("MilesAudioManager::get_cache_limit() returning "
+      <<_cache_limit);
+  return _cache_limit;
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: MilesAudioManager::release_sound
 //     Function: MilesAudioManager::release_sound
 //       Access: Public
 //       Access: Public
@@ -310,8 +368,8 @@ get_active() {
 //               'result' from the Windows registry.
 //               'result' from the Windows registry.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void MilesAudioManager::
 void MilesAudioManager::
-get_registry_entry(HKEY base, const char* subKeyName, const char* keyName, 
-  string& result) {
+get_registry_entry(HKEY base, const char* subKeyName, 
+    const char* keyName, string& result) {
   // Create a key to access the registry:
   // Create a key to access the registry:
   HKEY key;
   HKEY key;
   long r=RegCreateKeyEx(base, subKeyName, 0, "", 
   long r=RegCreateKeyEx(base, subKeyName, 0, "", 

+ 7 - 1
panda/src/audiotraits/milesAudioManager.h

@@ -40,7 +40,10 @@ public:
   bool is_valid();
   bool is_valid();
   
   
   PT(AudioSound) get_sound(const string& file_name);
   PT(AudioSound) get_sound(const string& file_name);
-  void drop_sound(const string& file_name);
+  void uncache_sound(const string& file_name);
+  void clear_cache();
+  void set_cache_limit(int count);
+  int get_cache_limit();
 
 
   void set_volume(float volume);
   void set_volume(float volume);
   float get_volume();
   float get_volume();
@@ -58,6 +61,7 @@ private:
   // State:
   // State:
   float _volume;
   float _volume;
   bool _active;
   bool _active;
+  int _cache_limit;
   // keep a count for startup and shutdown:
   // keep a count for startup and shutdown:
   static int _active_managers;
   static int _active_managers;
   // Optional Downloadable Sound field for software midi:
   // Optional Downloadable Sound field for software midi:
@@ -69,6 +73,8 @@ private:
   // Tell the manager that the sound dtor was called.
   // Tell the manager that the sound dtor was called.
   void release_sound(MilesAudioSound* audioSound);
   void release_sound(MilesAudioSound* audioSound);
   
   
+  void uncache_a_sound();
+
   // utility function that should be moved to another class:
   // utility function that should be moved to another class:
   void get_registry_entry(HKEY base, 
   void get_registry_entry(HKEY base, 
                           const char* subKeyName, 
                           const char* subKeyName,