-
Notifications
You must be signed in to change notification settings - Fork 9
/
midi-dump.c
175 lines (140 loc) · 3.71 KB
/
midi-dump.c
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
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#ifdef _WIN32
#include <windows.h>
#include <memoryapi.h>
static void win_err_helper(const char *func, const char *path)
{
const DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS;
const DWORD dw = GetLastError();
const DWORD lang = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
LPVOID lpMsgBuf;
FormatMessage(flags, NULL, dw, lang, (LPTSTR)&lpMsgBuf, 0, NULL);
printf("%s(%s): %s\n", func, path, (char *)lpMsgBuf);
LocalFree(lpMsgBuf);
}
#else
#include <sys/mman.h>
#endif
#include <midi-parser.h>
static void usage(const char *prog)
{
printf("usage: %s <file.midi>\n", prog);
}
static void parse_and_dump(struct midi_parser *parser)
{
enum midi_parser_status status;
while (1) {
status = midi_parse(parser);
switch (status) {
case MIDI_PARSER_EOB:
puts("eob");
return;
case MIDI_PARSER_ERROR:
puts("error");
return;
case MIDI_PARSER_INIT:
puts("init");
break;
case MIDI_PARSER_HEADER:
printf("header\n");
printf(" size: %d\n", parser->header.size);
printf(" format: %d [%s]\n", parser->header.format, midi_file_format_name(parser->header.format));
printf(" tracks count: %d\n", parser->header.tracks_count);
printf(" time division: %d\n", parser->header.time_division);
break;
case MIDI_PARSER_TRACK:
puts("track");
printf(" length: %d\n", parser->track.size);
break;
case MIDI_PARSER_TRACK_MIDI:
puts("track-midi");
printf(" time: %ld\n", parser->vtime);
printf(" status: %d [%s]\n", parser->midi.status, midi_status_name(parser->midi.status));
printf(" channel: %d\n", parser->midi.channel);
printf(" param1: %d\n", parser->midi.param1);
printf(" param2: %d\n", parser->midi.param2);
break;
case MIDI_PARSER_TRACK_META:
printf("track-meta\n");
printf(" time: %ld\n", parser->vtime);
printf(" type: %d [%s]\n", parser->meta.type, midi_meta_name(parser->meta.type));
printf(" length: %d\n", parser->meta.length);
break;
case MIDI_PARSER_TRACK_SYSEX:
puts("track-sysex");
printf(" time: %ld\n", parser->vtime);
break;
default:
printf("unhandled state: %d\n", status);
return;
}
}
}
static int parse_file(const char *path)
{
struct stat st;
if (stat(path, &st)) {
printf("stat(%s): %m\n", path);
return 1;
}
int fd = open(path, O_RDONLY);
if (fd < 0) {
printf("open(%s): %m\n", path);
return 1;
}
#ifdef _WIN32
HANDLE fhandle = (HANDLE)_get_osfhandle(fd);
if (st.st_size == 0) {
printf("file is empty\n");
close(fd);
return 1;
}
HANDLE hMapFile = CreateFileMapping(fhandle, NULL, PAGE_READONLY, 0, 0, NULL);
if (!hMapFile) {
win_err_helper("CreateFileMapping", path);
close(fd);
return 1;
}
void *mem = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
if (!mem) {
win_err_helper("MapViewOfFile", path);
CloseHandle(hMapFile);
close(fd);
return 1;
}
#else
void *mem = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (mem == MAP_FAILED) {
printf("mmap(%s): %m\n", path);
close(fd);
return 1;
}
#endif
struct midi_parser parser;
parser.state = MIDI_PARSER_INIT;
parser.size = st.st_size;
parser.in = mem;
parse_and_dump(&parser);
#ifdef _WIN32
UnmapViewOfFile(mem);
CloseHandle(hMapFile);
#else
munmap(mem, st.st_size);
#endif
close(fd);
return 0;
}
int main(int argc, char **argv)
{
if (argc != 2) {
usage(argv[0]);
return 1;
}
return parse_file(argv[1]);
}