changed audio caching

This commit is contained in:
Dave Schuyler 2002-01-17 06:31:28 +00:00
parent 31529c37fa
commit b8a7fd60fd
7 changed files with 121 additions and 15 deletions

View File

@ -42,12 +42,16 @@ PUBLISHED:
// Get a sound:
virtual PT(AudioSound) get_sound(const string& file_name) = 0;
// Tell the AudioManager there is no need to keep this one cached.
// This doesn't break any connection between AudioSounds that have
// already given by get_sound() from this manager. It's
// only affecting whether the AudioManager keeps a copy of the sound
// 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:
// FYI:

View File

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

View File

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

View File

@ -61,15 +61,46 @@ get_sound(const string&) {
}
////////////////////////////////////////////////////////////////////
// Function: NullAudioManager::drop_sound
// Function: NullAudioManager::uncache_sound
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
void NullAudioManager::
drop_sound(const string&) {
uncache_sound(const string&) {
// 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
// Access: Public

View File

@ -35,7 +35,10 @@ public:
virtual bool is_valid();
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 float get_volume();

View File

@ -46,6 +46,7 @@ MilesAudioManager() {
audio_debug(" audio_volume="<<audio_volume);
_active = audio_active;
_volume = audio_volume;
_cache_limit = audio_cache_limit;
_is_valid = true;
if (!_active_managers) {
S32 use_digital=(audio_play_wave || audio_play_mp3)?1:0;
@ -114,11 +115,7 @@ MilesAudioManager::
audio_debug("MilesAudioManager::~MilesAudioManager()");
// Be sure to delete associated sounds before deleting the manager:
nassertv(_soundsOnLoan.empty());
SoundMap::iterator i=_sounds.begin();
for (; i!=_sounds.end(); ++i) {
AIL_quick_unload(i->second);
}
_sounds.clear();
clear_cache();
--_active_managers;
audio_debug(" _active_managers="<<_active_managers);
if (!_active_managers) {
@ -185,6 +182,9 @@ get_sound(const string& file_name) {
// ...the sound was not found in the cache/pool.
audio=load(path);
if (audio) {
while (_sounds.size() >= _cache_limit) {
uncache_a_sound();
}
// Put it in the pool:
// The following is roughly like: _sounds[path] = audio;
// 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
// Description:
////////////////////////////////////////////////////////////////////
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<<"\")");
Filename path = file_name;
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
// Access: Public
@ -310,8 +368,8 @@ get_active() {
// 'result' from the Windows registry.
////////////////////////////////////////////////////////////////////
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:
HKEY key;
long r=RegCreateKeyEx(base, subKeyName, 0, "",

View File

@ -40,7 +40,10 @@ public:
bool is_valid();
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);
float get_volume();
@ -58,6 +61,7 @@ private:
// State:
float _volume;
bool _active;
int _cache_limit;
// keep a count for startup and shutdown:
static int _active_managers;
// Optional Downloadable Sound field for software midi:
@ -69,6 +73,8 @@ private:
// Tell the manager that the sound dtor was called.
void release_sound(MilesAudioSound* audioSound);
void uncache_a_sound();
// utility function that should be moved to another class:
void get_registry_entry(HKEY base,
const char* subKeyName,