From 0e87c509ceef7cda317a725aa28eb8c270fd4f7d Mon Sep 17 00:00:00 2001 From: Roger Hughston Date: Fri, 22 Sep 2006 19:32:30 +0000 Subject: [PATCH] Some work to make it work on a mac --- panda/src/audiotraits/milesAudioManager.cxx | 283 +++++++------------- panda/src/audiotraits/milesAudioManager.h | 33 +-- panda/src/audiotraits/milesAudioSound.cxx | 8 +- panda/src/audiotraits/milesAudioSound.h | 2 +- 4 files changed, 105 insertions(+), 221 deletions(-) diff --git a/panda/src/audiotraits/milesAudioManager.cxx b/panda/src/audiotraits/milesAudioManager.cxx index 894787c9e6..a1eae1a4fc 100644 --- a/panda/src/audiotraits/milesAudioManager.cxx +++ b/panda/src/audiotraits/milesAudioManager.cxx @@ -27,14 +27,12 @@ #include "config_express.h" #include "virtualFileSystem.h" #include "nullAudioSound.h" -#include "mutexHolder.h" #include TypeHandle MilesAudioManager::_type_handle; -Mutex *MilesAudioManager::_static_lock; int MilesAudioManager::_active_managers = 0; bool MilesAudioManager::_miles_active = false; HDLSFILEID MilesAudioManager::_dls_field = NULL; @@ -61,23 +59,10 @@ MilesAudioManager:: MilesAudioManager() { audio_debug("MilesAudioManager::MilesAudioManager(), this = " << (void *)this); - if (_static_lock == NULL) { - Mutex *new_lock = new Mutex; - void *result = AtomicAdjust::compare_and_exchange_ptr((void * TVOLATILE &)_static_lock, (void *)NULL, (void *)new_lock); - if (result != NULL) { - // Someone else must have assigned the mutex first. OK. - nassertv(_static_lock != new_lock); - delete new_lock; - } - nassertv((Mutex * TVOLATILE &)_static_lock != NULL); - } - - MutexHolder st_holder(*(Mutex * TVOLATILE &)_static_lock); if (_managers == (Managers *)NULL) { _managers = new Managers; } _managers->insert(this); - audio_debug(" audio_active="<resolve_filename(path, get_sound_path()); audio_debug(" resolved file_name is '"<= (unsigned int)_cache_limit) { - uncache_a_sound(); - } - - // The following is roughly like: _sounds[path] = sd; - // But, it gives us an iterator into the map. - pair ib = - _sounds.insert(SoundMap::value_type(path, sd)); - if (!ib.second) { - // The insert failed. - audio_debug(" failed map insert of "<= (unsigned int)_cache_limit) { + uncache_a_sound(); } + // Put it in the pool: + // The following is roughly like: _sounds[path] = sd; + // But, it gives us an iterator into the map. + pair ib + =_sounds.insert(SoundMap::value_type(path, sd)); + if (!ib.second) { + // The insert failed. + audio_debug(" failed map insert of "<set_active(_active); bool inserted = _sounds_on_loan.insert(milesAudioSound).second; nassertr(inserted, milesAudioSound.p()); - audioSound = milesAudioSound; + audioSound=milesAudioSound; } _hasMidiSounds |= (file_name.find(".mid")!=string::npos); audio_debug(" returning 0x" << (void*)audioSound); - assert(do_is_valid()); + assert(is_valid()); return audioSound; } @@ -465,19 +387,17 @@ get_sound(const string& file_name, bool) { // Description: //////////////////////////////////////////////////////////////////// void MilesAudioManager:: -uncache_sound(const string &file_name) { - MutexHolder holder(_lock); - +uncache_sound(const string& file_name) { audio_debug("MilesAudioManager::uncache_sound(file_name=\"" <resolve_filename(path, get_sound_path()); audio_debug(" path=\""<0); LRU::iterator lru_i=find(_lru.begin(), _lru.end(), &(i->first)); @@ -485,18 +405,18 @@ uncache_sound(const string &file_name) { _lru.erase(lru_i); _sounds.erase(i); } - assert(do_is_valid()); + assert(is_valid()); } //////////////////////////////////////////////////////////////////// // Function: MilesAudioManager::uncache_a_sound -// Access: Private -// Description: Assumes the lock is already held. +// Access: Public +// Description: //////////////////////////////////////////////////////////////////// void MilesAudioManager:: uncache_a_sound() { audio_debug("MilesAudioManager::uncache_a_sound()"); - assert(do_is_valid()); + assert(is_valid()); // uncache least recently used: assert(_lru.size()>0); LRU::reference path=_lru.front(); @@ -508,26 +428,26 @@ uncache_a_sound() { audio_debug(" uncaching \""<first<<"\""); _sounds.erase(i); } - assert(do_is_valid()); + assert(is_valid()); } //////////////////////////////////////////////////////////////////// // Function: MilesAudioManager::most_recently_used -// Access: Private -// Description: Assumes the lock is already held. +// Access: Public +// Description: //////////////////////////////////////////////////////////////////// void MilesAudioManager:: -most_recently_used(const string *path) { +most_recently_used(const string& path) { audio_debug("MilesAudioManager::most_recently_used(path=\"" < count) { uncache_a_sound(); } _cache_limit=count; - assert(do_is_valid()); + assert(is_valid()); } //////////////////////////////////////////////////////////////////// @@ -580,8 +487,6 @@ set_cache_limit(unsigned int count) { //////////////////////////////////////////////////////////////////// unsigned int MilesAudioManager:: get_cache_limit() const { - MutexHolder holder(_lock); - audio_debug("MilesAudioManager::get_cache_limit() returning " <<_cache_limit); return _cache_limit; @@ -594,8 +499,6 @@ get_cache_limit() const { //////////////////////////////////////////////////////////////////// void MilesAudioManager:: release_sound(MilesAudioSound* audioSound) { - MutexHolder holder(_lock); - audio_debug("MilesAudioManager::release_sound(audioSound=\"" <get_name()<<"\"), this = " << (void *)this); AudioSet::iterator ai = _sounds_on_loan.find(audioSound); @@ -771,6 +674,8 @@ stop_all_sounds() { reduce_sounds_playing_to(0); } + +#ifdef WIN32 //////////////////////////////////////////////////////////////////// // Function: MilesAudioManager::get_registry_entry // Access: private @@ -816,7 +721,7 @@ get_registry_entry(HKEY base, const char* subKeyName, return (r==ERROR_SUCCESS); } - +#endif //////////////////////////////////////////////////////////////////// // Function: MilesAudioManager::get_gm_file_path // Access: private @@ -824,12 +729,21 @@ get_registry_entry(HKEY base, const char* subKeyName, //////////////////////////////////////////////////////////////////// void MilesAudioManager:: get_gm_file_path(string& result) { +#ifdef WIN32 if(!get_registry_entry(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\DirectMusic", "GMFilePath", result)) { char sysdir[MAX_PATH+1]; GetSystemDirectory(sysdir,MAX_PATH+1); result = sysdir; result.append("\\drivers\\gm.dls"); } +#endif + +#ifdef IS_OSX +result = +//"gs_instruments.dls"; +"/System/Library/Components/CoreAudio.component/Contents/Resources/gs_instruments.dls"; +#endif + audio_debug("MilesAudioManager::get_gm_file_path() result out=\""<deviceid != MIDI_NULL_DRIVER) && (hMid->hMidiOut != NULL)) { audio_debug("MilesAudioManager::calling midiOutReset"); +#ifdef WIN32 midiOutReset(hMid->hMidiOut); +#endif } } @@ -868,40 +784,19 @@ force_midi_reset() { //////////////////////////////////////////////////////////////////// void MilesAudioManager:: cleanup() { - { - MutexHolder holder(_lock); - - audio_debug("MilesAudioManager::cleanup(), this = " << (void *)this - << ", _cleanup_required = " << _cleanup_required); - if (!_cleanup_required) { - return; - } - - // Be sure to cleanup associated sounds before cleaning up the manager: - AudioSet::iterator ai; - for (ai = _sounds_on_loan.begin(); ai != _sounds_on_loan.end(); ++ai) { - (*ai)->cleanup(); - } - - do_clear_cache(); - - _cleanup_required = false; + audio_debug("MilesAudioManager::cleanup(), this = " << (void *)this + << ", _cleanup_required = " << _cleanup_required); + if (!_cleanup_required) { + return; } - deactivate(); - - audio_debug("MilesAudioManager::cleanup() finished"); -} - -//////////////////////////////////////////////////////////////////// -// Function: MilesAudioManager::deactivate -// Access: Private, Static -// Description: Deactivates one manager. Grabs the static lock. -//////////////////////////////////////////////////////////////////// -void MilesAudioManager:: -deactivate() { - MutexHolder st_holder(*_static_lock); + // Be sure to cleanup associated sounds before cleaning up the manager: + AudioSet::iterator ai; + for (ai = _sounds_on_loan.begin(); ai != _sounds_on_loan.end(); ++ai) { + (*ai)->cleanup(); + } + clear_cache(); nassertv(_active_managers > 0); --_active_managers; audio_debug(" _active_managers="<<_active_managers); @@ -921,6 +816,8 @@ deactivate() { _miles_active = false; } } + _cleanup_required = false; + audio_debug("MilesAudioManager::cleanup() finished"); } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/audiotraits/milesAudioManager.h b/panda/src/audiotraits/milesAudioManager.h index 767869d82c..3f0e7208cd 100644 --- a/panda/src/audiotraits/milesAudioManager.h +++ b/panda/src/audiotraits/milesAudioManager.h @@ -28,7 +28,6 @@ #include "pset.h" #include "pmap.h" #include "pdeque.h" -#include "pmutex.h" class MilesAudioSound; @@ -66,8 +65,6 @@ public: void stop_all_sounds(); private: - Mutex _lock; // Protects all the members of this class. - // The sound cache: class SoundData : public ReferenceCount { public: @@ -85,16 +82,16 @@ private: typedef pmap SoundMap; SoundMap _sounds; - typedef pset AudioSet; + typedef pset AudioSet; // The offspring of this manager: AudioSet _sounds_on_loan; - typedef pset SoundsPlaying; + typedef pset SoundsPlaying; // The sounds from this manager that are currently playing: SoundsPlaying _sounds_playing; // The Least Recently Used mechanism: - typedef pdeque LRU; + typedef pdeque LRU; LRU _lru; // State: float _volume; @@ -102,51 +99,41 @@ private: bool _active; int _cache_limit; bool _cleanup_required; - + // keep a count for startup and shutdown: + static int _active_managers; + static bool _miles_active; unsigned int _concurrent_sound_limit; bool _is_valid; bool _hasMidiSounds; -private: - // Static members. Protected by the following lock. - static Mutex *_static_lock; - - // keep a count for startup and shutdown: - static int _active_managers; - static bool _miles_active; - // Optional Downloadable Sound field for software midi static HDLSFILEID _dls_field; typedef pset Managers; static Managers *_managers; - -private: - bool do_is_valid(); - void do_clear_cache(); - + PT(SoundData) load(Filename file_name); // Tell the manager that the sound dtor was called. void release_sound(MilesAudioSound* audioSound); - void most_recently_used(const string *path); + void most_recently_used(const string& path); void uncache_a_sound(); void starting_sound(MilesAudioSound* audio); void stopping_sound(MilesAudioSound* audio); - +#ifdef WIN32 // utility function that should be moved to another class: bool get_registry_entry(HKEY base, const char* subKeyName, const char* keyName, string& result); +#endif // get the default dls file path: void get_gm_file_path(string& result); void force_midi_reset(); void cleanup(); - static void deactivate(); friend class MilesAudioSound; diff --git a/panda/src/audiotraits/milesAudioSound.cxx b/panda/src/audiotraits/milesAudioSound.cxx index 2bba4fa71e..d421590120 100644 --- a/panda/src/audiotraits/milesAudioSound.cxx +++ b/panda/src/audiotraits/milesAudioSound.cxx @@ -66,7 +66,7 @@ namespace { namespace { AILSEQUENCECB sequence_callback = 0; AILSAMPLECB sample_callback = 0; - const user_data_index = 7; + const int user_data_index = 7; //////////////////////////////////////////////////////////////////// // Function: pandaAudioAilCallback_Sequence @@ -363,7 +363,7 @@ set_time(float time) { time = max_time; } - S32 millisecond_time=S32(1000*time); + S32 millisecond_time=(S32)(1000*time); AIL_quick_set_ms_position(_audio, millisecond_time); } @@ -462,7 +462,7 @@ set_play_rate(float play_rate) { case AIL_QUICK_XMIDI_TYPE: case AIL_QUICK_DLS_XMIDI_TYPE: // midi uses whole percentage values 100 == 100% - _audio->speed = S32(play_rate*100.0f); + _audio->speed = (S32)(play_rate*100.0f); if ((_audio->speed != -1) && (AIL_quick_status(_audio) == QSTAT_PLAYING)) { AIL_set_sequence_tempo((HSEQUENCE)_audio->handle, _audio->speed, 0); } @@ -471,7 +471,7 @@ set_play_rate(float play_rate) { case AIL_QUICK_DIGITAL_TYPE: case AIL_QUICK_MPEG_DIGITAL_TYPE: // wave and mp3 use sample rate (e.g. 44100) - _audio->speed = S32(play_rate*float(_original_playback_rate)); + _audio->speed = (S32)(play_rate*float(_original_playback_rate)); if ((_audio->speed != -1) && (AIL_quick_status(_audio) == QSTAT_PLAYING)) { AIL_set_sample_playback_rate((HSAMPLE)_audio->handle, _audio->speed); } diff --git a/panda/src/audiotraits/milesAudioSound.h b/panda/src/audiotraits/milesAudioSound.h index 5304315062..58298ee591 100644 --- a/panda/src/audiotraits/milesAudioSound.h +++ b/panda/src/audiotraits/milesAudioSound.h @@ -149,7 +149,7 @@ private: // itwas set inactive. bool _paused; - MilesAudioSound(MilesAudioManager *manager, MilesAudioManager::SoundData *sd, + MilesAudioSound(MilesAudioManager* manager, MilesAudioManager::SoundData *sd, string file_name, float length=0.0f); void cleanup();