summaryrefslogtreecommitdiff
path: root/Build/source/texk/gregorio/gregorio-src/src/gabc/gabc-score-determination.c
blob: 38b88ad4799bdbea3deeb966df01af2190f828cc (plain)
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
/*
 * Gregorio is a program that translates gabc files to GregorioTeX
 * This file implements the score parser.
 *
 * Gregorio score determination from gabc utilities.
 * Copyright (C) 2016-2019 The Gregorio Project (see CONTRIBUTORS.md)
 *
 * This file is part of Gregorio.
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "bool.h"
#include "struct.h"
#include "struct_iter.h"
#include "gabc.h"
#include "gabc-score-determination.h"
#include "messages.h"
#include "support.h"

void gabc_suppress_extra_custos_at_linebreak(gregorio_score *score)
{
    gregorio_syllable *syllable;
    gregorio_element **custos = NULL;

    for (syllable = score->first_syllable; syllable;
            syllable = syllable->next_syllable) {
        gregorio_element **element;
        for (element = syllable->elements; element && *element;
                element = &((*element)->next)) {
            switch ((*element)->type) {
            case GRE_CUSTOS:
                if (!((*element)->u.misc.pitched.force_pitch)) {
                    /* save the encountered non-forced custos */
                    custos = element;
                } else {
                    /* forget the (previous) custos */
                    custos = NULL;
                }
                break;
            case GRE_CLEF:
            case GRE_BAR:
                /* remember the custos if only these appear before linebreak */
                break;
            case GRE_END_OF_LINE:
                if (custos) {
                    /* suppress the custos when linebreak follows */
                    gregorio_free_one_element(custos);
                }
                /* fall through */
            default:
                /* forget the custos */
                custos = NULL;
                break;
            }
        }
    }
}

void gabc_fix_custos_pitches(gregorio_score *score_to_check)
{
    gregorio_syllable *current_syllable;
    gregorio_element *current_element;
    gregorio_element *custos_element;
    int newkey;
    int current_key;

    if (!score_to_check || !score_to_check->first_syllable
            || !score_to_check->first_voice_info) {
        return;
    }

    current_key = gregorio_calculate_new_key(
            score_to_check->first_voice_info->initial_clef);
    for (current_syllable = score_to_check->first_syllable; current_syllable;
            current_syllable = current_syllable->next_syllable) {
        for (current_element = (current_syllable->elements)[0]; current_element;
                current_element = current_element->next) {
            if (current_element->type == GRE_CLEF) {
                newkey = gregorio_calculate_new_key(
                        current_element->u.misc.clef);
                current_element->u.misc.clef.pitch_difference =
                        (signed char) newkey - (signed char) current_key;
                current_key = newkey;
            }
        }
    }

    custos_element = NULL;
    for (current_syllable = score_to_check->first_syllable; current_syllable;
            current_syllable = current_syllable->next_syllable) {
        for (current_element = (current_syllable->elements)[0]; current_element;
                current_element = current_element->next) {
            switch (current_element->type) {
            case GRE_CUSTOS:
                if (current_element->u.misc.pitched.force_pitch) {
                    /* forget about the preceding custos if a forced one is
                     * encountered */
                    custos_element = NULL;
                } else {
                    /* the pitch is not forced, so it may need to be adjusted */
                    custos_element = current_element;
                    custos_element->u.misc.pitched.pitch =
                            gregorio_determine_next_pitch(current_syllable,
                                    current_element, NULL, NULL);
                }
                break;

            case GRE_ELEMENT:
                /* if it's an element, forget any preceding custos */
                custos_element = NULL;
                break;

            case GRE_CLEF:
                if (custos_element) {
                    /* adjust the preceding custos for the clef */
                    custos_element->u.misc.pitched.pitch =
                            gregorio_adjust_pitch_into_staff(score_to_check,
                            custos_element->u.misc.pitched.pitch
                            - current_element->u.misc.clef.pitch_difference);
                }
                break;

            default:
                /* to prevent the warning */
                break;
            }
        }
    }
}

/*
 * A function that checks the score integrity.
 */

bool gabc_check_score_integrity(gregorio_score *score_to_check)
{
    bool good = true;

    gregorio_assert(score_to_check, check_score_integrity, "score is NULL",
            return false);

    if (score_to_check->first_syllable
            && score_to_check->first_syllable->elements
            && *(score_to_check->first_syllable->elements)) {
        gregorio_character *ch;
        if ((score_to_check->first_syllable->elements)[0]->type
                == GRE_END_OF_LINE) {
            gregorio_message(
                    "line break is not supported on the first syllable",
                    "check_score_integrity", VERBOSITY_ERROR, 0);
            good = false;
        }
        if (gregorio_get_clef_change(score_to_check->first_syllable)) {
            gregorio_message(
                    "clef change is not supported on the first syllable",
                    "check_score_integrity", VERBOSITY_ERROR, 0);
            good = false;
        }
        /* check first syllable for elision at the beginning */
        for (ch = score_to_check->first_syllable->text; ch;
                ch = ch->next_character) {
            if (ch->is_character) {
                break;
            } else if (ch->cos.s.style == ST_VERBATIM
                    || ch->cos.s.style == ST_SPECIAL_CHAR) {
                break;
            } else if (ch->cos.s.style == ST_ELISION) {
                gregorio_message(
                        _("score initial may not be in an elision"),
                        "check_score_integrity", VERBOSITY_ERROR, 0);
                break;
            }
        }
    }

    return good;
}

/*
 * Another function to be improved: this one checks the validity of the voice_infos.
 */

bool gabc_check_infos_integrity(gregorio_score *score_to_check)
{
    if (!score_to_check->name) {
        gregorio_message(_("no name specified, put `name:...;' at the "
                "beginning of the file, can be dangerous with some output "
                "formats"), "det_score", VERBOSITY_WARNING, 0);
    }
    return true;
}

static void set_oriscus_descending(const gregorio_note_iter_position *const p,
        void *const ignored __attribute__((unused)))
{
    switch(p->note->u.note.shape) {
    case S_ORISCUS_UNDETERMINED:
        p->note->u.note.shape = S_ORISCUS_DESCENDENS;
        break;
    case S_ORISCUS_SCAPUS_UNDETERMINED:
        p->note->u.note.shape = S_ORISCUS_SCAPUS_DESCENDENS;
        gregorio_assert_only(p->glyph->u.notes.glyph_type
                != G_PES_DESCENDENS_ORISCUS, set_oriscus_descending,
                "glyph type should not be G_PES_DESCENDENS_ORISCUS");
        if (p->glyph->u.notes.glyph_type == G_PES_ASCENDENS_ORISCUS) {
            p->glyph->u.notes.glyph_type = G_PES_DESCENDENS_ORISCUS;
        }
        break;
    default:
        break;
    }
}

static void set_oriscus_ascending(const gregorio_note_iter_position *const p,
        void *const ignored __attribute__((unused)))
{
    switch(p->note->u.note.shape) {
    case S_ORISCUS_UNDETERMINED:
        p->note->u.note.shape = S_ORISCUS_ASCENDENS;
        break;
    case S_ORISCUS_SCAPUS_UNDETERMINED:
        p->note->u.note.shape = S_ORISCUS_SCAPUS_ASCENDENS;
        gregorio_assert_only(p->glyph->u.notes.glyph_type
                != G_PES_DESCENDENS_ORISCUS, set_oriscus_ascending,
                "glyph type should not be G_PES_DESCENDENS_ORISCUS");
        break;
    default:
        break;
    }
}

/* data must be (gregorio_note_iter_position *) */
static void oriscus_orientation_visit(
        const gregorio_note_iter_position *const p, void *const data)
{
    gregorio_note *const note = p->note;
    gregorio_note_iter_position *const oriscus =
        (gregorio_note_iter_position *const)data;

    if (oriscus->note && note->u.note.pitch != oriscus->note->u.note.pitch) {
        if (note->u.note.pitch <= oriscus->note->u.note.pitch) {
            /* descending (or undetermined oriscus in unison) */
            gregorio_from_note_to_note(oriscus, p, false,
                    set_oriscus_descending, NULL, GRESTRUCT_NONE, NULL);
        } else {
            /* ascending */
            gregorio_from_note_to_note(oriscus, p, false,
                    set_oriscus_ascending, NULL, GRESTRUCT_NONE, NULL);
        }
        oriscus->syllable = NULL,
        oriscus->element = NULL,
        oriscus->note = NULL;
        oriscus->glyph = NULL;
    }

    if (!oriscus->note) {
        switch (note->u.note.shape) {
        case S_ORISCUS_UNDETERMINED:
        case S_ORISCUS_SCAPUS_UNDETERMINED:
            *oriscus = *p;
            break;

        default:
            break;
        }
    }
}

void gabc_determine_oriscus_orientation(const gregorio_score *const score)
{
    gregorio_note_iter_position oriscus = {
        /* .syllable = */ NULL,
        /* .element = */ NULL,
        /* .glyph = */ NULL,
        /* .note = */ NULL
    };

    gregorio_for_each_note(score, oriscus_orientation_visit, NULL,
            GRESTRUCT_NONE, &oriscus);

    if (oriscus.note) {
        gregorio_from_note_to_note(&oriscus, NULL, true,
                set_oriscus_descending, NULL, GRESTRUCT_NONE, NULL);
    }
}

typedef struct {
    gregorio_note_iter_position first, previous;
    gregorio_shape orientation;
    int running;
    int count;
    bool unison;
} punctum_inclinatum_vars;

/* data must be (gregorio_shape *) */
static void set_shape(const gregorio_note_iter_position *const p,
        void *const data)
{
    p->note->u.note.shape = *((gregorio_shape *)data);
}

static __inline void set_punctum_inclinatum_orientation(
        punctum_inclinatum_vars *const v)
{
    if (v->orientation == S_UNDETERMINED) {
        if (v->unison && (v->count > 1 || (v->count == 1 && v->running == 0))) {
            v->orientation = S_PUNCTUM_INCLINATUM_STANS;
        } else if (v->running > 0) {
            v->orientation = S_PUNCTUM_INCLINATUM_ASCENDENS;
        } else {
            v->orientation = S_PUNCTUM_INCLINATUM_DESCENDENS;
        }
    }
}

static __inline void finalize_punctum_inclinatum_orientation(
        punctum_inclinatum_vars *const v)
{
    if (v->first.note) {
        set_punctum_inclinatum_orientation(v);
        gregorio_assert_only(v->orientation != S_UNDETERMINED,
                punctum_inclinatum_orientation_end_item,
                "orientation should not be S_UNDETERMINED");
        gregorio_from_note_to_note(&v->first, &v->previous, true, set_shape,
                NULL, GRESTRUCT_NONE, &v->orientation);
    }

    v->first.syllable = NULL;
    v->first.element = NULL;
    v->first.glyph = NULL;
    v->first.note = NULL;
    v->unison = true;
}

/* data must be (punctum_inclinatum_vars *) */
static void punctum_inclinatum_orientation_visit(
        const gregorio_note_iter_position *const p, void *const data)
{
    const gregorio_shape shape = p->note->u.note.shape;
    punctum_inclinatum_vars *const v = (punctum_inclinatum_vars *)data;
    if (shape == S_PUNCTUM_INCLINATUM_UNDETERMINED) {
        if (v->orientation) {
            p->note->u.note.shape = v->orientation;
        } else {
            /* any cases not covered here will not change running */
            if (v->previous.note
                    && (v->first.note || v->previous.syllable == p->syllable)) {
                if (v->previous.note->u.note.pitch
                        < p->note->u.note.pitch) {
                    ++ v->running;
                    if (v->count > 0) {
                        v->unison = false;
                    }
                } else if (v->previous.note->u.note.pitch
                        > p->note->u.note.pitch) {
                    -- v->running;
                    if (v->count > 0) {
                        v->unison = false;
                    }
                }
            }
            if (!v->first.note) {
                v->first = *p;
            }
        }
        ++ v->count;
    } else { /* non-inclinatum or determined inclinatum */
        bool is_punctum_inclinatum;

        /* shape can't be S_PUNCTUM_INCLINATUM_UNDETERMINED here */
        switch (shape) {
        case S_PUNCTUM_INCLINATUM_ASCENDENS:
            v->orientation = S_PUNCTUM_INCLINATUM_ASCENDENS;
            is_punctum_inclinatum = true;
            break;
        case S_PUNCTUM_INCLINATUM_STANS:
            v->orientation = S_PUNCTUM_INCLINATUM_STANS;
            is_punctum_inclinatum = true;
            break;
        case S_PUNCTUM_INCLINATUM_DEMINUTUS:
        case S_PUNCTUM_INCLINATUM_AUCTUS:
            v->unison = false;
            /* fall through */
        case S_PUNCTUM_INCLINATUM_DESCENDENS:
            v->orientation = S_PUNCTUM_INCLINATUM_DESCENDENS;
            is_punctum_inclinatum = true;
            break;
        default:
            is_punctum_inclinatum = false;
            break;
        }

        if (v->first.note) {
            if (!is_punctum_inclinatum) {
                /* if v->first.note is not null,
                 * then v->previous.note is not null */
                if (v->previous.note->u.note.pitch
                        < p->note->u.note.pitch
                        && v->previous.syllable == p->syllable) {
                    ++ v->running;
                } else if (v->previous.note->u.note.pitch
                        > p->note->u.note.pitch) {
                    -- v->running;
                }
            }
            finalize_punctum_inclinatum_orientation(v);
        }

        if (is_punctum_inclinatum) {
            ++ v->count;
            /* and leave orientation alone */
        } else {
            v->running = 0;
            v->count = 0;
            v->orientation = S_UNDETERMINED;
        }
    }

    v->previous = *p;
}

/* data must be (punctum_inclinatum_vars *) */
static void punctum_inclinatum_orientation_end_item(
        const gregorio_note_iter_position *const p __attribute__((__unused__)),
        const gregorio_note_iter_item_type item_type,
        void *const data)
{
    punctum_inclinatum_vars *const v = (punctum_inclinatum_vars *)data;

    gregorio_assert_only(item_type == GRESTRUCT_SYLLABLE,
            punctum_inclinatum_orientation_end_item,
            "item type should be GRESTRUCT_SYLLABLE");

    finalize_punctum_inclinatum_orientation(v);
    v->count = 0;
}

void gabc_determine_punctum_inclinatum_orientation(
        const gregorio_score *const score)
{
    punctum_inclinatum_vars v = {
        /* .first = */ {
            /* .syllable = */ NULL,
            /* .element = */ NULL,
            /* .glyph = */ NULL,
            /* .note = */ NULL
        },
        /* .previous = */ {
            /* .syllable = */ NULL,
            /* .element = */ NULL,
            /* .glyph = */ NULL,
            /* .note = */ NULL
        },
        /* .orientation = */ S_UNDETERMINED, /* because it's 0 */
        /* .running = */ 0,
        /* .count = */ 0,
        /* .unison = */ true,
    };

    gregorio_for_each_note(score, punctum_inclinatum_orientation_visit,
            punctum_inclinatum_orientation_end_item, GRESTRUCT_SYLLABLE, &v);

    if (v.first.note) {
        set_punctum_inclinatum_orientation(&v);
        gregorio_from_note_to_note(&v.first, &v.previous, true, set_shape, NULL,
                GRESTRUCT_NONE, &v.orientation);
    }
}

typedef struct note_stack {
    gregorio_note *note;
    struct note_stack *prev;
} note_stack;

static void note_stack_push(note_stack **const stack, gregorio_note *note) {
    note_stack *item = gregorio_malloc(sizeof(note_stack));
    item->note = note;
    item->prev = *stack;
    *stack = item;
}

static gregorio_note *note_stack_pop(note_stack **const stack) {
    note_stack *item = *stack;
    if (item) {
        gregorio_note *note = item->note;
        note_stack *prev = item->prev;
        free(item);
        *stack = prev;
        return note;
    }
    return NULL;
}

static void note_stack_clear(note_stack **const stack) {
    note_stack *item = *stack;
    while (item) {
        note_stack *prev = item->prev;
        free(item);
        item = prev;
    }
    *stack = NULL;
}

typedef struct {
    note_stack *high, *low;
    gregorio_note *prev_note;
    signed char high_ledger_line_pitch;
    bool running_high, running_low;
} ledger_line_vars;

static __inline void clear_ledger_line_vars(ledger_line_vars *const v) {
    note_stack_clear(&v->high);
    note_stack_clear(&v->low);
    v->prev_note = NULL;
    v->running_high = false;
    v->running_low = false;
}

static __inline void adjust_ledger(const gregorio_note_iter_position *const p,
        const gregorio_ledger_specificity specificity, bool ledger_line,
        note_stack **const stack, bool *const running, gregorio_note *prev_note,
        const signed char high_ledger_line_pitch,
        bool (*extend_ledger)(gregorio_note *, const gregorio_note *,
            const gregorio_note *, signed char))
{
    if (specificity & LEDGER_DRAWN) {
        if (ledger_line) {
            gregorio_note *after = p->note;
            gregorio_note *note;
            /* process from this ledger backwards */
            while ((note = note_stack_pop(stack))) {
                if (!extend_ledger(note, NULL, after, high_ledger_line_pitch)) {
                    /* ledger has ended */
                    break;
                }
                after = note;
            }
            *running = true;
        } else {
            *running = false;
        }
        note_stack_clear(stack);
    } else {
        if (*running) {
            if (!extend_ledger(p->note, prev_note, NULL,
                    high_ledger_line_pitch)) {
                /* ledger has ended */
                note_stack_push(stack, p->note);
                *running = false;
            }
            /* else stack should be empty, keep it that way */
        } else {
            note_stack_push(stack, p->note);
        }
    }
}

static bool extend_high_ledger(gregorio_note *const note,
        const gregorio_note *const note_before,
        const gregorio_note *const note_after,
        const signed char high_ledger_line_pitch)
{
    bool extend = false;

    if (note_before) {
        extend = note_before->u.note.pitch < note->u.note.pitch
            || (note_before->u.note.pitch > high_ledger_line_pitch
                && note->u.note.pitch < high_ledger_line_pitch);
    } else if (note_after) {
        extend = note_after->u.note.pitch < note->u.note.pitch
            || (note_after->u.note.pitch > high_ledger_line_pitch
                && note->u.note.pitch < high_ledger_line_pitch);
    }

    if (extend) {
        note->high_ledger_line = true;
        note->high_ledger_specificity = LEDGER_DRAWN;
    }
    return extend;
}

static bool extend_low_ledger(gregorio_note *const note,
        const gregorio_note *const note_before,
        const gregorio_note *const note_after,
        const signed char high_ledger_line_pitch __attribute__((__unused__)))
{
    bool extend = false;

    if (note_before) {
        extend = note_before->u.note.pitch < note->u.note.pitch
            || (note_before->u.note.pitch > LOW_LEDGER_LINE_PITCH
                && note->u.note.pitch < LOW_LEDGER_LINE_PITCH);
    } else if (note_after) {
        extend = note_after->u.note.pitch < note->u.note.pitch
            || (note_after->u.note.pitch > LOW_LEDGER_LINE_PITCH
                && note->u.note.pitch < LOW_LEDGER_LINE_PITCH);
    }

    if (extend) {
        note->low_ledger_line = true;
        note->low_ledger_specificity = LEDGER_DRAWN;
    }
    return extend;
}

/* data must be (ledger_line_vars *) */
static void ledger_line_visit(const gregorio_note_iter_position *const p,
        void *const data)
{
    ledger_line_vars *const v = (ledger_line_vars *)data;

    adjust_ledger(p, p->note->high_ledger_specificity, p->note->high_ledger_line,
            &v->high, &v->running_high, v->prev_note, v->high_ledger_line_pitch,
            &extend_high_ledger);
    adjust_ledger(p, p->note->low_ledger_specificity, p->note->low_ledger_line,
            &v->low, &v->running_low, v->prev_note, v->high_ledger_line_pitch,
            &extend_low_ledger);

    v->prev_note = p->note;
}

/* data must be (ledger_line_vars *) */
static void ledger_line_end_item(
        const gregorio_note_iter_position *const p __attribute__((__unused__)),
        const gregorio_note_iter_item_type item_type, void *const data)
{
    if (item_type == GRESTRUCT_ELEMENT) {
        clear_ledger_line_vars((ledger_line_vars *)data);
    }
}

void gabc_determine_ledger_lines(const gregorio_score *const score)
{
    ledger_line_vars v;
    memset(&v, 0, sizeof v);
    v.high_ledger_line_pitch = score->high_ledger_line_pitch;

    gregorio_for_each_note(score, ledger_line_visit, ledger_line_end_item,
            GRESTRUCT_ELEMENT, &v);

    /* stacks should be cleared by ledger_line_end_item */
}