| 1 |
/* |
| 2 |
* Gregorio is a program that translates gabc files to GregorioTeX |
| 3 |
* This file implements the score parser. |
| 4 |
* |
| 5 |
* Gregorio score determination from gabc utilities. |
| 6 |
* Copyright (C) 2016-2025 The Gregorio Project (see CONTRIBUTORS.md) |
| 7 |
* |
| 8 |
* This file is part of Gregorio. |
| 9 |
* |
| 10 |
* This program is free software: you can redistribute it and/or modify it |
| 11 |
* under the terms of the GNU General Public License as published by the Free |
| 12 |
* Software Foundation, either version 3 of the License, or (at your option) |
| 13 |
* any later version. |
| 14 |
* |
| 15 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 18 |
* more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License along with |
| 21 |
* this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <assert.h> |
| 29 |
#include "bool.h" |
| 30 |
#include "struct.h" |
| 31 |
#include "struct_iter.h" |
| 32 |
#include "gabc.h" |
| 33 |
#include "gabc-score-determination.h" |
| 34 |
#include "messages.h" |
| 35 |
#include "support.h" |
| 36 |
|
| 37 |
void gabc_suppress_extra_custos_at_linebreak(gregorio_score *score) |
| 38 |
{ |
| 39 |
gregorio_syllable *syllable; |
| 40 |
gregorio_element **custos = NULL; |
| 41 |
|
| 42 |
for (syllable = score->first_syllable; syllable; |
| 43 |
syllable = syllable->next_syllable) { |
| 44 |
gregorio_element **element; |
| 45 |
for (element = syllable->elements; element && *element; |
| 46 |
element = &((*element)->next)) { |
| 47 |
switch ((*element)->type) { |
| 48 |
case GRE_CUSTOS: |
| 49 |
if (!((*element)->u.misc.pitched.force_pitch)) { |
| 50 |
/* save the encountered non-forced custos */ |
| 51 |
custos = element; |
| 52 |
} else { |
| 53 |
/* forget the (previous) custos */ |
| 54 |
custos = NULL; |
| 55 |
} |
| 56 |
break; |
| 57 |
case GRE_CLEF: |
| 58 |
case GRE_BAR: |
| 59 |
/* remember the custos if only these appear before linebreak */ |
| 60 |
break; |
| 61 |
case GRE_END_OF_LINE: |
| 62 |
if (custos) { |
| 63 |
/* suppress the custos when linebreak follows */ |
| 64 |
gregorio_free_one_element(custos); |
| 65 |
} |
| 66 |
/* fall through */ |
| 67 |
default: |
| 68 |
/* forget the custos */ |
| 69 |
custos = NULL; |
| 70 |
break; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
void gabc_fix_custos_pitches(gregorio_score *score_to_check) |
| 77 |
{ |
| 78 |
gregorio_syllable *current_syllable; |
| 79 |
gregorio_element *current_element; |
| 80 |
gregorio_element *custos_element; |
| 81 |
int newkey; |
| 82 |
int current_key; |
| 83 |
|
| 84 |
if (!score_to_check || !score_to_check->first_syllable |
| 85 |
|| !score_to_check->first_voice_info) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
current_key = gregorio_calculate_new_key( |
| 90 |
score_to_check->first_voice_info->initial_clef); |
| 91 |
for (current_syllable = score_to_check->first_syllable; current_syllable; |
| 92 |
current_syllable = current_syllable->next_syllable) { |
| 93 |
for (current_element = (current_syllable->elements)[0]; current_element; |
| 94 |
current_element = current_element->next) { |
| 95 |
if (current_element->type == GRE_CLEF) { |
| 96 |
newkey = gregorio_calculate_new_key( |
| 97 |
current_element->u.misc.clef); |
| 98 |
current_element->u.misc.clef.pitch_difference = |
| 99 |
(signed char) newkey - (signed char) current_key; |
| 100 |
current_key = newkey; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
custos_element = NULL; |
| 106 |
for (current_syllable = score_to_check->first_syllable; current_syllable; |
| 107 |
current_syllable = current_syllable->next_syllable) { |
| 108 |
for (current_element = (current_syllable->elements)[0]; current_element; |
| 109 |
current_element = current_element->next) { |
| 110 |
switch (current_element->type) { |
| 111 |
case GRE_CUSTOS: |
| 112 |
if (current_element->u.misc.pitched.force_pitch) { |
| 113 |
/* forget about the preceding custos if a forced one is |
| 114 |
* encountered */ |
| 115 |
custos_element = NULL; |
| 116 |
} else { |
| 117 |
/* the pitch is not forced, so it may need to be adjusted */ |
| 118 |
custos_element = current_element; |
| 119 |
custos_element->u.misc.pitched.pitch = |
| 120 |
gregorio_determine_next_pitch(current_syllable, |
| 121 |
current_element, NULL, NULL); |
| 122 |
} |
| 123 |
break; |
| 124 |
|
| 125 |
case GRE_ELEMENT: |
| 126 |
/* if it's an element, forget any preceding custos */ |
| 127 |
custos_element = NULL; |
| 128 |
break; |
| 129 |
|
| 130 |
case GRE_CLEF: |
| 131 |
if (custos_element) { |
| 132 |
/* adjust the preceding custos for the clef */ |
| 133 |
custos_element->u.misc.pitched.pitch = |
| 134 |
gregorio_adjust_pitch_into_staff(score_to_check, |
| 135 |
custos_element->u.misc.pitched.pitch |
| 136 |
- current_element->u.misc.clef.pitch_difference); |
| 137 |
} |
| 138 |
break; |
| 139 |
|
| 140 |
default: |
| 141 |
/* to prevent the warning */ |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
* A function that checks the score integrity. |
| 150 |
*/ |
| 151 |
|
| 152 |
bool gabc_check_score_integrity(gregorio_score *score_to_check) |
| 153 |
{ |
| 154 |
bool good = true; |
| 155 |
|
| 156 |
gregorio_assert(score_to_check, check_score_integrity, "score is NULL", |
| 157 |
return false); |
| 158 |
|
| 159 |
if (score_to_check->first_syllable |
| 160 |
&& score_to_check->first_syllable->elements |
| 161 |
&& *(score_to_check->first_syllable->elements)) { |
| 162 |
gregorio_character *ch; |
| 163 |
if ((score_to_check->first_syllable->elements)[0]->type |
| 164 |
== GRE_END_OF_LINE) { |
| 165 |
gregorio_message( |
| 166 |
"line break is not supported on the first syllable", |
| 167 |
"check_score_integrity", VERBOSITY_ERROR, 0); |
| 168 |
good = false; |
| 169 |
} |
| 170 |
if (gregorio_get_clef_change(score_to_check->first_syllable)) { |
| 171 |
gregorio_message( |
| 172 |
"clef change is not supported on the first syllable", |
| 173 |
"check_score_integrity", VERBOSITY_ERROR, 0); |
| 174 |
good = false; |
| 175 |
} |
| 176 |
/* check first syllable for elision at the beginning */ |
| 177 |
for (ch = score_to_check->first_syllable->text; ch; |
| 178 |
ch = ch->next_character) { |
| 179 |
if (ch->is_character) { |
| 180 |
break; |
| 181 |
} else if (ch->cos.s.style == ST_VERBATIM |
| 182 |
|| ch->cos.s.style == ST_SPECIAL_CHAR) { |
| 183 |
break; |
| 184 |
} else if (ch->cos.s.style == ST_ELISION) { |
| 185 |
gregorio_message( |
| 186 |
_("score initial may not be in an elision"), |
| 187 |
"check_score_integrity", VERBOSITY_ERROR, 0); |
| 188 |
break; |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return good; |
| 194 |
} |
| 195 |
|
| 196 |
/* |
| 197 |
* Another function to be improved: this one checks the validity of the voice_infos. |
| 198 |
*/ |
| 199 |
|
| 200 |
bool gabc_check_infos_integrity(gregorio_score *score_to_check) |
| 201 |
{ |
| 202 |
if (!score_to_check->name) { |
| 203 |
gregorio_message(_("no name specified, put `name:...;' at the " |
| 204 |
"beginning of the file, can be dangerous with some output " |
| 205 |
"formats"), "det_score", VERBOSITY_WARNING, 0); |
| 206 |
} |
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
static void set_oriscus_descending(const gregorio_note_iter_position *const p, |
| 211 |
void *const ignored __attribute__((unused))) |
| 212 |
{ |
| 213 |
switch(p->note->u.note.shape) { |
| 214 |
case S_ORISCUS_UNDETERMINED: |
| 215 |
p->note->u.note.shape = S_ORISCUS_DESCENDENS; |
| 216 |
break; |
| 217 |
case S_ORISCUS_SCAPUS_UNDETERMINED: |
| 218 |
p->note->u.note.shape = S_ORISCUS_SCAPUS_DESCENDENS; |
| 219 |
gregorio_assert_only(p->glyph->u.notes.glyph_type |
| 220 |
!= G_PES_DESCENDENS_ORISCUS, set_oriscus_descending, |
| 221 |
"glyph type should not be G_PES_DESCENDENS_ORISCUS"); |
| 222 |
if (p->glyph->u.notes.glyph_type == G_PES_ASCENDENS_ORISCUS) { |
| 223 |
p->glyph->u.notes.glyph_type = G_PES_DESCENDENS_ORISCUS; |
| 224 |
} |
| 225 |
break; |
| 226 |
default: |
| 227 |
break; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
static void set_oriscus_ascending(const gregorio_note_iter_position *const p, |
| 232 |
void *const ignored __attribute__((unused))) |
| 233 |
{ |
| 234 |
switch(p->note->u.note.shape) { |
| 235 |
case S_ORISCUS_UNDETERMINED: |
| 236 |
p->note->u.note.shape = S_ORISCUS_ASCENDENS; |
| 237 |
break; |
| 238 |
case S_ORISCUS_SCAPUS_UNDETERMINED: |
| 239 |
p->note->u.note.shape = S_ORISCUS_SCAPUS_ASCENDENS; |
| 240 |
gregorio_assert_only(p->glyph->u.notes.glyph_type |
| 241 |
!= G_PES_DESCENDENS_ORISCUS, set_oriscus_ascending, |
| 242 |
"glyph type should not be G_PES_DESCENDENS_ORISCUS"); |
| 243 |
break; |
| 244 |
default: |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/* data must be (gregorio_note_iter_position *) */ |
| 250 |
static void oriscus_orientation_visit( |
| 251 |
const gregorio_note_iter_position *const p, void *const data) |
| 252 |
{ |
| 253 |
gregorio_note *const note = p->note; |
| 254 |
gregorio_note_iter_position *const oriscus = |
| 255 |
(gregorio_note_iter_position *const)data; |
| 256 |
|
| 257 |
if (oriscus->note && note->u.note.pitch != oriscus->note->u.note.pitch) { |
| 258 |
if (note->u.note.pitch <= oriscus->note->u.note.pitch) { |
| 259 |
/* descending (or undetermined oriscus in unison) */ |
| 260 |
gregorio_from_note_to_note(oriscus, p, false, |
| 261 |
set_oriscus_descending, NULL, GRESTRUCT_NONE, NULL); |
| 262 |
} else { |
| 263 |
/* ascending */ |
| 264 |
gregorio_from_note_to_note(oriscus, p, false, |
| 265 |
set_oriscus_ascending, NULL, GRESTRUCT_NONE, NULL); |
| 266 |
} |
| 267 |
oriscus->syllable = NULL, |
| 268 |
oriscus->element = NULL, |
| 269 |
oriscus->note = NULL; |
| 270 |
oriscus->glyph = NULL; |
| 271 |
} |
| 272 |
|
| 273 |
if (!oriscus->note) { |
| 274 |
switch (note->u.note.shape) { |
| 275 |
case S_ORISCUS_UNDETERMINED: |
| 276 |
case S_ORISCUS_SCAPUS_UNDETERMINED: |
| 277 |
*oriscus = *p; |
| 278 |
break; |
| 279 |
|
| 280 |
default: |
| 281 |
break; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
void gabc_determine_oriscus_orientation(const gregorio_score *const score) |
| 287 |
{ |
| 288 |
gregorio_note_iter_position oriscus = { |
| 289 |
/* .syllable = */ NULL, |
| 290 |
/* .element = */ NULL, |
| 291 |
/* .glyph = */ NULL, |
| 292 |
/* .note = */ NULL |
| 293 |
}; |
| 294 |
|
| 295 |
gregorio_for_each_note(score, oriscus_orientation_visit, NULL, |
| 296 |
GRESTRUCT_NONE, &oriscus); |
| 297 |
|
| 298 |
if (oriscus.note) { |
| 299 |
gregorio_from_note_to_note(&oriscus, NULL, true, |
| 300 |
set_oriscus_descending, NULL, GRESTRUCT_NONE, NULL); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
typedef struct { |
| 305 |
gregorio_note_iter_position first, previous; |
| 306 |
gregorio_shape orientation; |
| 307 |
int running; |
| 308 |
int count; |
| 309 |
bool unison; |
| 310 |
} punctum_inclinatum_vars; |
| 311 |
|
| 312 |
/* data must be (gregorio_shape *) */ |
| 313 |
static void set_shape(const gregorio_note_iter_position *const p, |
| 314 |
void *const data) |
| 315 |
{ |
| 316 |
p->note->u.note.shape = *((gregorio_shape *)data); |
| 317 |
} |
| 318 |
|
| 319 |
static __inline void set_punctum_inclinatum_orientation( |
| 320 |
punctum_inclinatum_vars *const v) |
| 321 |
{ |
| 322 |
if (v->orientation == S_UNDETERMINED) { |
| 323 |
if (v->unison && (v->count > 1 || (v->count == 1 && v->running == 0))) { |
| 324 |
v->orientation = S_PUNCTUM_INCLINATUM_STANS; |
| 325 |
} else if (v->running > 0) { |
| 326 |
v->orientation = S_PUNCTUM_INCLINATUM_ASCENDENS; |
| 327 |
} else { |
| 328 |
v->orientation = S_PUNCTUM_INCLINATUM_DESCENDENS; |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
static __inline void finalize_punctum_inclinatum_orientation( |
| 334 |
punctum_inclinatum_vars *const v) |
| 335 |
{ |
| 336 |
if (v->first.note) { |
| 337 |
set_punctum_inclinatum_orientation(v); |
| 338 |
gregorio_assert_only(v->orientation != S_UNDETERMINED, |
| 339 |
punctum_inclinatum_orientation_end_item, |
| 340 |
"orientation should not be S_UNDETERMINED"); |
| 341 |
gregorio_from_note_to_note(&v->first, &v->previous, true, set_shape, |
| 342 |
NULL, GRESTRUCT_NONE, &v->orientation); |
| 343 |
} |
| 344 |
|
| 345 |
v->first.syllable = NULL; |
| 346 |
v->first.element = NULL; |
| 347 |
v->first.glyph = NULL; |
| 348 |
v->first.note = NULL; |
| 349 |
v->unison = true; |
| 350 |
} |
| 351 |
|
| 352 |
/* data must be (punctum_inclinatum_vars *) */ |
| 353 |
static void punctum_inclinatum_orientation_visit( |
| 354 |
const gregorio_note_iter_position *const p, void *const data) |
| 355 |
{ |
| 356 |
const gregorio_shape shape = p->note->u.note.shape; |
| 357 |
punctum_inclinatum_vars *const v = (punctum_inclinatum_vars *)data; |
| 358 |
if (shape == S_PUNCTUM_INCLINATUM_UNDETERMINED) { |
| 359 |
if (v->orientation) { |
| 360 |
p->note->u.note.shape = v->orientation; |
| 361 |
} else { |
| 362 |
/* any cases not covered here will not change running */ |
| 363 |
if (v->previous.note |
| 364 |
&& (v->first.note || v->previous.syllable == p->syllable)) { |
| 365 |
if (v->previous.note->u.note.pitch |
| 366 |
< p->note->u.note.pitch) { |
| 367 |
++ v->running; |
| 368 |
if (v->count > 0) { |
| 369 |
v->unison = false; |
| 370 |
} |
| 371 |
} else if (v->previous.note->u.note.pitch |
| 372 |
> p->note->u.note.pitch) { |
| 373 |
-- v->running; |
| 374 |
if (v->count > 0) { |
| 375 |
v->unison = false; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
if (!v->first.note) { |
| 380 |
v->first = *p; |
| 381 |
} |
| 382 |
} |
| 383 |
++ v->count; |
| 384 |
} else { /* non-inclinatum or determined inclinatum */ |
| 385 |
bool is_punctum_inclinatum; |
| 386 |
|
| 387 |
/* shape can't be S_PUNCTUM_INCLINATUM_UNDETERMINED here */ |
| 388 |
switch (shape) { |
| 389 |
case S_PUNCTUM_INCLINATUM_ASCENDENS: |
| 390 |
v->orientation = S_PUNCTUM_INCLINATUM_ASCENDENS; |
| 391 |
is_punctum_inclinatum = true; |
| 392 |
break; |
| 393 |
case S_PUNCTUM_INCLINATUM_STANS: |
| 394 |
v->orientation = S_PUNCTUM_INCLINATUM_STANS; |
| 395 |
is_punctum_inclinatum = true; |
| 396 |
break; |
| 397 |
case S_PUNCTUM_INCLINATUM_DEMINUTUS: |
| 398 |
case S_PUNCTUM_INCLINATUM_AUCTUS: |
| 399 |
v->unison = false; |
| 400 |
/* fall through */ |
| 401 |
case S_PUNCTUM_INCLINATUM_DESCENDENS: |
| 402 |
v->orientation = S_PUNCTUM_INCLINATUM_DESCENDENS; |
| 403 |
is_punctum_inclinatum = true; |
| 404 |
break; |
| 405 |
default: |
| 406 |
is_punctum_inclinatum = false; |
| 407 |
break; |
| 408 |
} |
| 409 |
|
| 410 |
if (v->first.note) { |
| 411 |
if (!is_punctum_inclinatum) { |
| 412 |
/* if v->first.note is not null, |
| 413 |
* then v->previous.note is not null */ |
| 414 |
if (v->previous.note->u.note.pitch |
| 415 |
< p->note->u.note.pitch |
| 416 |
&& v->previous.syllable == p->syllable) { |
| 417 |
++ v->running; |
| 418 |
} else if (v->previous.note->u.note.pitch |
| 419 |
> p->note->u.note.pitch) { |
| 420 |
-- v->running; |
| 421 |
} |
| 422 |
} |
| 423 |
finalize_punctum_inclinatum_orientation(v); |
| 424 |
} |
| 425 |
|
| 426 |
if (is_punctum_inclinatum) { |
| 427 |
++ v->count; |
| 428 |
/* and leave orientation alone */ |
| 429 |
} else { |
| 430 |
v->running = 0; |
| 431 |
v->count = 0; |
| 432 |
v->orientation = S_UNDETERMINED; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
v->previous = *p; |
| 437 |
} |
| 438 |
|
| 439 |
/* data must be (punctum_inclinatum_vars *) */ |
| 440 |
static void punctum_inclinatum_orientation_end_item( |
| 441 |
const gregorio_note_iter_position *const p __attribute__((__unused__)), |
| 442 |
const gregorio_note_iter_item_type item_type, |
| 443 |
void *const data) |
| 444 |
{ |
| 445 |
punctum_inclinatum_vars *const v = (punctum_inclinatum_vars *)data; |
| 446 |
|
| 447 |
gregorio_assert_only(item_type == GRESTRUCT_SYLLABLE, |
| 448 |
punctum_inclinatum_orientation_end_item, |
| 449 |
"item type should be GRESTRUCT_SYLLABLE"); |
| 450 |
|
| 451 |
finalize_punctum_inclinatum_orientation(v); |
| 452 |
v->count = 0; |
| 453 |
} |
| 454 |
|
| 455 |
void gabc_determine_punctum_inclinatum_orientation( |
| 456 |
const gregorio_score *const score) |
| 457 |
{ |
| 458 |
punctum_inclinatum_vars v = { |
| 459 |
/* .first = */ { |
| 460 |
/* .syllable = */ NULL, |
| 461 |
/* .element = */ NULL, |
| 462 |
/* .glyph = */ NULL, |
| 463 |
/* .note = */ NULL |
| 464 |
}, |
| 465 |
/* .previous = */ { |
| 466 |
/* .syllable = */ NULL, |
| 467 |
/* .element = */ NULL, |
| 468 |
/* .glyph = */ NULL, |
| 469 |
/* .note = */ NULL |
| 470 |
}, |
| 471 |
/* .orientation = */ S_UNDETERMINED, /* because it's 0 */ |
| 472 |
/* .running = */ 0, |
| 473 |
/* .count = */ 0, |
| 474 |
/* .unison = */ true, |
| 475 |
}; |
| 476 |
|
| 477 |
gregorio_for_each_note(score, punctum_inclinatum_orientation_visit, |
| 478 |
punctum_inclinatum_orientation_end_item, GRESTRUCT_SYLLABLE, &v); |
| 479 |
|
| 480 |
if (v.first.note) { |
| 481 |
set_punctum_inclinatum_orientation(&v); |
| 482 |
gregorio_from_note_to_note(&v.first, &v.previous, true, set_shape, NULL, |
| 483 |
GRESTRUCT_NONE, &v.orientation); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
typedef struct note_stack { |
| 488 |
gregorio_note *note; |
| 489 |
struct note_stack *prev; |
| 490 |
} note_stack; |
| 491 |
|
| 492 |
static void note_stack_push(note_stack **const stack, gregorio_note *note) { |
| 493 |
note_stack *item = gregorio_malloc(sizeof(note_stack)); |
| 494 |
item->note = note; |
| 495 |
item->prev = *stack; |
| 496 |
*stack = item; |
| 497 |
} |
| 498 |
|
| 499 |
static gregorio_note *note_stack_pop(note_stack **const stack) { |
| 500 |
note_stack *item = *stack; |
| 501 |
if (item) { |
| 502 |
gregorio_note *note = item->note; |
| 503 |
note_stack *prev = item->prev; |
| 504 |
free(item); |
| 505 |
*stack = prev; |
| 506 |
return note; |
| 507 |
} |
| 508 |
return NULL; |
| 509 |
} |
| 510 |
|
| 511 |
static void note_stack_clear(note_stack **const stack) { |
| 512 |
note_stack *item = *stack; |
| 513 |
while (item) { |
| 514 |
note_stack *prev = item->prev; |
| 515 |
free(item); |
| 516 |
item = prev; |
| 517 |
} |
| 518 |
*stack = NULL; |
| 519 |
} |
| 520 |
|
| 521 |
typedef struct { |
| 522 |
note_stack *high, *low; |
| 523 |
gregorio_note *prev_note; |
| 524 |
signed char high_ledger_line_pitch; |
| 525 |
bool running_high, running_low; |
| 526 |
} ledger_line_vars; |
| 527 |
|
| 528 |
static __inline void clear_ledger_line_vars(ledger_line_vars *const v) { |
| 529 |
note_stack_clear(&v->high); |
| 530 |
note_stack_clear(&v->low); |
| 531 |
v->prev_note = NULL; |
| 532 |
v->running_high = false; |
| 533 |
v->running_low = false; |
| 534 |
} |
| 535 |
|
| 536 |
static __inline void adjust_ledger(const gregorio_note_iter_position *const p, |
| 537 |
const gregorio_ledger_specificity specificity, bool ledger_line, |
| 538 |
note_stack **const stack, bool *const running, gregorio_note *prev_note, |
| 539 |
const signed char high_ledger_line_pitch, |
| 540 |
bool (*extend_ledger)(gregorio_note *, const gregorio_note *, |
| 541 |
const gregorio_note *, signed char)) |
| 542 |
{ |
| 543 |
if (specificity & LEDGER_DRAWN) { |
| 544 |
if (ledger_line) { |
| 545 |
gregorio_note *after = p->note; |
| 546 |
gregorio_note *note; |
| 547 |
/* process from this ledger backwards */ |
| 548 |
while ((note = note_stack_pop(stack))) { |
| 549 |
if (!extend_ledger(note, NULL, after, high_ledger_line_pitch)) { |
| 550 |
/* ledger has ended */ |
| 551 |
break; |
| 552 |
} |
| 553 |
after = note; |
| 554 |
} |
| 555 |
*running = true; |
| 556 |
} else { |
| 557 |
*running = false; |
| 558 |
} |
| 559 |
note_stack_clear(stack); |
| 560 |
} else { |
| 561 |
if (*running) { |
| 562 |
if (!extend_ledger(p->note, prev_note, NULL, |
| 563 |
high_ledger_line_pitch)) { |
| 564 |
/* ledger has ended */ |
| 565 |
note_stack_push(stack, p->note); |
| 566 |
*running = false; |
| 567 |
} |
| 568 |
/* else stack should be empty, keep it that way */ |
| 569 |
} else { |
| 570 |
note_stack_push(stack, p->note); |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
static bool extend_high_ledger(gregorio_note *const note, |
| 576 |
const gregorio_note *const note_before, |
| 577 |
const gregorio_note *const note_after, |
| 578 |
const signed char high_ledger_line_pitch) |
| 579 |
{ |
| 580 |
bool extend = false; |
| 581 |
|
| 582 |
if (note_before) { |
| 583 |
extend = note_before->u.note.pitch < note->u.note.pitch |
| 584 |
|| (note_before->u.note.pitch > high_ledger_line_pitch |
| 585 |
&& note->u.note.pitch < high_ledger_line_pitch); |
| 586 |
} else if (note_after) { |
| 587 |
extend = note_after->u.note.pitch < note->u.note.pitch |
| 588 |
|| (note_after->u.note.pitch > high_ledger_line_pitch |
| 589 |
&& note->u.note.pitch < high_ledger_line_pitch); |
| 590 |
} |
| 591 |
|
| 592 |
if (extend) { |
| 593 |
note->high_ledger_line = true; |
| 594 |
note->high_ledger_specificity = LEDGER_DRAWN; |
| 595 |
} |
| 596 |
return extend; |
| 597 |
} |
| 598 |
|
| 599 |
static bool extend_low_ledger(gregorio_note *const note, |
| 600 |
const gregorio_note *const note_before, |
| 601 |
const gregorio_note *const note_after, |
| 602 |
const signed char high_ledger_line_pitch __attribute__((__unused__))) |
| 603 |
{ |
| 604 |
bool extend = false; |
| 605 |
|
| 606 |
if (note_before) { |
| 607 |
extend = note_before->u.note.pitch < note->u.note.pitch |
| 608 |
|| (note_before->u.note.pitch > LOW_LEDGER_LINE_PITCH |
| 609 |
&& note->u.note.pitch < LOW_LEDGER_LINE_PITCH); |
| 610 |
} else if (note_after) { |
| 611 |
extend = note_after->u.note.pitch < note->u.note.pitch |
| 612 |
|| (note_after->u.note.pitch > LOW_LEDGER_LINE_PITCH |
| 613 |
&& note->u.note.pitch < LOW_LEDGER_LINE_PITCH); |
| 614 |
} |
| 615 |
|
| 616 |
if (extend) { |
| 617 |
note->low_ledger_line = true; |
| 618 |
note->low_ledger_specificity = LEDGER_DRAWN; |
| 619 |
} |
| 620 |
return extend; |
| 621 |
} |
| 622 |
|
| 623 |
/* data must be (ledger_line_vars *) */ |
| 624 |
static void ledger_line_visit(const gregorio_note_iter_position *const p, |
| 625 |
void *const data) |
| 626 |
{ |
| 627 |
ledger_line_vars *const v = (ledger_line_vars *)data; |
| 628 |
|
| 629 |
adjust_ledger(p, p->note->high_ledger_specificity, p->note->high_ledger_line, |
| 630 |
&v->high, &v->running_high, v->prev_note, v->high_ledger_line_pitch, |
| 631 |
&extend_high_ledger); |
| 632 |
adjust_ledger(p, p->note->low_ledger_specificity, p->note->low_ledger_line, |
| 633 |
&v->low, &v->running_low, v->prev_note, v->high_ledger_line_pitch, |
| 634 |
&extend_low_ledger); |
| 635 |
|
| 636 |
v->prev_note = p->note; |
| 637 |
} |
| 638 |
|
| 639 |
/* data must be (ledger_line_vars *) */ |
| 640 |
static void ledger_line_end_item( |
| 641 |
const gregorio_note_iter_position *const p __attribute__((__unused__)), |
| 642 |
const gregorio_note_iter_item_type item_type, void *const data) |
| 643 |
{ |
| 644 |
if (item_type == GRESTRUCT_ELEMENT) { |
| 645 |
clear_ledger_line_vars((ledger_line_vars *)data); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
void gabc_determine_ledger_lines(const gregorio_score *const score) |
| 650 |
{ |
| 651 |
ledger_line_vars v; |
| 652 |
memset(&v, 0, sizeof v); |
| 653 |
v.high_ledger_line_pitch = score->high_ledger_line_pitch; |
| 654 |
|
| 655 |
gregorio_for_each_note(score, ledger_line_visit, ledger_line_end_item, |
| 656 |
GRESTRUCT_ELEMENT, &v); |
| 657 |
|
| 658 |
/* stacks should be cleared by ledger_line_end_item */ |
| 659 |
} |
| 660 |
|
| 661 |
char *gabc_unescape(const char *const string) |
| 662 |
{ |
| 663 |
/* |
| 664 |
* in this context, unescape means to discard any special meaning of a |
| 665 |
* character that follows a dollar sign. Thus $-{something} is reduced |
| 666 |
* to {something} |
| 667 |
*/ |
| 668 |
char *result, *to; |
| 669 |
const char *from = string; |
| 670 |
int len = 1; |
| 671 |
result = to = (char *)gregorio_malloc(strlen(string) + 1); |
| 672 |
|
| 673 |
for (;;) { |
| 674 |
if (*from == '\0') { |
| 675 |
*to = *from; |
| 676 |
return (char *)gregorio_realloc(result, len); |
| 677 |
} else if (*from == '$') { |
| 678 |
*to = *(++ from); |
| 679 |
if (*from == '\0') { |
| 680 |
return (char *)gregorio_realloc(result, len); |
| 681 |
} |
| 682 |
++ to; |
| 683 |
++ from; |
| 684 |
++ len; |
| 685 |
} else { |
| 686 |
*(to ++) = *(from ++); |
| 687 |
++len; |
| 688 |
} |
| 689 |
} |
| 690 |
} |