-
Notifications
You must be signed in to change notification settings - Fork 14
/
ims2mid.c
441 lines (390 loc) · 10.2 KB
/
ims2mid.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// IMS2MID
// -------
// Programmed by Valley Bell
// January 2015 (original Visual Basic 6 version)
// 25 December 2015 (C version)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "stdtype.h"
#define INLINE static __inline
int main(int argc, char* argv[]);
static UINT8 OpenFile(const char* FileName, UINT32* retFileLen, UINT8** retFileData);
static UINT8 WriteFile(const char* FileName, const UINT32 FileLen, const UINT8* FileData);
static UINT8 IMS2MID(UINT32 imsLen, const UINT8* imsData, UINT32* refMidLen, UINT8** refmidData);
INLINE UINT32 Tempo2Mid(UINT8 baseBPM, UINT16 factor);
static void WriteEvent(UINT8* Buffer, UINT32* Pos, UINT8 Evt, UINT8 Val1, UINT8 Val2);
static void WriteMidiValue(UINT8* Buffer, UINT32* Pos, UINT32 Value);
static void WriteMetaEvent_Data(UINT8* Buffer, UINT32* Pos, UINT8 MetaType, UINT32 DataLen, const UINT8* Data);
INLINE UINT16 ReadLE16(const UINT8* Data);
INLINE UINT32 ReadLE32(const UINT8* Data);
INLINE void WriteBE16(UINT8* Buffer, UINT16 Value);
INLINE void WriteBE32(UINT8* Buffer, UINT32 Value);
int main(int argc, char* argv[])
{
UINT32 ImsLen;
UINT8* ImsData;
UINT32 MidLen;
UINT8* midData;
int argbase;
UINT8 retVal;
printf("IMS2MID\n");
printf("-------\n");
if (argc < 2)
{
printf("Usage: ims2mid.exe input.ims output.mid\n");
return 0;
}
argbase = 1;
/*while(argbase < argc && argv[argbase][0] == '-')
{
if (0)
fixFile = 1;
else
break;
argbase ++;
}*/
if (argbase + 1 >= argc)
{
printf("Not enough arguments!\n");
return 1;
}
ImsData = NULL;
retVal = OpenFile(argv[argbase + 0], &ImsLen, &ImsData);
if (retVal)
{
if (ImsData != NULL)
free(ImsData);
switch(retVal)
{
case 0x01:
printf("Unable to read complete file!\n");
break;
case 0xF0:
printf("Not enough memory for IMS file!\n");
break;
case 0xFF:
printf("Error opening file!\n");
break;
}
return -1;
}
MidLen = ImsLen * 2;
midData = (UINT8*)malloc(MidLen);
if (midData == NULL)
{
free(ImsData);
printf("Not enough memory for MIDI buffer!\n");
}
IMS2MID(ImsLen, ImsData, &MidLen, &midData);
retVal = WriteFile(argv[argbase + 1], MidLen, midData);
if (retVal == 0xFF)
{
free(ImsData); ImsData = NULL;
free(midData); midData = NULL;
printf("Error writing file!\n");
return 1;
}
printf("File written.\n");
free(ImsData); ImsData = NULL;
free(midData); midData = NULL;
//_getch();
return 0;
}
static UINT8 OpenFile(const char* FileName, UINT32* retFileLen, UINT8** retFileData)
{
FILE* hFile;
UINT32 FileLen;
UINT32 bytesWrt;
UINT8* FileData;
hFile = fopen(FileName, "rb");
if (hFile == NULL)
return 0xFF;
fseek(hFile, 0x00, SEEK_END);
FileLen = ftell(hFile);
// Read Data
FileData = (UINT8*)malloc(FileLen);
if (FileData == NULL)
{
fclose(hFile);
return 0xF0;
}
rewind(hFile);
bytesWrt = fread(FileData, 0x01, FileLen, hFile);
fclose(hFile);
*retFileLen = bytesWrt;
*retFileData = FileData;
if (bytesWrt < FileLen)
return 0x01;
return 0x00;
}
static UINT8 WriteFile(const char* FileName, const UINT32 FileLen, const UINT8* FileData)
{
FILE* hFile;
UINT32 bytesWrt;
hFile = fopen(FileName, "wb");
if (hFile == NULL)
return 0xFF;
bytesWrt = fwrite(FileData, 0x01, FileLen, hFile);
fclose(hFile);
if (bytesWrt < FileLen)
return 0x01;
return 0x00;
}
static UINT8 IMS2MID(UINT32 imsLen, const UINT8* imsData, UINT32* refMidLen, UINT8** refmidData)
{
UINT32 midLen;
UINT8* midData;
UINT32 midTrkBase;
UINT32 curPos;
UINT32 midPos;
UINT32 ticksPerBeat;
UINT32 curDelay;
UINT8 curEvt;
UINT8 fullEvtCmd;
UINT8 curChn;
UINT8 fileEnd;
UINT8 baseBPM;
UINT16 tempoFactor;
INT16 pbValue;
UINT32 midTempoVal;
UINT32 dataSize;
UINT8 tempArr[4];
UINT8 chnNote[0x10];
midLen = *refMidLen;
midData = *refmidData;
ticksPerBeat = imsData[0x24];
baseBPM = imsData[0x3C];
midPos = 0x00;
WriteBE32(&midData[midPos], 0x4D546864); midPos += 0x04; // 'MThd' Signature
WriteBE32(&midData[midPos], 0x00000006); midPos += 0x04; // Header Size
WriteBE16(&midData[midPos], 0x0000); midPos += 0x02; // Format: 0
WriteBE16(&midData[midPos], 0x0001); midPos += 0x02; // 1 Track
WriteBE16(&midData[midPos], ticksPerBeat); midPos += 0x02; // Ticks per Quarter
WriteBE32(&midData[midPos], 0x4D54726B); // write 'MTrk'
midPos += 0x08;
midTrkBase = midPos;
// write default Tempo
WriteMidiValue(midData, &midPos, 0); // write delay
midTempoVal = Tempo2Mid(baseBPM, 0);
WriteBE32(tempArr, midTempoVal);
WriteMetaEvent_Data(midData, &midPos, 0x51, 0x03, &tempArr[1]);
memset(chnNote, 0x00, 0x10);
fileEnd = 0;
curPos = 0x46;
while(! fileEnd && curPos < imsLen)
{
// process delay
curDelay = 0;
while(imsData[curPos] == 0xF8)
{
curDelay += ticksPerBeat;
curPos ++;
}
if (imsData[curPos] >= 0xF0)
printf("Unknown delay 0x%02X found at offset %04X!\n", imsData[curPos], curPos);
curDelay += imsData[curPos];
curPos ++;
WriteMidiValue(midData, &midPos, curDelay);
fullEvtCmd = (imsData[curPos] & 0x80);
if (fullEvtCmd)
{
curEvt = imsData[curPos]; curPos ++;
midData[midPos] = curEvt; midPos ++;
curChn = curEvt & 0x0F;
}
switch(curEvt & 0xF0)
{
case 0x80: // Note Retrigger
if (! fullEvtCmd)
{
midData[midPos] = 0x80 | curChn;
midPos ++;
}
midData[midPos + 0x00] = chnNote[curChn];
midData[midPos + 0x01] = 0x00;
midPos += 0x02;
WriteMidiValue(midData, &midPos, 0);
chnNote[curChn] = imsData[curPos + 0x00];
midData[midPos + 0x00] = 0x90 | curChn;
midData[midPos + 0x01] = imsData[curPos + 0x00];
if (imsData[curPos + 0x01])
midData[midPos + 0x02] = imsData[curPos + 0x01];
else
midData[midPos + 0x02] = 0x01;
curPos += 0x02; midPos += 0x03;
break;
case 0x90: // Note On/Off
if (imsData[curPos + 0x01])
chnNote[curChn] = imsData[curPos + 0x00];
else
chnNote[curChn] = 0x00;
midData[midPos + 0x00] = imsData[curPos + 0x00];
midData[midPos + 0x01] = imsData[curPos + 0x01];
curPos += 0x02; midPos += 0x02;
break;
case 0xA0: // Note Velocity Change
if (fullEvtCmd)
midData[midPos - 0x01] = 0xB0 | curChn;
midData[midPos + 0x00] = 0x27;
midData[midPos + 0x01] = imsData[curPos + 0x00];
curPos += 0x01; midPos += 0x02;
break;
case 0xC0: // Set Instrument
midData[midPos + 0x00] = imsData[curPos + 0x00];
curPos += 0x01; midPos += 0x01;
break;
case 0xE0: // Pitch Bend
pbValue = ((imsData[curPos + 0x00] & 0x7F) << 0) |
((imsData[curPos + 0x01] & 0x7F) << 7);
// IMS files (like ROL) support only a semitone range for pitch bends.
// MIDI files default to 2 semitones, so I need fix the value.
// 0x0000..0x2000..0x3FFF -> 0x1000..0x2000..0x2FFF
pbValue = (pbValue / 2) + 0x1000;
midData[midPos + 0x00] = (pbValue >> 0) & 0x7F;
midData[midPos + 0x01] = (pbValue >> 7) & 0x7F;
curPos += 0x02; midPos += 0x02;
break;
case 0xB0: // unused event types
case 0xD0:
printf("Unknown event 0x%02X found at offset %04X!\n", curEvt, curPos - 0x01);
curPos += 0x02;
break;
case 0xF0:
switch(curEvt)
{
case 0xF0: // Tempo
dataSize = 0x00;
while(curPos + dataSize < imsLen && imsData[curPos + dataSize] != 0xF7)
dataSize ++;
dataSize ++; // skip F7 terminator
if (imsData[curPos + 0x00] == 0x7F && imsData[curPos + 0x01] == 0x00)
{
if (fullEvtCmd)
midPos --; // remove F0 command
tempoFactor = ((imsData[curPos + 0x02] & 0x7F) << 7) |
((imsData[curPos + 0x03] & 0x7F) << 0);
midTempoVal = Tempo2Mid(baseBPM, tempoFactor);
WriteBE32(tempArr, midTempoVal);
WriteMetaEvent_Data(midData, &midPos, 0x51, 0x03, &tempArr[1]);
}
else
{
WriteMidiValue(midData, &midPos, dataSize);
memcpy(&midData[midPos], &imsData[curPos], dataSize);
midPos += dataSize;
}
curPos += dataSize;
break;
case 0xFC: // Track End
midPos --;
WriteEvent(midData, &midPos, 0xFF, 0x2F, 0x00);
fileEnd = 1;
break;
default:
printf("Unknown event 0x%02X found at offset %04X!\n", curEvt, curPos - 0x01);
break;
}
} // end switch(curEvt & 0xF0)
} // end while(! fileEnd)
WriteBE32(&midData[midTrkBase - 0x04], midPos - midTrkBase); // write Track Length
midLen = midPos;
*refMidLen = midLen;
*refmidData = midData;
return 0x00;
}
INLINE UINT32 Tempo2Mid(UINT8 baseBPM, UINT16 factor)
{
double finalBPM;
if (! factor)
factor = 0x80;
finalBPM = baseBPM * factor / 128.0;
if (finalBPM >= 1.0)
return (UINT32)(60000000.0 / finalBPM + 0.5);
else
return 0;
}
static void WriteEvent(UINT8* Buffer, UINT32* Pos, UINT8 Evt, UINT8 Val1, UINT8 Val2)
{
if (! (Evt & 0x80))
return;
switch(Evt & 0xF0)
{
case 0x80:
case 0x90:
case 0xA0:
case 0xB0:
case 0xE0:
Buffer[*Pos + 0x00] = Evt;
Buffer[*Pos + 0x01] = Val1;
Buffer[*Pos + 0x02] = Val2;
*Pos += 0x03;
break;
case 0xC0:
case 0xD0:
Buffer[*Pos + 0x00] = Evt;
Buffer[*Pos + 0x01] = Val1;
*Pos += 0x02;
break;
case 0xF0: // for Meta Event: Track End
Buffer[*Pos + 0x00] = Evt;
Buffer[*Pos + 0x01] = Val1;
Buffer[*Pos + 0x02] = Val2;
*Pos += 0x03;
break;
default:
break;
}
return;
}
static void WriteMetaEvent_Data(UINT8* Buffer, UINT32* Pos, UINT8 MetaType, UINT32 DataLen, const UINT8* Data)
{
Buffer[*Pos + 0x00] = 0xFF;
Buffer[*Pos + 0x01] = MetaType;
*Pos += 0x02;
WriteMidiValue(Buffer, Pos, DataLen);
memcpy(Buffer + *Pos, Data, DataLen);
*Pos += DataLen;
return;
}
static void WriteMidiValue(UINT8* Buffer, UINT32* Pos, UINT32 Value)
{
UINT8 ValSize;
UINT8* ValData;
UINT32 TempLng;
UINT32 CurPos;
ValSize = 0x00;
TempLng = Value;
do
{
TempLng >>= 7;
ValSize ++;
} while(TempLng);
ValData = &Buffer[*Pos];
CurPos = ValSize;
TempLng = Value;
do
{
CurPos --;
ValData[CurPos] = 0x80 | (TempLng & 0x7F);
TempLng >>= 7;
} while(TempLng);
ValData[ValSize - 1] &= 0x7F;
*Pos += ValSize;
return;
}
INLINE void WriteBE16(UINT8* Buffer, UINT16 Value)
{
Buffer[0x00] = (Value & 0xFF00) >> 8;
Buffer[0x01] = (Value & 0x00FF) >> 0;
return;
}
INLINE void WriteBE32(UINT8* Buffer, UINT32 Value)
{
Buffer[0x00] = (Value & 0xFF000000) >> 24;
Buffer[0x01] = (Value & 0x00FF0000) >> 16;
Buffer[0x02] = (Value & 0x0000FF00) >> 8;
Buffer[0x03] = (Value & 0x000000FF) >> 0;
return;
}