home *** CD-ROM | disk | FTP | other *** search
/ HOT Scene Stuff / hotscenestuffzyklop1996.iso / demos / sunknown / midihack.c < prev    next >
C/C++ Source or Header  |  1994-04-07  |  5KB  |  210 lines

  1. /*
  2. ** Copyright (c) 1992 Forte.  All rights reserved.
  3. ** Written late one night 6/27/93.
  4. **
  5. ** Note from Five, And Then Some:
  6. **   This file was originally included in the ultramid-package (or the
  7. **   Software Development Kit for the Gravis Ultrasound).
  8. **
  9. ** Additional programming by Ronny H.
  10. **
  11. */
  12.  
  13.  
  14. //#define DEBUG
  15. #define TRUE 1
  16. #define FALSE 0
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22. #include <conio.h>
  23. #include <dir.h>
  24. #include <dos.h>
  25.  
  26. /* midi files */
  27. #include "global.h"
  28. #include "midi.h"
  29. #include "totsr.h"
  30. #include "midihack.h"
  31.  
  32.  
  33. void midi_do_something(struct MIDILIB *ml, unsigned long now_time);
  34. unsigned long midi_getvarinum(struct track *track);
  35.  
  36.  
  37. struct header_type header;    /* format of midifile, # of tracks, and time */
  38. int midi_libs=0;
  39. int control_break = FALSE;
  40. FILE *fpIn=NULL;    /* file pointer */
  41. struct MIDILIB *ml;
  42. int i, j, ret;
  43. char *cp;
  44. int not_done_playing=1;
  45.  
  46. /* callback routine to read a character from the midi file */
  47. mygetc() { return(getc(fpIn)); }
  48.  
  49. /* callback routine for the midi library to print error messages */
  50. void myerror(s)
  51. char *s;
  52. {
  53.     midi_error("Error: %s\n", s);
  54. }
  55.  
  56. /* callback routine for the midi library to record header information */
  57. void myheader(format,ntrks,division)
  58. int format,ntrks,division;
  59. {
  60.     header.format = format;
  61.     header.ntrks = ntrks;
  62.     header.division.quarter_note = division;
  63. }
  64.  
  65. void mytrack(struct MIDILIB *ml, struct track *track)
  66. {
  67.     struct track *tp;
  68.  
  69.     if (ml->Mp_tracks) {
  70.         for (tp=ml->Mp_tracks; tp->next_track; tp = tp->next_track) ;
  71.         tp->next_track = track;
  72.     } else {
  73.         ml->Mp_tracks = track;
  74.     }
  75. }
  76.  
  77. /*
  78. ** This routine initalizes the midifile and midiplay libraries and
  79. ** sets up all of the callback functions
  80. */
  81. void init_funcs(struct MIDILIB *ml)
  82. {
  83.     int i;
  84.     int bm;
  85.  
  86.     ml->Mf_getc = mygetc;
  87.     ml->Mf_header = myheader;
  88.     ml->Mp_set_tempo = um_set_tempo;
  89.     ml->Mp_reset_tick_counter = um_reset_tick_counter;
  90.     ml->Mp_wait_for = um_wait_for;
  91.     ml->Mp_init_timers = um_init_timers;
  92.     ml->Mp_cleanup_timers = um_cleanup_timers;
  93.     ml->Mp_reset_midi = reset_um;
  94.     ml->Mp_note_on = um_note_on;
  95.     ml->Mp_note_off = um_note_off;
  96.     ml->Mp_pressure = um_pressure;
  97.     ml->Mp_parameter = um_parameter;
  98.     ml->Mp_pitch_bend = um_pitch_bend;
  99.     ml->Mp_program = um_program;
  100.     ml->Mp_chanpressure = um_chanpressure;
  101.     ml->Mp_init_hardware = um_init_hardware;
  102.     ml->Mp_cleanup = um_cleanup;
  103. }
  104.  
  105. void midi_error(char *fmt, ...)
  106. {
  107.     char buffer[512];
  108.     register int i;
  109.     va_list argptr;
  110.     int cnt;
  111.  
  112.     va_start(argptr, fmt);
  113.     cnt = vsprintf(buffer, fmt, argptr);
  114.     if (cnt != EOF) {
  115.         puts(buffer);
  116.     }
  117.     va_end(argptr);
  118. }
  119.  
  120. void midi_message(char *fmt, ...)
  121. {
  122.     char buffer[512];
  123.     register int i;
  124.     va_list argptr;
  125.     int cnt;
  126.  
  127.     va_start(argptr, fmt);
  128.     cnt = vsprintf(buffer, fmt, argptr);
  129.     if (cnt != EOF) {
  130.         puts(buffer);
  131.     }
  132.     va_end(argptr);
  133. }
  134.  
  135. void smidistart(void)
  136. {
  137.     struct track *track;
  138.     unsigned long next_event_time;
  139.     int i, iret, c;
  140.  
  141.   /* try and open the midifile */
  142.   fpIn = fopen(MIDIFILE, "rb");
  143.   if (fpIn == NULL) {
  144. #ifdef DEBUG
  145.       fprintf(stderr,"%s: unable to open file %s for reading.\n","smidistart()", MIDIFILE);
  146. #endif
  147.       exit(1);
  148.   }
  149.   ml = mfinit(myerror);
  150.   (void) init_funcs(ml); /* intialize midi libraries */
  151.   mfread(ml);         /* read midi file */
  152.   fclose(fpIn);
  153.  
  154.     not_done_playing = 1;
  155.     /* initialize track playback info */
  156.     next_event_time = -1L;
  157.     for (track=ml->Mp_tracks; track; track = track->next_track) {
  158.         track->playp = track->data;
  159.         track->position = 0L;
  160.         track->status = 0;
  161.         track->play_state = MPS_PLAYING;
  162.             track->play_time = midi_getvarinum(track);
  163.         if (track->play_time < next_event_time) {
  164.         next_event_time = track->play_time;
  165.         }
  166.     }
  167.     ml->Mp_next_event_time = next_event_time;
  168.     if (ml->Mp_set_tempo && header.division.quarter_note < 0) {
  169.         (*ml->Mp_set_tempo)(0, header.division.quarter_note);
  170.     }
  171.     if (ml->Mp_wait_for == 0L) return;
  172.     if (ml->Mp_reset_tick_counter) (*ml->Mp_reset_tick_counter)();
  173.     if (ml->Mp_reset_midi) (*ml->Mp_reset_midi)();
  174.     if (ml->Mp_init_hardware) {
  175.         iret = (*ml->Mp_init_hardware)(ml);
  176.         if (iret != 0) {
  177.         if (ml->Mp_cleanup) (*ml->Mp_cleanup)();
  178.         return;
  179.         }
  180.     }
  181. #ifdef DEBUG
  182.         midi_message("Now playing");
  183. #endif
  184.         if (ml->Mp_init_timers) {
  185.         (*ml->Mp_init_timers)();
  186.     }
  187.     not_done_playing = 1;
  188.  
  189. }
  190.  
  191. void smidireset(void)
  192. {
  193.   if (ml->Mp_cleanup_timers)
  194.     (*ml->Mp_cleanup_timers)();
  195.  
  196.     if (ml->Mp_cleanup)
  197.     (*ml->Mp_cleanup)();
  198.  
  199.   mfcleanup(ml);       // close the midifile library
  200.  
  201.   mfclose(&ml);           // close the midi libraries
  202. }
  203.  
  204. void smidigo(void)
  205. {
  206.   if (!((*ml->Mp_wait_for)(ml->Mp_next_event_time)))
  207.     midi_do_something(ml, ml->Mp_next_event_time);
  208. }
  209.  
  210.