-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc_parser.c
More file actions
1298 lines (1137 loc) · 49.4 KB
/
Copy pathabc_parser.c
File metadata and controls
1298 lines (1137 loc) · 49.4 KB
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
#include "abc_parser.h"
#include <string.h>
#ifndef ABC_NO_PRINT
#include <stdio.h> // only sheet_print() needs stdio
#endif
// ============================================================================
// Frequency lookup table indexed by MIDI note (frequency * 10, stored as uint16_t)
// ============================================================================
// Direct MIDI note to frequency*10 lookup for O(1) access
// MIDI 0 = rest, MIDI 12-95 = C0-B6, values outside usable range are 0 or clamped
const uint16_t midi_frequencies_x10[128] = {
// MIDI 0-11: Below C0, not typically used (0 = rest)
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// MIDI 12-23: Octave 0 (C0-B0)
164, 173, 184, 195, 206, 218, 231, 245, 260, 275, 291, 309,
// MIDI 24-35: Octave 1 (C1-B1)
327, 347, 367, 389, 412, 437, 463, 490, 519, 550, 583, 617,
// MIDI 36-47: Octave 2 (C2-B2)
654, 693, 734, 778, 824, 873, 925, 980, 1038, 1100, 1165, 1235,
// MIDI 48-59: Octave 3 (C3-B3)
1308, 1386, 1468, 1556, 1648, 1746, 1850, 1960, 2077, 2200, 2331, 2469,
// MIDI 60-71: Octave 4 (C4-B4) - Middle C is MIDI 60
2616, 2772, 2937, 3111, 3296, 3492, 3700, 3920, 4153, 4400, 4662, 4939,
// MIDI 72-83: Octave 5 (C5-B5)
5233, 5544, 5873, 6223, 6593, 6985, 7400, 7840, 8306, 8800, 9323, 9878,
// MIDI 84-95: Octave 6 (C6-B6)
10465, 11087, 11747, 12445, 13185, 13969, 14800, 15680, 16612, 17600, 18647, 19756,
// MIDI 96-127: Above B6, clamp to highest values (repeat B6 freq)
19756, 19756, 19756, 19756, 19756, 19756, 19756, 19756,
19756, 19756, 19756, 19756, 19756, 19756, 19756, 19756,
19756, 19756, 19756, 19756, 19756, 19756, 19756, 19756,
19756, 19756, 19756, 19756, 19756, 19756, 19756, 19756
};
// Map note names to semitone offsets from C
static const int8_t note_to_semitone[7] = { 0, 2, 4, 5, 7, 9, 11 };
// ============================================================================
// Key signature data
// ============================================================================
//
// Key signatures are computed from the circle of fifths rather than tabulated,
// so every tonic, accidental and church mode is supported (major/Ionian, minor/
// Aeolian, Dorian, Mixolydian, Phrygian, Lydian, Locrian).
//
// fifths = sharps in the *major* key of the tonic, then offset by the mode.
// A positive count adds sharps in the order F C G D A E B; a negative count
// adds flats in the order B E A D G C F.
// Sharps in the major key of each natural tonic (C D E F G A B order).
static const int8_t major_fifths[7] = { 0, 2, 4, -1, 1, 3, 5 };
// Note indices (NoteName order) for applying accidentals along the circle.
static const uint8_t sharp_order[7] = { 3, 0, 4, 1, 5, 2, 6 }; // F C G D A E B
static const uint8_t flat_order[7] = { 6, 2, 5, 1, 4, 0, 3 }; // B E A D G C F
// ============================================================================
// Parser state (stack allocated during parsing)
// ============================================================================
typedef struct {
const char *input;
uint16_t pos;
uint16_t len;
uint16_t tempo_bpm;
uint8_t default_num;
uint8_t default_den;
uint8_t meter_num;
uint8_t meter_den;
uint8_t tempo_note_num;
uint8_t tempo_note_den;
int8_t key_accidentals[7];
int8_t bar_accidentals[7];
int16_t repeat_start_index;
int16_t repeat_end_index;
int16_t ending_start_index; // pool index where the 1st numbered ending began (-1 = none)
uint8_t in_repeat;
uint8_t tuplet_remaining;
uint8_t tuplet_num;
uint8_t tuplet_in_time;
uint8_t current_voice; // Current voice index
int8_t broken_rhythm; // pending broken rhythm: +n for n '>', -n for n '<' (0 = none)
uint8_t tie_pending; // a '-' tie awaits the next note
uint8_t grace_count; // grace notes buffered from a {..} group
NoteName grace_names[ABC_GRACE_MAX];
int grace_octaves[ABC_GRACE_MAX];
int8_t grace_accs[ABC_GRACE_MAX];
uint8_t has_explicit_L; // L: field was explicitly set
uint8_t has_explicit_K; // K: field was explicitly set
uint8_t has_explicit_Q_note; // Q: field had explicit note value (e.g. Q:1/4=120 vs Q:120)
} ParserState;
// ============================================================================
// Utility functions
// ============================================================================
#ifndef ABC_NO_FLOAT
float note_to_frequency(NoteName name, int octave, int8_t acc) {
if (name == NOTE_REST) return 0.0f;
int midi = note_to_midi(name, octave, acc);
return midi_frequencies_x10[midi] / 10.0f;
}
#endif
int note_to_midi(NoteName name, int octave, int8_t acc) {
if (name == NOTE_REST) return 0;
int semitone = note_to_semitone[name];
// Handle accidentals: ACC_NATURAL (2) means no modification
// ACC_DOUBLE_SHARP (3) should add 2 semitones
if (acc == ACC_DOUBLE_SHARP) semitone += 2;
else if (acc != ACC_NATURAL) semitone += acc;
return 12 + (octave * 12) + semitone;
}
// MIDI to frequency lookup - direct table access O(1)
uint16_t midi_to_frequency_x10(uint8_t midi) {
return midi_frequencies_x10[midi];
}
// Map semitone (0-11) to NoteName
static const NoteName semitone_to_note[12] = {
NOTE_C, NOTE_C, NOTE_D, NOTE_D, NOTE_E, NOTE_F,
NOTE_F, NOTE_G, NOTE_G, NOTE_A, NOTE_A, NOTE_B
};
NoteName midi_to_note_name(uint8_t midi) {
if (midi == 0) return NOTE_REST;
return semitone_to_note[midi % 12];
}
uint8_t midi_to_octave(uint8_t midi) {
if (midi == 0) return 0;
return (midi / 12) - 1;
}
int midi_is_rest(uint8_t midi) {
return midi == 0;
}
// Convert MIDI ticks to milliseconds: ms = ticks * 60000 / (bpm * PPQ)
uint16_t ticks_to_ms(uint8_t ticks, uint16_t bpm) {
if (bpm == 0) bpm = 120; // Default BPM
return (uint16_t)((uint32_t)ticks * 60000 / ((uint32_t)bpm * ABC_PPQ));
}
// Get total duration in milliseconds for a pool
uint32_t pool_total_ms(const NotePool *pool, uint16_t bpm) {
if (!pool || bpm == 0) return 0;
return (uint32_t)pool->total_ticks * 60000 / ((uint32_t)bpm * ABC_PPQ);
}
const char *note_name_to_string(NoteName name) {
static const char *names[] = {"C", "D", "E", "F", "G", "A", "B", "z"};
return (name <= NOTE_REST) ? names[name] : "?";
}
const char *accidental_to_string(int8_t acc) {
switch (acc) {
case ACC_SHARP: return "#";
case ACC_FLAT: return "b";
case ACC_NATURAL: return "=";
case ACC_DOUBLE_SHARP: return "##";
case ACC_DOUBLE_FLAT: return "bb";
default: return "";
}
}
// ============================================================================
// Memory pool functions
// ============================================================================
void note_pool_init(NotePool *pool, struct note *buffer, uint16_t capacity, uint8_t max_chord_notes) {
if (!pool) return;
pool->notes = buffer;
pool->count = 0;
pool->capacity = capacity;
pool->max_chord_notes = max_chord_notes > 0 ? max_chord_notes : ABC_MAX_CHORD_NOTES;
pool->head_index = -1;
pool->tail_index = -1;
pool->total_ticks = 0;
pool->voice_id[0] = '\0';
}
void note_pool_reset(NotePool *pool) {
if (!pool) return;
pool->count = 0;
pool->head_index = -1;
pool->tail_index = -1;
pool->total_ticks = 0;
pool->voice_id[0] = '\0';
}
int note_pool_available(const NotePool *pool) {
return pool ? (pool->capacity - pool->count) : 0;
}
static int16_t note_pool_alloc(NotePool *pool) {
if (!pool || pool->count >= pool->capacity) return -1;
int16_t index = (int16_t)pool->count;
if (index < 0) return -1; // count exceeds int16_t index space
pool->count++;
// Count-only pool (notes == NULL): used by abc_count_notes(). Tally only.
if (!pool->notes) return index;
struct note *n = &pool->notes[index];
n->next_index = -1;
n->duration = 0;
n->chord_size = 0;
// Clear midi_note array (use compile-time size since struct is fixed)
for (int i = 0; i < ABC_MAX_CHORD_NOTES; i++) {
n->midi_note[i] = 0;
}
return index;
}
// ============================================================================
// Sheet functions
// ============================================================================
// Forward declaration (defined with the parser helpers below).
static void safe_strcpy(char *dest, uint8_t dest_size, const char *src, uint8_t src_len);
// Score defaults applied when the corresponding header is absent are taken from
// the ABC_DEFAULT_* macros (see abc_parser.h): Q:ABC_DEFAULT_TEMPO_BPM,
// M:ABC_DEFAULT_METER_NUM/DEN, K:ABC_DEFAULT_KEY, and L: either derived from the
// meter or fixed to ABC_DEFAULT_NOTE_NUM/DEN (per ABC_DERIVE_LENGTH_FROM_METER).
void sheet_init(struct sheet *sheet, NotePool *pools, uint8_t pool_count) {
if (!sheet) return;
sheet->pools = pools;
sheet->pool_count = pool_count;
sheet->voice_count = 0;
sheet->tempo_bpm = ABC_DEFAULT_TEMPO_BPM;
sheet->tempo_note_num = ABC_DEFAULT_NOTE_NUM;
sheet->tempo_note_den = ABC_DEFAULT_NOTE_DEN;
sheet->title[0] = '\0';
sheet->composer[0] = '\0';
safe_strcpy(sheet->key, ABC_MAX_KEY_LEN, ABC_DEFAULT_KEY, (uint8_t)sizeof(ABC_DEFAULT_KEY) - 1);
sheet->default_note_num = ABC_DEFAULT_NOTE_NUM;
sheet->default_note_den = ABC_DEFAULT_NOTE_DEN;
sheet->meter_num = ABC_DEFAULT_METER_NUM;
sheet->meter_den = ABC_DEFAULT_METER_DEN;
// Note: pools should already be initialized by caller via note_pool_init_ext()
}
void sheet_reset(struct sheet *sheet) {
if (!sheet) return;
for (uint8_t i = 0; i < sheet->pool_count; i++) {
note_pool_reset(&sheet->pools[i]);
}
sheet->voice_count = 0;
sheet->tempo_bpm = ABC_DEFAULT_TEMPO_BPM;
sheet->tempo_note_num = ABC_DEFAULT_NOTE_NUM;
sheet->tempo_note_den = ABC_DEFAULT_NOTE_DEN;
sheet->default_note_num = ABC_DEFAULT_NOTE_NUM;
sheet->default_note_den = ABC_DEFAULT_NOTE_DEN;
sheet->meter_num = ABC_DEFAULT_METER_NUM;
sheet->meter_den = ABC_DEFAULT_METER_DEN;
sheet->title[0] = '\0';
sheet->composer[0] = '\0';
safe_strcpy(sheet->key, ABC_MAX_KEY_LEN, ABC_DEFAULT_KEY, (uint8_t)sizeof(ABC_DEFAULT_KEY) - 1);
}
struct note *note_get(const NotePool *pool, int index) {
if (!pool) return NULL;
if (index < 0 || index >= (int)pool->count) return NULL;
return (struct note *)&pool->notes[index];
}
struct note *pool_first_note(const NotePool *pool) {
return pool ? note_get(pool, pool->head_index) : NULL;
}
struct note *note_next(const NotePool *pool, const struct note *current) {
return (pool && current) ? note_get(pool, current->next_index) : NULL;
}
// Legacy compatibility - uses first pool
struct note *sheet_first_note(const struct sheet *sheet) {
if (!sheet || !sheet->pools || sheet->pool_count == 0) return NULL;
return pool_first_note(&sheet->pools[0]);
}
// Append a note/chord to a specific pool (stores only MIDI notes)
static int pool_append_note(NotePool *pool, uint8_t chord_size,
NoteName *names, int *octaves,
int8_t *accs, uint8_t duration_ticks) {
if (!pool) return -1;
int16_t index = note_pool_alloc(pool);
if (index < 0) return -1;
if (!pool->notes) return 0; // count-only pool: allocation tallied, nothing to store
struct note *n = &pool->notes[index];
// Clamp chord size to pool's max (and struct's compile-time max)
uint8_t max_chord = pool->max_chord_notes;
if (max_chord > ABC_MAX_CHORD_NOTES) max_chord = ABC_MAX_CHORD_NOTES;
if (chord_size > max_chord) chord_size = max_chord;
n->chord_size = chord_size;
n->duration = duration_ticks;
n->next_index = -1;
for (uint8_t i = 0; i < chord_size; i++) {
// Only store MIDI note - other properties derived on demand
n->midi_note[i] = (uint8_t)note_to_midi(names[i], octaves[i], accs[i]);
}
if (pool->head_index < 0) {
pool->head_index = index;
pool->tail_index = index;
} else {
pool->notes[pool->tail_index].next_index = index;
pool->tail_index = index;
}
pool->total_ticks += duration_ticks;
return 0;
}
// ============================================================================
// Parser helper functions
// ============================================================================
static inline char peek(ParserState *s) {
return (s->pos < s->len) ? s->input[s->pos] : '\0';
}
static inline char advance(ParserState *s) {
return (s->pos < s->len) ? s->input[s->pos++] : '\0';
}
static void skip_whitespace(ParserState *s) {
while (s->pos < s->len) {
char c = s->input[s->pos];
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') s->pos++;
else break;
}
}
// Calculate duration in MIDI ticks (PPQ-based)
// Quarter note = ABC_PPQ ticks, so whole note = 4 * ABC_PPQ ticks
static uint8_t calculate_duration_ticks(ParserState *s, int num, int den) {
// Whole note = 4 * PPQ ticks (PPQ = ticks per quarter note)
// Ticks are tempo-independent; tempo_note only affects ticks_to_ms conversion
uint32_t whole_ticks = 4 * ABC_PPQ;
// Default note duration in ticks
uint32_t default_ticks = whole_ticks * s->default_num / s->default_den;
// Apply note multiplier/divisor
uint32_t duration = default_ticks * num / den;
if (s->tuplet_remaining > 0) {
duration = duration * s->tuplet_in_time / s->tuplet_num;
s->tuplet_remaining--;
}
// Clamp to uint8_t max (255)
if (duration > 255) duration = 255;
return (uint8_t)duration;
}
static inline char abc_lower(char c) {
return (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c;
}
// Parse a K: value (tonic [#/b] [mode]) and fill key_accidentals from the
// circle of fifths. Unknown/none keys yield no accidentals.
static void set_key_signature(ParserState *s, const char *key) {
memset(s->key_accidentals, 0, 7);
if (!key || !key[0]) return;
const char *p = key;
while (*p == ' ' || *p == '\t') p++;
int tonic = -1;
if (*p >= 'A' && *p <= 'G') tonic = (*p - 'A' + 5) % 7; // -> NoteName index
else if (*p >= 'a' && *p <= 'g') tonic = (*p - 'a' + 5) % 7;
if (tonic < 0) return; // K:none, K:HP, etc.
p++;
int fifths = major_fifths[tonic];
if (*p == '#') { fifths += 7; p++; }
else if (*p == 'b') { fifths -= 7; p++; }
while (*p == ' ') p++;
// Mode suffix (case-insensitive, first 3 letters; bare 'm' = minor).
char m0 = p[0] ? abc_lower(p[0]) : 0;
char m1 = (m0 && p[1]) ? abc_lower(p[1]) : 0;
char m2 = (m1 && p[2]) ? abc_lower(p[2]) : 0;
if (m0=='m' && m1=='a' && m2=='j') fifths += 0; // major
else if (m0=='i' && m1=='o' && m2=='n') fifths += 0; // Ionian
else if (m0=='m' && m1=='i' && m2=='x') fifths += -1; // Mixolydian
else if (m0=='m' && m1=='i' && m2=='n') fifths += -3; // minor
else if (m0=='a' && m1=='e' && m2=='o') fifths += -3; // Aeolian
else if (m0=='d' && m1=='o' && m2=='r') fifths += -2; // Dorian
else if (m0=='p' && m1=='h' && m2=='r') fifths += -4; // Phrygian
else if (m0=='l' && m1=='y' && m2=='d') fifths += +1; // Lydian
else if (m0=='l' && m1=='o' && m2=='c') fifths += -5; // Locrian
else if (m0=='m') fifths += -3; // bare 'm' = minor
if (fifths > 7) fifths = 7;
if (fifths < -7) fifths = -7;
if (fifths > 0) for (int i = 0; i < fifths; i++) s->key_accidentals[sharp_order[i]] = ACC_SHARP;
else for (int i = 0; i < -fifths; i++) s->key_accidentals[flat_order[i]] = ACC_FLAT;
}
static void safe_strcpy(char *dest, uint8_t dest_size, const char *src, uint8_t src_len) {
uint8_t len = (src_len < dest_size) ? src_len : (dest_size - 1);
memcpy(dest, src, len);
dest[len] = '\0';
}
// Find or create a voice by ID, returns voice index
static int find_or_create_voice(struct sheet *sheet, const char *voice_id, uint8_t id_len) {
// Search existing voices
for (uint8_t i = 0; i < sheet->voice_count; i++) {
if (strncmp(sheet->pools[i].voice_id, voice_id, id_len) == 0 &&
sheet->pools[i].voice_id[id_len] == '\0') {
return i;
}
}
// Create new voice if space available
if (sheet->voice_count < sheet->pool_count) {
uint8_t idx = sheet->voice_count;
safe_strcpy(sheet->pools[idx].voice_id, ABC_MAX_VOICE_ID_LEN, voice_id, id_len);
sheet->voice_count++;
return idx;
}
return -1; // No space for more voices
}
// Forward declaration (defined with the note-parsing helpers below).
static int copy_repeat_section(NotePool *pool, int16_t start_idx, int16_t end_idx);
// Apply one information field (used by both the header parser and inline
// [X:...] fields). Handles T, C, L, M, Q, K; ignores X and unknown fields.
// Voice (V) is handled by the callers since it affects parse control flow.
static void apply_field_value(ParserState *s, struct sheet *sheet, char field,
const char *val, uint8_t vlen) {
switch (field) {
case 'T': safe_strcpy(sheet->title, ABC_MAX_TITLE_LEN, val, vlen); break;
case 'C': safe_strcpy(sheet->composer, ABC_MAX_COMPOSER_LEN, val, vlen); break;
case 'L': {
int num = 0, den = 0;
uint8_t i = 0;
while (i < vlen && val[i] >= '0' && val[i] <= '9') num = num * 10 + (val[i++] - '0');
if (i < vlen && val[i] == '/') {
i++;
while (i < vlen && val[i] >= '0' && val[i] <= '9') den = den * 10 + (val[i++] - '0');
}
if (num > 0 && den > 0) {
s->default_num = sheet->default_note_num = (uint8_t)num;
s->default_den = sheet->default_note_den = (uint8_t)den;
s->has_explicit_L = 1;
}
break;
}
case 'M': {
int num = 0, den = 0;
uint8_t i = 0;
while (i < vlen && val[i] >= '0' && val[i] <= '9') num = num * 10 + (val[i++] - '0');
if (i < vlen && val[i] == '/') {
i++;
while (i < vlen && val[i] >= '0' && val[i] <= '9') den = den * 10 + (val[i++] - '0');
}
if (num > 0 && den > 0) {
s->meter_num = sheet->meter_num = (uint8_t)num;
s->meter_den = sheet->meter_den = (uint8_t)den;
}
break;
}
case 'Q': {
int tempo = 0, note_num = 0, note_den = 0;
uint8_t i = 0;
uint8_t eq_pos = 0;
for (uint8_t j = 0; j < vlen; j++) {
if (val[j] == '=') { eq_pos = j + 1; break; }
}
if (eq_pos > 0) {
while (i < eq_pos - 1 && val[i] >= '0' && val[i] <= '9') {
note_num = note_num * 10 + (val[i++] - '0');
}
if (i < eq_pos - 1 && val[i] == '/') {
i++;
while (i < eq_pos - 1 && val[i] >= '0' && val[i] <= '9') {
note_den = note_den * 10 + (val[i++] - '0');
}
}
if (note_num > 0 && note_den > 0) {
s->tempo_note_num = sheet->tempo_note_num = (uint8_t)note_num;
s->tempo_note_den = sheet->tempo_note_den = (uint8_t)note_den;
s->has_explicit_Q_note = 1;
}
}
i = eq_pos;
while (i < vlen && val[i] >= '0' && val[i] <= '9') {
tempo = tempo * 10 + (val[i++] - '0');
}
if (tempo > 0) s->tempo_bpm = sheet->tempo_bpm = (uint16_t)tempo;
break;
}
case 'K':
safe_strcpy(sheet->key, ABC_MAX_KEY_LEN, val, vlen);
set_key_signature(s, sheet->key);
s->has_explicit_K = 1;
break;
default: break; // X and unknown fields ignored
}
}
// Replay the common part of a repeat on entering a numbered ending.
// '1' : mark where the 1st ending begins (the common part precedes it).
// '2'+: a later ending begins; replay [repeat_start, ending_start) once so the
// unfolded stream reads common, 1st-ending, common, 2nd-ending.
static int handle_ending(ParserState *s, NotePool *pool, char digit) {
if (digit == '1') {
s->ending_start_index = (int16_t)pool->count;
return 0;
}
if (s->in_repeat && s->ending_start_index >= 0 && s->repeat_start_index >= 0) {
if (copy_repeat_section(pool, s->repeat_start_index,
(int16_t)(s->ending_start_index - 1)) < 0) return -1;
}
s->ending_start_index = -1;
s->in_repeat = 0;
s->repeat_start_index = -1;
return 0;
}
// ============================================================================
// Header parsing
// ============================================================================
static void parse_header(ParserState *s, struct sheet *sheet) {
while (s->pos < s->len) {
skip_whitespace(s);
if (s->pos + 1 >= s->len || s->input[s->pos + 1] != ':') break;
char field = s->input[s->pos];
uint16_t start = s->pos + 2;
uint16_t end = start;
while (end < s->len && s->input[end] != '\n' && s->input[end] != '\r') end++;
uint16_t line_end = end;
if (end < s->len) end++;
while (start < line_end && s->input[start] == ' ') start++;
while (line_end > start && (s->input[line_end-1] == ' ' || s->input[line_end-1] == '\t')) line_end--;
uint8_t vlen = (uint8_t)(line_end - start);
const char *val = s->input + start;
if (field == 'V') {
// V: marks start of body - don't consume it, let body parser handle it
return;
}
apply_field_value(s, sheet, field, val, vlen);
s->pos = end;
if (field == 'K') return; // K: ends the header
}
}
// ============================================================================
// Single pitch parsing (used for both single notes and chord members)
// ============================================================================
typedef struct {
NoteName name;
int octave;
int8_t accidental;
int dur_num;
int dur_den;
} ParsedPitch;
static int parse_pitch(ParserState *s, ParsedPitch *pitch) {
char c = peek(s);
int8_t acc = ACC_NONE;
int explicit_acc = 0;
while (c == '^' || c == '_' || c == '=') {
explicit_acc = 1;
if (c == '^') acc = (acc == ACC_SHARP) ? ACC_DOUBLE_SHARP : ACC_SHARP;
else if (c == '_') acc = (acc == ACC_FLAT) ? ACC_DOUBLE_FLAT : ACC_FLAT;
else acc = ACC_NATURAL;
advance(s);
c = peek(s);
}
NoteName name;
int octave = 4;
if (c >= 'A' && c <= 'G') { name = (NoteName)((c - 'A' + 5) % 7); octave = 4; advance(s); }
else if (c >= 'a' && c <= 'g') { name = (NoteName)((c - 'a' + 5) % 7); octave = 5; advance(s); }
else if (c == 'z' || c == 'Z') { name = NOTE_REST; advance(s); }
else return -1;
if (!explicit_acc && name != NOTE_REST) {
acc = s->bar_accidentals[name] ? s->bar_accidentals[name] : s->key_accidentals[name];
} else if (explicit_acc && name != NOTE_REST) {
s->bar_accidentals[name] = (acc == ACC_NATURAL) ? ACC_NONE : acc;
if (acc == ACC_NATURAL) acc = ACC_NONE;
}
c = peek(s);
while (c == '\'' || c == ',') {
if (c == '\'') octave++; else octave--;
advance(s);
c = peek(s);
}
if (octave < 0) octave = 0;
if (octave > 6) octave = 6;
// Parse duration modifiers
int num = 1, den = 1;
c = peek(s);
if (c >= '0' && c <= '9') {
num = 0;
while (c >= '0' && c <= '9') { num = num * 10 + (c - '0'); advance(s); c = peek(s); }
}
if (c == '/') {
advance(s);
c = peek(s);
if (c >= '0' && c <= '9') {
den = 0;
while (c >= '0' && c <= '9') { den = den * 10 + (c - '0'); advance(s); c = peek(s); }
} else {
den = 2;
while (peek(s) == '/') { advance(s); den *= 2; }
}
}
pitch->name = name;
pitch->octave = octave;
pitch->accidental = acc;
pitch->dur_num = num;
pitch->dur_den = den;
return 0;
}
// ============================================================================
// Ornament / rhythm modifiers
// ============================================================================
// Apply a pending broken-rhythm modifier (s->broken_rhythm) to the previous
// (tail) note and return the adjusted duration for the current note.
// '>' : previous note dotted (x3/2), current note cut (x1/2)
// '<' : previous note cut, current note dotted
// n consecutive signs scale the dotted side by (2^(n+1)-1)/2^n, the cut side
// by 1/2^n (so '>>' is x7/4 and x1/4). Fixes up pool->total_ticks.
static uint8_t apply_broken_rhythm(ParserState *s, NotePool *pool, uint8_t cur_dur) {
int8_t b = s->broken_rhythm;
s->broken_rhythm = 0;
if (b == 0) return cur_dur;
uint8_t n = (b > 0) ? (uint8_t)b : (uint8_t)(-b);
if (n > 4) n = 4;
uint16_t den = (uint16_t)(1u << n);
uint16_t dot = (uint16_t)((1u << (n + 1)) - 1);
uint16_t prev_num = (b > 0) ? dot : 1;
uint16_t cur_num = (b > 0) ? 1 : dot;
if (pool && pool->notes && pool->tail_index >= 0) {
struct note *prev = &pool->notes[pool->tail_index];
uint32_t nd = (uint32_t)prev->duration * prev_num / den;
if (nd > 255) nd = 255;
if (nd < 1) nd = 1;
pool->total_ticks += (uint8_t)nd - prev->duration;
prev->duration = (uint8_t)nd;
}
uint32_t cd = (uint32_t)cur_dur * cur_num / den;
if (cd > 255) cd = 255;
if (cd < 1) cd = 1;
return (uint8_t)cd;
}
// Emit any grace notes buffered from a {..} group ahead of a principal note of
// length principal_dur. Each grace note steals ABC_GRACE_NOTE_TICKS from the
// principal (so the bar length is unchanged); returns the total ticks stolen.
static uint8_t emit_grace_notes(ParserState *s, NotePool *pool, uint8_t principal_dur) {
uint8_t cnt = s->grace_count;
s->grace_count = 0;
if (cnt == 0 || !pool) return 0;
uint16_t per = ABC_GRACE_NOTE_TICKS;
if (per < 1) per = 1;
uint16_t total = (uint16_t)(per * cnt);
if (total >= principal_dur) { // keep >= 1 tick for the principal
if (principal_dur <= cnt) return 0; // too short to ornament; drop grace
per = (uint16_t)((principal_dur - 1) / cnt);
if (per == 0) return 0;
total = (uint16_t)(per * cnt);
}
for (uint8_t i = 0; i < cnt; i++) {
NoteName nm[1] = { s->grace_names[i] };
int oc[1] = { s->grace_octaves[i] };
int8_t ac[1] = { s->grace_accs[i] };
if (pool_append_note(pool, 1, nm, oc, ac, (uint8_t)per) < 0) break;
}
return (uint8_t)total;
}
// ============================================================================
// Note/Chord parsing
// ============================================================================
static int parse_note_or_chord(ParserState *s, struct sheet *sheet) {
skip_whitespace(s);
if (s->pos >= s->len) return 1;
char c = peek(s);
NotePool *pool = &sheet->pools[s->current_voice];
// Handle chord [...]
if (c == '[') {
advance(s); // skip '['
NoteName names[ABC_MAX_CHORD_NOTES];
int octaves[ABC_MAX_CHORD_NOTES];
int8_t accs[ABC_MAX_CHORD_NOTES];
uint8_t chord_size = 0;
int total_dur_num = 1, total_dur_den = 1;
while (peek(s) != ']' && s->pos < s->len && chord_size < ABC_MAX_CHORD_NOTES) {
skip_whitespace(s);
c = peek(s);
// Skip if not a note character
if (!((c >= 'A' && c <= 'G') || (c >= 'a' && c <= 'g') ||
c == 'z' || c == 'Z' || c == '^' || c == '_' || c == '=')) {
if (c == ']') break;
advance(s);
continue;
}
ParsedPitch pitch;
if (parse_pitch(s, &pitch) == 0) {
names[chord_size] = pitch.name;
octaves[chord_size] = pitch.octave;
accs[chord_size] = pitch.accidental;
// Use last pitch's duration for the chord
total_dur_num = pitch.dur_num;
total_dur_den = pitch.dur_den;
chord_size++;
}
}
if (peek(s) == ']') advance(s); // skip ']'
// Check for duration after chord
c = peek(s);
if (c >= '0' && c <= '9') {
total_dur_num = 0;
while (c >= '0' && c <= '9') { total_dur_num = total_dur_num * 10 + (c - '0'); advance(s); c = peek(s); }
}
if (c == '/') {
advance(s);
c = peek(s);
if (c >= '0' && c <= '9') {
total_dur_den = 0;
while (c >= '0' && c <= '9') { total_dur_den = total_dur_den * 10 + (c - '0'); advance(s); c = peek(s); }
} else {
total_dur_den = 2;
while (peek(s) == '/') { advance(s); total_dur_den *= 2; }
}
}
if (chord_size > 0) {
uint8_t duration = calculate_duration_ticks(s, total_dur_num, total_dur_den);
s->tie_pending = 0; // chord ties not modelled
duration = apply_broken_rhythm(s, pool, duration);
duration -= emit_grace_notes(s, pool, duration);
return pool_append_note(pool, chord_size, names, octaves, accs, duration);
}
return 0;
}
// Handle single note
ParsedPitch pitch;
if (parse_pitch(s, &pitch) == 0) {
NoteName names[1] = { pitch.name };
int octaves[1] = { pitch.octave };
int8_t accs[1] = { pitch.accidental };
uint8_t duration = calculate_duration_ticks(s, pitch.dur_num, pitch.dur_den);
// Tie: fold this note into the previous one if it is the same pitch.
if (s->tie_pending) {
s->tie_pending = 0;
if (pitch.name != NOTE_REST && pool->notes && pool->tail_index >= 0) {
struct note *prev = &pool->notes[pool->tail_index];
int midi = note_to_midi(pitch.name, pitch.octave, pitch.accidental);
if (prev->chord_size == 1 && prev->midi_note[0] == (uint8_t)midi) {
uint32_t nd = (uint32_t)prev->duration + duration;
if (nd > 255) nd = 255;
pool->total_ticks += (uint8_t)nd - prev->duration;
prev->duration = (uint8_t)nd;
return 0; // merged into previous note
}
}
}
duration = apply_broken_rhythm(s, pool, duration);
duration -= emit_grace_notes(s, pool, duration);
return pool_append_note(pool, 1, names, octaves, accs, duration);
}
return 1; // Not a note
}
static int copy_repeat_section(NotePool *pool, int16_t start_idx, int16_t end_idx) {
if (!pool || start_idx < 0) return 0;
// Count-only pool: notes are contiguous and unlinked, so the repeated span
// is simply [start_idx, end_idx]. Tally one allocation per source note.
if (!pool->notes) {
for (int16_t i = start_idx; i <= end_idx && i < (int16_t)pool->count; i++) {
if (note_pool_alloc(pool) < 0) return -1;
}
return 0;
}
int16_t cur = start_idx;
while (cur >= 0 && cur <= end_idx && cur < (int16_t)pool->count) {
struct note *src = &pool->notes[cur];
// Extract note properties from stored MIDI values
NoteName names[ABC_MAX_CHORD_NOTES];
int octaves[ABC_MAX_CHORD_NOTES];
int8_t accs[ABC_MAX_CHORD_NOTES];
for (uint8_t i = 0; i < src->chord_size && i < ABC_MAX_CHORD_NOTES; i++) {
names[i] = midi_to_note_name(src->midi_note[i]);
octaves[i] = midi_to_octave(src->midi_note[i]);
accs[i] = ACC_NONE; // Accidentals not preserved in repeat copies
}
if (pool_append_note(pool, src->chord_size, names, octaves, accs, src->duration) < 0) {
return -1;
}
cur = src->next_index;
if (cur < 0) break;
}
return 0;
}
static int parse_notes(ParserState *s, struct sheet *sheet) {
s->repeat_start_index = -1;
s->repeat_end_index = -1;
s->ending_start_index = -1;
s->in_repeat = 0;
s->current_voice = 0;
s->broken_rhythm = 0;
s->tie_pending = 0;
s->grace_count = 0;
// Don't create default voice yet - wait to see if V: line comes first
while (s->pos < s->len) {
skip_whitespace(s);
if (s->pos >= s->len) break;
char c = peek(s);
// Handle V: voice change (inline) BEFORE getting pool reference
if (c == 'V' && s->pos + 1 < s->len && s->input[s->pos + 1] == ':') {
advance(s); // V
advance(s); // :
// Skip leading whitespace
while (s->pos < s->len && (s->input[s->pos] == ' ' || s->input[s->pos] == '\t')) {
s->pos++;
}
// Read voice ID (alphanumeric characters only)
uint16_t id_start = s->pos;
while (s->pos < s->len) {
char ch = s->input[s->pos];
// Voice ID is alphanumeric, stop at whitespace or any other character
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
(ch >= '0' && ch <= '9') || ch == '_' || ch == '-') {
s->pos++;
} else {
break;
}
}
uint8_t id_len = (uint8_t)(s->pos - id_start);
if (id_len > 0) {
int voice_idx = find_or_create_voice(sheet, s->input + id_start, id_len);
if (voice_idx >= 0) {
s->current_voice = (uint8_t)voice_idx;
}
}
continue;
}
// Create default voice if none exists and we're about to parse notes
if (sheet->voice_count == 0 && sheet->pool_count > 0) {
sheet->voice_count = 1;
safe_strcpy(sheet->pools[0].voice_id, ABC_MAX_VOICE_ID_LEN, "default", 7);
}
NotePool *pool = &sheet->pools[s->current_voice];
if (c == '|') {
advance(s);
memset(s->bar_accidentals, 0, 7);
c = peek(s);
if (c == ':') { // |: start repeat
advance(s);
s->in_repeat = 1;
s->repeat_start_index = (int16_t)pool->count;
s->ending_start_index = -1;
} else if (c >= '1' && c <= '9') { // |1 / |2 numbered ending
advance(s);
if (handle_ending(s, pool, c) < 0) return -2;
} else if (c == '|' || c == ']') { // || or |]
advance(s);
}
continue;
}
if (c == ':') {
advance(s);
if (peek(s) == '|') { // :| end repeat
advance(s);
// Copy only the common part if a 1st ending was open.
int16_t copy_end = (s->ending_start_index >= 0)
? (int16_t)(s->ending_start_index - 1)
: (int16_t)(pool->count - 1);
s->repeat_end_index = copy_end;
if (copy_repeat_section(pool, s->repeat_start_index, copy_end) < 0) return -2;
if (peek(s) == ':') { // :|: start another repeat
advance(s);
s->repeat_start_index = (int16_t)pool->count;
s->ending_start_index = -1;
} else { // :|2 / :|[2 -> consume ending marker
if (peek(s) == '[') {
char d = (s->pos + 1 < s->len) ? s->input[s->pos + 1] : 0;
if (d >= '1' && d <= '9') { advance(s); advance(s); }
} else if (peek(s) >= '1' && peek(s) <= '9') {
advance(s);
}
s->in_repeat = 0;
s->repeat_start_index = -1;
s->ending_start_index = -1;
}
}
continue;
}
// Handle tuplet markers
if (c == '(') {
advance(s);
c = peek(s);
if (c >= '2' && c <= '9') {
uint8_t n = (uint8_t)(c - '0');
advance(s);
s->tuplet_num = n;
s->tuplet_remaining = n;
if (n == 2) s->tuplet_in_time = 3;
else if (n == 3) s->tuplet_in_time = 2;
else if (n == 4) s->tuplet_in_time = 3;
else if (n == 6) s->tuplet_in_time = 2;
else s->tuplet_in_time = n - 1;
}
continue;
}
// Broken rhythm: '>' dots the previous note and cuts the next, '<' vice
// versa. Applied when the next note is appended (see apply_broken_rhythm).
if (c == '>' || c == '<') {
char bc = c;
int8_t cnt = 0;
while (peek(s) == bc) { advance(s); if (cnt < 8) cnt++; }
s->broken_rhythm = (bc == '>') ? cnt : (int8_t)(-cnt);
continue;
}
// Tie: fold the next same-pitch note into the previous one.
if (c == '-') {
advance(s);
s->tie_pending = 1;
continue;
}
// Grace notes {..}: buffer their pitches; they steal a little time from
// the following principal note (see emit_grace_notes).
if (c == '{') {
advance(s); // '{'
if (peek(s) == '/') advance(s); // acciaccatura slash
s->grace_count = 0;
while (s->pos < s->len && peek(s) != '}') {
char gc = peek(s);
if ((gc >= 'A' && gc <= 'G') || (gc >= 'a' && gc <= 'g') ||
gc == 'z' || gc == 'Z' || gc == '^' || gc == '_' || gc == '=') {
ParsedPitch gp;
if (parse_pitch(s, &gp) == 0) {