[3ae31e9] | 1 | /*
|
---|
| 2 | =============================================================================
|
---|
| 3 | midcap.c -- capture and display MIDI data
|
---|
| 4 | See VERMSG below for version and date.
|
---|
| 5 | =============================================================================
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #define VERMSG "midcap -- Version 1.06 -- 1989-12-12 -- 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 MBUFSZ (unsigned short)(31*1024)
|
---|
| 17 |
|
---|
| 18 | #define SNAPFILE "MIDCAP.DAT" /* snap file name */
|
---|
| 19 | #define MAXNOL 16 /* max number of data bytes on a line */
|
---|
| 20 |
|
---|
| 21 | #define FORMAT1 "\n%5u %02.02x "
|
---|
| 22 | #define FORMAT2 "%02.02x "
|
---|
| 23 | #define FORMAT3 "\n%5u "
|
---|
| 24 |
|
---|
| 25 | struct iorec { /* structure for MIDI buffer description */
|
---|
| 26 |
|
---|
| 27 | char *ibuf; /* base address of buffer */
|
---|
| 28 | short ibufsz; /* buffer size in bytes */
|
---|
| 29 | short ibufhd; /* head index */
|
---|
| 30 | short ibuftl; /* tail index */
|
---|
| 31 | short ibuflo; /* low water mark index */
|
---|
| 32 | short ibufhi; /* high water mark index */
|
---|
| 33 | };
|
---|
| 34 |
|
---|
| 35 | extern char *malloc();
|
---|
| 36 |
|
---|
| 37 | void PrMIDI(), cleanbf();
|
---|
| 38 |
|
---|
| 39 | int SetMBuf(), midi_in(), m_stat();
|
---|
| 40 |
|
---|
| 41 | FILE *ofp; /* output file pointer */
|
---|
| 42 | FILE *sfp; /* snap file pointer */
|
---|
| 43 |
|
---|
| 44 | char *newbuf; /* expanded MIDI buffer pointer */
|
---|
| 45 | char *oldbuf; /* old MIDI buffer pointer */
|
---|
| 46 |
|
---|
| 47 | unsigned short indx; /* MIDI input byte number */
|
---|
| 48 |
|
---|
| 49 | short feseen; /* MIDI active sensing seen */
|
---|
| 50 | short nol; /* number of MIDI data bytes on the line */
|
---|
| 51 | short oldbsz; /* old MIDI buffer size */
|
---|
| 52 | short oldbhi; /* old MIDI buffer high water mark */
|
---|
| 53 | short oldblo; /* old MIDI buffer low water mark */
|
---|
| 54 |
|
---|
| 55 | struct iorec *m_buff; /* MIDI iorec pointer */
|
---|
| 56 |
|
---|
| 57 | /* |
---|
| 58 |
|
---|
| 59 | */
|
---|
| 60 |
|
---|
| 61 | /*
|
---|
| 62 | =============================================================================
|
---|
| 63 | SetMBuf() -- set up MIDI buffer
|
---|
| 64 | =============================================================================
|
---|
| 65 | */
|
---|
| 66 |
|
---|
| 67 | int
|
---|
| 68 | SetMBuf()
|
---|
| 69 | {
|
---|
| 70 | unsigned short size;
|
---|
| 71 |
|
---|
| 72 | size = MBUFSZ; /* MIDI buffer */
|
---|
| 73 |
|
---|
| 74 | m_buff = (struct iorec *)Iorec(2); /* pointer to buffer descriptor */
|
---|
| 75 |
|
---|
| 76 | oldbuf = m_buff->ibuf;
|
---|
| 77 | oldbsz = m_buff->ibufsz;
|
---|
| 78 | oldbhi = m_buff->ibufhi;
|
---|
| 79 | oldblo = m_buff->ibuflo;
|
---|
| 80 |
|
---|
| 81 | if ((char *)NULL EQ (newbuf = (char *)malloc(size))) {
|
---|
| 82 |
|
---|
| 83 | printf ("ERROR -- unable to allocate MIDI buffer.\n");
|
---|
| 84 | return(FAILURE);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /* clear out the buffer */
|
---|
| 88 |
|
---|
| 89 | m_buff->ibufhd = 0; /* reset the head index */
|
---|
| 90 | m_buff->ibuftl = 0; /* reset the tail index */
|
---|
| 91 |
|
---|
| 92 | /* we do this twice because we aren't disabling interrupts ... */
|
---|
| 93 |
|
---|
| 94 | m_buff->ibufhd = 0; /* reset the head index */
|
---|
| 95 | m_buff->ibuftl = 0; /* reset the tail index */
|
---|
| 96 |
|
---|
| 97 | m_buff->ibuf = newbuf; /* change address of buffer */
|
---|
| 98 | m_buff->ibufsz = size; /* change size of buffer */
|
---|
| 99 |
|
---|
| 100 | indx = 0; /* reset the byte index */
|
---|
| 101 |
|
---|
| 102 | return(SUCCESS);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /*
|
---|
| 106 | =============================================================================
|
---|
| 107 | midi_in() -- get MIDI byte
|
---|
| 108 | =============================================================================
|
---|
| 109 | */
|
---|
| 110 |
|
---|
| 111 | int
|
---|
| 112 | midi_in()
|
---|
| 113 | {
|
---|
| 114 | return((int)Bconin(3) & 0x00FF);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 |
|
---|
| 118 | /*
|
---|
| 119 | =============================================================================
|
---|
| 120 | PrMIDI() -- print a MIDI data byte
|
---|
| 121 | =============================================================================
|
---|
| 122 | */
|
---|
| 123 |
|
---|
| 124 | void
|
---|
| 125 | PrMIDI(M_Byte)
|
---|
| 126 | unsigned int M_Byte;
|
---|
| 127 | {
|
---|
| 128 | if ((0x00FF & M_Byte) EQ 0x00FE) {
|
---|
| 129 |
|
---|
| 130 | if (NOT feseen) {
|
---|
| 131 |
|
---|
| 132 | printf("\nActive sense is active\n");
|
---|
| 133 |
|
---|
| 134 | if ((FILE *)NULL NE ofp)
|
---|
| 135 | fprintf(ofp, "\nActive sense is active\n");
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | nol = 0;
|
---|
| 139 | feseen = TRUE;
|
---|
| 140 |
|
---|
| 141 | } else {
|
---|
| 142 |
|
---|
| 143 | ++indx;
|
---|
| 144 |
|
---|
| 145 | if (0x0080 & M_Byte) { /* new status byte */
|
---|
| 146 |
|
---|
| 147 | printf(FORMAT1, indx, M_Byte);
|
---|
| 148 | nol = 0;
|
---|
| 149 |
|
---|
| 150 | if ((FILE *)NULL NE ofp)
|
---|
| 151 | fprintf(ofp, FORMAT1, indx, M_Byte);
|
---|
| 152 |
|
---|
| 153 | } else { /* data byte */
|
---|
| 154 |
|
---|
| 155 | if (++nol > MAXNOL) {
|
---|
| 156 |
|
---|
| 157 | printf(FORMAT3, indx);
|
---|
| 158 | nol = 1;
|
---|
| 159 |
|
---|
| 160 | if ((FILE *)NULL NE ofp)
|
---|
| 161 | fprintf(ofp, FORMAT3, indx);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | printf(FORMAT2, M_Byte);
|
---|
| 165 |
|
---|
| 166 | if ((FILE *)NULL NE ofp)
|
---|
| 167 | fprintf(ofp, FORMAT2, M_Byte);
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | fflush(stdout);
|
---|
| 172 |
|
---|
| 173 | if ((FILE *)NULL NE ofp)
|
---|
| 174 | fflush(ofp);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | /*
|
---|
| 179 | =============================================================================
|
---|
| 180 | m_stat() -- check midi status
|
---|
| 181 | =============================================================================
|
---|
| 182 | */
|
---|
| 183 |
|
---|
| 184 | int
|
---|
| 185 | m_stat()
|
---|
| 186 | {
|
---|
| 187 | return((int)Bconstat(3) ? TRUE : FALSE);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 | /*
|
---|
| 192 | =============================================================================
|
---|
| 193 | cleanbf() -- clean out MIDI buffer
|
---|
| 194 | =============================================================================
|
---|
| 195 | */
|
---|
| 196 |
|
---|
| 197 | void
|
---|
| 198 | cleanbf()
|
---|
| 199 | {
|
---|
| 200 | int mstat;
|
---|
| 201 |
|
---|
| 202 | printf("Clearing MIDI input buffer ...\n");
|
---|
| 203 | feseen = FALSE;
|
---|
| 204 |
|
---|
| 205 | /* clear out the buffer by resetting the head and tail indices */
|
---|
| 206 |
|
---|
| 207 | m_buff->ibufhd = 0; /* reset the head index */
|
---|
| 208 | m_buff->ibuftl = 0; /* reset the tail index */
|
---|
| 209 |
|
---|
| 210 | /* we do this twice because we aren't disabling interrupts ... */
|
---|
| 211 |
|
---|
| 212 | m_buff->ibufhd = 0; /* reset the head index */
|
---|
| 213 | m_buff->ibuftl = 0; /* reset the tail index */
|
---|
| 214 |
|
---|
| 215 | /* make sure it's really drained */
|
---|
| 216 |
|
---|
| 217 | mstat = m_stat();
|
---|
| 218 |
|
---|
| 219 | while (mstat) {
|
---|
| 220 |
|
---|
| 221 | midi_in();
|
---|
| 222 | mstat = m_stat();
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | indx = 0;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | /* |
---|
| 229 |
|
---|
| 230 | */
|
---|
| 231 |
|
---|
| 232 | /*
|
---|
| 233 | =============================================================================
|
---|
| 234 | midcap -- capture and display MIDI data
|
---|
| 235 | =============================================================================
|
---|
| 236 | */
|
---|
| 237 |
|
---|
| 238 | main(argc, argv)
|
---|
| 239 | int argc;
|
---|
| 240 | char *argv[];
|
---|
| 241 | {
|
---|
| 242 | int ch, runtag;
|
---|
| 243 |
|
---|
| 244 | ofp = (FILE *)NULL;
|
---|
| 245 |
|
---|
| 246 | printf("\033E%s\n\n", VERMSG);
|
---|
| 247 | printf("Hit ESC to quit, / to clear buffer, space to pause output.\n\n");
|
---|
| 248 |
|
---|
| 249 | if (SetMBuf()) /* move MIDI buffer & increase its size */
|
---|
| 250 | exit(2);
|
---|
| 251 |
|
---|
| 252 | printf("%u byte MIDI buffer allocated at 0x%08.8lx\n",
|
---|
| 253 | MBUFSZ, (long)newbuf);
|
---|
| 254 |
|
---|
| 255 | cleanbf(); /* clear out MIDI buffer */
|
---|
| 256 |
|
---|
| 257 | if (argc EQ 2) {
|
---|
| 258 |
|
---|
| 259 | if ((FILE *)NULL EQ (ofp = fopen(argv[1], "w"))) {
|
---|
| 260 |
|
---|
| 261 | printf("ERROR -- Unable to open \"%s\" for output.\n",
|
---|
| 262 | argv[1]);
|
---|
| 263 |
|
---|
| 264 | exit(2);
|
---|
| 265 |
|
---|
| 266 | } else {
|
---|
| 267 |
|
---|
| 268 | printf("Outputting to file \"%s\".\n", argv[1]);
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | printf("Ready for MIDI data.\n");
|
---|
| 273 | runtag = TRUE;
|
---|
| 274 |
|
---|
| 275 | while (runtag) {
|
---|
| 276 |
|
---|
| 277 | if (Bconstat(2)) {
|
---|
| 278 |
|
---|
| 279 | ch = 0x00FF & Bconin(2);
|
---|
| 280 |
|
---|
| 281 | switch (ch) {
|
---|
| 282 |
|
---|
| 283 | case '\033': /* escape */
|
---|
| 284 |
|
---|
| 285 | runtag = FALSE;
|
---|
| 286 | break;
|
---|
| 287 |
|
---|
| 288 | case ' ': /* space = pause */
|
---|
| 289 |
|
---|
| 290 | printf("PAUSED");
|
---|
| 291 | Bconin(2);
|
---|
| 292 | printf("\b\b\b\b\b\b \b\b\b\b\b\b");
|
---|
| 293 |
|
---|
| 294 | break;
|
---|
| 295 |
|
---|
| 296 | case '/': /* / = clear buffer and screen */
|
---|
| 297 |
|
---|
| 298 | cleanbf();
|
---|
| 299 | printf("\033E");
|
---|
| 300 | printf("Ready for MIDI data.\n");
|
---|
| 301 |
|
---|
| 302 | if ((FILE *)NULL NE ofp) {
|
---|
| 303 |
|
---|
| 304 | fprintf(ofp, "\n\nMIDI buffer flushed.\n\n");
|
---|
| 305 | fflush(ofp);
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | break;
|
---|
| 309 |
|
---|
| 310 | case 'w': /* w = write to SNAPFILE */
|
---|
| 311 |
|
---|
| 312 | if ((FILE *)NULL EQ (sfp = fopenb(SNAPFILE, "w"))) {
|
---|
| 313 |
|
---|
| 314 | printf("ERROR -- Unable to open \"%s\" for output.\n",
|
---|
| 315 | SNAPFILE);
|
---|
| 316 |
|
---|
| 317 | exit(2);
|
---|
| 318 |
|
---|
| 319 | } else {
|
---|
| 320 |
|
---|
| 321 | printf("\n\nOutputting to file \"%s\".\n", SNAPFILE);
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | fwrite(newbuf, indx, 1, sfp);
|
---|
| 325 |
|
---|
| 326 | fflush(sfp);
|
---|
| 327 | fclose(sfp);
|
---|
| 328 |
|
---|
| 329 | printf("\nFile written and closed.\n\n");
|
---|
| 330 | break;
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | if (m_stat())
|
---|
| 335 | PrMIDI(midi_in());
|
---|
| 336 |
|
---|
| 337 | if ((FILE *)NULL NE ofp)
|
---|
| 338 | fflush(ofp);
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | if ((FILE *)NULL NE ofp) {
|
---|
| 342 |
|
---|
| 343 | fprintf(ofp, "\n");
|
---|
| 344 | fflush(ofp);
|
---|
| 345 | fclose(ofp);
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | /* clear out the buffer */
|
---|
| 349 |
|
---|
| 350 | m_buff->ibufhd = 0; /* reset the head index */
|
---|
| 351 | m_buff->ibuftl = 0; /* reset the tail index */
|
---|
| 352 |
|
---|
| 353 | /* we do this twice because we aren't disabling interrupts ... */
|
---|
| 354 |
|
---|
| 355 | m_buff->ibufhd = 0; /* reset the head index */
|
---|
| 356 | m_buff->ibuftl = 0; /* reset the tail index */
|
---|
| 357 |
|
---|
| 358 | m_buff->ibufsz = oldbsz; /* restore the old buffer size */
|
---|
| 359 | m_buff->ibuf = oldbuf; /* restore the old buffer address */
|
---|
| 360 |
|
---|
| 361 | free(newbuf); /* give back the big MIDI buffer */
|
---|
| 362 |
|
---|
| 363 | printf("\n");
|
---|
| 364 | fflush(stdout);
|
---|
| 365 | exit(0);
|
---|
| 366 | }
|
---|