Diff for /np2/win9x/cmmidi.cpp between versions 1.5 and 1.7

version 1.5, 2005/02/07 14:46:14 version 1.7, 2005/03/02 04:28:08
Line 2 Line 2
 #include        "np2.h"  #include        "np2.h"
 #include        "mimpidef.h"  #include        "mimpidef.h"
 #include        "commng.h"  #include        "commng.h"
 #if defined(VERMOUTH_LIB)  #if defined(VERMOUTH_LIB) || defined(MT32SOUND_DLL)
 #include        "sound.h"  #include        "sound.h"
   #endif
   #if defined(VERMOUTH_LIB)
 #include        "vermouth.h"  #include        "vermouth.h"
 #endif  #endif
   #if defined(MT32SOUND_DLL)
   #include        "mt32snd.h"
   #endif
 #include        "keydisp.h"  #include        "keydisp.h"
   
   
Line 19  extern MIDIMOD vermouth_module; Line 24  extern MIDIMOD vermouth_module;
 #define MIDIOUTS3(a)            ((*(UINT32 *)(a)) & 0xffffff)  #define MIDIOUTS3(a)            ((*(UINT32 *)(a)) & 0xffffff)
   
   
 const char cmmidi_midimapper[] = "MIDI MAPPER";  const OEMCHAR cmmidi_midimapper[] = OEMTEXT("MIDI MAPPER");
 #if defined(VERMOUTH_LIB)  #if defined(VERMOUTH_LIB)
 const char cmmidi_vermouth[] = "VERMOUTH";  const OEMCHAR cmmidi_vermouth[] = OEMTEXT("VERMOUTH");
   #endif
   #if defined(MT32SOUND_DLL)
   const OEMCHAR cmmidi_mt32sound[] = OEMTEXT("MT32Sound");
 #endif  #endif
 const char *cmmidi_mdlname[12] = {  
                         "MT-32",                "CM-32L",               "CM-64",  const OEMCHAR *cmmidi_mdlname[12] = {
                         "CM-300",               "CM-500(LA)",   "CM-500(GS)",                  OEMTEXT("MT-32"),       OEMTEXT("CM-32L"),              OEMTEXT("CM-64"),
                         "SC-55",                "SC-88",                "LA",                  OEMTEXT("CM-300"),      OEMTEXT("CM-500(LA)"),  OEMTEXT("CM-500(GS)"),
                         "GM",                   "GS",                   "XG"};                  OEMTEXT("SC-55"),       OEMTEXT("SC-88"),               OEMTEXT("LA"),
                   OEMTEXT("GM"),          OEMTEXT("GS"),                  OEMTEXT("XG")};
   
 enum {          MIDI_MT32 = 0,  MIDI_CM32L,             MIDI_CM64,  enum {          MIDI_MT32 = 0,  MIDI_CM32L,             MIDI_CM64,
                         MIDI_CM300,             MIDI_CM500LA,   MIDI_CM500GS,                          MIDI_CM300,             MIDI_CM500LA,   MIDI_CM500GS,
Line 70  enum { Line 79  enum {
 #if defined(VERMOUTH_LIB)  #if defined(VERMOUTH_LIB)
         CMMIDI_VERMOUTH         = 0x08,          CMMIDI_VERMOUTH         = 0x08,
 #endif  #endif
   #if defined(MT32SOUND_DLL)
           CMMIDI_MT32SOUND        = 0x10,
   #endif
   
         MIDICTRL_READY          = 0,          MIDICTRL_READY          = 0,
         MIDICTRL_2BYTES,          MIDICTRL_2BYTES,
Line 90  typedef struct { Line 102  typedef struct {
         UINT8   ctrl[28];          UINT8   ctrl[28];
 } _MIDICH, *MIDICH;  } _MIDICH, *MIDICH;
   
 struct _cmmidi {  typedef struct {
         UINT            opened;  
         void            (*outfn)(CMMIDI self, UINT32 msg);  
         HMIDIOUT        hmidiout;          HMIDIOUT        hmidiout;
         MIDIHDR         hmidiouthdr;          MIDIHDR         midihdr;
   } OUTFNWIN32;
   
   typedef union {
           OUTFNWIN32      win32;
 #if defined(VERMOUTH_LIB)  #if defined(VERMOUTH_LIB)
         MIDIHDL         vermouth;          MIDIHDL         vermouth;
 #endif  #endif
   } HMIDIFNOUT;
   
   struct _cmmidi {
           UINT            opened;
           void            (*shortout)(CMMIDI self, UINT32 msg);
           void            (*longout)(CMMIDI self, const UINT8 *msg, UINT leng);
           HMIDIFNOUT      out;
   
         HMIDIIN         hmidiin;          HMIDIIN         hmidiin;
         MIDIHDR         hmidiinhdr;          MIDIHDR         hmidiinhdr;
         UINT            midictrl;          UINT            midictrl;
Line 139  static UINT8 midictrlindex[128]; Line 161  static UINT8 midictrlindex[128];
   
 // ----  // ----
   
 static BOOL getmidioutid(const char *midiout, UINT *ret) {  static BRESULT getmidioutid(const OEMCHAR *midiout, UINT *ret) {
   
         UINT            num;          UINT            num;
         UINT            i;          UINT            i;
Line 160  static BOOL getmidioutid(const char *mid Line 182  static BOOL getmidioutid(const char *mid
         return(FAILURE);          return(FAILURE);
 }  }
   
 static BOOL getmidiinid(const char *midiin, UINT *ret) {  static BRESULT getmidiinid(const OEMCHAR *midiin, UINT *ret) {
   
         UINT            num;          UINT            num;
         UINT            i;          UINT            i;
Line 181  static BOOL getmidiinid(const char *midi Line 203  static BOOL getmidiinid(const char *midi
         return(FAILURE);          return(FAILURE);
 }  }
   
 static UINT module2number(const char *module) {  static UINT module2number(const OEMCHAR *module) {
   
         UINT    i;          UINT    i;
   
Line 193  static UINT module2number(const char *mo Line 215  static UINT module2number(const char *mo
         return(i);          return(i);
 }  }
   
   
   // ---- N/C
   
   static void midi_ncshort(CMMIDI midi, UINT32 msg) {
   
           (void)midi;
           (void)msg;
   }
   
   static void midi_nclong(CMMIDI midi, const UINT8 *msg, UINT leng) {
   
           (void)midi;
           (void)msg;
           (void)leng;
   }
   
   
   // ---- Win32 API
   
 static void waitlastexclusiveout(CMMIDI midi) {  static void waitlastexclusiveout(CMMIDI midi) {
   
         if (midi->midiexcvwait) {          if (midi->midiexcvwait) {
                 midi->midiexcvwait = 0;                  midi->midiexcvwait = 0;
                 while(midiOutUnprepareHeader(midi->hmidiout, &midi->hmidiouthdr,                  while(midiOutUnprepareHeader(midi->out.win32.hmidiout,
                                                                         sizeof(MIDIHDR)) == MIDIERR_STILLPLAYING);                                                                                  &midi->out.win32.midihdr,
                                                                                   sizeof(midi->out.win32.midihdr))
                                                                                                   == MIDIERR_STILLPLAYING) { }
         }          }
 }  }
   
 static void sendexclusive(CMMIDI midi, const UINT8 *buf, UINT leng) {  static void midi_win32short(CMMIDI midi, UINT32 msg) {
   
         CopyMemory(midi->excvbuf, buf, leng);          waitlastexclusiveout(midi);
         midi->hmidiouthdr.lpData = (char *)midi->excvbuf;          midiOutShortMsg(midi->out.win32.hmidiout, (DWORD)msg);
         midi->hmidiouthdr.dwFlags = 0;  
         midi->hmidiouthdr.dwBufferLength = leng;  
         midiOutPrepareHeader(midi->hmidiout, &midi->hmidiouthdr, sizeof(MIDIHDR));  
         midiOutLongMsg(midi->hmidiout, &midi->hmidiouthdr, sizeof(MIDIHDR));  
         midi->midiexcvwait = 1;  
 }  }
   
 static void midiout_none(CMMIDI midi, UINT32 msg) {  static void midi_win32long(CMMIDI midi, const UINT8 *msg, UINT leng) {
   
         (void)midi;          waitlastexclusiveout(midi);
         (void)msg;          CopyMemory(midi->excvbuf, msg, leng);
           midi->out.win32.midihdr.lpData = (char *)midi->excvbuf;
           midi->out.win32.midihdr.dwFlags = 0;
           midi->out.win32.midihdr.dwBufferLength = leng;
           midiOutPrepareHeader(midi->out.win32.hmidiout, &midi->out.win32.midihdr,
                                                                                           sizeof(midi->out.win32.midihdr));
           midiOutLongMsg(midi->out.win32.hmidiout, &midi->out.win32.midihdr,
                                                                                           sizeof(midi->out.win32.midihdr));
           midi->midiexcvwait = 1;
 }  }
   
 static void midiout_win32(CMMIDI midi, UINT32 msg) {  
   
         waitlastexclusiveout(midi);  // ---- Vermouth
         midiOutShortMsg(midi->hmidiout, (DWORD)msg);  
 }  
   
 #if defined(VERMOUTH_LIB)  #if defined(VERMOUTH_LIB)
 static void midiout_vermouth(CMMIDI midi, UINT32 msg) {  static void midi_vermouthshort(CMMIDI midi, UINT32 msg) {
   
           sound_sync();
           midiout_shortmsg(midi->out.vermouth, msg);
   }
   
   static void midi_vermouthlong(CMMIDI midi, const UINT8 *msg, UINT leng) {
   
         sound_sync();          sound_sync();
         midiout_shortmsg(midi->vermouth, msg);          midiout_longmsg(midi->out.vermouth, msg, leng);
 }  }
   
 static void SOUNDCALL vermouth_getpcm(MIDIHDL hdl, SINT32 *pcm, UINT count) {  static void SOUNDCALL vermouth_getpcm(MIDIHDL hdl, SINT32 *pcm, UINT count) {
Line 254  const SINT32 *ptr; Line 303  const SINT32 *ptr;
 }  }
 #endif  #endif
   
   
   // ---- MT-32
   
   #if defined(MT32SOUND_DLL)
   static void midi_mt32short(CMMIDI midi, UINT32 msg) {
   
           sound_sync();
           mt32sound_shortmsg(msg);
   }
   
   static void midi_mt32long(CMMIDI midi, const UINT8 *msg, UINT leng) {
   
           sound_sync();
           mt32sound_longmsg(msg, leng);
   }
   
   static void SOUNDCALL mt32_getpcm(MIDIHDL hdl, SINT32 *pcm, UINT count) {
   
           mt32sound_mix32(pcm, count);
   }
   #endif
   
   
   
   // ----
   
 static void midireset(CMMIDI midi) {  static void midireset(CMMIDI midi) {
   
 const UINT8     *excv;  const UINT8     *excv;
Line 295  const UINT8 *excv; Line 370  const UINT8 *excv;
                         break;                          break;
         }          }
         if (excv) {          if (excv) {
                 if (midi->opened & CMMIDI_MIDIOUT) {                  midi->longout(midi, excv, excvsize);
                         waitlastexclusiveout(midi);  
                         sendexclusive(midi, excv, excvsize);  
                 }  
 #if defined(VERMOUTH_LIB)  
                 else if (midi->opened & CMMIDI_VERMOUTH) {  
                         midiout_longmsg(midi->vermouth, excv, excvsize);  
                 }  
 #endif  
         }          }
   
         work[1] = 0x7b;          work[1] = 0x7b;
         work[2] = 0x00;          work[2] = 0x00;
         for (work[0]=0xb0; work[0]<0xc0; work[0]++) {          for (work[0]=0xb0; work[0]<0xc0; work[0]++) {
                 keydisp_midi(work);                  keydisp_midi(work);
                 midi->outfn(midi, MIDIOUTS3(work));                  midi->shortout(midi, MIDIOUTS3(work));
         }          }
 }  }
   
Line 323  static void midisetparam(CMMIDI midi) { Line 390  static void midisetparam(CMMIDI midi) {
         mch = midi->mch;          mch = midi->mch;
         for (i=0; i<16; i++, mch++) {          for (i=0; i<16; i++, mch++) {
                 if (mch->press != 0xff) {                  if (mch->press != 0xff) {
                         midi->outfn(midi, MIDIOUTS(0xa0+i, mch->press, 0));                          midi->shortout(midi, MIDIOUTS(0xa0+i, mch->press, 0));
                 }                  }
                 if (mch->bend != 0xffff) {                  if (mch->bend != 0xffff) {
                         midi->outfn(midi, (mch->bend << 8) + 0xe0+i);                          midi->shortout(midi, (mch->bend << 8) + 0xe0+i);
                 }                  }
                 for (j=0; j<NELEMENTS(midictrltbl); j++) {                  for (j=0; j<NELEMENTS(midictrltbl); j++) {
                         if (mch->ctrl[j+1] != 0xff) {                          if (mch->ctrl[j+1] != 0xff) {
                                 midi->outfn(midi,                                  midi->shortout(midi,
                                                         MIDIOUTS(0xb0+i, midictrltbl[j], mch->ctrl[j+1]));                                                          MIDIOUTS(0xb0+i, midictrltbl[j], mch->ctrl[j+1]));
                         }                          }
                 }                  }
                 if (mch->prog != 0xff) {                  if (mch->prog != 0xff) {
                         midi->outfn(midi, MIDIOUTS(0xc0+i, mch->prog, 0));                          midi->shortout(midi, MIDIOUTS(0xc0+i, mch->prog, 0));
                 }                  }
         }          }
 }  }
Line 455  static UINT midiwrite(COMMNG self, UINT8 Line 522  static UINT midiwrite(COMMNG self, UINT8
                                                 break;                                                  break;
                                 }                                  }
                                 keydisp_midi(midi->buffer);                                  keydisp_midi(midi->buffer);
                                 midi->outfn(midi, MIDIOUTS2(midi->buffer));                                  midi->shortout(midi, MIDIOUTS2(midi->buffer));
                                 midi->midictrl = MIDICTRL_READY;                                  midi->midictrl = MIDICTRL_READY;
                                 return(2);                                  return(2);
                         }                          }
Line 488  static UINT midiwrite(COMMNG self, UINT8 Line 555  static UINT midiwrite(COMMNG self, UINT8
                                                 break;                                                  break;
                                 }                                  }
                                 keydisp_midi(midi->buffer);                                  keydisp_midi(midi->buffer);
                                 midi->outfn(midi, MIDIOUTS3(midi->buffer));                                  midi->shortout(midi, MIDIOUTS3(midi->buffer));
                                 midi->midictrl = MIDICTRL_READY;                                  midi->midictrl = MIDICTRL_READY;
                                 return(3);                                  return(3);
                         }                          }
Line 496  static UINT midiwrite(COMMNG self, UINT8 Line 563  static UINT midiwrite(COMMNG self, UINT8
   
                 case MIDICTRL_EXCLUSIVE:                  case MIDICTRL_EXCLUSIVE:
                         if (data == MIDI_EOX) {                          if (data == MIDI_EOX) {
                                 if (midi->opened & CMMIDI_MIDIOUT) {                                  midi->longout(midi, midi->buffer, midi->mpos);
                                         waitlastexclusiveout(midi);  
                                         sendexclusive(midi, midi->buffer, midi->mpos);  
                                 }  
 #if defined(VERMOUTH_LIB)  
                                 else if (midi->opened & CMMIDI_VERMOUTH) {  
                                         midiout_longmsg(midi->vermouth, midi->buffer, midi->mpos);  
                                 }  
 #endif  
                                 midi->midictrl = MIDICTRL_READY;                                  midi->midictrl = MIDICTRL_READY;
                                 return(midi->mpos);                                  return(midi->mpos);
                         }                          }
Line 577  static long midimsg(COMMNG self, UINT ms Line 636  static long midimsg(COMMNG self, UINT ms
                         break;                          break;
   
                 case COMMSG_MIMPIDEFFILE:                  case COMMSG_MIMPIDEFFILE:
                         mimpidef_load(&midi->def, (char *)param);                          mimpidef_load(&midi->def, (OEMCHAR *)param);
                         return(1);                          return(1);
   
                 case COMMSG_MIMPIDEFEN:                  case COMMSG_MIMPIDEFEN:
Line 587  static long midimsg(COMMNG self, UINT ms Line 646  static long midimsg(COMMNG self, UINT ms
         return(0);          return(0);
 }  }
   
 static BOOL midiinhdlreg(CMMIDI midi, HMIDIIN hmidiin) {  static BRESULT midiinhdlreg(CMMIDI midi, HMIDIIN hmidiin) {
   
         if (midiinhdls < MIDIIN_MAX) {          if (midiinhdls < MIDIIN_MAX) {
                 midiinhdl[midiinhdls].hmidiin = hmidiin;                  midiinhdl[midiinhdls].hmidiin = hmidiin;
Line 637  static void midirelease(COMMNG self) { Line 696  static void midirelease(COMMNG self) {
         midi = (CMMIDI)(self + 1);          midi = (CMMIDI)(self + 1);
         if (midi->opened & CMMIDI_MIDIOUT) {          if (midi->opened & CMMIDI_MIDIOUT) {
                 waitlastexclusiveout(midi);                  waitlastexclusiveout(midi);
                 midiOutReset(midi->hmidiout);                  midiOutReset(midi->out.win32.hmidiout);
                 midiOutClose(midi->hmidiout);                  midiOutClose(midi->out.win32.hmidiout);
         }          }
         if (midi->opened & CMMIDI_MIDIIN) {          if (midi->opened & CMMIDI_MIDIIN) {
                 if (midi->opened & CMMIDI_MIDIINSTART) {                  if (midi->opened & CMMIDI_MIDIINSTART) {
Line 652  static void midirelease(COMMNG self) { Line 711  static void midirelease(COMMNG self) {
         }          }
 #if defined(VERMOUTH_LIB)  #if defined(VERMOUTH_LIB)
         if (midi->opened & CMMIDI_VERMOUTH) {          if (midi->opened & CMMIDI_VERMOUTH) {
                 midiout_destroy(midi->vermouth);                  midiout_destroy(midi->out.vermouth);
           }
   #endif
   #if defined(MT32SOUND_DLL)
           if (midi->opened & CMMIDI_MT32SOUND) {
                   mt32sound_close();
         }          }
 #endif  #endif
         _MFREE(self);          _MFREE(self);
Line 672  void cmmidi_initailize(void) { Line 736  void cmmidi_initailize(void) {
         midictrlindex[32] = 1;          midictrlindex[32] = 1;
 }  }
   
 COMMNG cmmidi_create(const char *midiout, const char *midiin,  COMMNG cmmidi_create(const OEMCHAR *midiout, const OEMCHAR *midiin,
                                                                                                                 const char *module) {                                                                                                          const OEMCHAR *module) {
   
         UINT            opened;          UINT            opened;
         UINT            id;          UINT            id;
         void            (*outfn)(CMMIDI midi, UINT32 msg);          void            (*shortout)(CMMIDI self, UINT32 msg);
         HMIDIOUT        hmidiout = NULL;          void            (*longout)(CMMIDI self, const UINT8 *msg, UINT leng);
           HMIDIFNOUT      out;
         HMIDIIN         hmidiin = NULL;          HMIDIIN         hmidiin = NULL;
 #if defined(VERMOUTH_LIB)  
         MIDIHDL         vermouth = NULL;  
 #endif  
         COMMNG          ret;          COMMNG          ret;
         CMMIDI          midi;          CMMIDI          midi;
   
         opened = 0;          opened = 0;
         outfn = midiout_none;          ZeroMemory(&out, sizeof(out));
           shortout = midi_ncshort;
           longout = midi_nclong;
         if (getmidioutid(midiout, &id) == SUCCESS) {          if (getmidioutid(midiout, &id) == SUCCESS) {
                 if (midiOutOpen(&hmidiout, id, 0, 0, CALLBACK_NULL)                  if (midiOutOpen(&out.win32.hmidiout, id, 0, 0, CALLBACK_NULL)
                                                                                                                 == MMSYSERR_NOERROR) {                                                                                                                  == MMSYSERR_NOERROR) {
                         midiOutReset(hmidiout);                          midiOutReset(out.win32.hmidiout);
                         outfn = midiout_win32;                          shortout = midi_win32short;
                           longout = midi_win32long;
                         opened |= CMMIDI_MIDIOUT;                          opened |= CMMIDI_MIDIOUT;
                 }                  }
         }          }
Line 705  COMMNG cmmidi_create(const char *midiout Line 770  COMMNG cmmidi_create(const char *midiout
         }          }
 #if defined(VERMOUTH_LIB)  #if defined(VERMOUTH_LIB)
         else if (!milstr_cmp(midiout, cmmidi_vermouth)) {          else if (!milstr_cmp(midiout, cmmidi_vermouth)) {
                 vermouth = midiout_create(vermouth_module, 512);                  out.vermouth = midiout_create(vermouth_module, 512);
                 if (vermouth != NULL) {                  if (out.vermouth != NULL) {
                         outfn = midiout_vermouth;                          shortout = midi_vermouthshort;
                           longout = midi_vermouthlong;
                         opened |= CMMIDI_VERMOUTH;                          opened |= CMMIDI_VERMOUTH;
                 }                  }
         }          }
 #endif  #endif
   #if defined(MT32SOUND_DLL)
           else if (!milstr_cmp(midiout, cmmidi_mt32sound)) {
                   if (mt32sound_open() == SUCCESS) {
                           shortout = midi_mt32short;
                           longout = midi_mt32long;
                           opened |= CMMIDI_MT32SOUND;
                   }
           }
   #endif
         if (!opened) {          if (!opened) {
                 goto cmcre_err1;                  goto cmcre_err1;
         }          }
Line 728  COMMNG cmmidi_create(const char *midiout Line 803  COMMNG cmmidi_create(const char *midiout
         midi = (CMMIDI)(ret + 1);          midi = (CMMIDI)(ret + 1);
         ZeroMemory(midi, sizeof(_CMMIDI));          ZeroMemory(midi, sizeof(_CMMIDI));
         midi->opened = opened;          midi->opened = opened;
         midi->outfn = outfn;          midi->shortout = shortout;
           midi->longout = longout;
           midi->out = out;
         midi->midictrl = MIDICTRL_READY;          midi->midictrl = MIDICTRL_READY;
         midi->hmidiout = hmidiout;  
 #if 1  #if 1
         midi->hmidiin = hmidiin;          midi->hmidiin = hmidiin;
         if (opened & CMMIDI_MIDIIN) {          if (opened & CMMIDI_MIDIIN) {
Line 745  COMMNG cmmidi_create(const char *midiout Line 821  COMMNG cmmidi_create(const char *midiout
         }          }
 #endif  #endif
 #if defined(VERMOUTH_LIB)  #if defined(VERMOUTH_LIB)
         midi->vermouth = vermouth;  
         if (opened & CMMIDI_VERMOUTH) {          if (opened & CMMIDI_VERMOUTH) {
                 sound_streamregist((void *)vermouth, (SOUNDCB)vermouth_getpcm);                  sound_streamregist((void *)out.vermouth, (SOUNDCB)vermouth_getpcm);
           }
   #endif
   #if defined(MT32SOUND_DLL)
           if (opened & CMMIDI_MT32SOUND) {
                   sound_streamregist(NULL, (SOUNDCB)mt32_getpcm);
         }          }
 #endif  #endif
 //      midi->midisyscnt = 0;  //      midi->midisyscnt = 0;
Line 761  COMMNG cmmidi_create(const char *midiout Line 841  COMMNG cmmidi_create(const char *midiout
   
 cmcre_err2:  cmcre_err2:
         if (opened & CMMIDI_MIDIOUT) {          if (opened & CMMIDI_MIDIOUT) {
                 midiOutReset(hmidiout);                  midiOutReset(out.win32.hmidiout);
                 midiOutClose(hmidiout);                  midiOutClose(out.win32.hmidiout);
         }          }
 #if defined(VERMOUTH_LIB)  #if defined(VERMOUTH_LIB)
         if (opened & CMMIDI_VERMOUTH) {          if (opened & CMMIDI_VERMOUTH) {
                 midiout_destroy(vermouth);                  midiout_destroy(out.vermouth);
           }
   #endif
   #if defined(MT32SOUND_DLL)
           if (opened & CMMIDI_MT32SOUND) {
                   mt32sound_close();
         }          }
 #endif  #endif
   

Removed from v.1.5  
changed lines
  Added in v.1.7


RetroPC.NET-CVS <cvs@retropc.net>