forked from xlvector/abcmidi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yapstree.c
3006 lines (2797 loc) · 69.8 KB
/
yapstree.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
/*
* yaps - program to convert abc files to PostScript.
* Copyright (C) 1999 James Allwright
* e-mail: J.R.Allwright@westminster.ac.uk
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
/* yapstree.c - back-end for abc parser. */
/* generates a data structure suitable for typeset music */
#define VERSION "1.68 April 19 2017 yaps"
#include <stdio.h>
#ifdef USE_INDEX
#define strchr index
#endif
/* for Microsoft VC 6++ or higher */
#ifdef _MSC_VER
#define ANSILIBS
#endif
#ifdef ANSILIBS
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#else
char* strchr();
#endif
#include "abc.h"
#include "parseabc.h"
#include "parser2.h"
#include "structs.h"
#include "drawtune.h"
extern void setscaling(char *s);
extern void font_command();
extern void setup_fonts();
extern void printtune(struct tune *t);
extern void set_keysig(struct key *k, struct key *newval);
programname fileprogram = YAPS;
extern int oldchordconvention; /* for handling +..+ chords */
struct voice* cv;
struct tune thetune;
char outputname[256];
char outputroot[256];
char matchstring[256];
int fileopen;
int repcheck;
int xinhead;
int xinbody;
int suppress;
int debugging;
int pagenumbering;
int separate_voices;
int print_xref;
int landscape;
int barnums,nnbars;
extern int gchords_above;
extern int decorators_passback[DECSIZE]; /* a kludge for passing
information from the event_handle_instruction to parsenote
in parseabc.c */
enum linestattype {fresh, midmusic, endmusicline, postfield};
enum linestattype linestat;
int dummydecorator[DECSIZE]; /* used in event_chord */
void setfract(f, a, b)
struct fract* f;
int a, b;
/* assign value to fraction */
{
f->num = a;
f->denom = b;
}
void reducef(f)
struct fract* f;
/* reducef fraction to smallest terms */
{
int t, n, m;
/* find HCF using Euclid's algorithm */
if (f->num > f->denom) {
n = f->num;
m = f->denom;
} else {
n = f->denom;
m = f->num;
};
while (m != 0) {
t = n % m;
n = m;
m = t;
};
f->num = f->num/n;
f->denom = f->denom/n;
}
static struct fract* newfract(int a, int b)
/* create an initialized fraction */
{
struct fract* f;
f = (struct fract*)checkmalloc(sizeof(struct fract));
f->num = a;
f->denom = b;
return(f);
}
static struct slurtie* newslurtie()
/* create a new slur/tie data structure */
{
struct slurtie* f;
f = (struct slurtie*)checkmalloc(sizeof(struct slurtie));
f->begin = NULL;
f->end = NULL;
f->crossline = 0;
return(f);
}
static struct atempo* newtempo(int count, int n, int m, int relative,
char *pre, char *post)
/* create an initialized tempo data structure */
{
struct atempo* t;
t = (struct atempo*)checkmalloc(sizeof(struct atempo));
t->count = count;
t->basenote.num = n;
t->basenote.denom = m;
t->relative = relative;
if (pre == NULL) {
t->pre = NULL;
} else {
t->pre = addstring(pre);
};
if (post == NULL) {
t->post = NULL;
} else {
t->post = addstring(post);
};
return(t);
}
static struct vertspacing* newvertspacing()
/* create new vertspacing structure */
{
struct vertspacing* p;
p = (struct vertspacing*)checkmalloc(sizeof(struct vertspacing));
p->height = 0.0;
p->descender = 0.0;
p->yend = 0.0;
p->yinstruct = 0.0;
p->ygchord = 0.0;
p->ywords = 0.0;
return(p);
}
static struct tuple* newtuple(int n, int q, int r, int label)
/* create tuple data structure */
{
struct tuple* f;
f = (struct tuple*)checkmalloc(sizeof(struct tuple));
f->n = n;
f->q = q;
f->r = r;
f->label = label;
f->beamed = 1;
return(f);
}
static struct chord* newchord()
/* create chord data structure */
{
struct chord* f;
f = (struct chord*)checkmalloc(sizeof(struct chord));
f->ytop = 0;
f->ybot = 0;
return(f);
}
struct aclef* newclef(enum cleftype t, int octave)
/* create and initialize clef data structure */
{
struct aclef* f;
f = (struct aclef*)checkmalloc(sizeof(struct aclef));
f->type = t;
f->octave = octave;
return(f);
}
struct key* newkey(char* name, int sharps, char accidental[], int mult[])
/* create and initialize key signature */
{
struct key* k;
int i;
k = (struct key*)checkmalloc(sizeof(struct key));
k->name = addstring(name);
k->sharps = sharps;
for (i=0; i<7; i++) {
k->map[i] = accidental[i];
k->mult[i] = mult[i];
};
return(k);
}
void init_llist(struct llist* l)
/* initialize a linked list */
{
l->first = NULL;
l->last = NULL;
l->place = NULL;
}
void addtolist(struct llist* p, void* item)
/* append an item to a linked list */
{
struct el* x;
x = (struct el*)checkmalloc(sizeof(struct el));
x->next = NULL;
x->datum = item;
if (p->first == NULL) {
p->first = x;
p->last = x;
} else {
p->last->next = x;
p->last = x;
};
}
void* firstitem(struct llist* p)
/* find the first item in the list */
/* also initialize for a traversal using nextitem() */
{
if (p == NULL) {
return(NULL);
};
p->place = p->first;
if (p->place == NULL) {
return(NULL);
} else {
return(p->place->datum);
};
}
void* nextitem(struct llist* p)
/* return 'next' item in the list. Successive calls return successive */
/* items or NULL after the end of the list has been reached. firstitem() */
/* must be called prior to the first call to nextitem() */
{
if (p->place == NULL) {
return(NULL);
} else {
p->place = p->place->next;
if (p->place == NULL) {
return(NULL);
} else {
return(p->place->datum);
};
};
}
void freellist(struct llist* l)
/* frees up all dynamically allocated memory used to build the linked list */
{
void* p;
struct el* e;
struct el* olde;
/* printf("freellist\n"); */
if (l != NULL) {
p = firstitem(l);
while (p != NULL) {
free(p);
p = nextitem(l);
};
e = l->first;
while (e != NULL) {
olde = e;
e = e->next;
free(olde);
};
init_llist(l);
};
}
static void closebeam(struct voice* v)
/* called after a run of notes to be beamed together */
{
struct note* n;
struct feature* ft;
int stemup;
int ingrace;
if (cv->tuplenotes > 0) {
cv->thistuple->beamed = 0;
};
if (v->beamroot == v->beamend) {
ft = v->beamroot;
n = ft->item;
n->beaming = single;
v->beamroot = NULL;
return;
};
if (v->beammax + v->beammin > 2*4) {
stemup = 0;
} else {
stemup = 1;
};
ft = v->beamroot;
ingrace = 0;
while ((ft != NULL) && (ft != v->beamend)) {
switch (ft->type) {
case NOTE:
if (ingrace == 0) {
n = ft->item;
n->stemup = stemup;
};
break;
case GRACEON:
ingrace = 1;
break;
case GRACEOFF:
ingrace = 0;
break;
default:
break;
};
ft = ft->next;
};
if (ft == v->beamend) {
n = ft->item;
n->stemup = stemup;
n->beaming = endbeam;
} else {
printf("closebeam: internal data error\n");
exit(1);
};
v->beamroot = NULL;
}
static void closegracebeam(struct voice* v)
/* called after a run of grace notes to be beamed together */
{
struct note* n;
struct feature* ft;
ft = v->gracebeamend;
if (ft == NULL) {
event_error("Missing grace notes");
} else {
n = ft->item;
if (v->gracebeamroot == v->gracebeamend) {
n->beaming = single;
} else {
n->beaming = endbeam;
};
};
}
static void insertnote(struct feature* chordplace, struct feature* newfeature)
/* place NOTE in decreasing pitch order within chord */
{
struct note* n;
struct note* newnote;
struct feature* f;
struct feature* previous;
int foundplace;
newnote = newfeature->item;
previous = chordplace;
f = chordplace->next;
foundplace = 0;
n = NULL;
while ((f != NULL)&&(f->type==NOTE)&&(foundplace == 0)) {
n = f->item;
if (newnote->y > n->y) {
foundplace = 1;
} else {
previous = f;
f = f->next;
};
};
/* printvoiceline in drawtune.c expects the gchord or
* instructions to be associated with the first note in
* chord. If the notes are reordered then we need
* to move these fields.
* if previous == chordplace then move n->gchords and
* n->instructions to newnote->gchords and newnote->instructions
*/
if (previous == chordplace && n != NULL) {
newnote->gchords = n->gchords;
newnote->instructions = n->instructions;
n->gchords = NULL;
n->instructions = NULL;
}
newfeature->next = previous->next;
previous->next = newfeature;
if (newfeature->next == NULL) {
cv->last = newfeature;
};
}
static void beamitem(featuretype mytype, void* newitem, struct feature* x)
/* This routine is responsible for working out which notes are to be */
/* beamed together and recording this in the note structure record */
{
struct note* n;
/* deal with beaming here */
if (cv->ingrace) {
if (mytype == NOTE) {
n = newitem;
n->stemup = 1;
if (cv->gracebeamroot == NULL) {
cv->gracebeamroot = x;
cv->gracebeamend = x;
n->beaming = startbeam;
} else {
cv->gracebeamend = x;
n->beaming = midbeam;
};
};
} else {
if (cv->beamroot != NULL) {
switch (mytype) {
case NOTE:
n = newitem;
if (n->base_exp >= -2) {
n->beaming = single;
closebeam(cv);
} else {
/* extend beam */
if (n->y > cv->beammax) {
cv->beammax = n->y;
};
if (n->y < cv->beammin) {
cv->beammin = n->y;
};
cv->beamend = x;
n->beaming = midbeam;
};
break;
case KEY:
case REST:
case NOBEAM:
case SINGLE_BAR:
case DOUBLE_BAR:
case BAR_REP:
case REP_BAR:
case BAR1:
case REP_BAR2:
case DOUBLE_REP:
case THICK_THIN:
case THIN_THICK:
case TUPLE:
case MUSICSTOP:
/* closebeam */
closebeam(cv);
break;
default:
break;
};
} else {
if (mytype == NOTE) {
n = newitem;
if (n->base_exp >= -2) {
n->beaming = single;
} else {
n->beaming = startbeam;
cv->beammax = n->y;
cv->beammin = n->y;
cv->beamroot = x;
cv->beamend = x;
};
};
};
};
}
static struct feature* addfeature(featuretype mytype, void* newitem)
/* append a new data element to the linked list for the current voice */
/* The element can be a note or lots of other things */
{
struct voice* p;
struct feature* x;
p = cv;
/* printf("in addfeature type=%d\n", mytype); */
if (cv == NULL) {
printf("ERROR: no current voice in addfeature type=%d\n", mytype);
printf("xinhead = %d xinbody = %d\n", xinhead, xinbody);
exit(0);
};
x = (struct feature*)checkmalloc(sizeof(struct feature));
x->next = NULL;
x->type = mytype;
x->item = newitem;
x->xleft = 0;
x->xright = 0;
x->yup = 0;
x->ydown = 0;
if (cv->first == NULL) {
cv->first = x;
cv->last = x;
beamitem(mytype, newitem, x);
} else {
if ((cv->last == NULL)||(cv->last->next != NULL)) {
printf("expecting NULL at list end!\n");
exit(0);
};
if ((cv->inchord)&&(mytype==NOTE)) {
insertnote(cv->chordplace, x);
} else {
cv->last->next = x;
cv->last = x;
beamitem(mytype, newitem, x);
};
};
return(x);
}
struct llist* newlist()
/* create and initialize a new linked list */
{
struct llist* l;
l = (struct llist*)checkmalloc(sizeof(struct llist));
init_llist(l);
return(l);
}
static int notenum(int octave, char ch, enum cleftype clef, int clefoctave)
/* converts note to number for stave position */
/* note E is zero (bottom line of stave) */
{
int n;
n = 5 + (7 + ch -'c')%7 + 7*(octave-1);
switch (clef) {
case treble:
break;
case soprano:
n = n + 2;
break;
case mezzo:
n = n + 4;
break;
case alto:
n = n + 6;
break;
case tenor:
n = n + 8;
break;
case baritone:
n = n + 10;
break;
case bass:
n = n + 12;
break;
case noclef:
break;
};
switch (clefoctave) {
case -22:
n = n + 21;
break;
case -15:
n = n + 14;
break;
case -8:
n = n + 7;
break;
case 8:
n = n - 7;
break;
case 15:
n = n - 14;
break;
case 22:
n = n - 21;
break;
default:
break;
};
return(n);
}
int count_dots(int *base, int *base_exp, int n, int m)
/* convert fraction to 2^base_exp followed by dots */
/* previously used 1/base instead of 2^base_exp */
/* -1 indicates a note value which is impossible to represent */
{
int dots;
int start;
int a, b;
*base = 1; /* set default value if we fail */
*base_exp = 0;
if ((n<1)||(m<1)) {
return(-1);
};
/* check denominator is power of 2 */
a = m;
while (a>1) {
if (a%2 == 1) {
return(-1);
};
a = a/2;
};
dots = 0;
start = m;
while (start < n) {
start = start*2;
*base_exp = *base_exp + 1;
};
while (start > n) {
start = start/2;
*base_exp = *base_exp - 1;
};
if (start == 0) {
printf("Problem with %d / %d\n", n, m);
exit(0);
};
*base = m/start;
a = n - start;
b = start;
while (a>0) {
dots = dots + 1;
b = b/2;
a = a - b;
if (a< 0) {
return(-1);
};
};
return(dots);
}
static char* decstring(int decorators[])
/* creates a string of decorators (ornament, staccato, roll, up-bow etc.) */
/* from a boolean array */
{
int i, j;
char decs[DECSIZE+1];
j = 0;
for (i=0; i<DECSIZE; i++) {
if (decorators[i]) {
decs[j] = decorations[i];
j = j + 1;
};
};
decs[j] = '\0';
if (j==0) {
return(NULL);
} else {
return(addstring(decs));
};
}
static struct note* newnote(decorators, xaccidental, xmult, xnote, xoctave,
a, b)
int decorators[DECSIZE];
int xmult;
char xaccidental, xnote;
int xoctave;
int a, b;
/* create and set up the fields for a note structure */
{
struct note* n;
n = (struct note*)checkmalloc(sizeof(struct note));
setfract(&n->len, a, b);
reducef(&n->len);
n->dots = count_dots(&n->base, &n->base_exp, n->len.num, n->len.denom);
/*
if (n->dots == -1) {
event_error("Illegal note length");
};
*/
n->accents = decstring(decorators);
n->accidental = xaccidental;
n->acc_offset = 0;
n->fliphead = 0;
n->mult = xmult;
n->octave = xoctave;
n->pitch = xnote;
n->y = notenum(xoctave, xnote, cv->clef->type, cv->clef->octave);
if (n->y < 4) {
n->stemup = 1;
} else {
n->stemup = 0;
};
n->stemlength = 0.0;
n->syllables = NULL;
if (cv->ingrace) {
n->gchords = NULL;
n->instructions = NULL;
} else {
n->gchords = cv->gchords_pending;
cv->gchords_pending = NULL;
n->instructions = cv->instructions_pending;
cv->instructions_pending = NULL;
};
return(n);
}
static struct rest* newrest(int a, int b, int multi)
/* create and set up a new rest structure */
{
struct rest* n;
n = (struct rest*)checkmalloc(sizeof(struct rest));
setfract(&n->len, a, b);
n->dots = count_dots(&n->base, &n->base_exp, a, b);
if (n->dots == -1) {
event_error("Illegal rest length");
};
n->multibar = multi;
if (cv->ingrace) {
n->gchords = NULL;
n->instructions = NULL;
} else {
n->gchords = cv->gchords_pending;
cv->gchords_pending = NULL;
n->instructions = cv->instructions_pending;
cv->instructions_pending = NULL;
};
return(n);
}
static void addunits(f, n, m)
struct fract* f;
int n, m;
/* add n/m to fraction pointed to by f */
{
f->num = n*f->denom*(cv->unitlen.num) + f->num*(m*cv->unitlen.denom);
f->denom = (m*cv->unitlen.denom)*f->denom;
reducef(f);
}
static void addfractions(f,n,m)
struct fract* f;
int n,m;
{
f->num = n*f->denom + f->num*m;
f->denom = m*f->denom;
reducef(f);
}
static struct voice* newvoice(int n)
/* create and set up a new voice data structure */
{
struct voice* v;
v = (struct voice*)checkmalloc(sizeof(struct voice));
v->first = NULL;
v->last = NULL;
v->voiceno = n;
v->octaveshift = thetune.octaveshift;
setfract(&v->unitlen, thetune.unitlen.num, thetune.unitlen.denom);
v->changetime = 0;
v->inslur = 0;
v->ingrace = 0;
v->inchord = 0;
v->expect_repeat = 0;
v->tuplenotes = 0;
v->thistuple = NULL;
v->tuple_count = 0;
v->brokenpending = -1;
v->tiespending = 0;
v->slurpending = 0;
v->slurcount = 0;
v->barno = 0;
v->barchecking = thetune.barchecking;
setfract(&v->barlen, thetune.meter.num, thetune.meter.denom);
v->clef = newclef(thetune.clef.type, thetune.clef.octave);
if (thetune.keysig == NULL) {
printf("Trying to set up voice with no key signature\n");
exit(0);
} else {
v->keysig = newkey(thetune.keysig->name, thetune.keysig->sharps,
thetune.keysig->map, thetune.keysig->mult);
};
v->tempo = NULL;
setfract(&v->barcount, 0, 1);
setfract(&v->meter, thetune.meter.num, thetune.meter.denom);
v->lastnote = NULL;
v->laststart = NULL;
v->lastend = NULL;
v->line = header;
v->thisstart = NULL;
v->thisend = NULL;
v->gchords_pending = NULL;
v->instructions_pending = NULL;
v->beamed_tuple_pending = 0;
v->linestart = NULL;
v->lineend = NULL;
v->more_lyrics = 0;
v->lyric_errors = 0;
v->thischord = NULL;
v->chordplace = NULL;
v->beamroot = NULL;
v->beamend = NULL;
v->gracebeamroot = NULL;
v->gracebeamend = NULL;
return(v);
}
static void setvoice(int n)
/* set current voice to voice n. If voice n does not exist, create it */
{
struct voice* v;
struct el* l;
int done;
if (thetune.voices.first == NULL) {
cv = newvoice(n);
v = cv;
addtolist(&thetune.voices, (void*)v);
} else {
l = thetune.voices.first;
done = 0;
while ((done == 0) && (l != NULL)) {
if (((struct voice*)l->datum)->voiceno == n) {
done = 1;
cv = (struct voice*)l->datum;
} else {
l = l->next;
};
};
if (done == 0) {
cv = newvoice(n);
v = cv;
addtolist(&thetune.voices, (void*)v);
};
};
}
static void init_tune(struct tune* t, int x)
/* initialize tune structure */
{
t->no = x;
t->octaveshift = 0;
init_llist(&t->title);
t->composer = NULL;
t->origin = NULL;
t->parts = NULL;
init_llist(&t->notes);
init_llist(&t->voices);
setfract(&t->meter, 0, 1);
setfract(&t->unitlen, 0, 1);
t->cv = NULL;
t->keysig = NULL;
t->clef.type = treble;
t->clef.octave = 0;
t->tempo = NULL;
init_llist(&t->words);
};
static void freekey(struct key* k)
/* free up memory allocated for key data structure */
{
if (k != NULL) {
if (k->name != NULL) {
free(k->name);
};
free(k);
};
}
static void freetempo(struct atempo *t)
/* free up memory allocated for temp data structure */
{
if (t->pre != NULL) {
free(t->pre);
};
if (t->post != NULL) {
free(t->post);
};
}
static void freefeature(void* item, featuretype type)
/* free memory allocated for feature in voice */
{
struct note *n;
struct rest *r;
switch(type) {
case NOTE:
n = item;
if (n->accents != NULL) {
free(n->accents);
};
if (n->syllables != NULL) {
freellist(n->syllables);
free(n->syllables);
};
if (n->gchords != NULL) {
freellist(n->gchords);
free(n->gchords);
};
if (n->instructions != NULL) {
freellist(n->instructions);
free(n->instructions);
};
free(n);
break;
case REST:
r = item;
if (r->gchords != NULL) {
freellist(r->gchords);
free(r->gchords);
};
if (r->instructions != NULL) {
freellist(r->instructions);
free(r->instructions);
};
free(r);
break;
case KEY:
freekey(item);
break;
case TEMPO:
freetempo(item);
break;
case CLEF:
case TIME:
case PART:
case CHORDON:
case TUPLE:
case SLUR_ON:
case TIE:
case PLAY_ON_REP:
case LEFT_TEXT:
case PRINTLINE:
if (item != NULL) {
free(item);
};
break;
default:
break;
};
}
static void freevoice(struct voice* v)
/* free up memory allocated for voice data structure and voice data */
{
struct feature* ft;
struct feature* oldft;
ft = v->first;
while (ft != NULL) {
freefeature(ft->item, ft->type);
oldft = ft;
ft = ft->next;
free(oldft);
};
if (v->keysig != NULL) {
freekey(v->keysig);
v->keysig = NULL;
};
if (v->tempo != NULL) {
freetempo(v->tempo);
v->tempo = NULL;
};
if (v->clef != NULL) {
free(v->clef);
};
v->clef = NULL;
}