From a43384d92a3e9663b77c4d828f09fa5c68651de2 Mon Sep 17 00:00:00 2001 From: zicodxx <> Date: Fri, 5 May 2006 14:18:42 +0000 Subject: [PATCH] midi repeat improvements --- arch/win32/hmpfile.c | 53 ++++++++++++++++++++++-------------- arch/win32/include/hmpfile.h | 10 +++++-- arch/win32/midi.c | 2 +- main/songs.c | 3 ++ 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/arch/win32/hmpfile.c b/arch/win32/hmpfile.c index 2c61fef51..807adc365 100755 --- a/arch/win32/hmpfile.c +++ b/arch/win32/hmpfile.c @@ -107,7 +107,7 @@ void hmp_stop(hmp_file *hmp) { hmp->evbuf = mhdr->lpNext; free(mhdr); } - + if (hmp->hmidi) { midiStreamClose(hmp->hmidi); hmp->hmidi = NULL; @@ -214,7 +214,7 @@ static int get_event(hmp_file *hmp, event *ev) { } else if (ev_num == 0xff) { ev->msg[1] = *(trk->cur++); trk->left--; - if (!(got = get_var_num(ev->data = trk->cur, + if (!(got = get_var_num(ev->data = trk->cur, trk->left, (unsigned long *)&ev->datalen))) return HMP_INVALID_FILE; trk->cur += ev->datalen; @@ -285,15 +285,20 @@ static int setup_buffers(hmp_file *hmp) { return 0; } -static void reset_tracks(struct hmp_file *hmp) { +static void reset_tracks(struct hmp_file *hmp) +{ int i; for (i = 0; i < hmp->num_trks; i++) { - hmp->trks[i].cur = hmp->trks[i].data; - hmp->trks[i].left = hmp->trks[i].len; + hmp->trks [i].cur = hmp->trks [i].data; + hmp->trks [i].left = hmp->trks [i].len; + hmp->trks [i].cur_time = 0; } + hmp->cur_time=0; } +extern int loop; + static void _stdcall midi_callback(HMIDISTRM hms, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) { MIDIHDR *mhdr; hmp_file *hmp; @@ -301,7 +306,7 @@ static void _stdcall midi_callback(HMIDISTRM hms, UINT uMsg, DWORD dwUser, DWORD if (uMsg != MOM_DONE) return; - + mhdr = ((MIDIHDR *)dw1); mhdr->dwBytesRecorded = 0; hmp = (hmp_file *)(mhdr->dwUser); @@ -310,9 +315,15 @@ static void _stdcall midi_callback(HMIDISTRM hms, UINT uMsg, DWORD dwUser, DWORD hmp->bufs_in_mm--; if (!hmp->stop) { - while (fill_buffer(hmp) == HMP_EOF) + while (fill_buffer(hmp) == HMP_EOF) { + if (loop) + hmp->stop = 0; + else + hmp->stop = 1; + reset_tracks(hmp); - if ((rc = midiStreamOut(hmp->hmidi, hmp->evbuf, + } + if ((rc = midiStreamOut(hmp->hmidi, hmp->evbuf, sizeof(MIDIHDR))) != MMSYSERR_NOERROR) { /* ??? */ } else { @@ -320,7 +331,6 @@ static void _stdcall midi_callback(HMIDISTRM hms, UINT uMsg, DWORD dwUser, DWORD hmp->bufs_in_mm++; } } - } static void setup_tempo(hmp_file *hmp, unsigned long tempo) { @@ -332,12 +342,14 @@ static void setup_tempo(hmp_file *hmp, unsigned long tempo) { mhdr->dwBytesRecorded += 12; } -int hmp_play(hmp_file *hmp) { + +int hmp_play(hmp_file *hmp, int bLoop) +{ int rc; MIDIPROPTIMEDIV mptd; -#if 0 +#if 1 unsigned int numdevs; - int i=0; + int i=0; // ZICO - LOOP HERE numdevs=midiOutGetNumDevs(); hmp->devid=-1; @@ -346,17 +358,19 @@ int hmp_play(hmp_file *hmp) { MIDIOUTCAPS devcaps; midiOutGetDevCaps(i,&devcaps,sizeof(MIDIOUTCAPS)); if ((devcaps.wTechnology==MOD_FMSYNTH) || (devcaps.wTechnology==MOD_SYNTH)) +// if ((devcaps.dwSupport & (MIDICAPS_VOLUME | MIDICAPS_STREAM)) == (MIDICAPS_VOLUME | MIDICAPS_STREAM)) hmp->devid=i; i++; } while ((i<(int)numdevs) && (hmp->devid==-1)); -#else - hmp->devid = MIDI_MAPPER; + if (hmp->devid == -1) #endif + hmp->bLoop = bLoop; + hmp->devid = MIDI_MAPPER; if ((rc = setup_buffers(hmp))) return rc; - if ((midiStreamOpen(&hmp->hmidi, &hmp->devid,1,(DWORD)midi_callback, - 0, CALLBACK_FUNCTION)) != MMSYSERR_NOERROR) { + if ((midiStreamOpen(&hmp->hmidi, &hmp->devid,1, (DWORD) (size_t) midi_callback, + 0, CALLBACK_FUNCTION)) != MMSYSERR_NOERROR) { hmp->hmidi = NULL; return HMP_MM_ERR; } @@ -382,15 +396,15 @@ int hmp_play(hmp_file *hmp) { return rc; } #if 0 - { FILE *f = fopen("dump","wb"); fwrite(hmp->evbuf->lpData, + { FILE *f = fopen("dump","wb"); fwrite(hmp->evbuf->lpData, hmp->evbuf->dwBytesRecorded,1,f); fclose(f); exit(1);} #endif - if ((rc = midiOutPrepareHeader((HMIDIOUT)hmp->hmidi, hmp->evbuf, + if ((rc = midiOutPrepareHeader((HMIDIOUT)hmp->hmidi, hmp->evbuf, sizeof(MIDIHDR))) != MMSYSERR_NOERROR) { /* FIXME: cleanup... */ return HMP_MM_ERR; } - if ((rc = midiStreamOut(hmp->hmidi, hmp->evbuf, + if ((rc = midiStreamOut(hmp->hmidi, hmp->evbuf, sizeof(MIDIHDR))) != MMSYSERR_NOERROR) { /* FIXME: cleanup... */ return HMP_MM_ERR; @@ -401,4 +415,3 @@ int hmp_play(hmp_file *hmp) { midiStreamRestart(hmp->hmidi); return 0; } - diff --git a/arch/win32/include/hmpfile.h b/arch/win32/include/hmpfile.h index d6e6cab35..eb3984237 100755 --- a/arch/win32/include/hmpfile.h +++ b/arch/win32/include/hmpfile.h @@ -6,6 +6,8 @@ #define HMP_TRACKS 32 #define HMP_BUFFERS 4 #define HMP_BUFSIZE 1024 +#define BE_INT(x) (x) +#define BE_SHORT(x) (x) typedef struct event { unsigned int delta; @@ -33,8 +35,10 @@ typedef struct hmp_file { unsigned char *pending; unsigned int pending_size; unsigned int pending_event; - int stop; /* 1 -> don't send more data */ - int bufs_in_mm; /* number of queued buffers */ + int stop; + int bufs_in_mm; + int bLoop; + unsigned int midi_division; } hmp_file; @@ -44,7 +48,7 @@ typedef struct hmp_file { #define HMP_EOF 1 hmp_file *hmp_open(const char *filename); -int hmp_play(hmp_file *hmp); +int hmp_play(hmp_file *hmp, int bLoop); void hmp_close(hmp_file *hmp); #endif diff --git a/arch/win32/midi.c b/arch/win32/midi.c index 020236d20..8edfdcaee 100755 --- a/arch/win32/midi.c +++ b/arch/win32/midi.c @@ -60,7 +60,7 @@ void digi_play_midi_song(char *filename, char *melodic_bank, char *drum_bank, in if ((hmp = hmp_open(filename))) { - hmp_play(hmp); + hmp_play(hmp,loop); digi_midi_song_playing = 1; digi_set_midi_volume(midi_volume); } diff --git a/main/songs.c b/main/songs.c index 2950508ea..b1f01c254 100755 --- a/main/songs.c +++ b/main/songs.c @@ -304,6 +304,7 @@ int songs_haved2_cd() } #endif +int loop; void songs_play_song( int songnum, int repeat ) { @@ -338,6 +339,8 @@ void songs_play_song( int songnum, int repeat ) digi_play_midi_song(songnum, repeat); #endif } + + loop = repeat; } int current_song_level;