-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMid2rdl.cpp
More file actions
1746 lines (1500 loc) · 45.4 KB
/
Copy pathMid2rdl.cpp
File metadata and controls
1746 lines (1500 loc) · 45.4 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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------
Dieter Neubacher Vers.: 1.0 26.02.93
1.1 28.06.94
1.2 22.02.95
1.4 03.06.96
1.5 20.06.97
-------------------------------------------------------------------
mid2rdl.c
Vers.: 1.2 Use *.BAR file for bar informations
Vers.: 1.4 erase tmp-files (if TempFileFlag == 0 )
option -d
Infoline in RDL file
Vers.: 1.5 Use system Temp-Files
-----------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "rameau.h"
#include "version.h"
#include "midi.h"
#include "rdl.h"
#define PROG_NAME "mid2rdl"
/*-----------------------------------*/
/* debug note in C, CIS, D, DIS, ... */
/* if note_code_flag == 1 */
/*-----------------------------------*/
/*#define DEBUG_NOTE_CODE
*/
/*#define DEBUG_NEXT_BAR_TIME
*/
/*---------------------------*/
/* debug note on / off */
/*---------------------------*/
/*
#define NOTE_DEBUG
*/
/*---------------------------*/
/* debug read_header_chunk() */
/*---------------------------*/
/*
#define RHC_DEBUG
*/
/*---------------------------*/
/* debug read_var_len() */
/*---------------------------*/
/*
#define RVL_DEBUG
*/
/*---------------------------*/
/* debug read_track_chunk() */
/*---------------------------*/
/*
#define RTC_DEBUG
*/
/*---------------------------*/
/* debug read_mtrk_event() */
/*---------------------------*/
/*
#define RTE_DEBUG
*/
/*---------------------------*/
/* debug read_mtrk_event() */
/* debug data input */
/*---------------------------*/
/*
#define RTE_INPUT_DEBUG
*/
/*---------------------------*/
/* debug read meta event's */
/*---------------------------*/
/*
#define ME_DEBUG
*/
/*---------------------------*/
/* debug time information */
/*---------------------------*/
/*
#define TIME_DEBUG
*/
#define RDL_FLUSH 0x0100 /* flush output buffers */
#define INIT_RDL_OUTPUT -1 /* init new rdl output */
/*---------------------------*/
/* function definitions */
/*---------------------------*/
void mid_to_rdl_usage (void);
int write_output_line (FILE * stream, long midi_clock, int *channel);
int input_read_error (void);
long read_var_len (FILE * stream);
char *read_n_byte (FILE * stream, long n);
int read_header_chunk (FILE * stream);
int read_track_chunk (FILE * stream);
int read_mtrk_event (FILE * stream);
int rdl_output (long time, int mode, int channel, int note);
int read_chunk_info (FILE * stream);
long read_line (FILE * tmp_stream, int channel_nodes[16]);
int my_getc (FILE * stream);
long to32bit (int c1, int c2, int c3, int c4);
int to16bit (int c1, int c2);
long read32bit (FILE * stream);
int read16bit (FILE * stream);
/* Vers.: 1.2 */
long GetNextBarTime( FILE * stream);
/*------------------*/
/* global variables */
/*------------------*/
long GlobalTime = 0;
uint NodeFlag = 0;
int channel, channel_buf[16];
int GlobalChannel = 0;
int GlobalChannelFlag = 0;
long TrackByteCount;
FILE *in_stream = NULL, *out_stream = NULL, *err_stream = stderr;
int FromBar = 0, ToBar = MAX_BAR_NUMBER;
int TicksPerQuaterNote;
long NextBarTime;
int BarFlag = 0;
int BarCount = 1;
int VerboseFlag = 0;
int ReadLineFlag = 0;
#ifdef TEMP_BAR_INFO
long BarTime;
#endif
/*------------------------*/
/* print note codes */
/*------------------------*/
int note_code_flag = 0;
/*------------------*/
/* global variables */
/*------------------*/
char note_code[12][5] =
{
" C ", /* 0 */
" CIS", /* 1 */
" D ", /* 2 */
" DIS", /* 3 */
" E ", /* 4 */
" F ", /* 5 */
" FIS", /* 6 */
" G ", /* 7 */
" GIS", /* 8 */
" A ", /* 9 */
" AIS", /* 10 */
" H " /* 11 */
};
/*-------------------------------------------------------------------------
---------------------------------------------------------------------------
*/
/*---------------------------------------------------------------------
-----------------------------------------------------------------------
*/
void
mid_to_rdl_usage ()
{
fprintf (err_stream, "usage : %s [flags] [%s_file_name]\n", PROG_NAME, INP_EXT);
fprintf (err_stream, "\n");
fprintf (err_stream, "flags : -? this output\n");
fprintf (err_stream, " : -h this output\n");
fprintf (err_stream, " : -v disp program version\n");
fprintf (err_stream, " : -o output to stdout\n");
fprintf (err_stream, " : -i input from stdin\n");
fprintf (err_stream, " : -l Disp Read line numbers\n");
fprintf (err_stream, " : -b=x/y off beat Time\n");
fprintf (err_stream, " : -s=x/y Time Signature\n");
fprintf (err_stream, " : -f=nnn Nots form\n");
fprintf (err_stream, " : -t=nnn Nots to\n");
// fprintf (err_stream, " : -d don't erase tmp-files\n");
#ifdef DEBUG_NOTE_CODE
fprintf (err_stream, " : -n note information in output file\n");
#endif /* */
fprintf (err_stream, "\n");
return;
}
/*---------------------------------------------------------------------
-----------------------------------------------------------------------
*/
int my_getc (FILE * stream)
{
int ch;
if ((ch = getc (stream)) == EOF)
{
/* END OF FILE */
fprintf (err_stream, "END OF FILE\n");
exit (0);
return 1;
}
else
{
return (ch);
}
}
/*---------------------------------------------------------------------
-----------------------------------------------------------------------
*/
long
read_var_len (FILE * stream)
{
register long value;
register char c;
#ifdef TEST_END_OF_LINE
if ((value = getc (stream)) == EOF)
{
fprintf (err_stream, "read_var_len() value = end of file\n");
exit (1);
}
#else /* TEST_END_OF_LINE */
value = getc (stream);
#endif /* TEST_END_OF_LINE */
if (value & 0x80)
{
value &= 0x7f;
do
{
if ((c = getc (stream)) == EOF)
{
fprintf (err_stream, "read_var_len() c = end of file\n");
break;
}
value = (value << 7) + (c & 0x7f);
}
while (c & 0x80);
}
#ifdef RVL_DEBUG
fprintf (err_stream, "read var len %8x\n", value);
#endif /* RVL_DEBUG */
return (value);
}
/*---------------------------------------------------------------------
read n bytes from midi-stream to buffer
-----------------------------------------------------------------------
*/
char *read_n_byte (FILE * stream, long n)
{
char *ret = NULL;
int i;
if ((ret = (char *) malloc (sizeof (char) * (n + 1))) == NULL)
{
fprintf (err_stream, "no more memory\n");
for (i = 0; i < n; i++)
my_getc (stream);
}
else
{
for (i = 0; i < n; i++)
ret[i] = my_getc (stream);
ret[n] = 0;
}
return (ret);
}
long
to32bit (int c1, int c2, int c3, int c4)
{
long value = 0L;
value = (c1 & 0xff);
value = (value << 8) + (c2 & 0xff);
value = (value << 8) + (c3 & 0xff);
value = (value << 8) + (c4 & 0xff);
return (value);
}
int
to16bit (int c1, int c2)
{
return ((c1 & 0xff) << 8) + (c2 & 0xff);
}
long
read32bit (FILE * stream)
{
int c1, c2, c3, c4;
c1 = my_getc (stream);
c2 = my_getc (stream);
c3 = my_getc (stream);
c4 = my_getc (stream);
return to32bit (c1, c2, c3, c4);
}
int
read16bit (FILE * stream)
{
int c1, c2;
c1 = my_getc (stream);
c2 = my_getc (stream);
return to16bit (c1, c2);
}
/*---------------------------------------------------------------------
Read Mid Header Chunk
return ntrks Number of Midi Tracks
-----------------------------------------------------------------------
*/
int
read_header_chunk (FILE * stream)
{
long length;
int type, ntrks, division;
char *str;
#ifdef DHC_DEBUG
fprintf (err_stream, "read_header_chunk()\n");
#endif /* DHC_DEBUG */
/* read chunk type */
str = read_n_byte (stream, 4);
if (strncmp (str, "MThd", 4))
{
fprintf (err_stream, "read_header_chunk() invalid chunk type : %s\n", str);
}
free (str);
if (feof (stream))
{
fprintf (err_stream, "read_header_chunk() end of file\n");
exit (1);
}
/* read length */
length = read32bit (stream);
#ifdef RHC_DEBUG
fprintf (err_stream, "length : %8ld\n", length);
#endif /* RHC_DEBUG */
/* read type */
my_getc (stream); /* first byte */
type = my_getc (stream); /* second byte */
switch (type)
{
case 0:
#ifdef DHC_DEBUG
fprintf (err_stream, "singel multi-channel track\n");
#endif /* DHC_DEBUG */
break;
case 1:
#ifdef DHC_DEBUG
fprintf (err_stream, "one or more simultaneous tracks\n");
#endif /* DHC_DEBUG */
//GlobalChannelFlag = 1;
break;
case 2:
#ifdef DHC_DEBUG
fprintf (err_stream, "one or more simultaneous independent single-track patterns\n");
#endif /* DHC_DEBUG */
fprintf (err_stream, "MIDI-FILE-TYPE : one or more simultaneous independent single-track patterns\n");
exit (0);
break;
}
/* read ntrks */
ntrks = read16bit (stream);
#ifdef RHC_DEBUG
fprintf (err_stream, "ntrks : %d\n", ntrks);
#endif /* RHC_DEBUG */
/* read division */
division = read16bit (stream);
#ifdef RHC_DEBUG
fprintf (err_stream, "division : %04x\n", division);
#endif /* RHC_DEBUG */
if (division & 0x8000)
{
#ifdef RHC_DEBUG
fprintf (err_stream, "negativ SMPTE format %d\n", ((division & 0x7F00) >> 8));
fprintf (err_stream, "ticks per frame %d\n", (division & 0xFF));
#endif /* RHC_DEBUG */
fprintf (err_stream, "negativ SMPTE format %d\n", ((division & 0x7F00) >> 8));
fprintf (err_stream, "ticks per frame %d\n", (division & 0xFF));
exit (1);
}
else
{
TicksPerQuaterNote = division & 0x7FFF;
#ifdef RHC_DEBUG
fprintf (err_stream, "ticks per quater-note %d\n", (division & 0x7FFF));
#endif /* RHC_DEBUG */
}
/* flush any extra stuff, in case the length of header is not 6 */
length -= 6;
while (length-- != 0L) my_getc (in_stream);
return (ntrks);
}
/*---------------------------------------------------------------------
Display chunk infos
-----------------------------------------------------------------------
*/
int
read_chunk_info (FILE * stream)
{
char *str;
long length, i;
/* read chunk type */
str = read_n_byte (stream, 4);
#ifdef RTC_DEBUG
fprintf (err_stream, "chunk type : %s\n", str);
#endif /* RTC_DEBUG */
fprintf (err_stream, "read_chunk_info() chunk type : %s\n", str);
free (str);
if (feof (stream))
{
fprintf (err_stream, "read_chunk_info() end of file\n");
}
/* read length */
length = read32bit (stream);
#ifdef RTC_DEBUG
fprintf (err_stream, "length : %8ld\n", length);
#endif /* RTC_DEBUG */
for (i = 0; i < length; i++)
{
my_getc (stream);
}
return (0);
}
/*---------------------------------------------------------------------
read midi track chunk
-----------------------------------------------------------------------
*/
int
read_track_chunk (FILE * stream)
{
char *str;
long length;
/* read chunk type */
str = read_n_byte (stream, 4);
#ifdef RTC_DEBUG
fprintf (err_stream, "chunk type : %s\n", str);
#endif /* RTC_DEBUG */
if (strncmp (str, "MTrk", 4))
{
fprintf (err_stream, "read header chunk invalid chunk type : %s\n", str);
}
free (str);
if (feof (stream))
{
fprintf (err_stream, "read_track_chunk() end of file\n");
}
/* read length */
length = read32bit (stream);
if (feof (stream))
{
fprintf (err_stream, "read_track_chunk() end of file\n");
}
#ifdef RTC_DEBUG
fprintf (err_stream, " length : %8ld", length);
#endif /* RTC_DEBUG */
GlobalTime = 0L;
rdl_output (0L, INIT_RDL_OUTPUT, 0, 0);
rdl_output (GlobalTime, RDL_FLUSH, 0, 0);
while (read_mtrk_event (stream) != ME_END_OF_TRACK);
return (0);
}
/*---------------------------------------------------------------------
-----------------------------------------------------------------------
*/
int
read_mtrk_event (FILE * stream)
{
long delta_time;
int ch, mode=NONE, channel;
uchar byte, running_status;
int note, velocity;
if (feof (stream))
{
/* flush output buffer */
rdl_output (GlobalTime, RDL_FLUSH, 0, 0);
return (ME_END_OF_TRACK);
}
delta_time = read_var_len (stream);
GlobalTime += delta_time;
#ifdef TIME_DEBUG
fprintf (err_stream, "delta time : %ld\n", delta_time);
#endif /* TIME_DEBUG */
ch = getc (stream);
if (feof (stream))
{
/* flush output buffer */
rdl_output (GlobalTime, RDL_FLUSH, 0, 0);
return (ME_END_OF_TRACK);
}
else
{
byte = (uchar) ch;
#ifdef RTE_INPUT_DEBUG
fprintf (err_stream, " %2X %3d %c\n", (int) ch, (int) ch, ch);
#endif /* RTE_INPUT_DEBUG */
if (!(byte & MIDI_EVENT)) /* midi event */
{
switch (mode)
{
case NOTE_ON:
case NOTE_OFF:
ungetc (byte, stream);
byte = running_status;
break;
default:
fprintf (err_stream, "%2x, %3d <-- no MIDI EVENT\n", byte, byte);
break;
}
}
{
mode = NONE;
running_status = byte;
switch (byte & 0xF0) /* midi channel events */
{
case NOTE_ON:
{
if (GlobalChannelFlag)
channel = GlobalChannel;
else
channel = byte & CHANNEL;
mode = NOTE_ON;
note = my_getc (stream);
velocity = my_getc (stream);
#ifdef NOTE_DEBUG
fprintf (err_stream, "note on %d ", channel);
fprintf (err_stream, "%2x ", note);
fprintf (err_stream, "%2x \n", velocity);
#endif /* NOTE_DEBUG */
if (velocity == 0)
rdl_output (GlobalTime, NOTE_OFF, channel, ++note);
else
rdl_output (GlobalTime, mode, channel, ++note);
}
break;
case NOTE_OFF:
{
if (GlobalChannelFlag)
channel = GlobalChannel;
else
channel = byte & CHANNEL;
mode = NOTE_OFF;
note = my_getc (stream);
velocity = my_getc (stream);
#ifdef NOTE_DEBUG
fprintf (err_stream, "%2x ", note);
fprintf (err_stream, "%2x ", velocity);
fprintf (err_stream, "note off %d\n", channel);
#endif /* NOTE_DEBUG */
rdl_output (GlobalTime, mode, channel, ++note);
}
break;
case ALL_NOTES_OFF:
{
if (GlobalChannelFlag)
channel = GlobalChannel;
else
channel = byte & CHANNEL;
mode = NONE;
#ifdef NOTE_DEBUG
fprintf (err_stream, "all notes off %d \n", channel);
#endif /* NOTE_DEBUG */
rdl_output (GlobalTime, mode, channel, 0);
}
break;
default:
break;
}
switch (byte) /* midi events */
{
case META_EVENT:
{
long len;
uchar meta_event;
meta_event = my_getc (stream);
#ifdef RTE_DEBUG
fprintf (err_stream, "meta event %x\n", meta_event);
#endif /* RTE_DEBUG */
len = read_var_len (stream);
if (feof (stream))
{
/* flush output buffer */
rdl_output (GlobalTime, RDL_FLUSH, 0, 0);
return (ME_END_OF_TRACK);
}
switch (meta_event)
{
case ME_SEQUENCE_NUMBER:
{
fprintf (out_stream, "* ME_SEQUENCE_NUMBER");
}
break;
case ME_TEXT_EVENT:
{
fprintf (out_stream, "* ME_TEXT_EVENT");
}
break;
case ME_COPYRIGTH:
{
fprintf (out_stream, "* ME_COPYRIGTH");
}
break;
case ME_TRACK_NAME:
{
fprintf (out_stream, "* ME_TRACK_NAME");
}
break;
case ME_INSTRUMENT_NAME:
{
fprintf (out_stream, "* ME_INSTRUMENT_NAME");
}
break;
case ME_LYRIC:
{
fprintf (out_stream, "* ME_LYRIC");
}
break;
case ME_MARKER:
{
fprintf (out_stream, "* ME_MARKER");
}
break;
case ME_CUE_POINT:
{
fprintf (out_stream, "* ME_CUE_POINT");
}
break;
case ME_CHANNEL_PREFIX:
{
fprintf (out_stream, "* ME_CHANNEL_PREFIX");
GlobalChannel = my_getc (stream);
//fprintf (stdout, "GLOBAL CHANNEL NUMBER %d", GlobalChannel);
len--;
}
break;
case ME_END_OF_TRACK:
{
/* flush output buffer */
rdl_output (GlobalTime, RDL_FLUSH, 0, 0);
fprintf (out_stream, "* ME_END_OF_TRACK\n");
return (ME_END_OF_TRACK);
}
break;
case ME_SET_TEMPO:
{
long tempo;
char *str;
fprintf (out_stream, "* ME_SET_TEMPO");
str = read_n_byte (stream, len);
len = 0;
tempo = to32bit (0, (int) str[0], (int) str[1], (int) str[2]);
if (str != NULL)
{
fprintf (out_stream, " microseconds-per-MIDI-quarter-note=%d\n", tempo);
free (str);
}
}
break;
case ME_SMPTE_OFFSET:
{
char *str;
fprintf (out_stream, "* ME_SMPTE_OFFSET");
str = read_n_byte (stream, len);
len = 0;
if (str != NULL)
{
fprintf (out_stream, " hour=%d minute=%d second=%d frame=%d fract-frame=%d\n",
(int) str[0], (int) str[1], (int) str[2], (int) str[3]);
free (str);
}
}
break;
case ME_TIME_SIGNATURE:
{
int denom = 1;
char *str;
fprintf (out_stream, "* ME_TIME_SIGNATURE");
str = read_n_byte (stream, len);
while (str[1]-- > 0)
denom *= 2;
len = 0;
if (str != NULL)
{
fprintf (out_stream, " %d/%d MIDI-clocks/click=%d 32nd-notes/24-MIDI-clocks=%d\n",
(int) str[0], denom, (int) str[2], (int) str[3]);
free (str);
}
}
break;
case ME_KEY_SIGNATUR:
{
fprintf (out_stream, "* ME_KEY_SIGNATUR");
}
break;
case ME_SEQUENCER_SPECIFIC:
{
fprintf (out_stream, "* ME_SEQUENCER_SPECIFIC");
}
break;
default:
fprintf (err_stream, "ERROR invalid metaevent\n");
break;
} /* end of meta event switch */
if (len > 0)
{
char *str;
str = read_n_byte (stream, len);
if (str != NULL)
{
fprintf (out_stream, " %s", str);
}
fputc ('\n', out_stream);
free (str);
}
}
break;
case SYS_EXCL_F0: /* system exclusiv massage */
case SYS_EXCL_F7: /* system exclusiv massage */
{
long len;
int i;
#ifdef RTE_DEBUG
fprintf (err_stream, "sys excl\n");
#endif /* RTE_DEBUG */
len = read_var_len (stream);
if (feof (stream))
{
/* flush output buffer */
rdl_output (GlobalTime, RDL_FLUSH, 0, 0);
return (ME_END_OF_TRACK);
}
#ifdef RTE_DEBUG
fprintf (err_stream, "length : %ld\n", len);
#endif /* RTE_DEBUG */
for (i = 0; i < len; i++)
{
#ifdef RTE_DEBUG
fprintf (err_stream, " %3d %x\n", i, my_getc (stream));
#endif /* RTE_DEBUG */
}
if (byte == SYS_EXCL_F0)
{
#ifdef RTE_DEBUG
fprintf (err_stream, " end %x\n", i, my_getc (stream));
#endif /* RTE_DEBUG */
}
}
break;
default:
break;
}
}
}
return (0);
}
/*---------------------------------------------------------------------
-----------------------------------------------------------------------
*/
int
input_read_error ()
{
fprintf (err_stream, "read error on input stream\n");
return (0);
}
/*---------------------------------------------------------------------
write_output_line
clock 1C 2C 3C ... 16C
xxxxxxxx #nnn#nnn#nnn ... #nnn
10 + 16*4 = 74 byte / line
-----------------------------------------------------------------------
*/
int
write_output_line (FILE * stream, long midi_clock, int *channel)
{
int i;
#ifdef DEBUG_NOTE_CODE
int nc;
#endif
#ifdef TEMP_BAR_INFO
if (BarFlag)
{
if (NextBarTime <= midi_clock)
{
do
{
NextBarTime += BarTime;
BarCount++;
if (BarCount < FromBar || BarCount > ToBar)
{
return (0);
}
else
{
fprintf (stream, "# bar %d\n", BarCount);
}
}
while (NextBarTime <= midi_clock);
}
if (BarCount < FromBar || BarCount > ToBar)
{
return (0);
}
}
#endif /* TEMP_BAR_INFO */
#ifdef DEBUG
fprintf (err_stream, "\n");
#endif /* */
#ifdef DEBUG_NOTE_CODE
if (note_code_flag)
{
fprintf (stream, "*** ");
for (i = 0; i < 16; i++)
{
if (channel[i] == 0)
{
fprintf (stream, "%s", " .. ");
}
else
{
nc = channel[i];
if (nc < 0)
nc = -nc;
nc--;
nc = nc % 12;
fputs (note_code[nc], stream);
}
}
fprintf (stream, "\n");
}
#endif /* */
fprintf (stream, "%9ld ", midi_clock);
for (i = 0; i < 16; i++)
{
if (channel[i] == 0)
{
fprintf (stream, "%s", " .. ");
}
else
{
fprintf (stream, "%+4d", channel[i]);
}
}
fprintf (stream, "\n");
return (0);
}
/*-------------------------------------------------------------------------
read line form temp file
return : timeinfo
timeinfo == -1 : end of file
---------------------------------------------------------------------------
*/
/* #define DEBUG_READ_LINE
*/
long read_line (FILE * tmp_stream, int channel_nodes[16])
{
long time;
int i = 0, ch, ret;
char buffer[100];
while ((ch = fgetc (tmp_stream)) == '*')
{
//putc (ch, out_stream);
while ((ch = my_getc (tmp_stream)) != '\n') /* commentar */
{
//putc (ch, out_stream);
}
//putc ('\n', out_stream);
}
ungetc (ch, tmp_stream);
if ((ret = fscanf (tmp_stream, "%9ld", &time)) != 1)