| [3ae31e9] | 1 | /* | 
|---|
|  | 2 | ============================================================================= | 
|---|
|  | 3 | mcapture.c -- capture MIDI data to a file | 
|---|
|  | 4 | See VERMSG below for version and date. | 
|---|
|  | 5 | ============================================================================= | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #define VERMSG  "mcapture -- Version 1.1 -- 1989-07-28 -- D.N. Lynx Crowe" | 
|---|
|  | 9 |  | 
|---|
|  | 10 | #include "stdio.h" | 
|---|
|  | 11 | #include "osbind.h" | 
|---|
|  | 12 | #include "stddefs.h" | 
|---|
|  | 13 |  | 
|---|
|  | 14 | #define void    int | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #define DFLTFILE        "mcapture.out" | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #define MIDI 2                          /* device number */ | 
|---|
|  | 19 |  | 
|---|
|  | 20 | #define MAXNOL  2 | 
|---|
|  | 21 |  | 
|---|
|  | 22 | #define FORMAT1         "\n%5u  %02.2xH  %-16.16s" | 
|---|
|  | 23 | #define FORMAT2         "  %02.2xH %3u %-10.10s" | 
|---|
|  | 24 | #define FORMAT3         "\n%5u                       " | 
|---|
|  | 25 |  | 
|---|
|  | 26 | extern char     *malloc(); | 
|---|
|  | 27 |  | 
|---|
|  | 28 | void    cleanbf(); | 
|---|
|  | 29 |  | 
|---|
|  | 30 | int     SetMBuf(), midi_in(), m_stat(); | 
|---|
|  | 31 |  | 
|---|
|  | 32 | struct iorec {          /* structure for MIDI buffer description */ | 
|---|
|  | 33 |  | 
|---|
|  | 34 | char *ibuf; | 
|---|
|  | 35 | short ibufsz; | 
|---|
|  | 36 | short ibufhd; | 
|---|
|  | 37 | short ibuftl; | 
|---|
|  | 38 | short ibuflo; | 
|---|
|  | 39 | short ibufhi; | 
|---|
|  | 40 | }; | 
|---|
|  | 41 |  | 
|---|
|  | 42 | unsigned int    index;          /* MIDI input byte number */ | 
|---|
|  | 43 |  | 
|---|
|  | 44 | int     nol;                    /* number of MIDI data bytes on the line */ | 
|---|
|  | 45 |  | 
|---|
|  | 46 | FILE    *ofp;                   /* output file pointer */ | 
|---|
|  | 47 |  | 
|---|
|  | 48 | char    *newbuf; | 
|---|
|  | 49 | char    *oldbuf;                /* old MIDI buffer pointer */ | 
|---|
|  | 50 | short   oldbsz; | 
|---|
|  | 51 | short   oldbhi; | 
|---|
|  | 52 | short   oldblo; | 
|---|
|  | 53 |  | 
|---|
|  | 54 | struct iorec    *m_buff;        /* MIDI iorec pointer */ | 
|---|
|  | 55 |  | 
|---|
|  | 56 | /* | 
|---|
|  | 57 |  | 
|---|
|  | 58 | */ | 
|---|
|  | 59 |  | 
|---|
|  | 60 | /*           FUNCTIONS          */ | 
|---|
|  | 61 |  | 
|---|
|  | 62 | /* set up MIDI buffer */ | 
|---|
|  | 63 |  | 
|---|
|  | 64 | int | 
|---|
|  | 65 | SetMBuf() | 
|---|
|  | 66 | { | 
|---|
|  | 67 | unsigned short size; | 
|---|
|  | 68 |  | 
|---|
|  | 69 | size = 16384;   /* 16K MIDI buffer */ | 
|---|
|  | 70 |  | 
|---|
|  | 71 | m_buff = (struct iorec *)Iorec(MIDI); /* pointer to buffer descriptor */ | 
|---|
|  | 72 |  | 
|---|
|  | 73 | oldbuf = m_buff->ibuf; | 
|---|
|  | 74 | oldbsz = m_buff->ibufsz; | 
|---|
|  | 75 | oldbhi = m_buff->ibufhi; | 
|---|
|  | 76 | oldblo = m_buff->ibuflo; | 
|---|
|  | 77 |  | 
|---|
|  | 78 | if ((char *)NULL EQ (newbuf = (char *)malloc(size))) { | 
|---|
|  | 79 |  | 
|---|
|  | 80 | printf ("ERROR -- unable to allocate MIDI buffer.\n"); | 
|---|
|  | 81 | return(FAILURE); | 
|---|
|  | 82 | } | 
|---|
|  | 83 |  | 
|---|
|  | 84 | /* clear out the buffer */ | 
|---|
|  | 85 |  | 
|---|
|  | 86 | m_buff->ibufhd = 0;             /* reset the head index */ | 
|---|
|  | 87 | m_buff->ibuftl = 0;             /* reset the tail index */ | 
|---|
|  | 88 |  | 
|---|
|  | 89 | /* we do this twice because we aren't disabling interrupts ... */ | 
|---|
|  | 90 |  | 
|---|
|  | 91 | m_buff->ibufhd = 0;             /* reset the head index */ | 
|---|
|  | 92 | m_buff->ibuftl = 0;             /* reset the tail index */ | 
|---|
|  | 93 |  | 
|---|
|  | 94 | m_buff->ibuf   = newbuf;        /* change address of buffer */ | 
|---|
|  | 95 | m_buff->ibufsz = size;          /* change size of buffer */ | 
|---|
|  | 96 |  | 
|---|
|  | 97 | index = 0;                      /* reset the byte index */ | 
|---|
|  | 98 |  | 
|---|
|  | 99 | return(SUCCESS); | 
|---|
|  | 100 | } | 
|---|
|  | 101 |  | 
|---|
|  | 102 | /* get MIDI byte */ | 
|---|
|  | 103 |  | 
|---|
|  | 104 | int | 
|---|
|  | 105 | midi_in() | 
|---|
|  | 106 | { | 
|---|
|  | 107 | return((int)Bconin(3) & 0x00ff); | 
|---|
|  | 108 | } | 
|---|
|  | 109 |  | 
|---|
|  | 110 | /* check midi status */ | 
|---|
|  | 111 |  | 
|---|
|  | 112 | int | 
|---|
|  | 113 | m_stat() | 
|---|
|  | 114 | { | 
|---|
|  | 115 | return((int)Bconstat(3) ? TRUE : FALSE); | 
|---|
|  | 116 | } | 
|---|
|  | 117 |  | 
|---|
|  | 118 | /* clean out MIDI buffer */ | 
|---|
|  | 119 |  | 
|---|
|  | 120 | void | 
|---|
|  | 121 | cleanbf() | 
|---|
|  | 122 | { | 
|---|
|  | 123 | int mstat; | 
|---|
|  | 124 |  | 
|---|
|  | 125 | printf("Clearing MIDI input buffer ...\n"); | 
|---|
|  | 126 |  | 
|---|
|  | 127 | /* clear out the buffer by resetting the head and tail indices */ | 
|---|
|  | 128 |  | 
|---|
|  | 129 | m_buff->ibufhd = 0;             /* reset the head index */ | 
|---|
|  | 130 | m_buff->ibuftl = 0;             /* reset the tail index */ | 
|---|
|  | 131 |  | 
|---|
|  | 132 | /* we do this twice because we aren't disabling interrupts ... */ | 
|---|
|  | 133 |  | 
|---|
|  | 134 | m_buff->ibufhd = 0;             /* reset the head index */ | 
|---|
|  | 135 | m_buff->ibuftl = 0;             /* reset the tail index */ | 
|---|
|  | 136 |  | 
|---|
|  | 137 | /* make sure it's really drained */ | 
|---|
|  | 138 |  | 
|---|
|  | 139 | mstat = m_stat(); | 
|---|
|  | 140 |  | 
|---|
|  | 141 | while (mstat) { | 
|---|
|  | 142 |  | 
|---|
|  | 143 | midi_in(); | 
|---|
|  | 144 | mstat = m_stat(); | 
|---|
|  | 145 | } | 
|---|
|  | 146 |  | 
|---|
|  | 147 | index = 0; | 
|---|
|  | 148 | } | 
|---|
|  | 149 |  | 
|---|
|  | 150 | /* | 
|---|
|  | 151 |  | 
|---|
|  | 152 | */ | 
|---|
|  | 153 |  | 
|---|
|  | 154 | /*            MAIN PROGRAM LOOP      */ | 
|---|
|  | 155 |  | 
|---|
|  | 156 | main(argc, argv) | 
|---|
|  | 157 | int argc; | 
|---|
|  | 158 | char *argv[]; | 
|---|
|  | 159 | { | 
|---|
|  | 160 | int ch, runtag; | 
|---|
|  | 161 |  | 
|---|
|  | 162 | ofp = (FILE *)NULL; | 
|---|
|  | 163 |  | 
|---|
|  | 164 | printf("\033E%s\n\n", VERMSG); | 
|---|
|  | 165 | printf("Hit / to clear buffer, ESC to finish.\n\n"); | 
|---|
|  | 166 |  | 
|---|
|  | 167 | if (SetMBuf())          /* move MIDI buffer & increase size */ | 
|---|
|  | 168 | exit(2); | 
|---|
|  | 169 |  | 
|---|
|  | 170 | cleanbf();              /* clear out MIDI buffer */ | 
|---|
|  | 171 |  | 
|---|
|  | 172 | if (argc EQ 2) { | 
|---|
|  | 173 |  | 
|---|
|  | 174 | if ((FILE *)NULL EQ (ofp = fopen(argv[1], "w"))) { | 
|---|
|  | 175 |  | 
|---|
|  | 176 | printf("ERROR -- Unable to open \"%s\" for output.\n", | 
|---|
|  | 177 | argv[1]); | 
|---|
|  | 178 |  | 
|---|
|  | 179 | exit(2); | 
|---|
|  | 180 |  | 
|---|
|  | 181 | } else { | 
|---|
|  | 182 |  | 
|---|
|  | 183 | printf("Program will output to file \"%s\".\n", argv[1]); | 
|---|
|  | 184 | } | 
|---|
|  | 185 |  | 
|---|
|  | 186 | } else { | 
|---|
|  | 187 |  | 
|---|
|  | 188 | if ((FILE *)NULL EQ (ofp = fopen(DFLTFILE, "w"))) { | 
|---|
|  | 189 |  | 
|---|
|  | 190 | printf("ERROR -- Unable to open \"%s\" for output.\n", | 
|---|
|  | 191 | DFLTFILE); | 
|---|
|  | 192 |  | 
|---|
|  | 193 | exit(2); | 
|---|
|  | 194 |  | 
|---|
|  | 195 | } else { | 
|---|
|  | 196 |  | 
|---|
|  | 197 | printf("Program will output to file \"%s\".\n", DFLTFILE); | 
|---|
|  | 198 | } | 
|---|
|  | 199 | } | 
|---|
|  | 200 |  | 
|---|
|  | 201 | printf("Ready for MIDI data.\n"); | 
|---|
|  | 202 |  | 
|---|
|  | 203 | runtag = TRUE; | 
|---|
|  | 204 |  | 
|---|
|  | 205 | while (runtag) { | 
|---|
|  | 206 |  | 
|---|
|  | 207 | if (Bconstat(2)) { | 
|---|
|  | 208 |  | 
|---|
|  | 209 | ch = 0x00FF & Bconin(2); | 
|---|
|  | 210 |  | 
|---|
|  | 211 | switch (ch) { | 
|---|
|  | 212 |  | 
|---|
|  | 213 | case '\033':    /* escape */ | 
|---|
|  | 214 |  | 
|---|
|  | 215 | runtag = FALSE; | 
|---|
|  | 216 | break; | 
|---|
|  | 217 |  | 
|---|
|  | 218 | case '/':       /* / = clear buffer and screen */ | 
|---|
|  | 219 |  | 
|---|
|  | 220 | cleanbf(); | 
|---|
|  | 221 | printf("\033E"); | 
|---|
|  | 222 | printf("Ready for MIDI data.\n"); | 
|---|
|  | 223 | break; | 
|---|
|  | 224 | } | 
|---|
|  | 225 | } | 
|---|
|  | 226 |  | 
|---|
|  | 227 | if (m_stat()) | 
|---|
|  | 228 | ProcMIDI(midi_in()); | 
|---|
|  | 229 | } | 
|---|
|  | 230 |  | 
|---|
|  | 231 | fflush(ofp); | 
|---|
|  | 232 | fclose(ofp); | 
|---|
|  | 233 |  | 
|---|
|  | 234 | /* clear out the buffer */ | 
|---|
|  | 235 |  | 
|---|
|  | 236 | m_buff->ibufhd = 0;             /* reset the head index */ | 
|---|
|  | 237 | m_buff->ibuftl = 0;             /* reset the tail index */ | 
|---|
|  | 238 |  | 
|---|
|  | 239 | /* we do this twice because we aren't disabling interrupts ... */ | 
|---|
|  | 240 |  | 
|---|
|  | 241 | m_buff->ibufhd = 0;             /* reset the head index */ | 
|---|
|  | 242 | m_buff->ibuftl = 0;             /* reset the tail index */ | 
|---|
|  | 243 |  | 
|---|
|  | 244 | m_buff->ibufsz = oldbsz;        /* restore the old buffer size */ | 
|---|
|  | 245 | m_buff->ibuf   = oldbuf;        /* restore the old buffer address */ | 
|---|
|  | 246 |  | 
|---|
|  | 247 | free(newbuf);                   /* give back the big MIDI buffer */ | 
|---|
|  | 248 |  | 
|---|
|  | 249 | printf("\n"); | 
|---|
|  | 250 | fflush(stdout); | 
|---|
|  | 251 | exit(0); | 
|---|
|  | 252 | } | 
|---|