-
Notifications
You must be signed in to change notification settings - Fork 15
/
toabc.c
2648 lines (2422 loc) · 64.9 KB
/
toabc.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
/*
* toabc.c - part of abc2abc - program to manipulate abc 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
*
*/
/* back-end for outputting (possibly modified) abc */
#define VERSION "1.86 May 05 2016 abc2abc"
/* for Microsoft Visual C++ 6.0 or higher */
#ifdef _MSC_VER
#define ANSILIBS
#endif
#include "abc.h"
#include "parseabc.h"
#include <stdio.h>
/* define USE_INDEX if your C libraries have index() instead of strchr() */
#ifdef USE_INDEX
#define strchr index
#endif
#ifdef ANSILIBS
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#else
extern char* strchr();
#endif
#define MAX_VOICES 30
/* should be plenty! */
programname fileprogram = ABC2ABC;
extern int oldchordconvention; /* for handling +..+ chords */
/* holds a fraction */
struct fract {
int num;
int denom;
};
struct fract barlen; /* length of a bar as given by the time signature */
struct fract unitlen; /* unit length as given by the L: field */
struct fract count; /* length of bar so far */
struct fract prevcount; /* length of bar before last increment */
struct fract tuplefactor; /* factor associated with a tuple (N */
struct fract chordfactor; /* factor of the first note in a chord [PHDM] 2013-03-10 */
struct fract breakpoint; /* used to break bar into beamed sets of notes */
int barno; /* number of bar within tune */
int newspacing; /* was -s option selected ? */
int barcheck, repcheck; /* indicate -b and -r options selected */
int echeck; /* was error-checking turned off ? (-e option) */
int newbreaks; /* was -n option selected ? */
int nodouble_accidentals;
int totalnotes, notecount;
int bars_per_line; /* number supplied after -n option */
int barcount;
int expect_repeat;
int tuplenotes, barend;
int xinhead, xinbody; /* are we in head or body of abc tune ? */
int inmusic; /* are we in a line of notes (in the tune body) ? */
int startline, blankline;
int transpose; /* number of semitones to transpose by (-t option) */
struct fract lenfactor; /* fraction to scale note lengths; -v,-d options */
int newkey; /* key after transposition (expressed as no. of sharps) */
int lines; /* used by transposition */
int orig_key_number; /* used for gchord transposition */
int new_key_number; /* used for gchord transposition */
int oldtable[7], newtable[7]; /* for handling transposition */
int inchord; /* are we in a chord [ ] ? */
int ingrace; /* are we in a grace note set { } ? */
int chordcount; /* number of notes or rests in current chord */
int inlinefield; /* boolean - are we in [<field>: ] ? */
int cleanup; /* boolean to indicate -u option (update notation) */
char tmp[2000]; /* buffer to hold abc output being assembled */
int output_on = 1; /* if 0 suppress output */
int passthru = 0; /* output original abc file [SS] 2011-06-07 */
long selected_voices = -1; /* all voices are selected [PHDM] 2013-03-08 */
int newrefnos; /* boolean for -X option (renumber X: fields) */
int newref; /* next new number for X: field */
int useflats=0; /* flag associated with nokey.*/
int adapt_useflats_to_gchords = 1; /* experimental flag */
int usekey = 0;
int drumchan=0; /* flag to suppress transposition */
int noplus; /* flag for outputting !..! instructions instead of +...+ */
extern int nokey; /* signals no key signature assumed */
extern int nokeysig; /* signals -nokeys or -nokeysf option */
extern int voicecodes ; /* from parseabc.c */
extern char voicecode[16][30]; /*for interpreting V: string */
struct voicetype { /* information needed for each voice */
int number; /* voice number from V: field */
int barcount;
int foundbar;
struct abctext* currentline;
int bars_remaining;
int bars_complete;
int drumchan;
} voice[MAX_VOICES];
int voicecount, this_voice, next_voice;
enum abctype {field, bar, barline};
/* linestat is used by -n for deciding when to generate a newline */
enum linestattype {fresh, midmusic, endmusicline, postfield};
enum linestattype linestat;
/* struct abctext is used to store abc lines for re-formatting (-n option) */
struct lyricwords{
struct lyricwords* nextverse;
char* words;
};
struct abctext{ /* linked list used to store output before re-formatting */
struct abctext* next;
char* text;
enum abctype type;
int notes;
struct lyricwords* lyrics;
};
struct abctext* head;
struct abctext* tail;
extern char *mode[];
extern int modekeyshift[];
int basemap[7], workmap[7]; /* for -nokey and pitchof() */
int workmul[7];
void copymap();
void printpitch(int);
void setup_sharps_flats (int sf);
int pitchof(char note,int accidental,int mult,int octave);
void transpose_note(char xaccidental,int xmult,char xnote,int xoctave,int transpose,
char* accidental, int* mult, char* note, int* octave);
static int purgespace(p)
char* p;
/* if string p is empty or consists of spaces, set p to the empty string */
/* and return 1, otherwise return 0. Used to test tmp */
/* part of new linebreak option (-n) */
{
int blank;
char *s;
blank = 1;
s = p;
while (*s != '\0') {
if (*s != ' ') blank = 0;
s = s + 1;
};
if (blank) {
*p = '\0';
};
return(blank);
}
int zero_barcount(foundbar)
/* initialize bar counter for abctext elements */
/* part of new linebreak option (-n) */
int *foundbar;
{
*foundbar = 0;
return(0);
}
int new_barcount(type, foundbar, oldcount)
enum abctype type;
int *foundbar;
int oldcount;
/* work out whether we have reached the end of a bar in abctext elements */
/* and increment barcount if we have */
/* part of new linebreak option (-n) */
{
int new_value;
new_value = oldcount;
if (type == bar) {
*foundbar = 1;
};
if ((type == barline) && (*foundbar == 1)) {
new_value = new_value + 1;
*foundbar = 0;
};
return(new_value);
}
static void setline(t)
enum linestattype t;
/* generates newline, or continuation, or nothing at all */
/* part of new linebreak option (-n) */
{
if ((t == fresh) && ((linestat == postfield) || (linestat == endmusicline))) {
printf("\n");
};
if ((t == fresh) && (linestat == midmusic)) {
printf("\\\n");
};
linestat = t;
}
static int flush_abctext(bars, termination)
int bars;
enum linestattype termination;
/* outputs up to the specified number of bars of stored music */
/* and frees up the storage allocated for those bars. */
/* returns the number of bars actually output */
/* part of new linebreak option (-n) */
{
struct abctext *p, *nextp;
struct lyricwords *q, *r;
struct lyricwords *barlyrics;
int count, donewords, wordline;
int i, foundtext;
int foundbar;
/* printf("flush_abctext called\n"); */
/* print music */
p = head;
count = zero_barcount(&foundbar);
while ((p != NULL) && (count < bars)) {
if (p->type == field) {
setline(fresh);
};
printf("%s", p->text);
if (p->type == field) {
setline(postfield);
setline(fresh);
} else {
setline(midmusic);
};
count = new_barcount(p->type, &foundbar, count);
if ((count == bars) && (p->type == barline)) {
setline(endmusicline);
};
p = p->next;
};
if (linestat == midmusic) {
setline(termination);
};
if (bars > 0) {
/* print out any w: lines */
donewords = 0;
wordline = 0;
while (donewords == 0) {
p = head;
foundtext = 0;
count = zero_barcount(&foundbar);
while ((p != NULL) && (count < bars)) {
barlyrics = p->lyrics;
for (i=0; i<wordline; i++) {
if (barlyrics != NULL) {
barlyrics = barlyrics->nextverse;
};
};
if (barlyrics != NULL) {
if (foundtext == 0) {
setline(fresh);
printf("w:");
foundtext = 1;
};
printf("%s",barlyrics->words);
};
count = new_barcount(p->type, &foundbar, count);
p = p->next;
};
if (foundtext == 0) {
donewords = 1;
} else {
setline(postfield);
setline(fresh);
};
wordline = wordline + 1;
};
};
/* move head on and free up space used by stuff printed out */
count = zero_barcount(&foundbar);
p = head;
foundbar = 0;
while ((p != NULL) && (count < bars)) {
if (p != NULL) {
free(p->text);
q = p->lyrics;
while (q != NULL) {
free(q->words);
r = q->nextverse;
free(q);
q = r;
};
count = new_barcount(p->type, &foundbar, count);
nextp = p->next;
free(p);
p = nextp;
};
head = p;
};
if (head == NULL) {
tail = NULL;
};
return(count);
}
void complete_bars(v)
/* mark all bars as completed (i.e. having associated w: fields parsed) */
/* and out put all music lines which contain the full set of bars */
/* part of new linebreak option (-n) */
struct voicetype *v;
{
int bars_done;
v->bars_complete = v->bars_complete + v->barcount;
v->barcount = 0;
while (v->bars_complete > v->bars_remaining) {
bars_done = flush_abctext(v->bars_remaining, endmusicline);
setline(fresh);
v->bars_complete = v->bars_complete - bars_done;
v->bars_remaining = v->bars_remaining - bars_done;
if (v->bars_remaining == 0) {
v->bars_remaining = bars_per_line;
};
};
}
void complete_all(v, termination)
struct voicetype *v;
enum linestattype termination;
/* output all remaining music and fields */
/* part of new linebreak option (-n) */
{
int bars_done;
complete_bars(v);
bars_done = flush_abctext(v->bars_remaining+1, termination);
v->bars_complete = v->bars_complete - bars_done;
v->bars_remaining = v->bars_remaining - bars_done;
if (v->bars_remaining == 0) {
v->bars_remaining = bars_per_line;
};
head = NULL;
tail = NULL;
voice[this_voice].currentline = NULL;
}
static struct abctext* newabctext(t)
enum abctype t;
/* called at newlines and barlines */
/* adds current output text to linked list structure */
/* part of new linebreak option (-n) */
{
struct abctext* p;
if (output_on == 0) {
p = NULL;
return(p);
};
if (newbreaks) {
/*
if ((t == field) && (!xinbody || (this_voice != next_voice))) {
*/
if (t == field) {
complete_all(&voice[this_voice], midmusic);
this_voice = next_voice;
};
p = (struct abctext*) checkmalloc(sizeof(struct abctext));
p->text = addstring(tmp);
tmp[0] = '\0';
p->next = NULL;
p->type = t;
p->lyrics = NULL;
if (t == bar) {
p->notes = notecount;
totalnotes = totalnotes + notecount;
notecount = 0;
} else {
p->notes = 0;
};
if (xinbody) {
voice[this_voice].barcount = new_barcount(t,
&voice[this_voice].foundbar,
voice[this_voice].barcount);
};
if (head == NULL) {
head = p;
tail = p;
} else {
tail->next = p;
tail = p;
};
if ((t != field) && (voice[this_voice].currentline == NULL)) {
voice[this_voice].currentline = p;
};
} else {
printf("%s", tmp); /* output to stdout is here */
tmp[0] = '\0';
p = NULL;
};
inmusic = 1;
return(p);
}
static int nextnotes()
/* return the number of notes in the next bar */
/* part of new linebreak option (-n) */
{
int n, got;
struct abctext* p;
p = head;
n = 100;
got = 0;
while ((p != NULL) && (!got)) {
if (p->type == bar) {
n = p->notes;
got = 1;
} else {
p = p->next;
};
};
return(n);
}
static void reduce(a, b)
int *a, *b;
{
int t, n, m;
/* 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;
*b = *b/n;
}
/*
* addunits must be called for each single note or rest and
* at each chord end. When called at chordoff time, make
* sure that inchord is still true. [PHDM] 2013-03-10
*/
static void addunits(n, m)
int n, m;
/* add fraction n/m to count */
{
if (inchord) { /* [PHDM] 2013-03-10 */
if (!chordcount) /* empty chord */
return;
n *= chordfactor.num;
m *= chordfactor.denom;
}
if (tuplenotes) { /* [PHDM] 2013-03-10 */
n *= tuplefactor.num;
m *= tuplefactor.denom;
tuplenotes = tuplenotes - 1;
};
count.num = n*count.denom + count.num*(m*unitlen.denom);
count.denom = (m*unitlen.denom)*count.denom;
reduce(&count.num, &count.denom);
}
void parse_voices_selection(voices_string) /* [PHDM] 2013-03-08 */
char *voices_string;
{
char *s = voices_string;
selected_voices = 0;
if (voices_string == 0x0) return; /* [SS] 2015-02-22 */
do {
int v = readnump(&s);
selected_voices |= 1 << v;
} while (*s++);
}
int must_emit_voice(n) /* [PHDM] 2013-03-08 */
int n;
{
return selected_voices & (1 << n);
}
void event_init(argc, argv, filename)
int argc;
char* argv[];
char** filename;
/* routine called on program start-up */
{
int targ, narg;
if ((getarg("-h", argc, argv) != -1) || (argc < 2)) {
printf("abc2abc version %s\n",VERSION);
printf("Usage: abc2abc <filename> [-s] [-n X] [-b] [-r] [-e] [-t X]\n");
printf(" [-u] [-d] [-v] [-V X[,Y,,,]] [-P X[,Y...]] [-ver] [-X n]\n");
printf(" -s for new spacing\n");
printf(" -n X to re-format the abc with a new linebreak every X bars\n");
printf(" -b to remove bar checking\n");
printf(" -r to remove repeat checking\n");
printf(" -e to remove all error reports\n");
printf(" -t X to transpose X semitones\n");
printf(" -nda No double accidentals in guitar chords\n");
printf(" -nokeys No key signature. Use sharps\n");
printf(" -nokeyf No key signature. Use flats\n");
printf(" -u to update notation ([] for chords and () for slurs)\n");
printf(" -usekey n Use key signature sf (sharps/flats)\n");
printf(" -d to notate with doubled note lengths\n");
printf(" -v to notate with halved note lengths\n");
printf(" -V X[,Y...] to output only voices X,Y...\n");
printf(" -P X[,Y...] restricts action to voice X,Y..., leaving other voices intact\n");
printf(" -ver prints version number and exits\n");
printf(" -X n renumber the all X: fields as n, n+1, ..\n");
printf(" -OCC old chord convention (eg. +CE+)\n");
/*printf(" -noplus use !...! instead of +...+ for instructions\n");
[SS] 2012-06-04
*/
exit(0);
} else {
*filename = argv[1];
};
nodouble_accidentals = 0; /* use correct guitar chords */
if (getarg("-ver",argc,argv) != -1) {
printf("%s\n",VERSION);
exit(0);
}
if (getarg("-u", argc, argv) == -1) {
cleanup = 0;
} else {
cleanup = 1;
oldchordconvention = 1;
};
if (getarg("-s", argc, argv) == -1) {
newspacing = 0;
} else {
newspacing = 1;
};
narg = getarg("-X", argc, argv);
if (narg == -1) {
newrefnos = 0;
} else {
newrefnos = 1;
if (narg < argc) {
newref = readnumf(argv[narg]);
} else {
newref = 1;
};
};
if (getarg("-e", argc, argv) == -1) {
echeck = 1;
} else {
echeck = 0;
};
narg = getarg("-n", argc, argv);
if (narg == -1) {
newbreaks = 0;
} else {
newbreaks = 1;
if (narg >= argc) {
event_error("No value for bars per line after -n");
bars_per_line = 4;
} else {
bars_per_line = readnumf(argv[narg]);
if (bars_per_line < 1) {
bars_per_line = 4;
};
};
};
if (getarg("-b", argc, argv) != -1) {
barcheck = 0;
} else {
barcheck = 1;
};
if (getarg("-r", argc, argv) != -1) {
repcheck = 0;
} else {
repcheck = 1;
};
if (getarg("-v", argc, argv) != -1) {
lenfactor.num = 1;
lenfactor.denom = 2;
} else {
if (getarg("-d", argc, argv) != -1) {
lenfactor.num = 2;
lenfactor.denom = 1;
} else {
lenfactor.num = 1;
lenfactor.denom = 1;
};
};
targ = getarg("-t", argc, argv);
if (targ == -1) {
transpose = 0;
} else {
if (targ >= argc) {
event_error("No tranpose value supplied");
} else {
if (*argv[targ] == '-') {
transpose = -readnumf(argv[targ]+1);
} else if (*argv[targ] == '+') {
transpose = readnumf(argv[targ]+1);
} else {
transpose = readnumf(argv[targ]);
};
};
};
targ = getarg("-nda",argc,argv);
if (targ != -1) nodouble_accidentals = 1;
targ = getarg("-nokeys",argc,argv);
if (targ != -1) nokeysig=1; /* [SS] 2016-03-03 */
targ = getarg("-nokeyf",argc,argv);
if (targ != -1) {nokeysig=1; useflats=1;} /* [SS] 2016-03-03 */
targ = getarg("-V", argc, argv);
if (targ != -1) {
parse_voices_selection(argv[targ]); /* [PHDM] 2013-03-08 */
};
targ = getarg("-P", argc, argv); /* [SS] 2011-06-07 */
if (targ != -1) {
passthru = 1;
parse_voices_selection(argv[targ]); /* [PHDM] 2013-03-08 */
}
targ = getarg("-usekey",argc,argv);
if (targ != -1) {
usekey = readsnumf(argv[targ]);
nokey = 1;
if (usekey < 0) useflats=1;
if (usekey <-5) usekey = -5;
if (usekey >5) usekey = 5;
setup_sharps_flats (usekey);
}
if (getarg("-OCC",argc,argv) != -1) oldchordconvention=1;
/*if (getarg("-noplus",argc,argv) != -1) noplus = 1; [SS] 2012-06-04*/
/* printf("%% output from abc2abc\n"); */
startline = 1;
blankline = 0;
xinbody =0;
inmusic = 0;
inchord = 0;
ingrace = 0;
head = NULL;
tail = NULL;
tmp[0] = '\0';
totalnotes = 0;
}
void emit_string(s)
char *s;
/* output string */
{
if (output_on) {
strcpy(tmp+strlen(tmp), s);
};
}
void emit_char(ch)
char ch;
/* output single character */
{
char *place;
if (output_on) {
place = tmp+strlen(tmp);
*place = ch;
*(place+1) = '\0';
};
}
void emit_int(n)
int n;
/* output integer */
{
if (output_on) {
sprintf(tmp+strlen(tmp), "%d", n);
};
}
void emit_string_sprintf(s1, s2)
char *s1;
char *s2;
/* output string containing string expression %s */
{
if (output_on) {
sprintf(tmp+strlen(tmp), s1, s2);
};
}
void emit_int_sprintf(s, n)
char *s;
int n;
/* output string containing int expression %d */
{
if (output_on) {
sprintf(tmp+strlen(tmp), s, n);
};
}
void unemit_inline()
/* remove previously output start of inline field */
/* needed for -V voice selection option */
{
int len;
len = strlen(tmp);
if ((len > 0) && (tmp[len-1] == '[')) {
tmp[len-1] = '\0'; /* delete last character */
} else {
event_error("Internal error - Could not delete [");
};
}
static void close_newabc()
/* output all remaining abc_text elements */
/* part of new linebreak option (-n) */
{
if (newbreaks) {
complete_all(&voice[this_voice], endmusicline);
if (linestat == midmusic) setline(endmusicline);
setline(fresh);
};
}
void event_eof()
{
close_newabc();
}
void event_blankline()
{
output_on = 1;
close_newabc();
/* if (newbreaks) [SS] 2006-09-23 */ printf("\n");
xinbody = 0;
xinhead = 0;
parseroff();
blankline = 1;
}
void event_text(p)
char *p;
{
emit_string_sprintf("%%%s", p);
inmusic = 0;
}
void event_reserved(p)
char p;
{
emit_char(p);
inmusic = 0;
}
void event_tex(s)
char *s;
{
emit_string(s);
inmusic = 0;
}
void print_inputline(); /* from parseabc.c */
void event_linebreak()
{
if (!output_on && passthru) print_inputline(); /* [SS] 2011-06-07*/
if (newbreaks) {
if (!purgespace(tmp)) {
if (inmusic) {
newabctext(bar);
} else {
newabctext(field);
};
};
} else {
newabctext(bar);
if (output_on) {
printf("\n"); /* linefeed to stdout is here */
};
/* don't output new line if voice is already suppressed
otherwise we will get lots of blank lines where we
are suppressing output. [SS] feb-10-2002.
*/
};
}
void event_startmusicline()
/* encountered the start of a line of notes */
{
voice[this_voice].currentline = NULL;
complete_bars(&voice[this_voice]);
}
void event_endmusicline(endchar)
char endchar;
/* encountered the end of a line of notes */
{
}
void event_error(s)
char *s;
{
if (echeck && output_on) { /* [SS] 2011-04-14 */
printf("\n%%Error : %s\n", s);
};
}
void event_warning(s)
char *s;
{
if (echeck && output_on) { /* [SS] 2011-04-14 */
printf("\n%%Warning : %s\n", s);
};
}
void event_comment(s)
char *s;
{
if (newbreaks && (!purgespace(tmp))) {
if (inmusic) {
newabctext(bar);
} else {
newabctext(field);
};
};
emit_string_sprintf("%%%s", s);
inmusic = 0;
}
void event_specific(package, s)
char *package, *s;
{
char command[40];
int ch;
char *p;
emit_string("%%");
emit_string(package);
emit_string(s);
inmusic = 0;
/* detect drum channel by searching for %%MIDI channel 10 */
if (strcmp(package,"MIDI") != 0) return;
p = s;
skipspace(&p);
readstr(command, &p, 40);
if (strcmp(command, "channel") != 0) return;
skipspace(&p);
ch = readnump(&p);
if(ch == 10) {
voice[next_voice].drumchan = 1;
drumchan = 1;
}
/* printf("event_specific: next_voice = %d\n",next_voice); */
}
void event_info(f)
/* handles info field I: */
char *f;
{
emit_string_sprintf("I:%s", f);
inmusic = 0;
}
void event_field(k, f)
char k;
char *f;
{
emit_char(k);
emit_char(':');
emit_string(f);
inmusic = 0;
}
struct abctext* getbar(place)
struct abctext *place;
/* find first element in list which is a bar of music */
{
struct abctext *newplace;
newplace = place;
while ((newplace != NULL) &&
((newplace->type != bar) ||
(newplace->notes == 0))) {
newplace = newplace->next;
};
return(newplace);
}
struct abctext* getnextbar(place)
struct abctext *place;
/* find next element in list which is a bar of music */
{
struct abctext *newplace;
newplace = place;
if (newplace != NULL) {
newplace = getbar(newplace->next);
};
return(newplace);
};
void append_lyrics(place, newwords)
struct abctext *place;
char *newwords;
/* add lyrics to end of lyric list associated with bar */
{
struct lyricwords* new_words;
struct lyricwords *new_place;
if (place == NULL) {
return;
};
/* printf("append_lyrics has %s at %s\n", newwords, place->text); */
new_words = (struct lyricwords*)checkmalloc(sizeof(struct lyricwords));
/* add words to bar */
new_words->nextverse = NULL;
new_words->words = addstring(newwords);
if (place->lyrics == NULL) {
place->lyrics = new_words;
} else {
new_place = place->lyrics;
/* find end of list */
while (new_place->nextverse != NULL) {
new_place = new_place->nextverse;
};
new_place->nextverse = new_words;
};
}
struct abctext* apply_bar(syll, place, notesleft, barwords)
/* advance to next bar (on finding '|' in a w: field) */
char* syll;
struct abctext *place;
int *notesleft;
struct vstring *barwords;
{
struct abctext* new_place;
if (place == NULL) {
return(NULL);
};
new_place = place;
addtext(syll, barwords);
append_lyrics(place, barwords->st);
/* go on to next bar */
clearvstring(barwords);
new_place = getnextbar(place);
if (new_place != NULL) {
*notesleft = new_place->notes;
};
return(new_place);
}
struct abctext* apply_syllable(syll, place, notesleft, barwords)
/* attach syllable to appropriate place in abctext structure */
char* syll;
struct abctext *place;
int *notesleft;
struct vstring *barwords;
{
struct abctext* new_place;
char msg[80];
if (place == NULL) {
sprintf(msg, "Cannot find note to match \"%s\"", syll);
event_error(msg);
return(NULL);
};
new_place = place;
addtext(syll, barwords);
*notesleft = *notesleft - 1;
if (*notesleft == 0) {
append_lyrics(place, barwords->st);
/* go on to next bar */
clearvstring(barwords);