summaryrefslogtreecommitdiff
path: root/muse/grepmidi/grepmidi.cpp
blob: d5d68a4dd8dc56e39fa977ed23e6e733309143cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//=========================================================
//  MusE
//  Linux Music Editor
//  $Id: grepmidi.cpp,v 1.1.1.1 2003/10/27 18:57:02 wschweer Exp $
//
//  (C) Copyright 1999/2000 Werner Schweer (ws@seh.de)
//=========================================================

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define XCHG_SHORT(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
#ifdef __i486__
#define XCHG_LONG(x) \
     ({ int __value; \
        asm ("bswap %1; movl %1,%0" : "=g" (__value) : "r" (x)); \
       __value; })
#else
#define XCHG_LONG(x) ((((x)&0xFF)<<24) | \
		      (((x)&0xFF00)<<8) | \
		      (((x)&0xFF0000)>>8) | \
		      (((x)>>24)&0xFF))
#endif

#if __BYTE_ORDER == __LITTLE_ENDIAN
#define BE_SHORT(x) XCHG_SHORT(x)
#define BE_LONG(x) XCHG_LONG(x)
#else
#define BE_SHORT(x) x
#define BE_LONG(x) x
#endif


int cpos;
bool printName = false;
const char* curName;

//---------------------------------------------------------
//   readLong
//---------------------------------------------------------

int readLong(FILE* f)
      {
      int format;
      if (fread(&format, 4, 1, f) != 1) {
            printf("read long failed\n");
            exit(1);
            }
      cpos += 4;
      return BE_LONG(format);
      }

//---------------------------------------------------------
//   readShort
//---------------------------------------------------------

int readShort(FILE* f)
      {
      short format;
      if (fread(&format, 2, 1, f) != 1) {
            printf("read short failed\n");
            exit(1);
            }
      cpos += 2;
      return BE_SHORT(format);
      }

/*---------------------------------------------------------
 *    getvl
 *    Read variable-length number (7 bits per byte, MSB first)
 *---------------------------------------------------------*/

int getvl(FILE* f)
      {
      int l = 0;
      for (int i = 0;i < 8; i++) {
            int c = getc(f);
            ++cpos;
            if (c == EOF)
                  break;
            c &= 0xff;
            l += (c & 0x7f);
            if (!(c & 0x80))
                  return l;
            l <<= 7;
            }
      printf("Variable Len too long\n");
      return -1;
      }

//---------------------------------------------------------
//   skip
//---------------------------------------------------------

void skip(FILE* f, int n)
      {
      while (n--) {
            ++cpos;
            if (getc(f) == EOF) {
                  printf("skip %d failed\n", n);
                  exit(1);
                  }
            }
      }

//---------------------------------------------------------
//   grepTrack
//---------------------------------------------------------

int grepTrack(FILE* f, int trackno)
      {
//      printf("TRACK %d\n", trackno);
      int mtype, mlen;
      int b;
      char* buffer;

      char tmp[4];
      fread(tmp, 4, 1, f);
      if (memcmp(tmp, "MTrk", 4))
            return -5;
      int len = readLong(f);
      int endpos = cpos + len;
      int runstate = -1;

      for (;;) {
/*            int nclick = */ getvl(f);
            int me     = getc(f);
            ++cpos;
            int a = -1;
            if ((me & 0x80) == 0) {
                  if (runstate == -1)
                        return -6;
                  a  = me;
                  me = runstate;
                  }
            switch (me & 0xf0) {
                  case 0x80:
                  case 0x90:
                  case 0xa0:
                  case 0xb0:
                  case 0xe0:
                        b = getc(f);
                        ++cpos;
                  case 0xc0:
                  case 0xd0:
                        if (a == -1) {
                              a = getc(f);
                              ++cpos;
                              }
                        runstate = me;
                        break;
                  case 0xf0:
                        switch(me & 0xf) {
                              case 0:   // SYSEX
                                    mlen = getvl(f);
                                    skip(f, mlen);
                                    break;
                              case 1:
                              case 2:
                              case 3:
                              case 4:
                              case 5:
                              case 6:
                                    if (printName)
                                          printf("%s: ", curName);
                                    printf("unknown Message\n");
                                    break;
                              case 7:     // EOX
                              case 8:     // timing clock
                              case 9:     // undefined
                              case 0xa:   // start
                              case 0xb:   // continue
                              case 0xc:   // stop
                              case 0xd:   // undefined
                              case 0xe:   // active sensing
                                    if (printName)
                                          printf("%s: ", curName);
                                    printf("RT Message??\n");
                                    break;
                              case 0xf:   // META
                                    mtype = getc(f);
                                    ++cpos;
                                    mlen  = getvl(f);
                                    buffer = new char[mlen+1];
                                    if (mlen) {
                                          if (fread(buffer, mlen, 1, f) != 1) {
                                                if (printName)
                                                      printf("%s: ", curName);
                                                printf("---meta %d too short (%d)\n", mtype, mlen);
                                                exit(1);
                                                }
                                          cpos += mlen;
                                          }
                                    switch(mtype) {
                                          case 0x2f:
                                                delete buffer;
                                                goto end;
                                          case 1:
                                          case 2:
                                          case 3:
                                          case 4:
                                          case 5:
                                          case 6:  // Marker
                                          case 7:
                                          case 8:
                                          case 9:
                                          case 10:
                                          case 11:
                                          case 12:
                                          case 13:
                                          case 14:
                                          case 15:
                                                buffer[mlen] = 0;
                                                if (printName)
                                                      printf("%s: ", curName);
                                                printf("%02d Meta %0d: <%s>\n", trackno, mtype, buffer);
                                          default:
                                                break;
                                          }
                                    delete buffer;
                                    break;
                              }
                        break;
                  }
            }
end:
      if (cpos != endpos) {
            if (printName)
                  printf("%s: ", curName);
            printf("   %d zu kurz\n", cpos - endpos);
            }
      if (endpos-cpos > 0)
            skip(f, endpos-cpos);
      return 0;
      }

//---------------------------------------------------------
//   grep
//---------------------------------------------------------

int grep(FILE* f)
      {
      char tmp[4];

      fread(tmp, 4, 1, f);
      if (memcmp(tmp, "MThd", 4) != 0)
            return -2;
      int len = readLong(f);
      if (len < 6)
            return -3;
      cpos += 8;
      int format     = readShort(f);
      int ntracks    = readShort(f);
      readShort(f);     // division
      if (len > 6)
            skip(f, len-6); /* skip the excess */
      int rv = 0;
      switch (format) {
            case 0: rv = grepTrack(f, 0); break;
            case 1:
                  for (int i = 0; i < ntracks; i++) {
                        rv = grepTrack(f, i);
                        if (rv != 0)
                              return rv;
                        }
                  break;
            default:
                  return -4;
            }

      return 0;
      }

//---------------------------------------------------------
//   grep
//---------------------------------------------------------

int grepMidi(const char* name)
      {
      curName = name;
      char*p = strrchr(name, '.');
      FILE* f;
      if (p && strcmp(p, ".gz") == 0) {
            char buffer[512];
            sprintf(buffer, "gunzip < %s", name);
            f = popen(buffer, "r");
            }
      else {
            p = 0;
            f = fopen(name, "r");
            }
      if (f == 0)
            return -1;
      cpos = 0;
      int rc = grep(f);
      if (p)
            pclose(f);
      else
            fclose(f);
      return rc;
      }

//---------------------------------------------------------
//   usage
//---------------------------------------------------------

void usage(const char* fname, const char* txt)
      {
      fprintf(stderr, "%s: %s\n", fname, txt);
//      fprintf(stderr, "usage:\n");
      }

//---------------------------------------------------------
//   main
//---------------------------------------------------------

int main(int argc, char* argv[])
      {
      int c;
      while ((c = getopt(argc, argv, "f")) != EOF) {
            switch (c) {
                  case 'f': printName = true; break;
                  default:  usage(argv[0], "bad argument"); return -1;
                  }
            }
      argc -= optind;
      ++argc;
      char* p = 0;
      for (int i = 1; i < argc; ++i) {
            switch (grepMidi(argv[i])) {
                  case 0:     break;
                  case -1:    p = "not found"; break;
                  case -2:    p = "no 'MThd': not a midi file"; break;
                  case -3:    p = "file too short"; break;
                  case -4:    p = "bad file type"; break;
                  case -5:    p = "no 'MTrk': not a midi file"; break;
                  case -6:    p = "no running state"; break;
                  default:
                        printf("was??\n");
                        return -1;
                  }
            }
      if (p)
            printf("Error: <%s>\n", p);
      return 0;
      }