-
Notifications
You must be signed in to change notification settings - Fork 15
/
midicopy.c
1977 lines (1617 loc) · 41.3 KB
/
midicopy.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
/*
* This program is a clone of midifile.c created by
* Thompson and Czeiszperger, 1989. As a result there
* are many remnant variables and functions from that
* file.
*
* The program is designed to copy a selected part or whole
* midi file onto a new midi file. You can select a specific
* time interval or specific tracks or channels. The new
* midi file can then be played using a standard midi player.
* Thus one can listen to any selected voice and time
* time interval of a midi file.
* In order to do this we must keep track of all notes running
* at a specific time so that we can turn them off when we
* come to the end of the time segment. The array notechan is
* used to keep track of which notes are still playing.
* When we start copying from the middle of a midi file,
* we must ensure that the player is in the proper state (right
* tempo, right programs) for that time. Many midi files
* may change the tempo or instrument arrangements. To ensure
* the midi file maintains this state we copy all messages
* except note_on/note_off prior to the beginning of the
* start of the output file. The midi time (determined
* by delta_time) is locked to zero until we reach the
* start time for the output file.
*
* Overview of function calls.
* ---------------------------
* All the work is done by mfwrite, which copies the
* tracks individually thru the function mf_write_track_chunk.
* mf_write_track_chunk either copies the whole track verbatim
* or else parses the track and copies each command individually
* using the function readtrack(). readtrack calls chanmessage()
* or metaevent () depending on the nature ofthe command.
*
*/
#define VERSION "1.22 November 15 2015"
#include "midicopy.h"
#define NULLFUNC 0
#define NULL 0
#ifndef PCCFIX
#include <stdlib.h>
#endif
#include <stdio.h>
char *strcpy (), *strcat ();
int strcmp();
/* Functions to be called while processing the MIDI file. */
int (*Mf_arbitrary) () = NULLFUNC;
int (*Mf_seqspecific) () = NULLFUNC;
/* Functions to implement in order to write a MIDI file */
int (*Mf_writetempotrack) () = NULLFUNC;
void mf_write_header_chunk (int, int, int);
void mf_write_tempo (long);
void mf_get_tempo_event (long tempo);
int mf_write_midi_event (int type, int chan, char *data, int size);
int mf_write_meta_event (int type, char *data, int size);
void mf_write_track_chunk (int which_track, FILE * fp);
void mferror (char *s);
int eputc (char c);
void append_to_string ();
void winamp_compatibility_measure ();
void writechanmsg_at_0 ();
void copy_noteoff (int chan, int c1, int c2);
int Mf_nomerge = 0; /* 1 => continue'ed system exclusives are */
/* not collapsed. */
long Mf_currtime = 0L; /* current time in delta-time units */
long Mf_toberead = 0L;
long delta_time;
float currentseconds = 0.0;
long max_currtime = 0;
long Mf_currcopytime = 0L; /* time of last copied event */
char *trackdata = NULL;
long trackdata_length, trackdata_size;
int activetrack;
int nochanmsg = 1;
long newtempo = 0; /* to handle tempo [SS] 2013-09-04 */
float speedfactor = 1.00; /* to handle tempo [SS] 2013-09-06 */
int newspeed = 0; /* signal application of speedfactor [SS] 2013-09-06 */
int selected_drum = 0; /* [SS] 2013-09-07 */
int drumlevel = 0; /* [SS] 2013-09-07 */
int nondrumlevel = 0; /* [SS] 2013-09-15 */
int mutenodrum = 0; /* [SS] 2013-09-15 */
int chosen_drum = 0; /* [SS] 2013-10-01 */
int drumvelocity = 0; /* [SS] 2013-10-01 */
long Mf_numbyteswritten = 0L;
long readvarinum ();
long read32bit ();
long to32bit ();
int read16bit ();
int to16bit ();
char *msg ();
int seconds_to_tick (float seconds);
float tick_to_seconds (int tick);
/* following declaration added 27/8/96 JRA (James Allwright)*/
void badbyte (int);
void metaevent (int);
void sysex ();
void chanmessage (int, int, int);
void msginit ();
int msgleng ();
void msgadd ();
void biggermsg ();
void WriteVarLen (long);
int notechan[2048]; /* keeps track of running voices */
int tocopy[32]; /* tracks to copy */
int ctocopy[16]; /* channels to copy */
int chnflag = 0; /* flag indicating not all channels selected */
int verbatim = 1; /* flag for verbatim transfer */
FILE *F_in, *fp;
int format, ntrks, division;
int start_tick, end_tick, flag_metaeot;
float start_seconds, end_seconds;
int use_seconds = 0;
int current_tempo = 500000;
float seconds_output;
struct tempostruc
{
int tick;
int tempo;
float seconds;
} tempo_array[2000];
int temposize, tempo_index;
/* Support stuff */
/* The next four functions are used to ensure that all
notes still on are turned off at the end of the time
segment.
*/
int
init_notechan ()
{
/* indicate that there are no active notes */
int i;
for (i = 0; i < 2048; i++)
notechan[i] = -1;
return 0;
}
int
open_note (int chan, int pitch)
/* When a note for a specific channel is turned on, we save the
time when it was switched on.
*/
{
int index;
/*printf("%f %d\n",Mf_currtime/(float) division,pitch); */
index = 128 * chan + pitch;
if (index < 0 || index > 2047)
printf ("illegal chan/pitch %d %d\n", chan, pitch);
notechan[index] = Mf_currtime;
return 0;
}
int
close_note (int chan, int pitch)
{
int index;
index = 128 * chan + pitch;
if (index < 0 || index > 2047)
printf ("illegal chan/pitch %d %d\n", chan, pitch);
notechan[index] = -1;
return 0;
}
/* send a noteoff channel command for all notes still playing. */
void
turn_off_all_playing_notes ()
{
int i, chan, pitch;
for (i = 0; i < 2048; i++)
if (notechan[i] >= 0)
{
chan = i / 128;
pitch = i % 128;
copy_noteoff (chan, pitch, 0);
}
}
void
append_to_string (int c)
{
trackdata[trackdata_length] = c;
trackdata_length++;
if (trackdata_length > trackdata_size)
{
printf ("trackdata overflow\n");
exit (1);
}
}
void
replace_byte_in_trackdata (int loc, char val)
{
trackdata[loc] = val;
}
void
error (char *s)
{
fprintf (stderr, "Error: %s\n", s);
}
int
cut_beginning ()
{
if (Mf_currtime < start_tick)
return 1;
return 0;
}
int
cut_ending ()
{
if (end_tick > 0 && Mf_currtime > end_tick)
return 1;
return 0;
}
/* Midi Channel Commands */
void
copy_noteon (int chan, int c1, int c2)
{
char data[2];
if (c2 > 0)
open_note (chan, c1);
else
close_note (chan, c1);
if (cut_beginning ())
return;
data[0] = (char) c1;
data[1] = (char) c2;
mf_write_midi_event (0x90, chan, data, 2);
}
void
copy_noteoff (int chan, int c1, int c2)
{
char data[2];
close_note (chan, c1);
if (cut_beginning ())
return;
data[0] = (char) c1;
data[1] = (char) c2;
mf_write_midi_event (0x80, chan, data, 2);
}
void
copy_pressure (int chan, int c1, int c2)
{
char data[2];
if (cut_beginning ())
return;
data[0] = (char) c1;
data[1] = (char) c2;
mf_write_midi_event (0xa0, chan, data, 2);
}
void
copy_parameter (int chan, int c1, int c2)
{
char data[2];
if (cut_beginning ())
return;
data[0] = (char) c1;
data[1] = (char) c2;
mf_write_midi_event (0xb0, chan, data, 2);
}
void
copy_program (int chan, int c1)
{
char data[1];
data[0] = (char) c1;
mf_write_midi_event (0xc0, chan, data, 1);
}
void
copy_chanpressure (int chan, int c1)
{
char data[1];
data[0] = (char) c1;
mf_write_midi_event (0xd0, chan, data, 1);
}
void
copy_pitchbend (int chan, int c1, int c2)
{
char data[2];
if (cut_beginning ())
return;
data[0] = c1;
data[1] = c2;
mf_write_midi_event (0xe0, chan, data, 2);
}
/* Meta commands are handled here */
void
copy_metatext (int type, int length, char *m)
{
mf_write_meta_event (type, m, length);
}
void
copy_timesig (c1, c2, c3, c4)
int c1, c2, c3, c4;
{
char data[4];
data[0] = (char) c1;
data[1] = (char) c2;
data[2] = (char) c3;
data[3] = (char) c4;
mf_write_meta_event (0x58, data, 4);
}
void
copy_keysig (c1, c2)
int c1, c2;
{
char data[2];
data[0] = (char) c1;
data[1] = (char) c2;
mf_write_meta_event (0x59, data, 2);
}
/* Metaevents */
/* 2015-11-14 [SS] */
void
copy_metaseqnum (c1, c2)
unsigned char c1,c2;
{
char data[2];
data[0] = (char) c1;
data[1] = (char) c2;
mf_write_meta_event (0x00, (char *) data, 2);
}
void
copy_metaspecial (int length, char *m)
{
mf_write_meta_event (0x7f, m, length);
}
void
copy_metamisc (int type, int length, char *m)
{
mf_write_meta_event (type, m, length);
}
void
copy_metaeot ()
{
void WriteVarLen ();
WriteVarLen (10);
Mf_currcopytime += 10;
eputc ((char) 0xff);
eputc ((char) 0x2f);
eputc ((char) 0x00);
}
void
copy_sysex (int length, char *s)
{
int i;
/* readtrack in midifile3 copies the starting 0xf0 sys command
followed by the message. In order to copy the input, we
need to also copy the length of the message. The midi
specs seem to be not clear, on whether length of message
should be included, or whether f0 f7 completely delineate
the sysex command.
*/
WriteVarLen (Mf_currtime - Mf_currcopytime);
if (!cut_beginning ())
Mf_currcopytime = Mf_currtime;
/*printf("delta_time = %d\n",delta_time);*/
eputc ((char) 0xf0);
WriteVarLen (length - 1); /* don't count the starting 0xf0 byte */
for (i = 1; i < length; i++)
eputc (s[i]);
}
/* The midi standard requires that we precede the stream
of midi commands in a track with a number indicating the
number of bytes in the track. Since we don't know this
in advance, we just append everything into a long string
and count the number of bytes in the string before writing
it to the output file. It uses more memory, but memory
is now cheap and there are not many midi files one megabyte
long.
*/
void
alloc_trackdata ()
{
if (trackdata != NULL)
free (trackdata);
/* double it since running status is not preserved [SS] 2013-10-08 */
trackdata = (char *) malloc (Mf_toberead + Mf_toberead );
trackdata_size = Mf_toberead + Mf_toberead ;
trackdata_length = 0;
trackdata[0] = 0;
}
int
readmt (s) /* read through the "MThd" or "MTrk" header string */
char *s;
{
int n = 0;
char *p = s;
int c;
while (n++ < 4 && (c = getc (F_in)) != EOF)
{
if (c != *p++)
{
char buff[32];
(void) strcpy (buff, "expecting ");
(void) strcat (buff, s);
mferror (buff);
}
}
return (c);
}
static int
egetc ()
{ /* read a single character and abort on EOF */
int c = getc (F_in);
if (c == EOF)
mferror ("premature EOF");
Mf_toberead--;
return (c);
}
void
readheader ()
{ /* read a header chunk */
if (readmt ("MThd") == EOF)
return;
Mf_toberead = read32bit ();
format = read16bit ();
ntrks = read16bit ();
division = read16bit ();
/* flush any extra stuff, in case the length of header is not 6 */
while (Mf_toberead > 0)
(void) egetc ();
}
void
append_rest_of_track ()
{
int err;
err = fread (trackdata + trackdata_length, 1, Mf_toberead, F_in);
trackdata_length += err;
if (err != Mf_toberead)
error ("fread error in append_rest_of_track()");
Mf_toberead = 0;
}
void
ignore_rest_of_track ()
{
while (Mf_toberead > 0)
egetc ();
}
void
copytrack_verbatim ()
{
if (readmt ("MTrk") == EOF)
return;
Mf_toberead = read32bit ();
alloc_trackdata ();
append_rest_of_track ();
}
int
readtrack ()
{ /* read a track chunk */
/* This array is indexed by the high half of a status byte. It's */
/* value is either the number of bytes needed (1 or 2) for a channel */
/* message, or 0 (meaning it's not a channel message). */
static int chantype[] = {
0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 through 0x70 */
2, 2, 2, 2, 1, 1, 2, 0 /* 0x80 through 0xf0 */
};
long lookfor;
int c, c1, type;
int sysexcontinue = 0; /* 1 if last message was an unfinished sysex */
int running = 0; /* 1 when running status used */
int status = 0; /* status value (e.g. 0x90==note-on) */
int laststatus; /* for running status [SS] 2013-09-10 */
int needed;
long varinum;
laststatus = 0; /* [SS] 2013-09-10 */
tempo_index = 0;
if (readmt ("MTrk") == EOF)
return (0);
Mf_toberead = read32bit ();
Mf_currtime = 0;
Mf_currcopytime = 0;
currentseconds = 0.0;
alloc_trackdata ();
if (cut_beginning ())
{
delta_time = 0;
Mf_currcopytime = start_tick; /* to avoid long gap at begining */
}
while (Mf_toberead > 0)
{
delta_time = readvarinum ();
Mf_currtime += delta_time;
if (cut_ending ())
{
flag_metaeot = 1;
/* delta_time += end_tick - Mf_currtime; */
/* Mf_currtime = end_tick; [SS] 2005-08-17 */
break;
}
c = egetc ();
if (sysexcontinue && c != 0xf7)
mferror ("didn't find expected continuation of a sysex");
/* if bit 7 not set, there is no status byte following the
* delta time, so it must a running status and we assume the
* last status occuring in the preceding channel message. */
if ((c & 0x80) == 0)
{ /* running status? */
if (status == 0)
mferror ("unexpected running status");
running = 1;
}
else
{
if (c >> 4 != 0x0f)
{ /* [SS] 2013-09-10 */
laststatus = c;
}
status = c;
running = 0;
}
/* [SS] 2013-09-10 */
if (running)
needed = chantype[(laststatus >> 4) & 0xf];
else
needed = chantype[(status >> 4) & 0xf];
if (needed)
{ /* ie. is it a channel message? */
/*if (Mf_currtime > Mf_currcopytime) accumulating = 1; */
/* [SS] 2013-09-10 */
if (running)
{
c1 = c;
chanmessage (laststatus, c1, (needed > 1) ? egetc () : 0);
}
else
{
c1 = egetc ();
chanmessage (status, c1, (needed > 1) ? egetc () : 0);
}
continue;;
}
switch (c)
{
case 0xff: /* meta event */
type = egetc ();
varinum = readvarinum ();
lookfor = Mf_toberead - varinum;
msginit ();
while (Mf_toberead > lookfor)
msgadd (egetc ());
metaevent (type);
break;
case 0xf0: /* start of system exclusive */
varinum = readvarinum ();
lookfor = Mf_toberead - varinum;
msginit ();
msgadd (0xf0);
while (Mf_toberead > lookfor)
msgadd (c = egetc ());
if (c == 0xf7 || Mf_nomerge == 0)
sysex ();
else
sysexcontinue = 1; /* merge into next msg */
break;
case 0xf7: /* sysex continuation or arbitrary stuff */
varinum = readvarinum ();
lookfor = Mf_toberead - varinum;
if (!sysexcontinue)
msginit ();
while (Mf_toberead > lookfor)
msgadd (c = egetc ());
if (!sysexcontinue)
{
if (Mf_arbitrary)
(*Mf_arbitrary) (msgleng (), msg ());
}
else if (c == 0xf7)
{
sysex ();
sysexcontinue = 0;
}
break;
default:
badbyte (c);
break;
}
}
if (max_currtime < Mf_currtime)
max_currtime = Mf_currtime;
return max_currtime;
}
int
get_tempo_info_from_track_1 ()
{
/* most of this code has been grabbed from readtrack().
The function is called to extract all the meta tempo
commands from track-1 and to store this in an array.
*/
/* This array is indexed by the high half of a status byte. It's */
/* value is either the number of bytes needed (1 or 2) for a channel */
/* message, or 0 (meaning it's not a channel message). */
static int chantype[] = {
0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 through 0x70 */
2, 2, 2, 2, 1, 1, 2, 0 /* 0x80 through 0xf0 */
};
long lookfor;
int c, c1, type;
int sysexcontinue = 0; /* 1 if last message was an unfinished sysex */
int running = 0; /* 1 when running status used */
int status = 0; /* status value (e.g. 0x90==note-on) */
int laststatus; /* [SS] 2013-09-10 */
int needed;
long varinum;
int position;
char *m;
laststatus = 0;
position = ftell (F_in);
/*printf("position = %d\n",position); */
if (readmt ("MTrk") == EOF)
return (0);
Mf_toberead = read32bit ();
Mf_currtime = 0;
/*alloc_trackdata(); */
while (Mf_toberead > 0)
{
delta_time = readvarinum ();
Mf_currtime += delta_time;
c = egetc ();
if (sysexcontinue && c != 0xf7)
mferror ("didn't find expected continuation of a sysex");
/* if bit 7 not set, there is no status byte following the
* delta time, so it must a running status and we assume the
* last status occuring in the preceding channel message. */
if ((c & 0x80) == 0)
{ /* running status? */
if (status == 0)
mferror ("unexpected running status");
running = 1;
}
else
{
/* if it is not a meta event, save the status byte */
if (c >> 4 != 0x0f)
{
laststatus = c; /* [SS] 2013-09-10 */
}
status = c;
running = 0;
}
/* [SS] 2011-09-10 */
if (running)
needed = chantype[(laststatus >> 4) & 0xf];
else
needed = chantype[(status >> 4) & 0xf];
if (needed)
{ /* ie. is it a channel message? */
if (running)
c1 = c;
else
c1 = egetc ();
/*chanmessage(status, c1, (needed > 1) ? egetc() : 0); */
continue;;
}
switch (c)
{
case 0xff: /* meta event */
type = egetc ();
varinum = readvarinum ();
lookfor = Mf_toberead - varinum;
msginit ();
while (Mf_toberead > lookfor)
msgadd (egetc ());
/* metaevent(type); */
if (type == 0x51)
{
m = msg ();
mf_get_tempo_event (to32bit (0, m[0], m[1], m[2]));
}
break;
case 0xf0: /* start of system exclusive */
varinum = readvarinum ();
lookfor = Mf_toberead - varinum;
msginit ();
msgadd (0xf0);
while (Mf_toberead > lookfor)
msgadd (c = egetc ());
if (c == 0xf7 || Mf_nomerge == 0)
/*sysex();[SS] 2006-08-08 */
break; /* [SS] 2006-08-08 */
else
sysexcontinue = 1; /* merge into next msg */
break;
case 0xf7: /* sysex continuation or arbitrary stuff */
varinum = readvarinum ();
lookfor = Mf_toberead - varinum;
if (!sysexcontinue)
msginit ();
while (Mf_toberead > lookfor)
msgadd (c = egetc ());
if (!sysexcontinue)
{
if (Mf_arbitrary)
(*Mf_arbitrary) (msgleng (), msg ());
}
else if (c == 0xf7)
{
/* sysex(); */
sysexcontinue = 0;
}
break;
default:
badbyte (c);
break;
}
}
fseek (F_in, position, SEEK_SET);
return (0);
}
void
badbyte (c)
int c;
{
char buff[32];
(void) sprintf (buff, "unexpected byte: 0x%02x", c);
mferror (buff);
}
void
metaevent (int type)
{
int leng;
unsigned char *m;
long qnote; /* [SS] 2013-09-06 */
leng = msgleng ();
m = msg ();
switch (type)
{
case 0x00:
/* 2015-11-14 [SS] */
copy_metaseqnum (m[0], m[1]);
break;
case 0x01: /* Text event */
case 0x02: /* Copyright notice */
case 0x03: /* Sequence/Track name */
case 0x04: /* Instrument name */
case 0x05: /* Lyric */
case 0x06: /* Marker */
case 0x07: /* Cue point */
case 0x08:
case 0x09:
case 0x0a:
case 0x0b:
case 0x0c:
case 0x0d:
case 0x0e:
case 0x0f:
/* These are all text events */
copy_metatext (type, leng, m);
break;
case 0x2f: /* End of Track */
copy_metaeot ();
break;
case 0x51: /* Set tempo */
if (newtempo > 0) /* [SS] 2013-09-04 */
mf_write_tempo (newtempo);
else if (newspeed)
{
qnote = m[2] + m[1] * 256 + m[0] * 65536;
qnote = (int) ((float) qnote / speedfactor);
mf_write_tempo (qnote);
}
else
mf_write_tempo (to32bit (0, m[0], m[1], m[2]));
break;
case 0x54:
mf_write_meta_event (0x54, m, 5);
break;
case 0x58:
copy_timesig (m[0], m[1], m[2], m[3]);
break;
case 0x59:
copy_keysig (m[0], m[1]);
break;
case 0x7f:
copy_metaspecial (leng, m);
break;
default:
copy_metamisc (type, leng, m);
}
}
void
sysex ()
{
copy_sysex (msgleng (), msg ());
}
void
chanmessage (int status, int c1, int c2)
{
int chan = status & 0xf;
if (!cut_beginning ())
nochanmsg = 0;
if (ctocopy[chan])
switch (status & 0xf0)
{
case 0x80:
copy_noteoff (chan, c1, c2);
break;
case 0x90:
if (chan == 9 && selected_drum != 0 && c1 != selected_drum)
c2 = drumlevel; /* [SS] 2013-09-07 */
if (chan != 9 && mutenodrum)
if (c2 > nondrumlevel)
c2 = nondrumlevel; /*[SS] 2013-09-15 */
if (chan == 9 && chosen_drum != 0 && c1 == chosen_drum)
c2 = drumvelocity; /* [SS] 2013-10-01 */
copy_noteon (chan, c1, c2);
break;
case 0xa0:
copy_pressure (chan, c1, c2);
break;
case 0xb0:
copy_parameter (chan, c1, c2);
break;
case 0xe0:
copy_pitchbend (chan, c1, c2);
break;
case 0xc0:
copy_program (chan, c1);
break;
case 0xd0:
copy_chanpressure (chan, c1);
break;