-
Notifications
You must be signed in to change notification settings - Fork 15
/
genmidi.c
3469 lines (3180 loc) · 92.3 KB
/
genmidi.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
/*
* abc2midi - program to convert abc files to MIDI files.
* Copyright (C) 1999 James Allwright
* e-mail: [email protected]
*
* 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
*
*/
/* genmidi.c
* This is the code for generating MIDI output from the stored music held
* in arrays feature, num, denom, pitch. The top-level routine is
* writetrack(). This file is part of abc2midi.
*
* 14th January 1999
* James Allwright
*
*/
/* for Microsoft Visual C++ Ver 6 and higher */
#ifdef _MSC_VER
#define ANSILIBS
#endif
#include "abc.h"
#include "parseabc.h"
#include "queues.h"
#include "genmidi.h"
#include "midifile.h"
#include <stdio.h>
#ifdef ANSILIBS
#include <string.h>
#include <ctype.h>
#endif
/* define USE_INDEX if your C libraries have index() instead of strchr() */
#ifdef USE_INDEX
#define strchr index
#endif
#include <stdlib.h>
#define MAX(A, B) ((A) > (B) ? (A) : (B))
void single_note_tuning_change(int midikey, float midipitch);
void addfract(int *xnum, int *xdenom, int a, int b);
/* [SS] 2011-08-17 */
void fdursum_at_segment(int segposnum, int segposden, int *val_num, int *val_den);
void articulated_stress_factors (int n, int *vel);
void write_event(int event_type, int channel, char data[], int n);
float ranfrac ()
{
return rand()/(float) RAND_MAX;
}
void setbeat();
static void parse_drummap(char **s);
/* global variables grouped roughly by function */
extern int lineno; /* source line being parsed */
extern int lineposition; /* character position in line */
extern char** atext;
/* Named guitar chords */
extern int chordnotes[MAXCHORDNAMES][10]; /* [SS] 2012-01-29 */
extern int chordlen[MAXCHORDNAMES];
/* general purpose storage structure */
/* these 6 arrays are used to hold the tune data */
extern int *pitch, *num, *denom;
extern int *bentpitch;
extern int *decotype; /* [SS] 2012-11-25 */
extern int *charloc; /* [SS] 2014-12-25 */
extern featuretype *feature;
extern int *stressvelocity; /* [SS] 2011-08-17 */
extern int notes;
extern int barflymode; /* [SS] 2011-08-24 */
extern int stressmodel; /* [SS] 2011-08-26 */
extern int verbose;
extern int quiet;
extern int sf, mi;
extern int silent; /* [SS] 2014-10-16 */
extern int retuning,bend; /* [SS] 2012-04-01 */
int drumbars;
int gchordbars;
int gchordbarcount;
int drumbarcount;
/* Part handling */
extern struct vstring part;
int parts, partno, partlabel;
int part_start[26], part_count[26];
long introlen, lastlen, partlen[26];
int partrepno;
/* int additive; not supported any more [SS] 2004-10-08*/
int err_num, err_denom;
extern int voicesused;
extern int dependent_voice[];
/* Tempo handling (Q: field) */
extern long tempo;
extern int time_num, time_denom; /* time sig. for the tune */
extern int mtime_num, mtime_denom; /* current time sig. when generating MIDI */
int div_factor;
int division = DIV;
long delta_time; /* time since last MIDI event */
long delta_time_track0; /* [SS] 2010-06-27 */
long tracklen, tracklen1;
/* output file generation */
extern int ntracks;
/* bar length checking */
int bar_num, bar_denom, barno, barsize;
int b_num, b_denom;
extern int barchecking;
/* time signature after header processed */
extern int header_time_num,header_time_denom;
/* generating MIDI output */
int beat;
int loudnote, mednote, softnote;
int beataccents;
int velocity_increment = 10; /* for crescendo and decrescendo */
char beatstring[100];
int nbeats;
int channel, program;
#define MAXCHANS 16
int channel_in_use[MAXCHANS+3];
int current_pitchbend[MAXCHANS];
int current_program[MAXCHANS];
int transpose;
int global_transpose=0;
int chordchannels[10]; /* for handling in voice chords and microtones */
int nchordchannels = 0;
extern int no_more_free_channels; /* [SS] 2015-03-23 from store.c */
/* [SS] 2015-09-07 */
int single_velocity_inc;
int single_velocity;
/* karaoke handling */
extern int karaoke, wcount;
int kspace;
char* wordlineptr;
extern char** words;
int thismline, thiswline, windex, thiswfeature;
int wordlineplace;
int nowordline;
int waitforbar;
int wlineno, syllcount;
int lyricsyllables, musicsyllables;
/* the following are booleans to select features in current track */
int wordson, noteson, gchordson, temposon, drumson, droneon;
int hyphenstate; /* [Bas Schoutsen] 2010-04-08 */
/* Generating accompaniment */
int gchords, g_started;
int basepitch, inversion, chordnum;
int gchordnotes[6],gchordnotes_size;
struct notetype {
int base;
int chan;
int vel;
};
struct notetype gchord, fun;
int g_num, g_denom;
int g_next;
char gchord_seq[40];
int gchord_len[40];
int g_ptr;
int tracknumber; /* [SS] 2014-11-17 */
/* [SS] 2015-05-21 */
struct dronestruct {
int chan; /* MIDI channel assigned to drone */
int event; /* stores time in MIDI pulses when last drone event occurred*/
int prog; /* MIDI program (instrument) to use for drone */
int pitch1;/* MIDI pitch of first drone tone */
int vel1; /* MIDI velocity (loudness) of first drone tone */
int pitch2;/* MIDI pitch of second drone tone */
int vel2; /* MIDI velocity (loudnress) of second drone tone */
} drone = {1, 0, 70, 45, 80, 33, 80}; /* bassoon a# */
/* Generating drum track */
int drum_num, drum_denom;
char drum_seq[40];
int drum_len[40];
int drum_velocity[40], drum_program[40];
int drum_ptr, drum_on;
int notecount=0; /* number of notes in a chord [ABC..] */
int notedelay=10; /* time interval in MIDI ticks between */
/* start of notes in chord */
int chordattack=0;
int staticnotedelay=10; /* introduced to handle !arpeggio! */
int staticchordattack=0;
int totalnotedelay=0; /* total time delay introduced */
int trim=1; /* to add a silent gap to note */
int trim_num = 1;
int trim_denom = 5;
/* [SS] 2015-06-16 */
int expand=0; /* overlap note past next note */
int expand_num = 0;
int expand_denom = 5;
/* channel 10 drum handling */
int drum_map[256];
int gchord_error = 0; /* [SS] 2010-07-11 */
extern struct trackstruct trackdescriptor[40]; /* trackstruct defined in genmidi.h*/
/* [SS] 2011-07-04 */
int beatmodel = 0; /* flag selecting standard or Phil's model */
/* [SS] 2012-12-12 */
int bendvelocity = 100;
int bendacceleration = 300;
/* [SS] 2014-09-09 */
int bendstate = 8192; /* also linked with queues.c */
/* [SS] 2015-09-10 2015-10-03 */
int benddata[256];
int bendnvals;
int bendtype = 1;
/* [SS] 2015-07-24 2015-10-03 */
#define MAXLAYERS 3
int controldata[MAXLAYERS][256];
int controlnvals[MAXLAYERS];
int controldefaults[128]; /* [SS] 2015-08-10 */
int nlayers = 0; /* [SS] 2015-08-20 */
int controlcombo = 0; /* [SS] 2015-08-20 */
/* for handling stress models */
int nseg; /* number of segments */
int ngain[32]; /* gain factor for each segment */
float maxdur; /* maximum duration */
int segnum,segden; /* segment width computed from M: and L: parameters*/
float fdur[32]; /* duration modifier for each segment */
float fdursum[32]; /* for mapping segment address into a position */
char *featname[] = {
"SINGLE_BAR", "DOUBLE_BAR", "BAR_REP", "REP_BAR",
"PLAY_ON_REP", "REP1", "REP2", "BAR1",
"REP_BAR2", "DOUBLE_REP", "THICK_THIN", "THIN_THICK",
"PART", "TEMPO", "TIME", "KEY",
"REST", "TUPLE", "NOTE", "NONOTE",
"OLDTIE", "TEXT", "SLUR_ON", "SLUR_OFF",
"TIE", "CLOSE_TIE", "TITLE", "CHANNEL",
"TRANSPOSE", "RTRANSPOSE", "GTRANSPOSE", "GRACEON",
"GRACEOFF", "SETGRACE", "SETC", "SETTRIM","EXPAND", "GCHORD",
"GCHORDON", "GCHORDOFF", "VOICE", "CHORDON",
"CHORDOFF", "CHORDOFFEX", "DRUMON", "DRUMOFF",
"DRONEON", "DRONEOFF", "SLUR_TIE", "TNOTE",
"LT", "GT", "DYNAMIC", "LINENUM",
"MUSICLINE", "MUSICSTOP", "WORDLINE", "WORDSTOP",
"INSTRUCTION", "NOBEAM", "CHORDNOTE", "CLEF",
"PRINTLINE", "NEWPAGE", "LEFT_TEXT", "CENTRE_TEXT",
"VSKIP", "COPYRIGHT", "COMPOSER", "ARPEGGIO",
"SPLITVOICE", "META", "PEDAL_ON", "PEDAL_OFF", "EFFECT"
};
void reduce(a, b)
/* elimate common factors in fraction a/b */
int *a, *b;
{
int sign;
int t, n, m;
if (*a < 0) {
sign = -1;
*a = -*a;
} else {
sign = 1;
};
/* find HCF using Euclid's algorithm */
if (*a > *b) {
n = *a;
m = *b;
} else {
n = *b;
m = *a;
};
while (m != 0) {
t = n % m;
n = m;
m = t;
};
*a = (*a/n)*sign;
*b = *b/n;
}
int gtfract(anum,adenom, bnum,bdenom)
/* compare two fractions anum/adenom > bnum/bdenom */
/* returns (a > b) */
int anum,adenom,bnum,bdenom;
{
if ((anum*bdenom) > (bnum*adenom)) {
return(1);
} else {
return(0);
};
}
void addunits(a, b)
/* add a/b to the count of units in the bar */
int a, b;
{
bar_num = bar_num*(b*b_denom) + (a*b_num)*bar_denom;
bar_denom = bar_denom * (b*b_denom);
reduce(&bar_num, &bar_denom);
/*printf("position = %d/%d\n",bar_num,bar_denom);*/
}
void configure_gchord()
/* creates a list of notes to played as chord for
* a specific guitar chord. Most of the code figures out
* how to order the notes when inversions are encountered.
*/
{
int j;
int inchord, note;
gchordnotes_size = 0;
inchord = 0;
if (inversion != -1) {
/* try to match inversion with basepitch+chordnotes.. */
for (j=0; j<chordlen[chordnum]; j++) {
if ((basepitch + chordnotes[chordnum][j]) % 12 == inversion % 12) {
inchord = j;
};
};
/* do not add strange note to chord [SS] 2008-09-24 */
/* if ((inchord == 0) && (inversion > basepitch)) {
** inversion = inversion - 12;
** gchordnotes[gchordnotes_size] = inversion+gchord.base;
** gchordnotes_size++;
** };
***/
};
for (j=0; j<chordlen[chordnum]; j++) {
note = basepitch + chordnotes[chordnum][j];
if (j < inchord)
note += 12;
gchordnotes[gchordnotes_size] = gchord.base+note;
gchordnotes_size++;
};
}
void set_gchords(s)
char* s;
/* set up a string which indicates how to generate accompaniment from */
/* guitar chords (i.e. "A", "G" in abc). */
/* called from dodeferred(), startfile() and setbeat() */
{
int seq_len;
char* p;
int j;
p = s;
j = 0;
seq_len = 0;
while ((strchr("zcfbghijGHIJx", *p) != NULL) && (j <39)) {
if (*p == 0) break;
gchord_seq[j] = *p;
p = p + 1;
if ((*p >= '0') && (*p <= '9')) {
gchord_len[j] = readnump(&p);
} else {
gchord_len[j] = 1;
};
seq_len = seq_len + gchord_len[j];
j = j + 1;
};
if (seq_len == 0) {
event_error("Bad gchord");
gchord_seq[0] = 'z';
gchord_len[0] = 1;
seq_len = 1;
};
gchord_seq[j] = '\0';
if (j == 39) {
event_error("Sequence string too long");
};
/* work out unit delay in 1/4 notes*/
g_num = mtime_num * 4*gchordbars;
g_denom = mtime_denom * seq_len;
reduce(&g_num, &g_denom);
/* printf("%s %d %d\n",s,g_num,g_denom); */
}
void set_drums(s)
char* s;
/* set up a string which indicates drum pattern */
/* called from dodeferred() */
{
int seq_len, count, drum_hits;
char* p;
int i, j, place;
p = s;
count = 0;
drum_hits = 0;
seq_len = 0;
while (((*p == 'z') || (*p == 'd')) && (count<39)) {
if (*p == 'd') {
drum_hits = drum_hits + 1;
};
drum_seq[count] = *p;
p = p + 1;
if ((*p >= '0') && (*p <= '9')) {
drum_len[count] = readnump(&p);
} else {
drum_len[count] = 1;
};
seq_len = seq_len + drum_len[count];
count = count + 1;
};
drum_seq[count] = '\0';
if (seq_len == 0) {
event_error("Bad drum sequence");
drum_seq[0] = 'z';
drum_len[0] = 1;
seq_len = 1;
};
if (count == 39) {
event_error("Drum sequence string too long");
};
/* look for program and velocity specifiers */
for (i = 0; i<count; i++) {
drum_program[i] = 35;
drum_velocity[i] = 80;
};
skipspace(&p);
i = 0;
place = 0;
while (isdigit(*p)) {
j = readnump(&p);
if (i < drum_hits) {
while (drum_seq[place] != 'd') {
place = place + 1;
};
if (j > 127) {
event_error("Drum program must be in the range 0-127");
} else {
drum_program[place] = j;
};
place = place + 1;
} else {
if (i < 2*count) {
if (i == drum_hits) {
place = 0;
};
while (drum_seq[place] != 'd') {
place = place + 1;
};
if ((j < 1) || (j > 127)) {
event_error("Drum velocity must be in the range 1-127");
} else {
drum_velocity[place] = j;
};
place = place + 1;
};
};
i = i + 1;
skipspace(&p);
};
if (i > 2*drum_hits) {
event_error("Too many data items for drum sequence");
};
/* work out unit delay in 1/4 notes*/
drum_num = mtime_num * 4*drumbars;
drum_denom = mtime_denom * seq_len;
reduce(&drum_num, &drum_denom);
}
static void checkbar(pass)
/* check to see we have the right number of notes in the bar */
int pass;
{
char msg[80];
if (barchecking) {
/* only generate these errors once */
if (noteson && (partrepno == 0)) {
/* allow zero length bars for typesetting purposes */
if ((bar_num-barsize*(bar_denom) != 0) &&
(bar_num != 0) && ((pass == 2) || (barno != 0))) {
/* [SS] 2014-11-17 added tracknumber */
sprintf(msg, "Track %d Bar %d has %d",tracknumber, barno, bar_num);
if (bar_denom != 1) {
sprintf(msg+strlen(msg), "/%d", bar_denom);
};
sprintf(msg+strlen(msg), " units instead of %d", barsize);
if (pass == 2) {
strcat(msg, " in repeat");
};
if (quiet == -1) event_warning(msg);
};
};
};
if (bar_num > 0) {
barno = barno + 1;
};
bar_num = 0;
bar_denom = 1;
/* zero place in gchord sequence */
if (gchordson) {
if (gchordbarcount < 1) {
g_ptr = 0;
addtoQ(0, g_denom, -1, g_ptr,0, 0);
gchordbarcount = gchordbars;
}
gchordbarcount--;
};
if (drumson) {
if (drumbarcount < 1) {
drum_ptr = 0;
addtoQ(0, drum_denom, -1, drum_ptr,0, 0);
drumbarcount = drumbars;
}
drumbarcount--;
};
}
static void softcheckbar(pass)
/* allows repeats to be in mid-bar */
int pass;
{
if (barchecking) {
if ((bar_num-barsize*(bar_denom) >= 0) || (barno <= 0)) {
checkbar(pass);
};
};
}
static void save_state(vec, a, b, c, d, e, f)
/* save status when we go into a repeat */
int vec[6];
int a, b, c, d, e, f;
{
vec[0] = a;
vec[1] = b;
vec[2] = c;
vec[3] = d;
vec[4] = e;
vec[5] = f; /* [SS] 2013-11-02 */
}
static void restore_state(vec, a, b, c, d, e, f)
/* restore status when we loop back to do second repeat */
int vec[6];
int *a, *b, *c, *d, *e, *f;
{
*a = vec[0];
*b = vec[1];
*c = vec[2];
*d = vec[3];
*e = vec[4];
*f = vec[5]; /* [SS] 2013-11-02 */
}
/* 2015-03-16 [SS] changed channels[] to channel_in_use[] */
static int findchannel()
/* work out next available channel */
{
int j;
j = 0;
while ((j<MAXCHANS) && (channel_in_use[j] != 0)) {
j = j + 1;
};
if (j >= MAXCHANS && !no_more_free_channels) {
event_error("All 16 MIDI channels used up.");
no_more_free_channels = 1;
j = 0;
};
channel_in_use[j] = 1;
return (j);
}
static void fillvoice(partno, xtrack, voice)
/* check length of this voice at the end of a part */
/* if it is zero, extend it to the correct length */
int partno, xtrack, voice;
{
char msg[100];
long now;
if (partlabel <-1 || partlabel >25) printf("genmidi.c:fillvoice partlabel %d out of range\n",partlabel);
now = tracklen + delta_time;
if (partlabel == -1) {
if (xtrack == 1) {
introlen = now;
} else {
if (now == 0) {
delta_time = delta_time + introlen;
now = introlen;
} else {
if (now != introlen) {
sprintf(msg, "Time 0-%ld voice %d, has length %ld",
introlen, voice, now);
event_error(msg);
};
};
};
} else {
if (xtrack == 1) {
partlen[partlabel] = now - lastlen;
} else {
if (now - lastlen == 0) {
delta_time = delta_time + partlen[partlabel];
now = now + partlen[partlabel];
} else {
if (now - lastlen != partlen[partlabel]) {
sprintf(msg, "Time %ld-%ld voice %d, part %c has length %ld",
lastlen, lastlen+partlen[partlabel], voice,
(char) (partlabel + (int) 'A'),
now-lastlen);
event_error(msg);
};
};
};
};
lastlen = now;
}
static int findpart(j)
int j;
/* find out where next part starts and update partno */
{
int place;
place = j;
partno = partno + 1;
if (partno < parts) {
partlabel = (int)part.st[partno] - (int)'A';
}
while ((partno < parts) &&
(part_start[partlabel] == -1)) {
if (!silent) event_error("Part not defined");
partno = partno + 1;
if (partno < parts) {
partlabel = (int)part.st[partno] - (int)'A';
}
};
if (partno >= parts) {
place = notes;
} else {
partrepno = part_count[partlabel];
part_count[partlabel]++;
place = part_start[partlabel];
};
if (verbose) {
if (partno < parts) {
printf("Doing part %c number %d of %d\n", part.st[partno], partno, parts);
};
};
return(place);
}
static int partbreak(xtrack, voice, place)
/* come to part label in note data - check part length, then advance to */
/* next part if there was a P: field in the header */
int xtrack, voice, place;
{
int newplace;
newplace = place;
if (dependent_voice[voice]) return newplace;
if (xtrack > 0) {
fillvoice(partno, xtrack, voice);
};
if (parts != -1) {
/* go to next part label */
newplace = findpart(newplace);
};
partlabel = (int) pitch[newplace] - (int)'A';
return(newplace);
}
static int findvoice(initplace, voice, xtrack)
/* find where next occurrence of correct voice is */
int initplace;
int voice, xtrack;
{
int foundvoice;
int j;
foundvoice = 0;
j = initplace;
while ((j < notes) && (foundvoice == 0)) {
if (feature[j] == PART) {
j = partbreak(xtrack, voice, j);
if (voice == 1) {
foundvoice = 1;
} else {
j = j + 1;
};
} else {
if ((feature[j] == VOICE) && (pitch[j] == voice)) {
foundvoice = 1;
} else {
j = j + 1;
};
};
};
return(j);
}
static void text_data(s)
/* write text event to MIDI file */
char* s;
{
mf_write_meta_event(delta_time, text_event, s, strlen(s));
tracklen = tracklen + delta_time;
delta_time = 0L;
}
static void karaokestarttrack (track)
int track;
/* header information for karaoke track based on w: fields */
{
int j;
int done;
char atitle[200];
/*
* Print Karaoke file headers in track 0.
* @KMIDI KARAOKE FILE - Karaoke midi file marker)
*/
if (track == 0)
{
text_data("@KMIDI KARAOKE FILE");
}
/*
* Name track 2 "Words" for the lyrics track.
* @LENGL - language
* Print @T information.
* 1st @T line signifies title.
* 2nd @T line signifies author.
* 3rd @T line signifies copyright.
*/
if (track == 2)
{
mf_write_meta_event(0L, sequence_name, "Words", 5);
kspace = 0;
text_data("@LENGL");
strcpy(atitle, "@T");
}
else
/*
* Write name of song as sequence name in track 0 and as track 1 name.
* Print general information about the file using @I marker.
* Add to tracks 0 and 1 for various Karaoke (and midi) players to find.
*/
strcpy(atitle, "@I");
j = 0;
done = 3;
while ((j < notes) && (done > 0))
{
j = j+1;
if (feature[j] == TITLE) {
if (track != 2)
mf_write_meta_event(0L, sequence_name, atext[pitch[j]], strlen (atext[pitch[j]]));
strcpy(atitle+2, atext[pitch[j]]);
text_data(atitle);
done--;
}
if (feature[j] == COMPOSER) {
strcpy(atitle+2, atext[pitch[j]]);
text_data(atitle);
done--;
}
if (feature[j] == COPYRIGHT) {
strcpy(atitle+2, atext[pitch[j]]);
text_data(atitle);
done--;
}
}
}
static int findwline(startline)
int startline;
/* Find next line of lyrics at or after startline. */
{
int place;
int done;
int newwordline;
int inwline, extending;
int versecount, target;
/* printf("findwline called with %d\n", startline); */
done = 0;
inwline = 0;
nowordline = 0;
newwordline = -1;
target = partrepno;
if (startline == thismline) {
versecount = 0;
extending = 0;
} else {
versecount = target;
extending = 1;
};
if (thismline == -1) {
event_error("First lyrics line must come after first music line");
} else {
place = startline + 1;
/* search for corresponding word line */
while ((place < notes) && (!done)) {
switch (feature[place]) {
case WORDLINE:
inwline = 1;
/* wait for words for this pass */
if (versecount == target) {
thiswfeature = place;
newwordline = place;
windex = pitch[place];
wordlineplace = 0;
done = 1;
};
break;
case WORDSTOP:
if (inwline) {
versecount = versecount + 1;
};
inwline = 0;
/* stop if we are part-way through a lyric set */
if (extending) {
done = 1;
};
break;
case PART:
done = 1;
break;
case VOICE:
done = 1;
break;
case MUSICLINE:
done = 1;
break;
default:
break;
};
place = place + 1;
if (done && (newwordline == -1) && (versecount > 0) && (!extending)) {
target = partrepno % versecount ;
versecount = 0;
place = startline+1;
done = 0;
inwline = 0;
};
};
if (newwordline == -1) {
/* remember that we couldn't find lyrics */
nowordline = 1;
if (lyricsyllables == 0) {
event_warning("Line of music without lyrics");
};
};
};
return(newwordline);
}
static int getword(place, w)
/* picks up next syllable out of w: field.
* It strips out all the control codes ~ - _ * in the
* words and sends each syllable out to the Karaoke track.
* Using the place variable, it loops through each character
* in the word until it encounters a space or next control
* code. The syllstatus variable controls the loop. After,
* the syllable is sent, it then positions the place variable
* to the next syllable or control code.
* inword --> grabbing the characters in the syllable and
* putting them into syllable for output.
* postword --> finished grabbing all characters
* foundnext--> ready to repeat process for next syllable
* empty --> between syllables.
*
* The variable i keeps count of the number of characters
* inserted into the syllable[] char for output to the
* karaoke track. The kspace variables signals that a
* space was encountered.
*/
int* place;
int w;
{
char syllable[200];
unsigned char c; /* [BY] 2012-10-03 */
int i;
int syllcount;
enum {empty, inword, postword, foundnext, failed} syllstatus;
/* [BY] 2012-10-03 Big5 chinese character support */
int isBig5; /* boolean check for first byte of Big-5: 0xA140 ~ 0xF9FE */
/*printf("GETWORD: w = %d\n",c);*/
i = 0;
syllcount = 0;
if (w >= wcount) {
syllable[i] = '\0';
return ('\0');
};
if (*place == 0) {
if ((w % 2) == 0) {
syllable[i] = '/';
} else {
syllable[i] = '\\';
};
i = i + 1;
};
if (kspace) {
syllable[i] = ' ';
i = i + 1;
};
syllstatus = empty;
c = *(words[w]+(*place));
isBig5 = 0; /* [BI] 2012-10-03 */
while ((syllstatus != postword) && (syllstatus != failed)) {
syllable[i] = c;
/* printf("syllstatus = %d c = %c i = %d place = %d row= %d \n",syllstatus,c,i,*place,w); */
if (isBig5) { /* [BI] 2012-10-03 */
i = i + 1;
*place = *place + 1;
isBig5 = 0;
} else {
switch(c) {
case '\0':
if (syllstatus == empty) {
syllstatus = failed;
} else {
syllstatus = postword;
kspace = 1;
};
break;
case '~':
syllable[i] = ' ';
syllstatus = inword;
*place = *place + 1;
i = i + 1;
hyphenstate = 0; /* [Bas Schoutsen] 2010-04-08 */
break;
case '\\':
if (*(words[w]+(*place+1)) == '-') {
syllable[i] = '-';
syllstatus = inword;
*place = *place + 2;
i = i + 1;
} else {
/* treat like plain text */
*place = *place + 1;
if (i>0) {
syllstatus = inword;