native_midi_win32.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. native_midi: Hardware Midi support for the SDL_mixer library
  3. Copyright (C) 2000,2001 Florian 'Proff' Schulze
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Florian 'Proff' Schulze
  16. florian.proff.schulze@gmx.net
  17. */
  18. #include "SDL.h"
  19. /* everything below is currently one very big bad hack ;) Proff */
  20. #define WIN32_LEAN_AND_MEAN
  21. #include <windows.h>
  22. #include <windowsx.h>
  23. #include <mmsystem.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <limits.h>
  27. #include "native_midi.h"
  28. #include "native_midi_common.h"
  29. struct _NativeMidiSong {
  30. int MusicLoaded;
  31. int MusicPlaying;
  32. MIDIHDR MidiStreamHdr;
  33. MIDIEVENT *NewEvents;
  34. Uint16 ppqn;
  35. int Size;
  36. int NewPos;
  37. };
  38. static UINT MidiDevice=MIDI_MAPPER;
  39. static HMIDISTRM hMidiStream;
  40. static NativeMidiSong *currentsong;
  41. static int BlockOut(NativeMidiSong *song)
  42. {
  43. MMRESULT err;
  44. int BlockSize;
  45. if ((song->MusicLoaded) && (song->NewEvents))
  46. {
  47. // proff 12/8/98: Added for savety
  48. midiOutUnprepareHeader((HMIDIOUT)hMidiStream,&song->MidiStreamHdr,sizeof(MIDIHDR));
  49. if (song->NewPos>=song->Size)
  50. return 0;
  51. BlockSize=(song->Size-song->NewPos);
  52. if (BlockSize<=0)
  53. return 0;
  54. if (BlockSize>36000)
  55. BlockSize=36000;
  56. song->MidiStreamHdr.lpData=(void *)((unsigned char *)song->NewEvents+song->NewPos);
  57. song->NewPos+=BlockSize;
  58. song->MidiStreamHdr.dwBufferLength=BlockSize;
  59. song->MidiStreamHdr.dwBytesRecorded=BlockSize;
  60. song->MidiStreamHdr.dwFlags=0;
  61. err=midiOutPrepareHeader((HMIDIOUT)hMidiStream,&song->MidiStreamHdr,sizeof(MIDIHDR));
  62. if (err!=MMSYSERR_NOERROR)
  63. return 0;
  64. err=midiStreamOut(hMidiStream,&song->MidiStreamHdr,sizeof(MIDIHDR));
  65. return 0;
  66. }
  67. return 1;
  68. }
  69. static void MIDItoStream(NativeMidiSong *song, MIDIEvent *evntlist)
  70. {
  71. int eventcount;
  72. MIDIEvent *event;
  73. MIDIEVENT *newevent;
  74. eventcount=0;
  75. event=evntlist;
  76. while (event)
  77. {
  78. eventcount++;
  79. event=event->next;
  80. }
  81. song->NewEvents=malloc(eventcount*3*sizeof(DWORD));
  82. if (!song->NewEvents)
  83. return;
  84. memset(song->NewEvents,0,(eventcount*3*sizeof(DWORD)));
  85. eventcount=0;
  86. event=evntlist;
  87. newevent=song->NewEvents;
  88. while (event)
  89. {
  90. int status = (event->status&0xF0)>>4;
  91. switch (status)
  92. {
  93. case MIDI_STATUS_NOTE_OFF:
  94. case MIDI_STATUS_NOTE_ON:
  95. case MIDI_STATUS_AFTERTOUCH:
  96. case MIDI_STATUS_CONTROLLER:
  97. case MIDI_STATUS_PROG_CHANGE:
  98. case MIDI_STATUS_PRESSURE:
  99. case MIDI_STATUS_PITCH_WHEEL:
  100. newevent->dwDeltaTime=event->time;
  101. newevent->dwEvent=(event->status|0x80)|(event->data[0]<<8)|(event->data[1]<<16)|(MEVT_SHORTMSG<<24);
  102. newevent=(MIDIEVENT*)((char*)newevent+(3*sizeof(DWORD)));
  103. eventcount++;
  104. break;
  105. case MIDI_STATUS_SYSEX:
  106. if (event->status == 0xFF && event->data[0] == 0x51) /* Tempo change */
  107. {
  108. int tempo = (event->extraData[0] << 16) |
  109. (event->extraData[1] << 8) |
  110. event->extraData[2];
  111. newevent->dwDeltaTime=event->time;
  112. newevent->dwEvent=(MEVT_TEMPO<<24) | tempo;
  113. newevent=(MIDIEVENT*)((char*)newevent+(3*sizeof(DWORD)));
  114. eventcount++;
  115. }
  116. break;
  117. }
  118. event=event->next;
  119. }
  120. song->Size=eventcount*3*sizeof(DWORD);
  121. {
  122. int time;
  123. int temptime;
  124. song->NewPos=0;
  125. time=0;
  126. newevent=song->NewEvents;
  127. while (song->NewPos<song->Size)
  128. {
  129. temptime=newevent->dwDeltaTime;
  130. newevent->dwDeltaTime-=time;
  131. time=temptime;
  132. if ((song->NewPos+12)>=song->Size)
  133. newevent->dwEvent |= MEVT_F_CALLBACK;
  134. newevent=(MIDIEVENT*)((char*)newevent+(3*sizeof(DWORD)));
  135. song->NewPos+=12;
  136. }
  137. }
  138. song->NewPos=0;
  139. song->MusicLoaded=1;
  140. }
  141. void CALLBACK MidiProc( HMIDIIN hMidi, UINT uMsg, unsigned long dwInstance,
  142. unsigned long dwParam1, unsigned long dwParam2 )
  143. {
  144. switch( uMsg )
  145. {
  146. case MOM_DONE:
  147. if ((currentsong->MusicLoaded) && (dwParam1 == (unsigned long)&currentsong->MidiStreamHdr))
  148. BlockOut(currentsong);
  149. break;
  150. case MOM_POSITIONCB:
  151. if ((currentsong->MusicLoaded) && (dwParam1 == (unsigned long)&currentsong->MidiStreamHdr))
  152. currentsong->MusicPlaying=0;
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. int native_midi_detect()
  159. {
  160. MMRESULT merr;
  161. HMIDISTRM MidiStream;
  162. merr=midiStreamOpen(&MidiStream,&MidiDevice,(DWORD)1,(unsigned long)MidiProc,(unsigned long)0,CALLBACK_FUNCTION);
  163. if (merr!=MMSYSERR_NOERROR)
  164. return 0;
  165. midiStreamClose(MidiStream);
  166. return 1;
  167. }
  168. NativeMidiSong *native_midi_loadsong(const char *midifile)
  169. {
  170. NativeMidiSong *newsong;
  171. MIDIEvent *evntlist = NULL;
  172. SDL_RWops *rw;
  173. newsong=malloc(sizeof(NativeMidiSong));
  174. if (!newsong)
  175. return NULL;
  176. memset(newsong,0,sizeof(NativeMidiSong));
  177. /* Attempt to load the midi file */
  178. rw = SDL_RWFromFile(midifile, "rb");
  179. if (rw) {
  180. evntlist = CreateMIDIEventList(rw, &newsong->ppqn);
  181. SDL_RWclose(rw);
  182. if (!evntlist)
  183. {
  184. free(newsong);
  185. return NULL;
  186. }
  187. }
  188. MIDItoStream(newsong, evntlist);
  189. FreeMIDIEventList(evntlist);
  190. return newsong;
  191. }
  192. NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
  193. {
  194. NativeMidiSong *newsong;
  195. MIDIEvent *evntlist = NULL;
  196. newsong=malloc(sizeof(NativeMidiSong));
  197. if (!newsong)
  198. return NULL;
  199. memset(newsong,0,sizeof(NativeMidiSong));
  200. /* Attempt to load the midi file */
  201. evntlist = CreateMIDIEventList(rw, &newsong->ppqn);
  202. if (!evntlist)
  203. {
  204. free(newsong);
  205. return NULL;
  206. }
  207. MIDItoStream(newsong, evntlist);
  208. FreeMIDIEventList(evntlist);
  209. return newsong;
  210. }
  211. void native_midi_freesong(NativeMidiSong *song)
  212. {
  213. if (hMidiStream)
  214. {
  215. midiStreamStop(hMidiStream);
  216. midiStreamClose(hMidiStream);
  217. }
  218. if (song)
  219. {
  220. if (song->NewEvents)
  221. free(song->NewEvents);
  222. free(song);
  223. }
  224. }
  225. void native_midi_start(NativeMidiSong *song)
  226. {
  227. MMRESULT merr;
  228. MIDIPROPTIMEDIV mptd;
  229. native_midi_stop();
  230. if (!hMidiStream)
  231. {
  232. merr=midiStreamOpen(&hMidiStream,&MidiDevice,1,(DWORD)&MidiProc,0,CALLBACK_FUNCTION);
  233. if (merr!=MMSYSERR_NOERROR)
  234. {
  235. hMidiStream=0;
  236. return;
  237. }
  238. //midiStreamStop(hMidiStream);
  239. currentsong=song;
  240. currentsong->NewPos=0;
  241. currentsong->MusicPlaying=1;
  242. mptd.cbStruct=sizeof(MIDIPROPTIMEDIV);
  243. mptd.dwTimeDiv=currentsong->ppqn;
  244. merr=midiStreamProperty(hMidiStream,(LPBYTE)&mptd,MIDIPROP_SET | MIDIPROP_TIMEDIV);
  245. BlockOut(song);
  246. merr=midiStreamRestart(hMidiStream);
  247. }
  248. }
  249. void native_midi_stop()
  250. {
  251. if (!hMidiStream)
  252. return;
  253. midiStreamStop(hMidiStream);
  254. midiStreamClose(hMidiStream);
  255. currentsong=NULL;
  256. hMidiStream = 0;
  257. }
  258. int native_midi_active()
  259. {
  260. return currentsong->MusicPlaying;
  261. }
  262. void native_midi_setvolume(int volume)
  263. {
  264. int calcVolume;
  265. if (volume > 128)
  266. volume = 128;
  267. if (volume < 0)
  268. volume = 0;
  269. calcVolume = (65535 * volume / 128);
  270. midiOutSetVolume((HMIDIOUT)hMidiStream, MAKELONG(calcVolume , calcVolume));
  271. }
  272. const char *native_midi_error(void)
  273. {
  274. return "";
  275. }