midiparse.c++
1.75 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <bstring.h>
#include <assert.h>
#include <getopt.h>
#include <math.h>
#include "event.h"
#include "midifile.h"
#include "seqfile.h"
#include "time.h"
static char usage[] = "-verbose <filename>";
main(int argc, char **argv)
{
int c;
extern char *optarg;
extern int optind;
int errflg=0;
char *ifile;
char *ofile=0;
int verbose=0;
while ((c = getopt(argc, argv, "vo:")) != EOF) {
switch (c) {
case 'v':
verbose = 1;
break;
case 'o':
ofile = optarg;
break;
case '?':
errflg++;
break;
}
}
if (errflg || optind == argc) {
(void)fprintf(stderr, "%s %s\n", argv[0], usage);
exit (2);
}
ifile = argv[optind++];
if (optind != argc) {
fprintf(stderr, "warning: only first file (%s) used, rest ignored\n",
ifile);
}
if (ofile == 0)
ofile = "tmp.seq";
MIDIFile *midifile = new MIDIFile;
if (!midifile) exit(1);
midifile->Open(ifile, "r");
SeqFile *seqfile = new SeqFile(midifile->GetDivision());
if (!seqfile) exit(1);
seqfile->Open(ofile, "w");
// merge all event data into one track. Note: this is not very
// efficient at this point ( in fact, its probably pretty bad),
// but it does the job.
int tracks = midifile->GetTrackCount();
for (int i = 0; i < tracks; i++) {
Event *event;
while ((event = midifile->GetNextEvent(i)) != 0){
seqfile->AddEvent(event);
}
}
if (verbose)
seqfile->Print();
seqfile->Write();
midifile->Close();
seqfile->Close();
}