Florian Schulze - Sun Aug 19 14:55:37 PDT 2001
authorSam Lantinga <slouken@libsdl.org>
Sun, 19 Aug 2001 21:52:48 +0000
changeset 98f00eb57224a5
parent 97 2bf733171d56
child 99 06246f9170dc
Florian Schulze - Sun Aug 19 14:55:37 PDT 2001
* Added native MIDI music support on Windows
CHANGES
music.c
native_midi/native_midi.h
native_midi/native_midi_win32.c
     1.1 --- a/CHANGES	Sun Aug 19 21:19:17 2001 +0000
     1.2 +++ b/CHANGES	Sun Aug 19 21:52:48 2001 +0000
     1.3 @@ -1,5 +1,7 @@
     1.4  
     1.5  1.2.1:
     1.6 +Florian Schulze - Sun Aug 19 14:55:37 PDT 2001
     1.7 + * Added native MIDI music support on Windows
     1.8  Sam Lantinga - Sun Aug 19 02:20:55 PDT 2001
     1.9   * Added Project Builder projects for building MacOS X framework
    1.10  Darrell Walisser - Sun Aug 19 00:47:22 PDT 2001
     2.1 --- a/music.c	Sun Aug 19 21:19:17 2001 +0000
     2.2 +++ b/music.c	Sun Aug 19 21:52:48 2001 +0000
     2.3 @@ -56,6 +56,9 @@
     2.4  #endif
     2.5  #ifdef MID_MUSIC
     2.6  #include "timidity.h"
     2.7 +#ifdef USE_NATIVE_MIDI
     2.8 +#include "native_midi.h"
     2.9 +#endif
    2.10  #endif
    2.11  #ifdef OGG_MUSIC
    2.12  #include "music_ogg.h"
    2.13 @@ -97,6 +100,9 @@
    2.14  #endif
    2.15  #ifdef MID_MUSIC
    2.16  		MidiSong *midi;
    2.17 +#ifdef USE_NATIVE_MIDI
    2.18 +		NativeMidiSong *nativemidi;
    2.19 +#endif
    2.20  #endif
    2.21  #ifdef OGG_MUSIC
    2.22  		OGG_music *ogg;
    2.23 @@ -113,6 +119,9 @@
    2.24  };
    2.25  #ifdef MID_MUSIC
    2.26  static int timidity_ok;
    2.27 +#ifdef USE_NATIVE_MIDI
    2.28 +static int native_midi_ok;
    2.29 +#endif
    2.30  #endif
    2.31  
    2.32  /* Used to calculate fading steps */
    2.33 @@ -224,7 +233,10 @@
    2.34  #endif
    2.35  #ifdef MID_MUSIC
    2.36  			case MUS_MID:
    2.37 -				Timidity_PlaySome(stream, len/samplesize);
    2.38 +				if ( timidity_ok ) {
    2.39 +					int samples = len / samplesize;
    2.40 +  					Timidity_PlaySome(stream, samples);
    2.41 +				}
    2.42  				break;
    2.43  #endif
    2.44  #ifdef OGG_MUSIC
    2.45 @@ -312,11 +324,20 @@
    2.46  	}
    2.47  #endif
    2.48  #ifdef MID_MUSIC
    2.49 -	samplesize	 = mixer->size/mixer->samples;
    2.50 -	if ( Timidity_Init(mixer->freq,
    2.51 -			mixer->format, mixer->channels, mixer->samples) == 0 ) {
    2.52 -		timidity_ok = 1;
    2.53 +	samplesize = mixer->size/mixer->samples;
    2.54 +#ifdef USE_NATIVE_MIDI
    2.55 +	if ( native_midi_init() ) {
    2.56 +		native_midi_ok = 1;
    2.57  	} else {
    2.58 +		native_midi_ok = 0;
    2.59 +	}
    2.60 +	timidity_ok = !native_midi_ok;
    2.61 +#else
    2.62 +	timidity_ok = 1;
    2.63 +#endif
    2.64 +	if ( timidity_ok &&
    2.65 +	     (Timidity_Init(mixer->freq, mixer->format,
    2.66 +	                    mixer->channels, mixer->samples) != 0) ) {
    2.67  		timidity_ok = 0;
    2.68  	}
    2.69  #endif
    2.70 @@ -395,14 +416,22 @@
    2.71  	/* MIDI files have the magic four bytes "MThd" */
    2.72  	if ( strcmp(magic, "MThd") == 0 ) {
    2.73  		music->type = MUS_MID;
    2.74 +#ifdef USE_NATIVE_MIDI
    2.75 +  		if ( native_midi_ok ) {
    2.76 +  			music->data.nativemidi = native_midi_loadsong((char *)file);
    2.77 +	  		if ( music->data.nativemidi == NULL ) {
    2.78 +		  		Mix_SetError("%s", native_midi_error());
    2.79 +			  	music->error = 1;
    2.80 +			}
    2.81 +      		} else
    2.82 +#endif
    2.83  		if ( timidity_ok ) {
    2.84  			music->data.midi = Timidity_LoadSong((char *)file);
    2.85  			if ( music->data.midi == NULL ) {
    2.86  				Mix_SetError("%s", Timidity_Error());
    2.87  				music->error = 1;
    2.88  			}
    2.89 -		}
    2.90 -		else {
    2.91 +		} else {
    2.92  			Mix_SetError("%s", Timidity_Error());
    2.93  			music->error = 1;
    2.94  		}
    2.95 @@ -484,7 +513,14 @@
    2.96  #endif
    2.97  #ifdef MID_MUSIC
    2.98  			case MUS_MID:
    2.99 -				Timidity_FreeSong(music->data.midi);
   2.100 +#ifdef USE_NATIVE_MIDI
   2.101 +  				if ( native_midi_ok ) {
   2.102 +					native_midi_freesong(music->data.nativemidi);
   2.103 +				} else
   2.104 +#endif
   2.105 +				if ( timidity_ok ) {
   2.106 +					Timidity_FreeSong(music->data.midi);
   2.107 +				}
   2.108  				break;
   2.109  #endif
   2.110  #ifdef OGG_MUSIC
   2.111 @@ -532,8 +568,16 @@
   2.112  #endif
   2.113  #ifdef MID_MUSIC
   2.114  		case MUS_MID:
   2.115 -			Timidity_SetVolume(music_volume);
   2.116 -			Timidity_Start(music->data.midi);
   2.117 +#ifdef USE_NATIVE_MIDI
   2.118 +  			if ( native_midi_ok ) {
   2.119 +				native_midi_setvolume(music_volume);
   2.120 +				native_midi_start(music->data.nativemidi);
   2.121 +			} else
   2.122 +#endif
   2.123 +			if ( timidity_ok ) {
   2.124 +				Timidity_SetVolume(music_volume);
   2.125 +				Timidity_Start(music->data.midi);
   2.126 +			}
   2.127  			break;
   2.128  #endif
   2.129  #ifdef OGG_MUSIC
   2.130 @@ -629,7 +673,14 @@
   2.131  #endif
   2.132  #ifdef MID_MUSIC
   2.133  		case MUS_MID:
   2.134 -			Timidity_SetVolume(music_volume);
   2.135 +#ifdef USE_NATIVE_MIDI
   2.136 +			if ( native_midi_ok ) {
   2.137 +				native_midi_setvolume(music_volume);
   2.138 +			} else
   2.139 +#endif
   2.140 +			if ( timidity_ok ) {
   2.141 +				Timidity_SetVolume(music_volume);
   2.142 +			}
   2.143  			break;
   2.144  #endif
   2.145  #ifdef OGG_MUSIC
   2.146 @@ -670,7 +721,14 @@
   2.147  #endif
   2.148  #ifdef MID_MUSIC
   2.149  	case MUS_MID:
   2.150 -		Timidity_Stop();
   2.151 +#ifdef USE_NATIVE_MIDI
   2.152 +		if ( native_midi_ok ) {
   2.153 +			native_midi_stop();
   2.154 +		} else
   2.155 +#endif
   2.156 +		if ( timidity_ok ) {
   2.157 +			Timidity_Stop();
   2.158 +		}
   2.159  		break;
   2.160  #endif
   2.161  #ifdef OGG_MUSIC
   2.162 @@ -762,6 +820,15 @@
   2.163  			SMPEG_rewind(music_playing->data.mp3);
   2.164  			break;
   2.165  #endif
   2.166 +#ifdef MID_MUSIC
   2.167 +		case MUS_MID:
   2.168 +#ifdef USE_NATIVE_MIDI
   2.169 +			if ( native_midi_ok ) {
   2.170 +				native_midi_stop();
   2.171 +			}
   2.172 +#endif
   2.173 +			break;
   2.174 +#endif
   2.175  		default:
   2.176  			/* TODO: Implement this for other music backends */
   2.177  			break;
   2.178 @@ -802,8 +869,15 @@
   2.179  #endif
   2.180  #ifdef MID_MUSIC
   2.181  			case MUS_MID:
   2.182 -				if ( ! Timidity_Active() ) {
   2.183 -					return(0);
   2.184 +#ifdef USE_NATIVE_MIDI
   2.185 +				if ( native_midi_ok ) {
   2.186 +					if ( ! native_midi_active() )
   2.187 +						return(0);
   2.188 +				} else
   2.189 +#endif
   2.190 +				if ( timidity_ok ) {
   2.191 +					if ( ! Timidity_Active() )
   2.192 +						return(0);
   2.193  				}
   2.194  				break;
   2.195  #endif
   2.196 @@ -816,7 +890,7 @@
   2.197  #endif
   2.198  #ifdef MP3_MUSIC
   2.199  			case MUS_MP3:
   2.200 -				if(SMPEG_status(music_playing->data.mp3)!=SMPEG_PLAYING)
   2.201 +				if ( SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
   2.202  					return(0);
   2.203  				break;
   2.204  #endif
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/native_midi/native_midi.h	Sun Aug 19 21:52:48 2001 +0000
     3.3 @@ -0,0 +1,72 @@
     3.4 +/*
     3.5 +    native_midi:  Hardware Midi support for the SDL_mixer library
     3.6 +    Copyright (C) 2000  Florian 'Proff' Schulze
     3.7 +
     3.8 +    This library is free software; you can redistribute it and/or
     3.9 +    modify it under the terms of the GNU Library General Public
    3.10 +    License as published by the Free Software Foundation; either
    3.11 +    version 2 of the License, or (at your option) any later version.
    3.12 +
    3.13 +    This library is distributed in the hope that it will be useful,
    3.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.16 +    Library General Public License for more details.
    3.17 +
    3.18 +    You should have received a copy of the GNU Library General Public
    3.19 +    License along with this library; if not, write to the Free
    3.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    3.21 +
    3.22 +    Florian 'Proff' Schulze
    3.23 +    florian.proff.schulze@gmx.net
    3.24 +*/
    3.25 +
    3.26 +#ifndef _NATIVE_MIDI_H_
    3.27 +#define _NATIVE_MIDI_H_
    3.28 +
    3.29 +#ifdef _WIN32
    3.30 +#define WIN32_LEAN_AND_MEAN
    3.31 +#include <windows.h>
    3.32 +#include <windowsx.h>
    3.33 +#include <mmsystem.h>
    3.34 +
    3.35 +#define MIDI_TRACKS 32
    3.36 +
    3.37 +typedef unsigned char byte;
    3.38 +typedef struct MIDI                    /* a midi file */
    3.39 +{
    3.40 +   int divisions;                      /* number of ticks per quarter note */
    3.41 +   struct {
    3.42 +      unsigned char *data;             /* MIDI message stream */
    3.43 +      int len;                         /* length of the track data */
    3.44 +   } track[MIDI_TRACKS];
    3.45 +   int loaded;
    3.46 +} MIDI;
    3.47 +
    3.48 +struct _NativeMidiSong {
    3.49 +  MIDI mididata;
    3.50 +  int MusicLoaded;
    3.51 +  int MusicPlaying;
    3.52 +  MIDIEVENT *MidiEvents[MIDI_TRACKS];
    3.53 +  MIDIHDR MidiStreamHdr;
    3.54 +  MIDIEVENT *NewEvents;
    3.55 +  int NewSize;
    3.56 +  int NewPos;
    3.57 +  int BytesRecorded[MIDI_TRACKS];
    3.58 +  int BufferSize[MIDI_TRACKS];
    3.59 +  int CurrentTrack;
    3.60 +  int CurrentPos;
    3.61 +};
    3.62 +#endif /* WINDOWS */
    3.63 +
    3.64 +typedef struct _NativeMidiSong NativeMidiSong;
    3.65 +
    3.66 +int native_midi_init();
    3.67 +NativeMidiSong *native_midi_loadsong(char *midifile);
    3.68 +void native_midi_freesong(NativeMidiSong *song);
    3.69 +void native_midi_start(NativeMidiSong *song);
    3.70 +void native_midi_stop();
    3.71 +int native_midi_active();
    3.72 +void native_midi_setvolume(int volume);
    3.73 +char *native_midi_error();
    3.74 +
    3.75 +#endif /* _NATIVE_MIDI_H_ */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/native_midi/native_midi_win32.c	Sun Aug 19 21:52:48 2001 +0000
     4.3 @@ -0,0 +1,461 @@
     4.4 +/*
     4.5 +    native_midi:  Hardware Midi support for the SDL_mixer library
     4.6 +    Copyright (C) 2000,2001  Florian 'Proff' Schulze
     4.7 +
     4.8 +    This library is free software; you can redistribute it and/or
     4.9 +    modify it under the terms of the GNU Library General Public
    4.10 +    License as published by the Free Software Foundation; either
    4.11 +    version 2 of the License, or (at your option) any later version.
    4.12 +
    4.13 +    This library is distributed in the hope that it will be useful,
    4.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    4.16 +    Library General Public License for more details.
    4.17 +
    4.18 +    You should have received a copy of the GNU Library General Public
    4.19 +    License along with this library; if not, write to the Free
    4.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    4.21 +
    4.22 +    Florian 'Proff' Schulze
    4.23 +    florian.proff.schulze@gmx.net
    4.24 +*/
    4.25 +
    4.26 +/* everything below is currently one very big bad hack ;) Proff */
    4.27 +
    4.28 +#include <stdio.h>
    4.29 +#include <stdlib.h>
    4.30 +#include <limits.h>
    4.31 +#include "native_midi.h"
    4.32 +
    4.33 +#define XCHG_SHORT(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
    4.34 +# define XCHG_LONG(x) ((((x)&0xFF)<<24) | \
    4.35 +		      (((x)&0xFF00)<<8) | \
    4.36 +		      (((x)&0xFF0000)>>8) | \
    4.37 +		      (((x)>>24)&0xFF))
    4.38 +
    4.39 +#define LE_SHORT(x) x
    4.40 +#define LE_LONG(x) x
    4.41 +#define BE_SHORT(x) XCHG_SHORT(x)
    4.42 +#define BE_LONG(x) XCHG_LONG(x)
    4.43 +
    4.44 +static UINT MidiDevice=MIDI_MAPPER;
    4.45 +static HMIDISTRM hMidiStream;
    4.46 +static NativeMidiSong *currentsong;
    4.47 +
    4.48 +static int getvl(NativeMidiSong *song)
    4.49 +{
    4.50 +  int l=0;
    4.51 +  byte c;
    4.52 +  for (;;)
    4.53 +  {
    4.54 +    c=song->mididata.track[song->CurrentTrack].data[song->CurrentPos];
    4.55 +    song->CurrentPos++;
    4.56 +    l += (c & 0x7f);
    4.57 +    if (!(c & 0x80)) 
    4.58 +      return l;
    4.59 +    l<<=7;
    4.60 +  }
    4.61 +}
    4.62 +
    4.63 +static void AddEvent(NativeMidiSong *song, DWORD at, DWORD type, byte event, byte a, byte b)
    4.64 +{
    4.65 +  MIDIEVENT *CurEvent;
    4.66 +
    4.67 +  if ((song->BytesRecorded[song->CurrentTrack]+(int)sizeof(MIDIEVENT))>=song->BufferSize[song->CurrentTrack])
    4.68 +  {
    4.69 +    song->BufferSize[song->CurrentTrack]+=100*sizeof(MIDIEVENT);
    4.70 +    song->MidiEvents[song->CurrentTrack]=realloc(song->MidiEvents[song->CurrentTrack],song->BufferSize[song->CurrentTrack]);
    4.71 +  }
    4.72 +  CurEvent=(MIDIEVENT *)((byte *)song->MidiEvents[song->CurrentTrack]+song->BytesRecorded[song->CurrentTrack]);
    4.73 +  memset(CurEvent,0,sizeof(MIDIEVENT));
    4.74 +  CurEvent->dwDeltaTime=at;
    4.75 +  CurEvent->dwEvent=event+(a<<8)+(b<<16)+(type<<24);
    4.76 +  song->BytesRecorded[song->CurrentTrack]+=3*sizeof(DWORD);
    4.77 +}
    4.78 +
    4.79 +static void MidiTracktoStream(NativeMidiSong *song)
    4.80 +{
    4.81 +  DWORD atime,len;
    4.82 +  byte event,type,a,b,c;
    4.83 +  byte laststatus,lastchan;
    4.84 +
    4.85 +  song->CurrentPos=0;
    4.86 +  laststatus=0;
    4.87 +  lastchan=0;
    4.88 +  atime=0;
    4.89 +  for (;;)
    4.90 +  {
    4.91 +    if (song->CurrentPos>=song->mididata.track[song->CurrentTrack].len)
    4.92 +      return;
    4.93 +    atime+=getvl(song);
    4.94 +    event=song->mididata.track[song->CurrentTrack].data[song->CurrentPos];
    4.95 +    song->CurrentPos++;
    4.96 +    if(event==0xF0 || event == 0xF7) // SysEx event
    4.97 +    {
    4.98 +      len=getvl(song);
    4.99 +      song->CurrentPos+=len;
   4.100 +    }
   4.101 +    else if(event==0xFF) // Meta event
   4.102 +    {
   4.103 +      type=song->mididata.track[song->CurrentTrack].data[song->CurrentPos];
   4.104 +      song->CurrentPos++;
   4.105 +      len=getvl(song);
   4.106 +      switch(type)
   4.107 +        {
   4.108 +        case 0x2f:
   4.109 +          return;
   4.110 +        case 0x51: // Tempo
   4.111 +          a=song->mididata.track[song->CurrentTrack].data[song->CurrentPos];
   4.112 +          song->CurrentPos++;
   4.113 +          b=song->mididata.track[song->CurrentTrack].data[song->CurrentPos];
   4.114 +          song->CurrentPos++;
   4.115 +          c=song->mididata.track[song->CurrentTrack].data[song->CurrentPos];
   4.116 +          song->CurrentPos++;
   4.117 +          AddEvent(song, atime, MEVT_TEMPO, c, b, a);
   4.118 +          break;
   4.119 +        default:
   4.120 +          song->CurrentPos+=len;
   4.121 +          break;
   4.122 +        }
   4.123 +    }
   4.124 +    else
   4.125 +    {
   4.126 +      a=event;
   4.127 +      if (a & 0x80) // status byte
   4.128 +      {
   4.129 +        lastchan=a & 0x0F;
   4.130 +        laststatus=(a>>4) & 0x07;
   4.131 +        a=song->mididata.track[song->CurrentTrack].data[song->CurrentPos];
   4.132 +        song->CurrentPos++;
   4.133 +        a &= 0x7F;
   4.134 +      }
   4.135 +      switch(laststatus)
   4.136 +      {
   4.137 +      case 0: // Note off
   4.138 +        b=song->mididata.track[song->CurrentTrack].data[song->CurrentPos];
   4.139 +        song->CurrentPos++;
   4.140 +        b &= 0x7F;
   4.141 +        AddEvent(song, atime, MEVT_SHORTMSG, (byte)((laststatus<<4)+lastchan+0x80), a, b);
   4.142 +        break;
   4.143 +
   4.144 +      case 1: // Note on
   4.145 +        b=song->mididata.track[song->CurrentTrack].data[song->CurrentPos];
   4.146 +        song->CurrentPos++;
   4.147 +        b &= 0x7F;
   4.148 +        AddEvent(song, atime, MEVT_SHORTMSG, (byte)((laststatus<<4)+lastchan+0x80), a, b);
   4.149 +        break;
   4.150 +
   4.151 +      case 2: // Key Pressure
   4.152 +        b=song->mididata.track[song->CurrentTrack].data[song->CurrentPos];
   4.153 +        song->CurrentPos++;
   4.154 +        b &= 0x7F;
   4.155 +        AddEvent(song, atime, MEVT_SHORTMSG, (byte)((laststatus<<4)+lastchan+0x80), a, b);
   4.156 +        break;
   4.157 +
   4.158 +      case 3: // Control change
   4.159 +        b=song->mididata.track[song->CurrentTrack].data[song->CurrentPos];
   4.160 +        song->CurrentPos++;
   4.161 +        b &= 0x7F;
   4.162 +        AddEvent(song, atime, MEVT_SHORTMSG, (byte)((laststatus<<4)+lastchan+0x80), a, b);
   4.163 +        break;
   4.164 +
   4.165 +      case 4: // Program change
   4.166 +        a &= 0x7f;
   4.167 +        AddEvent(song, atime, MEVT_SHORTMSG, (byte)((laststatus<<4)+lastchan+0x80), a, 0);
   4.168 +        break;
   4.169 +
   4.170 +      case 5: // Channel pressure
   4.171 +        a &= 0x7f;
   4.172 +        AddEvent(song, atime, MEVT_SHORTMSG, (byte)((laststatus<<4)+lastchan+0x80), a, 0);
   4.173 +        break;
   4.174 +
   4.175 +      case 6: // Pitch wheel
   4.176 +        b=song->mididata.track[song->CurrentTrack].data[song->CurrentPos];
   4.177 +        song->CurrentPos++;
   4.178 +        b &= 0x7F;
   4.179 +        AddEvent(song, atime, MEVT_SHORTMSG, (byte)((laststatus<<4)+lastchan+0x80), a, b);
   4.180 +        break;
   4.181 +
   4.182 +      default: 
   4.183 +        break;
   4.184 +      }
   4.185 +    }
   4.186 +  }
   4.187 +}
   4.188 +
   4.189 +static int BlockOut(NativeMidiSong *song)
   4.190 +{
   4.191 +  MMRESULT err;
   4.192 +  int BlockSize;
   4.193 +
   4.194 +  if ((song->MusicLoaded) && (song->NewEvents))
   4.195 +  {
   4.196 +    // proff 12/8/98: Added for savety
   4.197 +    midiOutUnprepareHeader(hMidiStream,&song->MidiStreamHdr,sizeof(MIDIHDR));
   4.198 +    if (song->NewPos>=song->NewSize)
   4.199 +      return 0;
   4.200 +    BlockSize=(song->NewSize-song->NewPos);
   4.201 +    if (BlockSize<=0)
   4.202 +      return 0;
   4.203 +    if (BlockSize>36000)
   4.204 +      BlockSize=36000;
   4.205 +    song->MidiStreamHdr.lpData=(void *)((byte *)song->NewEvents+song->NewPos);
   4.206 +    song->NewPos+=BlockSize;
   4.207 +    song->MidiStreamHdr.dwBufferLength=BlockSize;
   4.208 +    song->MidiStreamHdr.dwBytesRecorded=BlockSize;
   4.209 +    song->MidiStreamHdr.dwFlags=0;
   4.210 +//    lprintf(LO_DEBUG,"Data: %p, Size: %i\n",MidiStreamHdr.lpData,BlockSize);
   4.211 +    err=midiOutPrepareHeader(hMidiStream,&song->MidiStreamHdr,sizeof(MIDIHDR));
   4.212 +    if (err!=MMSYSERR_NOERROR)
   4.213 +      return 0;
   4.214 +    err=midiStreamOut(hMidiStream,&song->MidiStreamHdr,sizeof(MIDIHDR));
   4.215 +      return 0;
   4.216 +  }
   4.217 +  return 1;
   4.218 +}
   4.219 +
   4.220 +static void MIDItoStream(NativeMidiSong *song)
   4.221 +{
   4.222 +  int BufferPos[MIDI_TRACKS];
   4.223 +  MIDIEVENT *CurEvent;
   4.224 +  MIDIEVENT *NewEvent;
   4.225 +  int lTime;
   4.226 +  int Dummy;
   4.227 +  int Track;
   4.228 +
   4.229 +  //if (!hMidiStream)
   4.230 +    //return;
   4.231 +  song->NewSize=0;
   4.232 +  for (song->CurrentTrack=0;song->CurrentTrack<MIDI_TRACKS;song->CurrentTrack++)
   4.233 +  {
   4.234 +    song->MidiEvents[song->CurrentTrack]=NULL;
   4.235 +    song->BytesRecorded[song->CurrentTrack]=0;
   4.236 +    song->BufferSize[song->CurrentTrack]=0;
   4.237 +    MidiTracktoStream(song);
   4.238 +    song->NewSize+=song->BytesRecorded[song->CurrentTrack];
   4.239 +    BufferPos[song->CurrentTrack]=0;
   4.240 +  }
   4.241 +  song->NewEvents=realloc(song->NewEvents,song->NewSize);
   4.242 +  if (song->NewEvents)
   4.243 +  {
   4.244 +    song->NewPos=0;
   4.245 +    while (1)
   4.246 +    {
   4.247 +      lTime=INT_MAX;
   4.248 +      Track=-1;
   4.249 +      for (song->CurrentTrack=MIDI_TRACKS-1;song->CurrentTrack>=0;song->CurrentTrack--)
   4.250 +      {
   4.251 +        if ((song->BytesRecorded[song->CurrentTrack]>0) && (BufferPos[song->CurrentTrack]<song->BytesRecorded[song->CurrentTrack]))
   4.252 +          CurEvent=(MIDIEVENT *)((byte *)song->MidiEvents[song->CurrentTrack]+BufferPos[song->CurrentTrack]);
   4.253 +        else 
   4.254 +          continue;
   4.255 +        if ((int)CurEvent->dwDeltaTime<=lTime)
   4.256 +        {
   4.257 +          lTime=CurEvent->dwDeltaTime;
   4.258 +          Track=song->CurrentTrack;
   4.259 +        }
   4.260 +      }
   4.261 +      if (Track==-1)
   4.262 +        break;
   4.263 +      else
   4.264 +      {
   4.265 +        CurEvent=(MIDIEVENT *)((byte *)song->MidiEvents[Track]+BufferPos[Track]);
   4.266 +        BufferPos[Track]+=12;
   4.267 +        NewEvent=(MIDIEVENT *)((byte *)song->NewEvents+song->NewPos);
   4.268 +        memcpy(NewEvent,CurEvent,12);
   4.269 +        song->NewPos+=12;
   4.270 +      }
   4.271 +    }
   4.272 +    song->NewPos=0;
   4.273 +    lTime=0;
   4.274 +    while (song->NewPos<song->NewSize)
   4.275 +    {
   4.276 +      NewEvent=(MIDIEVENT *)((byte *)song->NewEvents+song->NewPos);
   4.277 +      Dummy=NewEvent->dwDeltaTime;
   4.278 +      NewEvent->dwDeltaTime-=lTime;
   4.279 +      lTime=Dummy;
   4.280 +      if ((song->NewPos+12)>=song->NewSize)
   4.281 +        NewEvent->dwEvent |= MEVT_F_CALLBACK;
   4.282 +      song->NewPos+=12;
   4.283 +    }
   4.284 +    song->NewPos=0;
   4.285 +    song->MusicLoaded=1;
   4.286 +    //BlockOut(song);
   4.287 +  }
   4.288 +  for (song->CurrentTrack=0;song->CurrentTrack<MIDI_TRACKS;song->CurrentTrack++)
   4.289 +  {
   4.290 +    if (song->MidiEvents[song->CurrentTrack])
   4.291 +      free(song->MidiEvents[song->CurrentTrack]);
   4.292 +  }
   4.293 +}
   4.294 +
   4.295 +void CALLBACK MidiProc( HMIDIIN hMidi, UINT uMsg, DWORD dwInstance,
   4.296 +                        DWORD dwParam1, DWORD dwParam2 )
   4.297 +{
   4.298 +    switch( uMsg )
   4.299 +    {
   4.300 +    case MOM_DONE:
   4.301 +      if ((currentsong->MusicLoaded) && ((DWORD)dwParam1 == (DWORD)&currentsong->MidiStreamHdr))
   4.302 +        BlockOut(currentsong);
   4.303 +      break;
   4.304 +    case MOM_POSITIONCB:
   4.305 +      if ((currentsong->MusicLoaded) && ((DWORD)dwParam1 == (DWORD)&currentsong->MidiStreamHdr))
   4.306 +        currentsong->MusicPlaying=0;
   4.307 +      break;
   4.308 +    default:
   4.309 +      break;
   4.310 +    }
   4.311 +}
   4.312 +
   4.313 +int native_midi_init()
   4.314 +{
   4.315 +  MMRESULT merr;
   4.316 +  HMIDISTRM MidiStream;
   4.317 +
   4.318 +  merr=midiStreamOpen(&MidiStream,&MidiDevice,1,(DWORD)&MidiProc,0,CALLBACK_FUNCTION);
   4.319 +  if (merr!=MMSYSERR_NOERROR)
   4.320 +    MidiStream=0;
   4.321 +  midiStreamClose(MidiStream);
   4.322 +  if (!MidiStream)
   4.323 +    return 0;
   4.324 +  else
   4.325 +    return 1;
   4.326 +}
   4.327 +
   4.328 +typedef struct
   4.329 +{
   4.330 +  char  ID[4];
   4.331 +  int   size;
   4.332 +  short format;
   4.333 +  short tracks;
   4.334 +  short deltatime;
   4.335 +} fmidihdr;
   4.336 +
   4.337 +typedef struct
   4.338 +{
   4.339 +  char  ID[4];
   4.340 +  int   size;
   4.341 +} fmiditrack;
   4.342 +
   4.343 +static void load_mididata(MIDI *mididata, FILE *fp)
   4.344 +{
   4.345 +  fmidihdr midihdr;
   4.346 +  fmiditrack trackhdr;
   4.347 +  int i;
   4.348 +
   4.349 +  if (!mididata)
   4.350 +    return;
   4.351 +  if (!fp)
   4.352 +    return;
   4.353 +  fread(&midihdr,1,14,fp);
   4.354 +  mididata->divisions=BE_SHORT(midihdr.deltatime);
   4.355 +  for (i=0;i<BE_SHORT(midihdr.tracks);i++)
   4.356 +  {
   4.357 +    fread(&trackhdr,1,8,fp);
   4.358 +    mididata->track[i].len=BE_LONG(trackhdr.size);
   4.359 +    mididata->track[i].data=malloc(mididata->track[i].len);
   4.360 +    if (mididata->track[i].data)
   4.361 +      fread(mididata->track[i].data,1,mididata->track[i].len,fp);
   4.362 +  }
   4.363 +  mididata->loaded=1;
   4.364 +}
   4.365 +
   4.366 +NativeMidiSong *native_midi_loadsong(char *midifile)
   4.367 +{
   4.368 +  FILE *fp;
   4.369 +  NativeMidiSong *newsong;
   4.370 +
   4.371 +  newsong=malloc(sizeof(NativeMidiSong));
   4.372 +  if (!newsong)
   4.373 +    return NULL;
   4.374 +  memset(newsong,0,sizeof(NativeMidiSong));
   4.375 +  /* Open the file */
   4.376 +  fp = fopen(midifile, "rb");
   4.377 +  if ( fp != NULL )
   4.378 +  {
   4.379 +    newsong->mididata.loaded=0;
   4.380 +    load_mididata(&newsong->mididata, fp);
   4.381 +    if (!newsong->mididata.loaded)
   4.382 +    {
   4.383 +      free(newsong);
   4.384 +      fclose(fp);
   4.385 +      return NULL;
   4.386 +    }
   4.387 +    fclose(fp);
   4.388 +  }
   4.389 +  else
   4.390 +  {
   4.391 +    free(newsong);
   4.392 +    return NULL;
   4.393 +  }
   4.394 +  MIDItoStream(newsong);
   4.395 +
   4.396 +  return newsong;
   4.397 +}
   4.398 +
   4.399 +void native_midi_freesong(NativeMidiSong *song)
   4.400 +{
   4.401 +  if (hMidiStream)
   4.402 +  {
   4.403 +    midiStreamStop(hMidiStream);
   4.404 +    midiStreamClose(hMidiStream);
   4.405 +  }
   4.406 +  if (song)
   4.407 +  {
   4.408 +    if (song->NewEvents)
   4.409 +      free(song->NewEvents);
   4.410 +    free(song);
   4.411 +  }
   4.412 +}
   4.413 +
   4.414 +void native_midi_start(NativeMidiSong *song)
   4.415 +{
   4.416 +  MMRESULT merr;
   4.417 +  MIDIPROPTIMEDIV mptd;
   4.418 +
   4.419 +  native_midi_stop();
   4.420 +  if (!hMidiStream)
   4.421 +  {
   4.422 +    merr=midiStreamOpen(&hMidiStream,&MidiDevice,1,(DWORD)&MidiProc,0,CALLBACK_FUNCTION);
   4.423 +    if (merr!=MMSYSERR_NOERROR)
   4.424 +    {
   4.425 +      hMidiStream=0;
   4.426 +      return;
   4.427 +    }
   4.428 +    //midiStreamStop(hMidiStream);
   4.429 +//    MusicLoop=looping;
   4.430 +    currentsong=song;
   4.431 +    currentsong->NewPos=0;
   4.432 +    currentsong->MusicPlaying=1;
   4.433 +    mptd.cbStruct=sizeof(MIDIPROPTIMEDIV);
   4.434 +    mptd.dwTimeDiv=currentsong->mididata.divisions;
   4.435 +    merr=midiStreamProperty(hMidiStream,(LPBYTE)&mptd,MIDIPROP_SET | MIDIPROP_TIMEDIV);
   4.436 +    BlockOut(song);
   4.437 +    merr=midiStreamRestart(hMidiStream);
   4.438 +  }
   4.439 +}
   4.440 +
   4.441 +void native_midi_stop()
   4.442 +{
   4.443 +  if (!hMidiStream)
   4.444 +    return;
   4.445 +  midiStreamStop(hMidiStream);
   4.446 +  midiStreamClose(hMidiStream);
   4.447 +  currentsong=NULL;
   4.448 +  hMidiStream = 0;
   4.449 +}
   4.450 +
   4.451 +int native_midi_active()
   4.452 +{
   4.453 +  return currentsong->MusicPlaying;
   4.454 +}
   4.455 +
   4.456 +void native_midi_setvolume(int volume)
   4.457 +{
   4.458 +}
   4.459 +
   4.460 +char *native_midi_error()
   4.461 +{
   4.462 +  return "";
   4.463 +}
   4.464 +