forked from Tronix286/AIL2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMIDIREC.C
447 lines (373 loc) · 11.5 KB
/
MIDIREC.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
442
443
444
445
446
447
//ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
//ÛÛ ÛÛ
//ÛÛ MIDIREC.C ÛÛ
//ÛÛ ÛÛ
//ÛÛ MIDI Format 0 file recorder ÛÛ
//ÛÛ ÛÛ
//ÛÛ V1.00 of 04-Sep-91 ÛÛ
//ÛÛ V1.10 of 23-Oct-91: SMF sysex recording format ÛÛ
//ÛÛ ÛÛ
//ÛÛ Project: IBM Audio Interface Library ÛÛ
//ÛÛ Author: John Miles ÛÛ
//ÛÛ ÛÛ
//ÛÛ C source compatible with Turbo C++ v1.0 or later ÛÛ
//ÛÛ ÛÛ
//ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
//ÛÛ ÛÛ
//ÛÛ midirec.obj: midirec.c gen.h ail.h ÛÛ
//ÛÛ bcc -ml -c -v midirec.c ÛÛ
//ÛÛ ÛÛ
//ÛÛ midirec.exe: midirec.obj gen.lib ail.obj ÛÛ
//ÛÛ tlink @midirec.lls ÛÛ
//ÛÛ ÛÛ
//ÛÛ Contents of MIDIREC.LLS: ÛÛ
//ÛÛ /c /v /x + ÛÛ
//ÛÛ \bc\lib\c0l.obj + ÛÛ
//ÛÛ midirec ail, + ÛÛ
//ÛÛ midirec.exe, + ÛÛ
//ÛÛ midirec.map, + ÛÛ
//ÛÛ \bc\lib\cl.lib gen.lib ÛÛ
//ÛÛ ÛÛ
//ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
//ÛÛ ÛÛ
//ÛÛ Copyright (C) 1991, 1992 Miles Design, Inc. ÛÛ
//ÛÛ ÛÛ
//ÛÛ Miles Design, Inc. ÛÛ
//ÛÛ 10926 Jollyville #308 ÛÛ
//ÛÛ Austin, TX 78759 ÛÛ
//ÛÛ (512) 345-2642 / FAX (512) 338-9630 / BBS (512) 454-9990 ÛÛ
//ÛÛ ÛÛ
//ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <alloc.h>
#include <string.h>
#include <io.h>
#include <conio.h>
#include "gen.h" // General DOS and system functions
#include "ail.h" // Include AIL API for timer resources
const char VERSION[] = "1.10";
unsigned DATA_IO; // base addr
unsigned STAT_COM; // base addr+1
unsigned IRQ; // MPU-401 IRQ
#define QLEN 16384
unsigned char far queue[QLEN];
unsigned far deltas[QLEN];
unsigned head,tail; // incoming MIDI message buffer & pointers
unsigned orig_PIC;
void interrupt (*default_isr)();
HTIMER timer;
FILE *out;
unsigned event_type[256];
unsigned data_bytes[256];
unsigned long delta_time;
unsigned char MIDI_header[] = {'M','T','h','d',0,0,0,6,
0,0,0,1,0,120, // Format 0, one track, 120 ticks/quarter note
'M','T','r','k',0,0,0,15, // 1st track, initial chunk len = 15
0,0xff,0x58,4,4,2,0x18,8, // Time signature: 4/4
0,0xff,0x51,0x03,0x07,0xa1,0x20}; // Tempo: 500K uS/quarter note
unsigned char end_of_track[] = {0x00,0xff,0x2f,0x00};
typedef struct
{
unsigned byte;
unsigned delta;
}
inbyte;
/***************************************************************/
unsigned send_cmd(unsigned cmd)
{
unsigned i;
disable();
i = 65535;
while (--i)
if (!(inportb(STAT_COM) & 0x40)) break;
if (!i)
{
enable();
return 0;
}
outportb(STAT_COM,cmd);
for (i=65535;i;i--)
{
if (inportb(STAT_COM) & 0x80) continue;
if (inportb(DATA_IO) == 0xfe)
{
enable();
return 1;
}
}
enable();
return 0;
}
/***************************************************************/
void send_byte(unsigned b)
{
unsigned i;
i = 65535;
while (--i)
if (!(inportb(STAT_COM) & 0x40)) break;
if (!i)
return;
outportb(DATA_IO,b);
}
/***************************************************************/
void init_queue(void)
{
head = 0;
tail = 0;
}
unsigned check_queue(void)
{
return (head!=tail);
}
inbyte read_queue(void)
{
inbyte temp;
temp.byte = queue[tail];
temp.delta = deltas[tail];
++tail;
if (tail==QLEN) tail=0;
return temp;
}
int watch_queue(void)
{
unsigned n;
n = queue[tail];
return n;
}
void write_queue(unsigned n)
{
queue[head] = n;
deltas[head] = (delta_time > 65535L) ? 65535:(unsigned) delta_time;
delta_time = 0L;
++head;
if (head==QLEN) head=0;
}
/***************************************************************/
void interrupt MPU_isr(void)
{
unsigned in;
while (!(inportb(STAT_COM) & 0x80))
{
in = inportb(DATA_IO);
send_byte(in);
if ((in == 0xf0) || (in == 0xf7) || (in < 0xf0))
write_queue(in);
}
outportb(0x20,0x20);
}
/***************************************************************/
void far delta_update(void)
{
++delta_time;
}
/***************************************************************/
void write_VLN(unsigned dtime)
{
int i,n;
unsigned char bytefield[3];
bytefield[2] = (dtime & 0x7f);
bytefield[1] = ((dtime >> 7) & 0x7f) | 0x80;
bytefield[0] = ((dtime >> 14) & 0x7f) | 0x80;
n=2;
for (i=0;i<=2;i++)
if (bytefield[i] & 0x7f)
{
n=i;
break;
}
for (i=n;i<=2;i++)
fputc(bytefield[i],out);
}
void quit(void)
{
setvect(IRQ+8,default_isr);
outportb(0x21,orig_PIC);
fclose(out);
send_cmd(0xff);
AIL_shutdown(NULL);
}
/***************************************************************/
void main(int argc, char *argv[])
{
char outfn[64];
int argma,argmi;
int bad,strcnt;
int i;
inbyte in;
unsigned status;
unsigned expect_bytes;
unsigned long accum_delta;
unsigned sysex_active;
unsigned long flen;
static unsigned char sysex_buff[1024];
unsigned sbptr;
printf("\nMIDIREC version %s Copyright (C) 1991, 1992 Miles Design, Inc.\n",VERSION);
printf("-------------------------------------------------------------------------------\n\n");
argma=argmi=-1;
bad=strcnt=0;
for (i=1;i<argc;i++)
if (argv[i][0] != '/')
if (strcnt == 1)
{
bad=1; break;
}
else
{
strcpy(outfn,argv[i]);
strcnt++;
}
else
if (!strnicmp(argv[i],"/MA:",4))
argma = (int) val(&argv[i][4],16);
else if (!strnicmp(argv[i],"/MI:",4))
argmi = (int) val(&argv[i][4],10);
else
{
bad=1; break;
}
if (bad || (!strcnt))
{
printf("This program writes incoming MIDI data to a specified MIDI Format 0 file.\n\n");
printf("Usage: MIDIREC filename [/MA:xx] [/MI:xx]\n\n");
printf("where /MA:xx = MPU-401 MIDI interface I/O address xx (hex)\n");
printf(" /MI:xx = MPU-401 MIDI interface interrupt number xx\n\n");
printf("Parameters in brackets [] are optional, and may appear in any order.\n");
exit(1);
}
for (i=0;i<256;i++)
{
event_type[i] = 0;
data_bytes[i] = 0;
}
for (i=0x80;i<=0xef;i++)
{
event_type[i] = 1;
data_bytes[i] = 2 - ((i >= 0xc0) && (i <= 0xdf));
}
event_type[0xf0] = 2; event_type[0xf7] = 3;
DATA_IO = 0x330;
STAT_COM = 0x331;
IRQ = 2;
if (argmi != -1) IRQ = argmi;
if (argma != -1)
{
DATA_IO = argma;
STAT_COM = argma+1;
}
if (!send_cmd(0xff))
if (!send_cmd(0xff))
{
printf("Can't initialize MPU-401 MIDI interface.\n");
exit(1);
}
AIL_startup();
timer=AIL_register_timer(delta_update);
AIL_set_timer_frequency(timer,240L);
out=fopen(outfn,"w+b");
if (out == NULL)
{
printf("Can't open %s.\n",outfn);
AIL_shutdown(NULL);
exit(1);
}
fwrite(MIDI_header,sizeof(MIDI_header),1,out);
init_queue();
send_cmd(0x3f);
disable();
default_isr = getvect(IRQ+8);
orig_PIC = inportb(0x21);
setvect(IRQ+8,MPU_isr);
outportb(0x21,inportb(0x21) & (~(1 << IRQ)));
enable();
printf("Waiting for SysEx or Channel Voice message status...\n\n");
while (kbhit()) getch();
while (!kbhit())
if (check_queue())
if (event_type[watch_queue()]) break;
if (kbhit())
{
getch();
printf("Aborted.\n");
quit();
unlink(outfn);
exit(0);
}
printf("MIDI recording in progress. Press any key to stop.\n\n");
AIL_start_timer(timer);
delta_time = 0L;
status = 0;
accum_delta=0L;
expect_bytes = 0;
sysex_active = 0;
sbptr = 0;
//
// Main loop
//
while (kbhit()) getch();
while (!kbhit())
{
if (!check_queue()) continue;
in = read_queue();
accum_delta += (unsigned long) in.delta;
if (accum_delta >= 65535L) accum_delta=65535L;
switch (event_type[in.byte])
{
case 0: // data byte
if ((sysex_active) && (sbptr < 1024))
{
sysex_buff[sbptr++] = in.byte;
break;
}
if (expect_bytes)
{
--expect_bytes;
fputc(in.byte,out);
break;
}
case 1: // Channel Voice status (or running status)
sysex_active = 0;
if (in.byte >= 0x80) status = in.byte;
if (status < 0x80) break;
write_VLN(accum_delta);
accum_delta = 0L;
fputc(in.byte,out);
expect_bytes = data_bytes[status] - (in.byte < 0x80);
break;
case 2: // System Exclusive begin
write_VLN(accum_delta);
accum_delta = 0L;
fputc(in.byte,out);
status = 0;
sbptr = 0;
sysex_active = 1;
break;
case 3: // System Exclusive end
if (!sysex_active)
{
status = 0;
break;
}
write_VLN(sbptr+1);
for (i=0;i<sbptr;i++)
fputc(sysex_buff[i],out);
fputc(in.byte,out);
sysex_active = 0;
break;
}
}
getch();
fwrite(end_of_track,sizeof(end_of_track),1,out);
fflush(out);
flen = filelength(fileno(out)) - sizeof(MIDI_header);
fseek(out,18L,SEEK_SET);
flen += 15L;
flen = wswap(flen);
fwrite(&flen,4,1,out);
printf("MIDIREC stopped.\n");
quit();
exit(0);
}