Browse Source

hack to turn off midi after crash exit

cxgeorge 23 years ago
parent
commit
af4c7fd215

+ 1 - 1
panda/src/audiotraits/Sources.pp

@@ -7,7 +7,7 @@
   #define USE_PACKAGES rad_mss
   #define USE_PACKAGES rad_mss
   #define BUILDING_DLL BUILDING_MILES_AUDIO
   #define BUILDING_DLL BUILDING_MILES_AUDIO
   #define LOCAL_LIBS audio
   #define LOCAL_LIBS audio
-  #define WIN_SYS_LIBS $[WIN_SYS_LIBS] user32.lib advapi32.lib
+  #define WIN_SYS_LIBS $[WIN_SYS_LIBS] user32.lib advapi32.lib winmm.lib
   
   
   #define COMBINED_SOURCES $[TARGET]_composite1.cxx  
   #define COMBINED_SOURCES $[TARGET]_composite1.cxx  
 
 

+ 60 - 4
panda/src/audiotraits/milesAudioManager.cxx

@@ -28,14 +28,60 @@
 #include "virtualFileSystem.h"
 #include "virtualFileSystem.h"
 #include <algorithm>
 #include <algorithm>
 
 
-int MilesAudioManager::_active_managers;
-HDLSFILEID MilesAudioManager::_dls_field;
+int MilesAudioManager::_active_managers = 0;
+HDLSFILEID MilesAudioManager::_dls_field = NULL;
+bool bMilesShutdownCalled = false;
+bool bMilesShutdownAtExitRegistered = false;
 
 
 PT(AudioManager) Create_AudioManager() {
 PT(AudioManager) Create_AudioManager() {
   audio_debug("Create_AudioManager() Miles.");
   audio_debug("Create_AudioManager() Miles.");
   return new MilesAudioManager();
   return new MilesAudioManager();
 }
 }
 
 
+void fMilesShutdown(void) {
+    if(bMilesShutdownCalled)
+      return;
+
+    bMilesShutdownCalled = true;
+
+    if (MilesAudioManager::_dls_field!=NULL) {
+      HDLSDEVICE dls= NULL;
+      audio_cat->info() << "xxxx m1\n";
+      AIL_quick_handles(0, 0, &dls);
+      audio_cat->info() << "dls == 0x" << (void*)dls << endl;
+      if(dls!=NULL) {
+          AIL_DLS_unload(dls,MilesAudioManager::_dls_field);
+      }
+
+      #ifndef NDEBUG //[
+        // Clear _dls_field in debug version (for assert in ctor):
+        MilesAudioManager::_dls_field = NULL;  
+      #endif //]
+    }
+
+
+   #define SHUTDOWN_HACK
+   
+   // if python crashes, the midi notes are left on,
+   // so we need to turn them off.  Unfortunately
+   // in Miles 6.5, AIL_quick_shutdown() crashes
+   // when it's called from atexit after a python crash.
+   // workaround: use these internal values in the miles struct
+   //             to reset the win32 midi ourselves
+   
+   #ifdef SHUTDOWN_HACK
+    HMDIDRIVER hMid=NULL;
+    AIL_quick_handles(0, &hMid, 0);
+    if ((hMid!=NULL) && (hMid->deviceid != MIDI_NULL_DRIVER) && (hMid->hMidiOut != NULL)) {
+      midiOutReset(hMid->hMidiOut);
+      midiOutClose(hMid->hMidiOut);
+    }
+   #else
+    audio_debug("  AIL_quick_shutdown()");
+    AIL_quick_shutdown();
+   #endif
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: MilesAudioManager::MilesAudioManager
 //     Function: MilesAudioManager::MilesAudioManager
 //       Access: Public
 //       Access: Public
@@ -85,6 +131,7 @@ MilesAudioManager() {
         if (!_dls_field) {
         if (!_dls_field) {
           audio_error("  AIL_DLS_load_file() failed, \""<<AIL_last_error()
           audio_error("  AIL_DLS_load_file() failed, \""<<AIL_last_error()
               <<"\" Switching to hardware midi");
               <<"\" Switching to hardware midi");
+
           AIL_quick_shutdown();
           AIL_quick_shutdown();
           if (!AIL_quick_startup(use_digital, 1, audio_output_rate,
           if (!AIL_quick_startup(use_digital, 1, audio_output_rate,
               audio_output_bits, audio_output_channels)) {
               audio_output_bits, audio_output_channels)) {
@@ -112,7 +159,15 @@ MilesAudioManager() {
   // either way.
   // either way.
   ++_active_managers;
   ++_active_managers;
   audio_debug("  _active_managers="<<_active_managers);
   audio_debug("  _active_managers="<<_active_managers);
-  if (_is_valid) { assert(is_valid()); }
+
+  if (_is_valid)  {
+    assert(is_valid());
+
+    if(!bMilesShutdownAtExitRegistered) {
+       bMilesShutdownAtExitRegistered = true;
+       atexit(fMilesShutdown);
+    }
+  }
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -138,8 +193,9 @@ MilesAudioManager::
         _dls_field=0;
         _dls_field=0;
       #endif //]
       #endif //]
     }
     }
-    AIL_quick_shutdown();
     audio_debug("  AIL_quick_shutdown()");
     audio_debug("  AIL_quick_shutdown()");
+    AIL_quick_shutdown();
+    bMilesShutdownCalled = true;
   }
   }
 }
 }
 
 

+ 4 - 2
panda/src/audiotraits/milesAudioManager.h

@@ -52,6 +52,10 @@ public:
   void set_active(bool active);
   void set_active(bool active);
   bool get_active();
   bool get_active();
 
 
+  // Optional Downloadable Sound field for software midi:
+  // made public so C atexit fn can access it
+  static HDLSFILEID _dls_field;
+
 private:
 private:
   // The sound cache:
   // The sound cache:
   typedef pmap<string, HAUDIO > SoundMap;
   typedef pmap<string, HAUDIO > SoundMap;
@@ -68,8 +72,6 @@ private:
   int _cache_limit;
   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:
-  static HDLSFILEID _dls_field;
   
   
   bool _is_valid;
   bool _is_valid;
   
   

+ 4 - 0
panda/src/audiotraits/milesAudioSound.cxx

@@ -26,6 +26,8 @@
 #define NEED_MILES_LENGTH_WORKAROUND
 #define NEED_MILES_LENGTH_WORKAROUND
 #endif
 #endif
 
 
+//#define NEED_MILES_LENGTH_WORKAROUND
+
 #ifndef NDEBUG //[
 #ifndef NDEBUG //[
   namespace {
   namespace {
     char
     char
@@ -251,6 +253,8 @@ length() const {
         }
         }
    #endif
    #endif
   }
   }
+  
+  //audio_cat->info() << "MilesAudioSound::length() returning " << _length << endl;
   audio_debug("MilesAudioSound::length() returning "<<_length);
   audio_debug("MilesAudioSound::length() returning "<<_length);
   return _length;
   return _length;
 }
 }