forked from xlvector/abcmidi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi2abc.c
3593 lines (3245 loc) · 94 KB
/
midi2abc.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
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
/*
* midi2abc - program to convert MIDI files to abc notation.
* Copyright (C) 1998 James Allwright
* e-mail: J.R.Allwright@westminster.ac.uk
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
/* new midi2abc - converts MIDI file to abc format files
*
*
* re-written to use dynamic data structures
* James Allwright
* 5th June 1998
*
* added output file option -o
* added summary option -sum
* added option -u to enter xunit directly
* fixed computation of xunit using -b option
* added -obpl (one bar per line) option
* add check for carriage return embedded inside midi text line
* Seymour Shlien 04/March/00
* made to conform as much as possible to the official version.
* check for drum track added
* when midi program channel is command encountered, we ensure that
* we are using the correct channel number for the Voice by sending
* a %%MIDI channel message.
*
* Many more changes (see doc/CHANGES)
*
* Seymour Shlien 2005
*
* based on public domain 'midifilelib' package.
*/
#define VERSION "3.01 January 01 2017"
#define SPLITCODE
/* Microsoft Visual C++ Version 6.0 or higher */
#ifdef _MSC_VER
#define ANSILIBS
#endif
#include <stdio.h>
#ifdef PCCFIX
#define stdout 1
#endif
/* define USE_INDEX if your C libraries have index() instead of strchr() */
#ifdef USE_INDEX
#define strchr index
#endif
#ifdef ANSILIBS
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#else
extern char* malloc();
extern char* strchr();
#endif
#include "midifile.h"
#define BUFFSIZE 200
/* declare MIDDLE C */
#define MIDDLE 72
void initfuncs();
void setupkey(int);
int testtrack(int trackno, int barbeats, int anacrusis);
int open_note(int chan, int pitch, int vol);
int close_note(int chan, int pitch, int *initvol);
/* Global variables and structures */
extern long Mf_toberead;
static FILE *F;
static FILE *outhandle; /* for producing the abc file */
int tracknum=0; /* track number */
int division; /* pulses per quarter note defined in MIDI header */
long tempo = 500000; /* the default tempo is 120 quarter notes/minute */
int unitlen; /* abc unit length usually defined in L: field */
int header_unitlen; /* first unitlen set */
int unitlen_set =0; /* once unitlen is set don't allow it to change */
int parts_per_unitlen = 2; /* specifies minimum quantization size */
long laston = 0; /* length of MIDI track in pulses or ticks */
char textbuff[BUFFSIZE]; /*buffer for handling text output to abc file*/
int trans[256], back[256]; /*translation tables for MIDI pitch to abc note*/
char atog[256]; /* translation tables for MIDI pitch to abc note */
int symbol[256]; /*translation tables for MIDI pitch to abc note */
int key[12];
int sharps;
int trackno, maintrack;
int format; /* MIDI file type */
int karaoke, inkaraoke;
int midline;
int tempocount=0; /* number of tempo indications in MIDI file */
int gotkeysig=0; /*set to 1 if keysignature found in MIDI file */
/* global parameters that may be set by command line options */
int xunit; /* pulses per abc unit length */
int tsig_set; /* flag - time signature already set by user */
int ksig_set; /* flag - key signature already set by user */
int xunit_set;/* flat - xunit already set by user */
int extracta; /* flag - get anacrusis from strong beat */
int guessu; /* flag - estimate xunit from note durations */
int guessa; /* flag - get anacrusis by minimizing tied notes */
int guessk; /* flag - guess key signature */
int summary; /* flag - output summary info of MIDI file */
int keep_short; /*flag - preserve short notes */
int swallow_rests; /* flag - absorb short rests */
int midiprint; /* flag - run midigram instead of midi2abc */
#ifdef SPLITCODE
int usesplits; /* flag - split measure into parts if needed */
#endif
int restsize; /* smallest rest to absorb */
/* [SS] 2017-01-01 */
/*int no_triplets; flag - suppress triplets or broken rhythm */
int allow_triplets; /* flag to allow triplets */
int allow_broken; /* flag to allow broken rhythms > < */
int obpl = 0; /* flag to specify one bar per abc text line */
int nogr = 0; /* flag to put a space between every note */
int bars_per_line=4; /* number of bars per output line */
int bars_per_staff=4; /* number of bars per music staff */
int asig, bsig; /* time signature asig/bsig */
int header_asig =0; /* first time signature encountered */
int header_bsig =0; /* first time signature encountered */
int header_bb; /* first ticks/quarter note encountered */
int active_asig,active_bsig; /* last time signature declared */
int last_asig, last_ksig; /* last time signature printed */
int barsize; /* barsize in parts_per_unitlen units */
int Qval; /* tempo - quarter notes per minute */
int verbosity=0; /* control amount of detail messages in abcfile*/
/* global arguments dependent on command line options or computed */
int anacrusis=0;
int bars;
int keysig;
int header_keysig= -50; /* header key signature */
int active_keysig = -50; /* last key signature declared */
int xchannel; /* channel number to be extracted. -1 means all */
/* structure for storing music notes */
struct anote {
int pitch; /* MIDI pitch */
int chan; /* MIDI channel */
int vel; /* MIDI velocity */
long time; /* MIDI onset time in pulses */
long dtnext; /* time increment to next note in pulses */
long tplay; /* note duration in pulses */
int xnum; /* number of xunits to next note */
int playnum; /* note duration in number of xunits */
int posnum; /* note position in xunits */
int splitnum; /* voice split number */
/* int denom; */
};
/* linked list of notes */
struct listx {
struct listx* next;
struct anote* note;
};
/* linked list of text items (strings) */
struct tlistx {
struct tlistx* next;
char* text;
long when; /* time in pulses to output */
int type; /* 0 - comments, other - field commands */
};
/* a MIDI track */
struct atrack {
struct listx* head; /* first note */
struct listx* tail; /* last note */
struct tlistx* texthead; /* first text string */
struct tlistx* texttail; /* last text string */
int notes; /* number of notes in track */
long tracklen;
long startwait;
int startunits;
int drumtrack;
};
/* can cope with up to 64 track MIDI files */
struct atrack track[64];
int trackcount = 0;
int maxbarcount = 0;
/* maxbarcount is used to return the numbers of bars created.*/
/* obpl is a flag for one bar per line. */
/* double linked list of notes */
/* used for temporary list of chords while abc is being generated */
struct dlistx {
struct dlistx* next;
struct dlistx* last;
struct anote* note;
};
int notechan[2048],notechanvol[2048]; /*for linking on and off midi
channel commands */
int last_tick; /* for getting last pulse number in MIDI file */
char *title = NULL; /* for pasting title from argv[] */
char *origin = NULL; /* for adding O: info from argv[] */
void remove_carriage_returns();
int validnote();
void printpitch(struct anote*);
void printfract(int, int);
/* Stage 1. Parsing MIDI file */
/* Functions called during the reading pass of the MIDI file */
/* The following C routines are required by midifilelib. */
/* They specify the action to be taken when various items */
/* are encountered in the MIDI. The mfread function scans*/
/* the MIDI file and calls these functions when needed. */
int filegetc()
{
return(getc(F));
}
void fatal_error(s)
char* s;
/* fatal error encounterd - abort program */
{
fprintf(stderr, "%s\n", s);
exit(1);
}
void event_error(s)
char *s;
/* problem encountered but OK to continue */
{
char msg[256];
sprintf(msg, "Error: Time=%ld Track=%d %s\n", Mf_currtime, trackno, s);
printf("%s",msg);
}
int* checkmalloc(bytes)
/* malloc with error checking */
int bytes;
{
int *p;
p = (int*) malloc(bytes);
if (p == NULL) {
fatal_error("Out of memory error - cannot malloc!");
};
return (p);
}
char* addstring(s)
/* create space for string and store it in memory */
char* s;
{
char* p;
p = (char*) checkmalloc(strlen(s)+1);
strcpy(p, s);
return(p);
}
void addtext(s, type)
/* add structure for text */
/* used when parsing MIDI file */
char* s;
int type;
{
struct tlistx* newx;
newx = (struct tlistx*) checkmalloc(sizeof(struct tlistx));
newx->next = NULL;
newx->text = addstring(s);
newx->type = type;
newx->when = Mf_currtime;
if (track[trackno].texthead == NULL) {
track[trackno].texthead = newx;
track[trackno].texttail = newx;
}
else {
track[trackno].texttail->next = newx;
track[trackno].texttail = newx;
};
}
/* The MIDI file has separate commands for starting */
/* and stopping a note. In order to determine the duration of */
/* the note it is necessary to find the note_on command associated */
/* with the note off command. We rely on the note's pitch and channel*/
/* number to find the right note. While we are parsing the MIDI file */
/* we maintain a list of all the notes that are currently on */
/* head and tail of list of notes still playing. */
/* The following doubly linked list is used for this purpose */
struct dlistx* playinghead;
struct dlistx* playingtail;
void noteplaying(p)
/* This function adds a new note to the playinghead list. */
struct anote* p;
{
struct dlistx* newx;
newx = (struct dlistx*) checkmalloc(sizeof(struct dlistx));
newx->note = p;
newx->next = NULL;
newx->last = playingtail;
if (playinghead == NULL) {
playinghead = newx;
};
if (playingtail == NULL) {
playingtail = newx;
}
else {
playingtail->next = newx;
playingtail = newx;
};
}
void addnote(p, ch, v)
/* add structure for note */
/* used when parsing MIDI file */
int p, ch, v;
{
struct listx* newx;
struct anote* newnote;
track[trackno].notes = track[trackno].notes + 1;
newx = (struct listx*) checkmalloc(sizeof(struct listx));
newnote = (struct anote*) checkmalloc(sizeof(struct anote));
newx->next = NULL;
newx->note = newnote;
if (track[trackno].head == NULL) {
track[trackno].head = newx;
track[trackno].tail = newx;
}
else {
track[trackno].tail->next = newx;
track[trackno].tail = newx;
};
if (ch == 9) {
track[trackno].drumtrack = 1;
};
newnote->pitch = p;
newnote->chan = ch;
newnote->vel = v;
newnote->time = Mf_currtime;
laston = Mf_currtime;
newnote->tplay = Mf_currtime;
noteplaying(newnote);
}
void notestop(p, ch)
/* MIDI note stops */
/* used when parsing MIDI file */
int p, ch;
{
struct dlistx* i;
int found;
char msg[80];
i = playinghead;
found = 0;
while ((found == 0) && (i != NULL)) {
if ((i->note->pitch == p)&&(i->note->chan==ch)) {
found = 1;
}
else {
i = i->next;
};
};
if (found == 0) {
sprintf(msg, "Note terminated when not on - pitch %d", p);
event_error(msg);
return;
};
/* fill in tplay field */
i->note->tplay = Mf_currtime - (i->note->tplay);
/* remove note from list */
if (i->last == NULL) {
playinghead = i->next;
}
else {
(i->last)->next = i->next;
};
if (i->next == NULL) {
playingtail = i->last;
}
else {
(i->next)->last = i->last;
};
free(i);
}
FILE *
efopen(name,mode)
char *name;
char *mode;
{
FILE *f;
if ( (f=fopen(name,mode)) == NULL ) {
char msg[256];
sprintf(msg,"Error - Cannot open file %s",name);
fatal_error(msg);
}
return(f);
}
void error(s)
char *s;
{
fprintf(stderr,"Error: %s\n",s);
}
void txt_header(xformat,ntrks,ldivision)
int xformat, ntrks, ldivision;
{
division = ldivision;
format = xformat;
if (format != 0) {
/* fprintf(outhandle,"%% format %d file %d tracks\n", format, ntrks);*/
if(summary>0) printf("This midi file has %d tracks\n\n",ntrks);
}
else {
/* fprintf(outhandle,"%% type 0 midi file\n"); */
if(summary>0) {
printf("This is a type 0 midi file.\n");
printf("All the channels are in one track.\n");
printf("You may need to process the channels separately\n\n");
}
}
}
void txt_trackstart()
{
laston = 0L;
track[trackno].notes = 0;
track[trackno].head = NULL;
track[trackno].tail = NULL;
track[trackno].texthead = NULL;
track[trackno].texttail = NULL;
track[trackno].tracklen = Mf_currtime;
track[trackno].drumtrack = 0;
}
void txt_trackend()
{
/* check for unfinished notes */
if (playinghead != NULL) {
printf("Error in MIDI file - notes still on at end of track!\n");
};
track[trackno].tracklen = Mf_currtime - track[trackno].tracklen;
trackno = trackno + 1;
trackcount = trackcount + 1;
}
void txt_noteon(chan,pitch,vol)
int chan, pitch, vol;
{
if ((xchannel == -1) || (chan == xchannel)) {
if (vol != 0) {
addnote(pitch, chan, vol);
}
else {
notestop(pitch, chan);
};
};
}
void txt_noteoff(chan,pitch,vol)
int chan, pitch, vol;
{
if ((xchannel == -1) || (chan == xchannel)) {
notestop(pitch, chan);
};
}
void txt_pressure(chan,pitch,press)
int chan, pitch, press;
{
}
void txt_parameter(chan,control,value)
int chan, control, value;
{
}
void txt_pitchbend(chan,lsb,msb)
int chan, msb, lsb;
{
}
void txt_program(chan,program)
int chan, program;
{
/*
sprintf(textbuff, "%%%%MIDI program %d %d",
chan+1, program);
*/
sprintf(textbuff, "%%%%MIDI program %d", program);
addtext(textbuff,0);
/* abc2midi does not use the same channel number as specified in
the original midi file, so we should not specify that channel
number in the %%MIDI program. If we leave it out the program
will refer to the current channel assigned to this voice.
*/
}
void txt_chanpressure(chan,press)
int chan, press;
{
}
void txt_sysex(leng,mess)
int leng;
char *mess;
{
}
void txt_metamisc(type,leng,mess)
int type, leng;
char *mess;
{
}
void txt_metaspecial(type,leng,mess)
int type, leng;
char *mess;
{
}
void txt_metatext(type,leng,mess)
int type, leng;
char *mess;
{
char *ttype[] = {
NULL,
"Text Event", /* type=0x01 */
"Copyright Notice", /* type=0x02 */
"Sequence/Track Name",
"Instrument Name", /* ... */
"Lyric",
"Marker",
"Cue Point", /* type=0x07 */
"Unrecognized"
};
int unrecognized = (sizeof(ttype)/sizeof(char *)) - 1;
unsigned char c;
int n;
char *p = mess;
char *buff;
char buffer2[BUFFSIZE];
if ((type < 1)||(type > unrecognized))
type = unrecognized;
buff = textbuff;
for (n=0; n<leng; n++) {
c = *p++;
if (buff - textbuff < BUFFSIZE - 6) {
sprintf(buff,
(isprint(c)||isspace(c)) ? "%c" : "\\0x%02x" , c);
buff = buff + strlen(buff);
};
}
if (strncmp(textbuff, "@KMIDI KARAOKE FILE", 14) == 0) {
karaoke = 1;
}
else {
if ((karaoke == 1) && (*textbuff != '@')) {
addtext(textbuff,0);
}
else {
if (leng < BUFFSIZE - 3) {
sprintf(buffer2, " %s", textbuff);
addtext(buffer2,0);
};
};
};
}
void txt_metaseq(num)
int num;
{
sprintf(textbuff, "%%Meta event, sequence number = %d",num);
addtext(textbuff,0);
}
void txt_metaeot()
/* Meta event, end of track */
{
}
void txt_keysig(sf,mi)
char sf, mi;
{
int accidentals;
gotkeysig =1;
sprintf(textbuff,
"%% MIDI Key signature, sharp/flats=%d minor=%d",
(int) sf, (int) mi);
if(verbosity) addtext(textbuff,0);
sprintf(textbuff,"%d %d\n",sf,mi);
if (!ksig_set) {
addtext(textbuff,1);
keysig=sf;
}
if (header_keysig == -50) header_keysig = keysig;
if (summary <= 0) return;
/* There may be several key signature changes in the midi
file so that key signature in the mid file does not conform
with the abc file. Show all key signature changes.
*/
accidentals = (int) sf;
if (accidentals <0 )
{
accidentals = -accidentals;
printf("Key signature: %d flats", accidentals);
}
else
printf("Key signature : %d sharps", accidentals);
if (ksig_set) printf(" suppressed\n");
else printf("\n");
}
void txt_tempo(ltempo)
long ltempo;
{
if(tempocount>0) return; /* ignore other tempo indications */
tempo = ltempo;
tempocount++;
}
void setup_timesig(nn, denom, bb)
int nn,denom,bb;
{
asig = nn;
bsig = denom;
/* we must keep unitlen and xunit fixed for the entire tune */
if (unitlen_set == 0) {
unitlen_set = 1;
if ((asig*4)/bsig >= 3) {
unitlen =8;
}
else {
unitlen = 16;
};
}
/* set xunit for this unitlen */
if(!xunit_set) xunit = (division*bb*4)/(8*unitlen);
barsize = parts_per_unitlen*asig*unitlen/bsig;
/* printf("setup_timesig: unitlen=%d xunit=%d barsize=%d\n",unitlen,xunit,barsize); */
if (header_asig ==0) {header_asig = asig;
header_bsig = bsig;
header_unitlen = unitlen;
header_bb = bb;
}
}
void txt_timesig(nn,dd,cc,bb)
int nn, dd, cc, bb;
{
int denom = 1;
while ( dd-- > 0 )
denom *= 2;
sprintf(textbuff,
"%% Time signature=%d/%d MIDI-clocks/click=%d 32nd-notes/24-MIDI-clocks=%d",
nn,denom,cc,bb);
if (verbosity) addtext(textbuff,0);
sprintf(textbuff,"%d %d %d\n",nn,denom,bb);
if (!tsig_set) {
addtext(textbuff,2);
setup_timesig(nn, denom,bb);
}
if (summary>0) {
if(tsig_set) printf("Time signature = %d/%d suppressed\n",nn,denom);
else printf("Time signature = %d/%d\n",nn,denom);
}
}
void txt_smpte(hr,mn,se,fr,ff)
int hr, mn, se, fr, ff;
{
}
void txt_arbitrary(leng,mess)
char *mess;
int leng;
{
}
/* Dummy functions for handling MIDI messages.
* */
void no_op0() {}
void no_op1(int dummy1) {}
void no_op2(int dummy1, int dummy2) {}
void no_op3(int dummy1, int dummy2, int dummy3) { }
void no_op4(int dummy1, int dummy2, int dummy3, int dummy4) { }
void no_op5(int dummy1, int dummy2, int dummy3, int dummy4, int dummy5) { }
void print_txt_noteon(chan, pitch, vol)
int chan, pitch, vol;
{
int start_time;
int initvol;
if (vol > 0)
open_note(chan, pitch, vol);
else {
start_time = close_note(chan, pitch,&initvol);
if (start_time >= 0)
/* printf("%8.4f %8.4f %d %d %d %d\n",
(double) start_time/(double) division,
(double) Mf_currtime/(double) division,
trackno+1, chan +1, pitch,initvol);
*/
printf("%d %ld %d %d %d %d\n",
start_time, Mf_currtime, trackno+1, chan +1, pitch,initvol);
if(Mf_currtime > last_tick) last_tick = Mf_currtime;
}
}
void print_txt_noteoff(chan, pitch, vol)
int chan, pitch, vol;
{
int start_time,initvol;
start_time = close_note(chan, pitch, &initvol);
if (start_time >= 0)
/*
printf("%8.4f %8.4f %d %d %d %d\n",
(double) start_time/(double) division,
(double) Mf_currtime/(double) division,
trackno+1, chan+1, pitch,initvol);
*/
printf("%d %ld %d %d %d %d\n",
start_time, Mf_currtime, trackno+1, chan +1, pitch,initvol);
if(Mf_currtime > last_tick) last_tick = Mf_currtime;
}
/* In order to associate a channel note off message with its
* corresponding note on message, we maintain the information
* the notechan array. When a midi pitch (0-127) is switched
* on for a particular channel, we record the time that it
* was turned on in the notechan array. As there are 16 channels
* and 128 pitches, we initialize an array 128*16 = 2048 elements
* long.
**/
void init_notechan()
{
/* signal that there are no active notes */
int i;
for (i = 0; i < 2048; i++) notechan[i] = -1;
}
/* The next two functions update notechan when a channel note on
or note off is encountered. The second function close_note,
returns the time when the note was turned on.
*/
int open_note(int chan, int pitch, int vol)
{
notechan[128 * chan + pitch] = Mf_currtime;
notechanvol[128 * chan + pitch] = vol;
return 0;
}
int close_note(int chan, int pitch, int *initvol)
{
int index, start_tick;
index = 128 * chan + pitch;
if (notechan[index] < 0)
return -1;
start_tick = notechan[index];
*initvol = notechanvol[index];
notechan[index] = -1;
return start_tick;
}
/* mftext mode */
int prtime()
{
/* if(Mf_currtime >= pulses) ignore=0;
if (ignore) return 1;
linecount++;
if(linecount > maxlines) {fclose(F); exit(0);}
*/
int units;
units = 2;
if(units==1)
/*seconds*/
printf("%6.2f ",mf_ticks2sec(Mf_currtime,division,tempo));
else if (units==2)
/*beats*/
printf("%6.2f ",(float) Mf_currtime/(float) division);
else
/*pulses*/
printf("%6ld ",Mf_currtime);
return 0;
}
char * pitch2key(int note)
{
static char name[5];
char* s = name;
switch(note % 12)
{
case 0: *s++ = 'c'; break;
case 1: *s++ = 'c'; *s++ = '#'; break;
case 2: *s++ = 'd'; break;
case 3: *s++ = 'd'; *s++ = '#'; break;
case 4: *s++ = 'e'; break;
case 5: *s++ = 'f'; break;
case 6: *s++ = 'f'; *s++ = '#'; break;
case 7: *s++ = 'g'; break;
case 8: *s++ = 'g'; *s++ = '#'; break;
case 9: *s++ = 'a'; break;
case 10: *s++ = 'a'; *s++ = '#'; break;
case 11: *s++ = 'b'; break;
}
sprintf(s, "%d", (note / 12)-1); /* octave (assuming Piano C4 is 60)*/
return name;
}
void pitch2drum(midipitch)
int midipitch;
{
static char *drumpatches[] = {
"Acoustic Bass Drum", "Bass Drum 1", "Side Stick", "Acoustic Snare",
"Hand Clap", "Electric Snare", "Low Floor Tom", "Closed Hi Hat",
"High Floor Tom", "Pedal Hi-Hat", "Low Tom", "Open Hi-Hat",
"Low-Mid Tom", "Hi Mid Tom", "Crash Cymbal 1", "High Tom",
"Ride Cymbal 1", "Chinese Cymbal", "Ride Bell", "Tambourine",
"Splash Cymbal", "Cowbell", "Crash Cymbal 2", "Vibraslap",
"Ride Cymbal 2", "Hi Bongo", "Low Bongo", "Mute Hi Conga",
"Open Hi Conga", "Low Conga", "High Timbale", "Low Timbale",
"High Agogo", "Low Agogo", "Cabasa", "Maracas",
"Short Whistle", "Long Whistle", "Short Guiro", "Long Guiro",
"Claves", "Hi Wood Block", "Low Wood Block", "Mute Cuica",
"Open Cuica", "Mute Triangle", "Open Triangle" };
if (midipitch >= 35 && midipitch <= 81) {
printf(" (%s)",drumpatches[midipitch-35]);
}
}
void mftxt_header (int format, int ntrks, int ldivision)
{
division = ldivision;
printf("Header format=%d ntrks=%d division=%d\n",format,ntrks,division);
}
void mftxt_trackstart()
{
int numbytes;
tracknum++;
numbytes = Mf_toberead;
/*if(track != 0 && tracknum != track) {ignore_bytes(numbytes); return;} */
printf("Track %d contains %d bytes\n",tracknum,numbytes);
}
void mftxt_noteon(chan,pitch,vol)
int chan, pitch, vol;
{
char *key;
/*
if (onlychan >=0 && chan != onlychan) return;
*/
if (prtime()) return;
key = pitch2key(pitch);
printf("Note on %2d %2d (%3s) %3d",chan+1, pitch, key,vol);
if (chan == 9) pitch2drum(pitch);
printf("\n");
}
void mftxt_noteoff(chan,pitch,vol)
int chan, pitch, vol;
{
char *key;
/*
if (onlychan >=0 && chan != onlychan) return;
*/
if (prtime()) return;
key = pitch2key(pitch);
printf("Note off %2d %2d (%3s) %3d\n",chan+1,pitch, key,vol);
}
void mftxt_pressure(chan,pitch,press)
int chan, pitch, press;
{
char *key;
if (prtime()) return;
key = pitch2key(pitch);
printf("Pressure %2d %3s %3d\n",chan+1,key,press);
}
void mftxt_pitchbend(chan,lsb,msb)
int chan, lsb, msb;
{
float bend;
int pitchbend;
/*
if (onlychan >=0 && chan != onlychan) return;
*/
if (prtime()) return;
/* [SS] 2014-01-05 2015-08-04*/
pitchbend = (msb*128 + lsb);
bend = (float) (pitchbend - 8192);
bend = bend/4096.0f;
printf("Pitchbend %2d %d bend = %6.4f\n",chan+1,pitchbend,bend);
}
void mftxt_program(chan,program)
int chan, program;
{
static char *patches[] = {
"Acoustic Grand","Bright Acoustic","Electric Grand","Honky-Tonk",
"Electric Piano 1","Electric Piano 2","Harpsichord","Clav",
"Celesta", "Glockenspiel", "Music Box", "Vibraphone",
"Marimba", "Xylophone", "Tubular Bells", "Dulcimer",
"Drawbar Organ", "Percussive Organ", "Rock Organ", "Church Organ",
"Reed Organ", "Accordian", "Harmonica", "Tango Accordian",
"Acoustic Guitar (nylon)", "Acoustic Guitar (steel)",
"Electric Guitar (jazz)", "Electric Guitar (clean)",
"Electric Guitar (muted)", "Overdriven Guitar",
"Distortion Guitar", "Guitar Harmonics",
"Acoustic Bass", "Electric Bass (finger)",
"Electric Bass (pick)", "Fretless Bass",
"Slap Bass 1", "Slap Bass 2", "Synth Bass 1", "Synth Bass 2",
"Violin", "Viola", "Cello", "Contrabass",
"Tremolo Strings", "Pizzicato Strings",
"Orchestral Strings", "Timpani",