forked from lewdlime/abcm2ps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
5307 lines (4994 loc) · 114 KB
/
parse.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
/*
* Parsing functions.
*
* This file is part of abcm2ps.
*
* Copyright (C) 1998-2014 Jean-François Moine
* Adapted from abc2ps, Copyright (C) 1996,1997 Michael Methfessel
*
* 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, Suite 500, Boston, MA 02110-1335 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include "abc2ps.h"
#include "slre.h"
/* options = external formatting */
struct symsel_s { /* symbol selection */
short bar;
short time;
char seq;
};
struct brk_s { /* music line break */
struct brk_s *next;
struct symsel_s symsel;
};
struct voice_opt_s { /* voice options */
struct voice_opt_s *next;
struct SYMBOL *s; /* list of options (%%xxx) */
};
struct tune_opt_s { /* tune options */
struct tune_opt_s *next;
struct voice_opt_s *voice_opts;
struct SYMBOL *s; /* list of options (%%xxx) */
};
struct STAFF_S staff_tb[MAXSTAFF]; /* staff table */
int nstaff; /* (0..MAXSTAFF-1) */
struct SYMBOL *tsfirst; /* first symbol in the time sorted list */
struct VOICE_S voice_tb[MAXVOICE]; /* voice table */
static struct VOICE_S *curvoice; /* current voice while parsing */
struct VOICE_S *first_voice; /* first voice */
struct SYSTEM *cursys; /* current system */
static struct SYSTEM *parsys; /* current system while parsing */
struct FORMAT dfmt; /* current global format */
unsigned short *micro_tb; /* ptr to the microtone table of the tune */
int nbar; /* current measure number */
static struct voice_opt_s *voice_opts;
static struct tune_opt_s *tune_opts, *cur_tune_opts;
static struct brk_s *brks;
static struct symsel_s clip_start, clip_end;
static INFO info_glob; /* global info definitions */
static unsigned char deco_glob[256]; /* global decoration table */
static int over_time; /* voice overlay start time */
static int over_mxtime; /* voice overlay max time */
static short over_bar; /* voice overlay in a measure */
static short over_voice; /* main voice in voice overlay */
static int staves_found; /* time of the last %%staves */
static int abc2win;
float multicol_start; /* (for multicol) */
static float multicol_max;
static float lmarg, rmarg;
static int mc_in_tune; /* multicol started in tune */
static void get_clef(struct SYMBOL *s);
static struct abcsym *get_info(struct abcsym *as,
struct abctune *t);
static void get_key(struct SYMBOL *s);
static void get_meter(struct SYMBOL *s);
static void get_voice(struct SYMBOL *s);
static void get_note(struct SYMBOL *s);
static struct abcsym *process_pscomment(struct abcsym *as);
static void set_tblt(struct VOICE_S *p_voice);
static void set_tuplet(struct SYMBOL *s);
static void sym_link(struct SYMBOL *s, int type);
/* -- weight of the symbols -- */
static signed char w_tb[NSYMTYPES] = { /* !! index = symbol type !! */
0,
9, /* 1- note / rest */
1, /* 2- space */
3, /* 3- bar */
2, /* 4- clef */
5, /* 5- timesig */
4, /* 6- keysig */
0, /* 7- tempo */
0, /* 8- staves */
9, /* 9- mrest */
0, /* 10- part */
// 8, /* 11- grace */
1, /* 11- grace */
0, /* 12- fmtchg */
7, /* 13- tuplet */
6, /* 14- stbrk */
6 /* 15- custos */
};
/* key signature transposition tables */
static signed char cde2fcg[7] = {0, 2, 4, -1, 1, 3, 5};
static char cgd2cde[7] = {0, 4, 1, 5, 2, 6, 3};
/* -- expand a multi-rest into single rests and measure bars -- */
static void mrest_expand(struct SYMBOL *s)
{
struct VOICE_S *p_voice;
struct SYMBOL *s2, *next;
struct deco dc;
int nb, dt;
nb = s->as.u.bar.len;
dt = s->dur / nb;
/* change the multi-rest (type bar) to a single rest */
memcpy(&dc, &s->as.u.bar.dc, sizeof dc);
memset(&s->as.u.note, 0, sizeof s->as.u.note);
s->type = NOTEREST;
s->as.type = ABC_T_REST;
s->as.u.note.nhd = 0;
s->dur = s->as.u.note.lens[0] = dt;
s->head = H_FULL;
s->nflags = -2;
/* add the bar(s) and rest(s) */
next = s->next;
p_voice = &voice_tb[s->voice];
p_voice->last_sym = s;
p_voice->time = s->time + dt;
p_voice->cstaff = s->staff;
s2 = s;
while (--nb > 0) {
s2 = sym_add(p_voice, BAR);
s2->as.type = ABC_T_BAR;
s2->as.u.bar.type = B_SINGLE;
s2 = sym_add(p_voice, NOTEREST);
s2->as.type = ABC_T_REST;
s2->as.flags = s->as.flags;
s2->dur = s2->as.u.note.lens[0] = dt;
s2->head = H_FULL;
s2->nflags = -2;
p_voice->time += dt;
}
s2->next = next;
if (next)
next->prev = s2;
/* copy the mrest decorations to the last rest */
memcpy(&s2->as.u.note.dc, &dc, sizeof s2->as.u.note.dc);
}
/* -- sort all symbols by time and vertical sequence -- */
static void sort_all(void)
{
struct SYSTEM *sy;
struct SYMBOL *s, *prev, *s2;
struct VOICE_S *p_voice;
int fl, voice, time, w, wmin, multi, mrest_time;
int nv, nb, r, sysadv;
struct SYMBOL *vtb[MAXVOICE];
signed char vn[MAXVOICE]; /* voice indexed by range */
/* memset(vtb, 0, sizeof vtb); */
mrest_time = -1;
for (p_voice = first_voice; p_voice; p_voice = p_voice->next)
vtb[p_voice - voice_tb] = s = p_voice->sym;
/* initialize the voice order */
sy = cursys;
sysadv = 1;
prev = NULL;
fl = 1; /* set start of sequence */
multi = -1; /* (have gcc happy) */
for (;;) {
if (sysadv) {
sysadv = 0;
memset(vn, -1, sizeof vn);
multi = -1;
for (p_voice = first_voice;
p_voice;
p_voice = p_voice->next) {
voice = p_voice - voice_tb;
r = sy->voice[voice].range;
if (r < 0)
continue;
vn[r] = voice;
multi++;
}
}
/* search the min time and symbol weight */
wmin = time = (unsigned) ~0 >> 1; /* max int */
nv = nb = 0;
for (r = 0; r < MAXVOICE; r++) {
voice = vn[r];
if (voice < 0)
break;
s = vtb[voice];
if (!s || s->time > time)
continue;
w = w_tb[s->type];
if (s->time < time) {
time = s->time;
wmin = w;
nb = 0;
} else if (w < wmin) {
wmin = w;
nb = 0;
}
if (!(s->sflags & S_SECOND)) {
nv++;
if (s->type == BAR)
nb++;
}
if (s->type == MREST) {
if (s->as.u.bar.len == 1)
mrest_expand(s);
else if (multi)
mrest_time = time;
}
}
if (wmin > 127)
break; /* done */
#if 0
/* align the measure bars */
if (nb != 0 && nb != nv) { /* if other symbol than bars */
wmin = (unsigned) ~0 >> 1;
for (r = 0; r < MAXVOICE; r++) {
voice = vn[r];
if (voice < 0)
break;
s = vtb[voice];
if (!s || s->time > time
|| s->type == BAR)
continue;
w = w_tb[s->type];
if (w < wmin)
wmin = w;
}
if (wmin > 127)
wmin = w_tb[BAR];
}
#endif
/* if some multi-rest and many voices, expand */
if (time == mrest_time) {
nb = 0;
for (r = 0; r < MAXVOICE; r++) {
voice = vn[r];
if (voice < 0)
break;
s = vtb[voice];
if (!s || s->time != time)
continue;
w = w_tb[s->type];
if (w != wmin)
continue;
if (s->type != MREST) {
mrest_time = -1; /* some note or rest */
break;
}
if (nb == 0) {
nb = s->as.u.bar.len;
} else if (nb != s->as.u.bar.len) {
mrest_time = -1; /* different duration */
break;
}
}
if (mrest_time < 0) {
for (r = 0; r < MAXVOICE; r++) {
voice = vn[r];
if (voice < 0)
break;
s = vtb[voice];
if (s && s->type == MREST)
mrest_expand(s);
}
}
}
/* link the vertical sequence */
for (r = 0; r < MAXVOICE; r++) {
voice = vn[r];
if (voice < 0)
break;
s = vtb[voice];
if (!s || s->time != time)
continue;
w = w_tb[s->type];
if (w != wmin)
continue;
if (fl) {
fl = 0;
s->sflags |= S_SEQST;
}
s->ts_prev = prev;
if (prev) {
prev->ts_next = s;
if (s->type == BAR
&& (s->sflags & S_SECOND)
&& prev->type != BAR
&& !(s->as.flags & ABC_F_INVIS))
error(1, s, "Bad measure bar");
} else {
tsfirst = s;
}
prev = s;
vtb[voice] = s->next;
if (s->type == STAVES) {
sy = sy->next;
sysadv = 1;
}
}
fl = wmin; /* start a new sequence if some space */
}
if (!prev)
return;
/* if no bar or format_change at end of tune, add a dummy symbol */
if (prev->type != BAR && prev->type != FMTCHG) {
p_voice = &voice_tb[prev->voice];
p_voice->last_sym = prev;
s = sym_add(p_voice, FMTCHG);
s->u = -1;
s->time = prev->time + prev->dur;
s->sflags = S_SEQST;
prev->ts_next = s;
s->ts_prev = prev;
for (;;) {
prev->sflags &= ~S_EOLN;
if (prev->sflags & S_SEQST)
break;
prev = prev->ts_prev;
}
}
/* if Q: from tune header, put it at start of the music */
s2 = info['Q' - 'A'];
if (!s2)
return;
info['Q' - 'A'] = NULL;
s = tsfirst->extra;
if (s) {
do {
if (s->type == TEMPO)
return; /* already a tempo */
s = s->next;
} while (s);
}
s = tsfirst;
s2->type = TEMPO;
s2->voice = s->voice;
s2->staff = s->staff;
s2->time = s->time;
if (s->extra) {
s2->next = s->extra;
s2->next->prev = s2;
}
s->extra = s2;
}
/* -- move the symbols with no width to the next symbol -- */
static void voice_compress(void)
{
struct VOICE_S *p_voice;
struct SYMBOL *s, *s2, *ns;
// int sflags;
for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
if (p_voice->ignore)
continue;
for (s = p_voice->sym; s; s = s->next) {
if (s->time >= staves_found)
break;
}
ns = NULL;
// sflags = 0;
for ( ; s; s = s->next) {
switch (s->type) {
case KEYSIG: /* remove the empty key signatures */
if (s->as.u.key.empty) {
if (s->prev)
s->prev->next = s->next;
else
p_voice->sym = s->next;
if (s->next)
s->next->prev = s->prev;
continue;
}
break;
case FMTCHG:
s2 = s->extra;
if (s2) { /* dummy format */
if (!ns)
ns = s2;
if (s->prev) {
s->prev->next = s2;
s2->prev = s->prev;
}
if (!s->next) {
ns = NULL;
break;
}
while (s2->next)
s2 = s2->next;
s->next->prev = s2;
s2->next = s->next;
}
/* fall thru */
case TEMPO:
case PART:
case TUPLET:
if (!ns)
ns = s;
// sflags |= s->sflags;
continue;
case MREST: /* don't shift P: and Q: */
if (!ns)
continue;
s2 = (struct SYMBOL *) getarena(sizeof *s);
memset(s2, 0, sizeof *s2);
s2->type = SPACE;
s2->as.u.note.lens[1] = -1;
s2->as.flags = ABC_F_INVIS;
s2->voice = s->voice;
s2->staff = s->staff;
s2->time = s->time;
s2->sflags = s->sflags;
s2->next = s;
s2->prev = s->prev;
s2->prev->next = s2;
s->prev = s2;
s = s2;
break;
}
if (s->as.flags & ABC_F_GRACE) {
if (!ns)
ns = s;
while (!(s->as.flags & ABC_F_GR_END))
s = s->next;
s2 = (struct SYMBOL *) getarena(sizeof *s);
memcpy(s2, s, sizeof *s2);
s2->as.type = 0;
s2->type = GRACE;
s2->dur = 0;
s2->next = s->next;
if (s2->next)
s2->next->prev = s2;
else
p_voice->last_sym = s2;
s2->prev = s;
s->next = s2;
s = s2;
}
if (!ns)
continue;
s->extra = ns;
// s->sflags |= (sflags & S_EOLN);
// sflags = 0;
s->prev->next = NULL;
s->prev = ns->prev;
if (s->prev)
s->prev->next = s;
else
p_voice->sym = s;
ns->prev = NULL;
ns = NULL;
}
/* when symbols with no space at end of tune,
* add a dummy format */
if (ns) {
s = sym_add(p_voice, FMTCHG);
s->u = -1; /* nothing */
s->extra = ns;
s->prev->next = NULL; /* unlink */
s->prev = ns->prev;
if (s->prev)
s->prev->next = s;
else
p_voice->sym = s;
ns->prev = NULL;
}
}
}
/* -- duplicate the voices as required -- */
static void voice_dup(void)
{
struct VOICE_S *p_voice, *p_voice2;
struct SYMBOL *s, *s2, *g, *g2;
int voice;
for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
if ((voice = p_voice->clone) < 0)
continue;
p_voice->clone = -1;
p_voice2 = &voice_tb[voice];
for (s = p_voice->sym; s; s = s->next) {
//fixme: there may be other symbols before the %%staves at this same time
if (s->time >= staves_found)
break;
}
for ( ; s; s = s->next) {
s2 = (struct SYMBOL *) getarena(sizeof *s2);
memcpy(s2, s, sizeof *s2);
s2->prev = p_voice2->last_sym;
s2->next = NULL;
if (p_voice2->sym)
p_voice2->last_sym->next = s2;
else
p_voice2->sym = s2;
p_voice2->last_sym = s2;
s2->voice = voice;
s2->staff = p_voice2->staff;
if (p_voice2->second)
s2->sflags |= S_SECOND;
else
s2->sflags &= ~S_SECOND;
if (p_voice2->floating)
s2->sflags |= S_FLOATING;
else
s2->sflags &= ~S_FLOATING;
s2->ly = NULL;
g = s2->extra;
if (!g)
continue;
g2 = (struct SYMBOL *) getarena(sizeof *g2);
memcpy(g2, g, sizeof *g2);
s2->extra = g2;
s2 = g2;
s2->voice = voice;
s2->staff = p_voice2->staff;
for (g = g->next; g; g = g->next) {
g2 = (struct SYMBOL *) getarena(sizeof *g2);
memcpy(g2, g, sizeof *g2);
s2->next = g2;
g2->prev = s2;
s2 = g2;
s2->voice = voice;
s2->staff = p_voice2->staff;
}
}
}
}
/* -- create a new staff system -- */
static void system_new(void)
{
struct SYSTEM *new_sy;
int staff, voice;
new_sy = (struct SYSTEM *) getarena(sizeof *new_sy);
if (!parsys) {
memset(new_sy, 0, sizeof *new_sy);
for (voice = 0; voice < MAXVOICE; voice++) {
new_sy->voice[voice].range = -1;
new_sy->voice[voice].clef.line = 2;
new_sy->voice[voice].clef.stafflines = 5;
new_sy->voice[voice].clef.staffscale = 1;
}
cursys = new_sy;
} else {
for (voice = 0; voice < MAXVOICE; voice++) {
if (parsys->voice[voice].range < 0
|| parsys->voice[voice].second)
continue;
staff = parsys->voice[voice].staff;
memcpy(&parsys->staff[staff].clef,
&parsys->voice[voice].clef,
sizeof parsys->staff[staff].clef);
}
memcpy(new_sy, parsys, sizeof *new_sy);
for (voice = 0; voice < MAXVOICE; voice++) {
new_sy->voice[voice].range = -1;
new_sy->voice[voice].second = 0;
}
for (staff = 0; staff < MAXSTAFF; staff++)
new_sy->staff[staff].flags = 0;
parsys->next = new_sy;
}
parsys = new_sy;
}
/* -- set the staves -- */
static void staves_init(void)
{
struct SYSTEM *sy, *new_sy;
struct SYMBOL *s, *staves;
int staff, voice;
sy = cursys;
for (voice = 0; voice < MAXVOICE; voice++) {
if (sy->voice[voice].range < 0
|| sy->voice[voice].second)
continue;
staff = sy->voice[voice].staff;
memcpy(&sy->staff[staff].clef,
&sy->voice[voice].clef,
sizeof (struct clef_s));
sy->staff[staff].sep = sy->voice[voice].sep;
sy->staff[staff].maxsep = sy->voice[voice].maxsep;
}
staves = NULL;
for (s = tsfirst; s; s = s->ts_next) {
switch (s->type) {
case STAVES:
sy = sy->next;
for (voice = 0; voice < MAXVOICE; voice++) {
if (sy->voice[voice].range < 0
|| sy->voice[voice].second)
continue;
staff = sy->voice[voice].staff;
memcpy(&sy->staff[staff].clef,
&sy->voice[voice].clef,
sizeof (struct clef_s));
sy->staff[staff].sep = sy->voice[voice].sep;
sy->staff[staff].maxsep = sy->voice[voice].maxsep;
}
staves = s;
continue;
case CLEF:
if (s->as.u.clef.type < 0)
break;
{
int scale, lines;
scale = sy->voice[s->voice].clef.staffscale;
lines = sy->voice[s->voice].clef.stafflines;
sy->voice[s->voice].clef = s->as.u.clef;
sy->voice[s->voice].clef.staffscale = scale;
sy->voice[s->voice].clef.stafflines = lines;
}
continue; /* normal clef change */
case KEYSIG:
case TIMESIG:
case TEMPO:
case PART:
case FMTCHG:
continue;
default:
staves = NULL;
continue;
}
/* CLEF with change of #lines or scale of staff */
voice = s->voice;
if (!staves) {
staves = s; /* create a new staff system */
new_sy = (struct SYSTEM *) getarena(sizeof *new_sy);
memcpy(new_sy, sy, sizeof *new_sy);
for (voice = 0; voice < MAXVOICE; voice++) {
if (new_sy->voice[voice].range < 0
|| new_sy->voice[voice].second)
continue;
staff = new_sy->voice[voice].staff;
memcpy(&new_sy->staff[staff].clef,
&new_sy->voice[voice].clef,
sizeof (struct clef_s));
}
new_sy->next = sy->next;
sy->next = new_sy;
sy = new_sy;
s->type = STAVES; /* and set the marker */
} else { /* remove the CLEF */
if (s->prev)
s->prev->next = s->next;
else
voice_tb[voice].sym = s->next;
if (s->ts_next) {
s->ts_next->ts_prev = s->ts_prev;
if (s->sflags & S_SEQST)
s->ts_next->sflags |= S_SEQST;
}
if (s->ts_prev)
s->ts_prev->ts_next = s->ts_next;
else
tsfirst = s->ts_next;
if (s->next)
s->next->prev = s->prev;
}
staff = sy->voice[voice].staff;
if (s->as.u.clef.stafflines >= 0)
sy->voice[voice].clef.stafflines
= sy->staff[staff].clef.stafflines
= s->as.u.clef.stafflines;
if (s->as.u.clef.staffscale != 0)
sy->voice[voice].clef.staffscale
= sy->staff[staff].clef.staffscale
= s->as.u.clef.staffscale;
}
}
/* -- initialize the voices and staves -- */
/* this routine is called when starting the generation */
static void system_init(void)
{
voice_compress();
voice_dup();
sort_all(); /* define the time / vertical sequences */
if (!tsfirst)
return;
parsys->nstaff = nstaff; /* save the number of staves */
staves_init();
}
/* go to a global (measure + time) */
static struct SYMBOL *go_global_time(struct SYMBOL *s,
struct symsel_s *symsel)
{
struct SYMBOL *s2;
int bar_time;
if (symsel->bar <= 1) { /* special case: there is no measure 0/1 */
// && nbar == -1) { /* see set_bar_num */
if (symsel->bar == 0)
goto chk_time;
for (s2 = s; s2; s2 = s2->ts_next) {
if (s2->type == BAR
&& s2->time != 0)
break;
}
if (s2->time < voice_tb[cursys->top_voice].meter.wmeasure)
s = s2;
goto chk_time;
}
for ( ; s; s = s->ts_next) {
if (s->type == BAR
&& s->u >= symsel->bar)
break;
}
if (!s)
return NULL;
if (symsel->seq != 0) {
int seq;
seq = symsel->seq;
for (s = s->ts_next; s; s = s->ts_next) {
if (s->type == BAR
&& s->u == symsel->bar) {
if (--seq == 0)
break;
}
}
if (!s)
return 0;
}
chk_time:
if (symsel->time == 0)
return s;
bar_time = s->time + symsel->time;
while (s->time < bar_time) {
s = s->ts_next;
if (!s)
return s;
}
do {
s = s->ts_prev; /* go back to the previous sequence */
} while (!(s->sflags & S_SEQST));
return s;
}
/* treat %%clip */
static void do_clip(void)
{
struct SYMBOL *s, *s2;
struct SYSTEM *sy;
struct VOICE_S *p_voice;
int voice;
/* remove the beginning of the tune */
s = tsfirst;
if (clip_start.bar > 0
|| clip_start.time > 0) {
s = go_global_time(s, &clip_start);
if (!s) {
tsfirst = NULL;
return;
}
/* update the start of voices */
sy = cursys;
for (s2 = tsfirst; s2 != s; s2 = s2->ts_next) {
switch (s2->type) {
case STAVES:
sy = sy->next;
break;
case CLEF:
memcpy(&voice_tb[s2->voice].clef, &s2->as.u.clef,
sizeof voice_tb[0].clef);
break;
case KEYSIG:
memcpy(&voice_tb[s2->voice].key, &s2->as.u.key,
sizeof voice_tb[0].key);
break;
case TIMESIG:
memcpy(&voice_tb[s2->voice].meter, &s2->as.u.meter,
sizeof voice_tb[0].meter);
break;
}
}
cursys = sy;
for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
voice = p_voice - voice_tb;
for (s2 = s; s2; s2 = s2->ts_next) {
if (s2->voice == voice) {
s2->prev = NULL;
break;
}
}
p_voice->sym = s2;
}
tsfirst = s;
s->ts_prev = NULL;
}
/* remove the end of the tune */
s = go_global_time(s, &clip_end);
if (!s)
return;
/* keep the current sequence */
do {
s = s->ts_next;
if (!s)
return;
} while (!(s->sflags & S_SEQST));
/* cut the voices */
for (p_voice = first_voice; p_voice; p_voice = p_voice->next) {
voice = p_voice - voice_tb;
for (s2 = s->ts_prev; s2; s2 = s2->ts_prev) {
if (s2->voice == voice) {
s2->next = NULL;
break;
}
}
if (!s2)
p_voice->sym = NULL;
}
s->ts_prev->ts_next = NULL;
}
/* -- set the bar numbers and treat %%clip / %%break -- */
static void set_bar_num(void)
{
struct SYMBOL *s;
int bar_time, wmeasure;
int bar_num, bar_rep;
wmeasure = voice_tb[cursys->top_voice].meter.wmeasure;
if (wmeasure == 0) /* if M:none */
wmeasure = 1;
bar_rep = nbar;
/* don't count a bar at start of line */
for (s = tsfirst; ; s = s->ts_next) {
if (!s)
return;
switch (s->type) {
case TIMESIG:
case CLEF:
case KEYSIG:
case FMTCHG:
case STBRK:
continue;
case BAR:
if (s->u != 0) {
nbar = s->u; /* (%%setbarnb) */
break;
}
if (s->as.u.bar.repeat_bar
&& s->as.text
&& cfmt.contbarnb == 0) {
if (s->as.text[0] == '1') {
bar_rep = nbar;
} else {
nbar = bar_rep; /* restart bar numbering */
s->u = nbar;
}
}
break;
}
break;
}
/* set the measure number on the top bars */
bar_time = s->time + wmeasure; /* for incomplete measure at start of tune */
bar_num = nbar;
for ( ; s; s = s->ts_next) {
switch (s->type) {
case TIMESIG:
wmeasure = s->as.u.meter.wmeasure;
if (wmeasure == 0)
wmeasure = 1;
if (s->time < bar_time)
bar_time = s->time + wmeasure;
break;
case MREST:
bar_num += s->as.u.bar.len - 1;
while (s->ts_next
&& s->ts_next->type != BAR)
s = s->ts_next;
break;
case BAR: {
struct SYMBOL *s2;
int tim;
// if (s->as.flags & ABC_F_INVIS)
// break;
if (s->u != 0) {
bar_num = s->u; /* (%%setbarnb) */
if (s->time < bar_time) {
s->u = 0;
break;
}
} else {
if (s->time < bar_time) /* incomplete measure */
break;
bar_num++;
}
/* check if any repeat bar at this time */
tim = s->time;
s2 = s;
do {
if (s2->type == BAR
&& s2->as.u.bar.repeat_bar
&& s2->as.text
&& cfmt.contbarnb == 0) {
if (s2->as.text[0] == '1')
bar_rep = bar_num;
else /* restart bar numbering */
bar_num = bar_rep;
break;
}
s2 = s2->next;
} while (s2 && s2->time == tim);
s->u = bar_num;
bar_time = s->time + wmeasure;
break;
}
}
}
/* do the %%clip stuff */
if (clip_start.bar >= 0) {
if (bar_num <= clip_start.bar
|| nbar > clip_end.bar) {
tsfirst = NULL;
return;
}
do_clip();
}
/* do the %%break stuff */
{
struct brk_s *brk;
int nbar_min;
// if (nbar == 1)
// nbar = -1; /* see go_global_time */
nbar_min = nbar;
if (nbar_min == 1)
nbar_min = -1;
for (brk = brks; brk; brk = brk->next) {
if (brk->symsel.bar <= nbar_min
|| brk->symsel.bar > bar_num)
continue;
s = go_global_time(tsfirst, &brk->symsel);
if (s)
s->sflags |= S_EOLN;
}
}
if (cfmt.measurenb < 0) /* if no display of measure bar */
nbar = bar_num; /* update in case of more music to come */