-
Notifications
You must be signed in to change notification settings - Fork 13
/
syx2mid.c
284 lines (231 loc) · 6.35 KB
/
syx2mid.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
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
// SYX -> MID Converter
// --------------------
// Written by Valley Bell, 14 October 2019
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "stdtype.h"
#ifndef INLINE
#if defined(_MSC_VER)
#define INLINE static __inline
#elif defined(__GNUC__)
#define INLINE static __inline__
#else
#define INLINE static inline
#endif
#endif // INLINE
typedef struct file_information
{
UINT32 alloc; // allocated bytes
UINT32 pos; // current file offset
UINT8* data; // file data
} FILE_INF;
UINT8 Syx2Mid(void);
static UINT32 GetEventLength(UINT32 startPos);
static UINT8 WriteFileData(UINT32 dataLen, const UINT8* data, const char* fileName);
static void WriteMidiHeader(FILE_INF* fInf, UINT16 format, UINT16 tracks, UINT16 resolution);
static void WriteMidiValue(FILE_INF* fInf, UINT32 value);
static void WriteLongEvent(FILE_INF* fInf, UINT32 delay, UINT8 evt, UINT32 dataLen, const void* data);
static void WriteMetaEvent(FILE_INF* fInf, UINT32 delay, UINT8 metaType, UINT32 dataLen, const void* data);
INLINE void WriteBE32(UINT8* buffer, UINT32 value);
INLINE void WriteBE16(UINT8* buffer, UINT16 value);
static UINT32 SyxLen;
static UINT8* SyxData;
static UINT32 MidLen;
static UINT8* MidData;
static UINT16 MIDI_RES = 480;
static UINT32 MIDI_TEMPO = 500000; // 120 BPM
int main(int argc, char* argv[])
{
int argbase;
FILE* hFile;
UINT8 retVal;
if (argc < 3)
{
printf("Usage: syx2mid.exe input.syx output.mid\n");
return 0;
}
argbase = 1;
if (argc < argbase + 2)
{
printf("Not enough arguments.\n");
return 0;
}
hFile = fopen(argv[argbase + 0], "rb");
if (hFile == NULL)
{
printf("Error opening file!\n");
return 1;
}
fseek(hFile, 0x00, SEEK_END);
SyxLen = ftell(hFile);
if (SyxLen > 0x100000)
SyxLen = 0x100000;
fseek(hFile, 0x00, SEEK_SET);
SyxData = (UINT8*)malloc(SyxLen);
fread(SyxData, 0x01, SyxLen, hFile);
fclose(hFile);
retVal = Syx2Mid();
if (! retVal)
WriteFileData(MidLen, MidData, argv[argbase + 1]);
free(MidData); MidData = NULL;
free(SyxData); SyxData = NULL;
return 0;
}
UINT8 Syx2Mid(void)
{
FILE_INF midFileInf;
UINT32 trkBasePos;
UINT32 inPos;
UINT32 evtLen;
UINT8 tempArr[4];
UINT32 ticksSec; // ticks per second
UINT32 delay;
midFileInf.alloc = SyxLen * 3 / 2 + 0x20;
midFileInf.data = (UINT8*)malloc(midFileInf.alloc);
midFileInf.pos = 0x00;
WriteMidiHeader(&midFileInf, 0, 1, MIDI_RES);
WriteBE32(&midFileInf.data[midFileInf.pos + 0x00], 0x4D54726B); // write 'MTrk'
WriteBE32(&midFileInf.data[midFileInf.pos + 0x04], 0x00000000); // write dummy length
midFileInf.pos += 0x08;
trkBasePos = midFileInf.pos;
WriteBE32(tempArr, MIDI_TEMPO);
WriteMetaEvent(&midFileInf, 0, 0x51, 0x03, &tempArr[0x01]);
ticksSec = MIDI_RES * 1000000 / MIDI_TEMPO;
inPos = 0x00;
delay = 0;
while(inPos < SyxLen)
{
evtLen = GetEventLength(inPos);
if (! evtLen)
break;
if (SyxData[inPos] < 0xF0)
{
WriteMidiValue(&midFileInf, delay);
memcpy(&midFileInf.data[midFileInf.pos], &SyxData[inPos], evtLen);
midFileInf.pos += evtLen;
}
else
{
WriteLongEvent(&midFileInf, delay, SyxData[inPos], evtLen - 1, &SyxData[inPos + 1]);
}
inPos += evtLen;
// MIDI data is sent via UART at 31250 bps.
// For each byte of data, 10 bits are sent. (1 start bit, 8 bits of data, 1 stop bit)
// Thus, we get a transfer rate of 3125 data bytes per second.
delay = (ticksSec * evtLen + 3124) / 3125; // calculate with integer ceil()
}
WriteMetaEvent(&midFileInf, delay, 0x2F, 0x00, NULL);
WriteBE32(&midFileInf.data[trkBasePos - 0x04], midFileInf.pos - trkBasePos); // write Track Length
MidData = midFileInf.data;
MidLen = midFileInf.pos;
return 0x00;
}
static UINT32 GetEventLength(UINT32 startPos)
{
UINT8 evtType;
UINT32 curPos;
curPos = startPos;
evtType = SyxData[curPos]; curPos ++;
if (evtType < 0x80)
return 0;
if (evtType < 0xF0)
return ((evtType & 0xE0) == 0xC0) ? 0x02 : 0x03;
switch(evtType)
{
case 0xF0:
case 0xF7:
while(curPos < SyxLen && ! (SyxData[curPos] & 0x80))
curPos ++;
if (curPos < SyxLen && SyxData[curPos] == 0xF7)
curPos ++; // skip last F7 byte
return curPos - startPos;
default:
return 0;
}
}
static UINT8 WriteFileData(UINT32 dataLen, const UINT8* data, const char* fileName)
{
FILE* hFile;
hFile = fopen(fileName, "wb");
if (hFile == NULL)
{
printf("Error opening %s!\n", fileName);
return 0xFF;
}
fwrite(data, 0x01, dataLen, hFile);
fclose(hFile);
return 0;
}
static void WriteMidiHeader(FILE_INF* fInf, UINT16 format, UINT16 tracks, UINT16 resolution)
{
WriteBE32(&fInf->data[fInf->pos + 0x00], 0x4D546864); // write 'MThd'
WriteBE32(&fInf->data[fInf->pos + 0x04], 0x00000006); // Header Length
fInf->pos += 0x08;
WriteBE16(&fInf->data[fInf->pos + 0x00], format); // MIDI Format (0/1/2)
WriteBE16(&fInf->data[fInf->pos + 0x02], tracks); // number of tracks
WriteBE16(&fInf->data[fInf->pos + 0x04], resolution); // Ticks per Quarter
fInf->pos += 0x06;
return;
}
static void WriteMidiValue(FILE_INF* fInf, UINT32 value)
{
UINT8 valSize;
UINT8* valData;
UINT32 tempLng;
UINT32 curPos;
valSize = 0x00;
tempLng = value;
do
{
tempLng >>= 7;
valSize ++;
} while(tempLng);
valData = &fInf->data[fInf->pos];
curPos = valSize;
tempLng = value;
do
{
curPos --;
valData[curPos] = 0x80 | (tempLng & 0x7F);
tempLng >>= 7;
} while(tempLng);
valData[valSize - 1] &= 0x7F;
fInf->pos += valSize;
return;
}
static void WriteLongEvent(FILE_INF* fInf, UINT32 delay, UINT8 evt, UINT32 dataLen, const void* data)
{
WriteMidiValue(fInf, delay);
fInf->data[fInf->pos + 0x00] = evt;
fInf->pos += 0x01;
WriteMidiValue(fInf, dataLen);
memcpy(&fInf->data[fInf->pos], data, dataLen);
fInf->pos += dataLen;
return;
}
static void WriteMetaEvent(FILE_INF* fInf, UINT32 delay, UINT8 metaType, UINT32 dataLen, const void* data)
{
WriteMidiValue(fInf, delay);
fInf->data[fInf->pos + 0x00] = 0xFF;
fInf->data[fInf->pos + 0x01] = metaType;
fInf->pos += 0x02;
WriteMidiValue(fInf, dataLen);
memcpy(&fInf->data[fInf->pos], data, dataLen);
fInf->pos += dataLen;
return;
}
INLINE void WriteBE32(UINT8* buffer, UINT32 value)
{
buffer[0x00] = (value >> 24) & 0xFF;
buffer[0x01] = (value >> 16) & 0xFF;
buffer[0x02] = (value >> 8) & 0xFF;
buffer[0x03] = (value >> 0) & 0xFF;
return;
}
INLINE void WriteBE16(UINT8* buffer, UINT16 value)
{
buffer[0x00] = (value >> 8) & 0xFF;
buffer[0x01] = (value >> 0) & 0xFF;
return;
}