mkmidilibrary 0.2.0

Music scoring and MIDI library for Rust
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
//! Note representation
//!
//! A Note combines a Pitch with a Duration and includes additional
//! notation properties like ties, articulations, and lyrics.

use std::cmp::Ordering;
use std::fmt;

use super::{Duration, Fraction, Interval, Pitch};

/// Tie type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TieType {
    /// Start of a tie
    Start,
    /// Continuation of a tie
    Continue,
    /// End of a tie
    Stop,
    /// Let ring (no explicit end)
    LetRing,
}

/// A tie connecting notes
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tie {
    /// Type of tie
    pub type_: TieType,
    /// Tie placement (above/below)
    pub placement: Option<Placement>,
}

impl Tie {
    /// Create a new tie
    pub fn new(type_: TieType) -> Self {
        Self {
            type_,
            placement: None,
        }
    }

    /// Create a start tie
    pub fn start() -> Self {
        Self::new(TieType::Start)
    }

    /// Create a stop tie
    pub fn stop() -> Self {
        Self::new(TieType::Stop)
    }
}

/// Placement for notation elements
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Placement {
    Above,
    Below,
}

/// Stem direction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum StemDirection {
    #[default]
    Auto,
    Up,
    Down,
    None,
}

/// Notehead type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum NoteHeadType {
    #[default]
    Normal,
    Diamond,
    Square,
    Triangle,
    Slash,
    Cross,
    X,
    CircleX,
    Arrow,
    Cluster,
    None,
}

/// Notehead properties
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct NoteHead {
    /// Notehead type
    pub type_: NoteHeadType,
    /// Whether the notehead is filled
    pub filled: Option<bool>,
    /// Whether the notehead has parentheses
    pub parenthesis: bool,
}

impl NoteHead {
    /// Create a new normal notehead
    pub fn normal() -> Self {
        Self::default()
    }

    /// Create a diamond notehead
    pub fn diamond() -> Self {
        Self {
            type_: NoteHeadType::Diamond,
            ..Default::default()
        }
    }

    /// Create an X notehead
    pub fn x() -> Self {
        Self {
            type_: NoteHeadType::X,
            ..Default::default()
        }
    }
}

/// Type of articulation mark. This is the single, unified articulation
/// enum for the whole crate: it used to be duplicated as a poorer
/// `core::note::ArticulationType` (used by `Note`/`Articulation`) and a
/// richer `notation::articulation::ArticulationMark` (with symbols,
/// velocity/duration multipliers, and fermata-shape variants) that nothing
/// actually attached to a `Note` could reach. `notation::articulation`
/// re-exports this type for backward-compatible access from that module
/// path.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArticulationMark {
    /// Accent (>)
    Accent,
    /// Strong accent (^)
    StrongAccent,
    /// Staccato (.)
    Staccato,
    /// Staccatissimo (wedge)
    Staccatissimo,
    /// Tenuto (-)
    Tenuto,
    /// Detached legato (tenuto + staccato)
    DetachedLegato,
    /// Marcato (^)
    Marcato,
    /// Fermata
    Fermata,
    /// Short fermata
    ShortFermata,
    /// Long fermata
    LongFermata,
    /// Breath mark
    BreathMark,
    /// Caesura
    Caesura,
    /// Up bow (string)
    UpBow,
    /// Down bow (string)
    DownBow,
    /// Harmonic
    Harmonic,
    /// Open string
    OpenString,
    /// Stopped (brass)
    Stopped,
    /// Pizzicato
    Pizzicato,
    /// Snap pizzicato
    SnapPizzicato,
    /// Thumb position
    ThumbPosition,
    /// Pluck (guitar)
    Pluck,
    /// Double tongue
    DoubleTongue,
    /// Triple tongue
    TripleTongue,
    /// Heel (organ pedal)
    Heel,
    /// Toe (organ pedal)
    Toe,
    /// Spiccato (bounced bow)
    Spiccato,
    /// Scoop (slide into a note from below)
    Scoop,
    /// Plop (slide into a note from above)
    Plop,
    /// Doit (slide up and away after a note)
    Doit,
    /// Falloff (slide down and away after a note)
    Falloff,
    /// Stress (early-music metric-accent mark)
    Stress,
    /// Unstress (early-music de-emphasis mark)
    Unstress,
    /// Natural string harmonic (distinct from the generic `Harmonic`,
    /// which doesn't specify the production technique)
    StringHarmonic,
    /// Fret position indication for a fretted instrument (fret number)
    Fret(u8),
    /// String number indication for a fretted/bowed-string instrument
    StringNumber(u8),
    /// Handbell technique indication (e.g. martellato, mallet, damp) —
    /// simplified to a single generic marker rather than a full handbell
    /// technique taxonomy.
    HandbellIndication,
    /// Harp fingernails (pluck with fingernails for a metallic timbre)
    HarpFingerNails,
    /// Generic woodwind special-technique indication (e.g.
    /// flutter-tongue, half-hole) — simplified to a single marker rather
    /// than a full per-technique taxonomy.
    WoodwindIndication,
    /// Generic brass special-technique indication (e.g. muted, flutter,
    /// half-valve) — simplified to a single marker rather than a full
    /// per-technique taxonomy.
    BrassIndication,
}

impl ArticulationMark {
    /// Get the symbol
    pub fn symbol(&self) -> &'static str {
        match self {
            ArticulationMark::Accent => ">",
            ArticulationMark::StrongAccent => "^",
            ArticulationMark::Staccato => ".",
            ArticulationMark::Staccatissimo => "",
            ArticulationMark::Tenuto => "-",
            ArticulationMark::DetachedLegato => "-.",
            ArticulationMark::Marcato => "^",
            ArticulationMark::Fermata => "𝄐",
            ArticulationMark::ShortFermata => "𝄑",
            ArticulationMark::LongFermata => "𝄒",
            ArticulationMark::BreathMark => ",",
            ArticulationMark::Caesura => "//",
            ArticulationMark::UpBow => "",
            ArticulationMark::DownBow => "",
            ArticulationMark::Harmonic => "",
            ArticulationMark::OpenString => "",
            ArticulationMark::Stopped => "+",
            ArticulationMark::Pizzicato => "+",
            ArticulationMark::SnapPizzicato => "",
            ArticulationMark::ThumbPosition => "",
            ArticulationMark::Pluck => "i",
            ArticulationMark::DoubleTongue => "",
            ArticulationMark::TripleTongue => "",
            ArticulationMark::Heel => "U",
            ArticulationMark::Toe => "^",
            ArticulationMark::Spiccato => "'",
            ArticulationMark::Scoop => "",
            ArticulationMark::Plop => "",
            ArticulationMark::Doit => "",
            ArticulationMark::Falloff => "",
            ArticulationMark::Stress => "",
            ArticulationMark::Unstress => "",
            ArticulationMark::StringHarmonic => "",
            ArticulationMark::Fret(_) => "fr.",
            ArticulationMark::StringNumber(_) => "#",
            ArticulationMark::HandbellIndication => "hb.",
            ArticulationMark::HarpFingerNails => "n.",
            ArticulationMark::WoodwindIndication => "ww.",
            ArticulationMark::BrassIndication => "br.",
        }
    }

    /// Get the name
    pub fn name(&self) -> &'static str {
        match self {
            ArticulationMark::Accent => "accent",
            ArticulationMark::StrongAccent => "strong accent",
            ArticulationMark::Staccato => "staccato",
            ArticulationMark::Staccatissimo => "staccatissimo",
            ArticulationMark::Tenuto => "tenuto",
            ArticulationMark::DetachedLegato => "detached legato",
            ArticulationMark::Marcato => "marcato",
            ArticulationMark::Fermata => "fermata",
            ArticulationMark::ShortFermata => "short fermata",
            ArticulationMark::LongFermata => "long fermata",
            ArticulationMark::BreathMark => "breath mark",
            ArticulationMark::Caesura => "caesura",
            ArticulationMark::UpBow => "up bow",
            ArticulationMark::DownBow => "down bow",
            ArticulationMark::Harmonic => "harmonic",
            ArticulationMark::OpenString => "open string",
            ArticulationMark::Stopped => "stopped",
            ArticulationMark::Pizzicato => "pizzicato",
            ArticulationMark::SnapPizzicato => "snap pizzicato",
            ArticulationMark::ThumbPosition => "thumb position",
            ArticulationMark::Pluck => "pluck",
            ArticulationMark::DoubleTongue => "double tongue",
            ArticulationMark::TripleTongue => "triple tongue",
            ArticulationMark::Heel => "heel",
            ArticulationMark::Toe => "toe",
            ArticulationMark::Spiccato => "spiccato",
            ArticulationMark::Scoop => "scoop",
            ArticulationMark::Plop => "plop",
            ArticulationMark::Doit => "doit",
            ArticulationMark::Falloff => "falloff",
            ArticulationMark::Stress => "stress",
            ArticulationMark::Unstress => "unstress",
            ArticulationMark::StringHarmonic => "string harmonic",
            ArticulationMark::Fret(_) => "fret",
            ArticulationMark::StringNumber(_) => "string number",
            ArticulationMark::HandbellIndication => "handbell indication",
            ArticulationMark::HarpFingerNails => "harp fingernails",
            ArticulationMark::WoodwindIndication => "woodwind indication",
            ArticulationMark::BrassIndication => "brass indication",
        }
    }

    /// The fret number, for a `Fret` marking.
    pub fn fret_number(&self) -> Option<u8> {
        match self {
            ArticulationMark::Fret(n) => Some(*n),
            _ => None,
        }
    }

    /// The string number, for a `StringNumber` marking.
    pub fn string_number(&self) -> Option<u8> {
        match self {
            ArticulationMark::StringNumber(n) => Some(*n),
            _ => None,
        }
    }

    /// Additive velocity shift (added directly to the base MIDI velocity,
    /// unlike `velocity_multiplier`'s proportional scaling), used
    /// alongside the multiplicative model rather than replacing it.
    /// Mirrors music21's `Accent`, whose accent effect is modeled
    /// additively — a fixed boost sounds musically correct at both a
    /// quiet base dynamic (where a multiplier alone barely moves the
    /// needle) and a loud one (where a multiplier alone can overshoot).
    pub fn volume_shift(&self) -> i8 {
        match self {
            ArticulationMark::Accent => 16,
            ArticulationMark::StrongAccent | ArticulationMark::Marcato => 24,
            _ => 0,
        }
    }

    /// Get the velocity multiplier used by `Volume::get_realized`
    pub fn velocity_multiplier(&self) -> f64 {
        match self {
            ArticulationMark::Accent => 1.2,
            ArticulationMark::StrongAccent | ArticulationMark::Marcato => 1.4,
            ArticulationMark::Staccato => 0.9,
            ArticulationMark::Staccatissimo => 1.1,
            ArticulationMark::Tenuto => 1.0,
            ArticulationMark::DetachedLegato => 0.95,
            _ => 1.0,
        }
    }

    /// Get the duration multiplier used by `Note::realized_quarter_length`
    pub fn duration_multiplier(&self) -> f64 {
        match self {
            ArticulationMark::Staccato => 0.5,
            ArticulationMark::Staccatissimo => 0.25,
            ArticulationMark::Tenuto => 1.0,
            ArticulationMark::DetachedLegato => 0.75,
            _ => 1.0,
        }
    }

    /// Check if this affects note duration
    pub fn affects_duration(&self) -> bool {
        matches!(
            self,
            ArticulationMark::Staccato
                | ArticulationMark::Staccatissimo
                | ArticulationMark::DetachedLegato
        )
    }

    /// Check if this is a fermata type
    pub fn is_fermata(&self) -> bool {
        matches!(
            self,
            ArticulationMark::Fermata
                | ArticulationMark::ShortFermata
                | ArticulationMark::LongFermata
        )
    }
}

impl fmt::Display for ArticulationMark {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name())
    }
}

/// An articulation marking
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Articulation {
    /// Articulation type
    pub type_: ArticulationMark,
    /// Placement (above/below)
    pub placement: Option<Placement>,
}

impl Articulation {
    /// Create a new articulation
    pub fn new(type_: ArticulationMark) -> Self {
        Self {
            type_,
            placement: None,
        }
    }

    /// Create a staccato
    pub fn staccato() -> Self {
        Self::new(ArticulationMark::Staccato)
    }

    /// Create an accent
    pub fn accent() -> Self {
        Self::new(ArticulationMark::Accent)
    }

    /// Create a tenuto
    pub fn tenuto() -> Self {
        Self::new(ArticulationMark::Tenuto)
    }

    /// Create a fermata
    pub fn fermata() -> Self {
        Self::new(ArticulationMark::Fermata)
    }
}

/// Expression type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ExpressionType {
    Trill,
    Turn,
    InvertedTurn,
    Mordent,
    InvertedMordent,
    Tremolo,
    Vibrato,
    Glissando,
    Slide,
    ArpeggioUp,
    ArpeggioDown,
}

/// An expression marking
#[derive(Debug, Clone, PartialEq)]
pub struct Expression {
    /// Expression type
    pub type_: ExpressionType,
    /// Placement (above/below)
    pub placement: Option<Placement>,
}

impl Expression {
    /// Create a new expression
    pub fn new(type_: ExpressionType) -> Self {
        Self {
            type_,
            placement: None,
        }
    }

    /// Create a trill
    pub fn trill() -> Self {
        Self::new(ExpressionType::Trill)
    }

    /// Create a mordent
    pub fn mordent() -> Self {
        Self::new(ExpressionType::Mordent)
    }
}

/// Syllabic type for lyrics
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Syllabic {
    #[default]
    Single,
    Begin,
    Middle,
    End,
}

/// A lyric syllable
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Lyric {
    /// The lyric text
    pub text: String,
    /// Syllabic type
    pub syllabic: Syllabic,
    /// Verse number (1-indexed)
    pub number: u8,
    /// Optional verse identifier/name (distinct from the numeric
    /// `number`), e.g. "chorus" or "refrain".
    pub identifier: Option<String>,
}

impl Lyric {
    /// Create a new lyric
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            syllabic: Syllabic::Single,
            number: 1,
            identifier: None,
        }
    }

    /// Set the syllabic type
    pub fn with_syllabic(mut self, syllabic: Syllabic) -> Self {
        self.syllabic = syllabic;
        self
    }

    /// Set the verse number
    pub fn with_number(mut self, number: u8) -> Self {
        self.number = number;
        self
    }

    /// Set the verse identifier/name.
    pub fn with_identifier(mut self, identifier: impl Into<String>) -> Self {
        self.identifier = Some(identifier.into());
        self
    }

    /// Get the verse identifier/name.
    pub fn identifier(&self) -> Option<&str> {
        self.identifier.as_deref()
    }

    /// Parse raw hyphenated lyric text into one or more syllable-tagged
    /// `Lyric`s, e.g. `"con-tra-ry"` -> `[Begin("con"), Middle("tra"),
    /// End("ry")]`. Text with no hyphens produces a single
    /// `Syllabic::Single` lyric. Mirrors music21's `Lyric.setTextAndSyllabic`
    /// raw-text auto-syllabification.
    pub fn from_raw_text(raw_text: &str, number: u8) -> Vec<Lyric> {
        let parts: Vec<&str> = raw_text.split('-').collect();
        if parts.len() == 1 {
            vec![Lyric::new(raw_text).with_number(number)]
        } else {
            let last = parts.len() - 1;
            parts
                .into_iter()
                .enumerate()
                .map(|(i, part)| {
                    let syllabic = if i == 0 {
                        Syllabic::Begin
                    } else if i == last {
                        Syllabic::End
                    } else {
                        Syllabic::Middle
                    };
                    Lyric::new(part).with_syllabic(syllabic).with_number(number)
                })
                .collect()
        }
    }
}

/// Whether a set of lyrics (e.g. as produced by `Lyric::from_raw_text`)
/// represents a composite (multi-syllable) lyric line rather than a single
/// whole-word lyric.
pub fn is_composite_lyric_set(lyrics: &[Lyric]) -> bool {
    lyrics.len() > 1
}

/// Volume/velocity information
#[derive(Debug, Clone, PartialEq)]
pub struct Volume {
    /// MIDI velocity (0-127)
    pub velocity: u8,
    /// Volume scalar (0.0-1.0)
    pub realized_volume: f64,
}

impl Volume {
    /// Create from MIDI velocity
    pub fn from_velocity(velocity: u8) -> Self {
        Self {
            velocity,
            realized_volume: velocity as f64 / 127.0,
        }
    }

    /// Create from scalar (0.0-1.0)
    pub fn from_scalar(scalar: f64) -> Self {
        Self {
            velocity: (scalar.clamp(0.0, 1.0) * 127.0) as u8,
            realized_volume: scalar.clamp(0.0, 1.0),
        }
    }

    /// Default mezzo-forte velocity
    pub fn mf() -> Self {
        Self::from_velocity(80)
    }

    /// Compute the realized (effective) volume after applying the given
    /// articulations' velocity multipliers (e.g. an accent boosts velocity).
    /// Multipliers compound multiplicatively across all attached
    /// articulations and the result is clamped to the valid MIDI velocity
    /// range. Use `Note::realized_volume` to apply a note's own
    /// articulations automatically.
    pub fn get_realized(&self, articulations: &[Articulation]) -> Volume {
        let multiplier: f64 = articulations
            .iter()
            .map(|a| a.type_.velocity_multiplier())
            .product();
        let shift: i32 = articulations.iter().map(|a| a.type_.volume_shift() as i32).sum();
        let shifted = (self.velocity as f64 * multiplier).round() as i32 + shift;
        Volume::from_velocity(shifted.clamp(0, 127) as u8)
    }
}

impl Default for Volume {
    fn default() -> Self {
        Self::mf()
    }
}

/// A musical note (pitch + duration)
#[derive(Debug, Clone)]
pub struct Note {
    /// The pitch
    pitch: Pitch,
    /// The duration
    duration: Duration,
    /// Offset within the stream (in quarter lengths)
    offset: Fraction,
    /// Tie information
    tie: Option<Tie>,
    /// Lyrics
    lyrics: Vec<Lyric>,
    /// Articulations
    articulations: Vec<Articulation>,
    /// Expressions
    expressions: Vec<Expression>,
    /// Volume/velocity
    volume: Volume,
    /// Notehead
    notehead: NoteHead,
    /// Stem direction
    stem_direction: StemDirection,
    /// Whether this is a grace note, and if so, whether it is slashed
    /// (`Some(true)` = acciaccatura/short grace, `Some(false)` =
    /// appoggiatura/long grace). `None` means this is not a grace note.
    grace_slash: Option<bool>,
}

impl Note {
    /// Create a new note
    pub fn new(pitch: Pitch, duration: Duration) -> Self {
        Self {
            pitch,
            duration,
            offset: Fraction::new(0, 1),
            tie: None,
            lyrics: Vec::new(),
            articulations: Vec::new(),
            expressions: Vec::new(),
            volume: Volume::default(),
            notehead: NoteHead::default(),
            stem_direction: StemDirection::default(),
            grace_slash: None,
        }
    }

    /// Create a note from pitch string and duration type
    pub fn from_str(pitch: &str, duration: Duration) -> Result<Self, super::ParseError> {
        Ok(Self::new(pitch.parse()?, duration))
    }

    /// Create a quarter note
    pub fn quarter(pitch: Pitch) -> Self {
        Self::new(pitch, Duration::quarter())
    }

    /// Create a half note
    pub fn half(pitch: Pitch) -> Self {
        Self::new(pitch, Duration::half())
    }

    /// Create a whole note
    pub fn whole(pitch: Pitch) -> Self {
        Self::new(pitch, Duration::whole())
    }

    /// Create an eighth note
    pub fn eighth(pitch: Pitch) -> Self {
        Self::new(pitch, Duration::eighth())
    }

    /// Get the pitch
    pub fn pitch(&self) -> &Pitch {
        &self.pitch
    }

    /// Get mutable pitch
    pub fn pitch_mut(&mut self) -> &mut Pitch {
        &mut self.pitch
    }

    /// Set the pitch
    pub fn set_pitch(&mut self, pitch: Pitch) {
        self.pitch = pitch;
    }

    /// Get the duration
    pub fn duration(&self) -> &Duration {
        &self.duration
    }

    /// Get mutable duration
    pub fn duration_mut(&mut self) -> &mut Duration {
        &mut self.duration
    }

    /// Set the duration
    pub fn set_duration(&mut self, duration: Duration) {
        self.duration = duration;
    }

    /// Get the offset
    pub fn offset(&self) -> Fraction {
        self.offset
    }

    /// Set the offset
    pub fn set_offset(&mut self, offset: Fraction) {
        self.offset = offset;
    }

    /// Get the quarter length (convenience for duration.quarter_length())
    pub fn quarter_length(&self) -> Fraction {
        self.duration.quarter_length()
    }

    /// Get the MIDI note number
    pub fn midi(&self) -> u8 {
        self.pitch.midi()
    }

    /// Get the name (e.g., "C4")
    pub fn name(&self) -> String {
        self.pitch.name_with_octave()
    }

    /// Get the tie
    pub fn tie(&self) -> Option<&Tie> {
        self.tie.as_ref()
    }

    /// Set the tie
    pub fn set_tie(&mut self, tie: Option<Tie>) {
        self.tie = tie;
    }

    /// Get the lyrics
    pub fn lyrics(&self) -> &[Lyric] {
        &self.lyrics
    }

    /// Add a lyric
    pub fn add_lyric(&mut self, lyric: Lyric) {
        self.lyrics.push(lyric);
    }

    /// Add a lyric from text
    pub fn add_lyric_text(&mut self, text: impl Into<String>) {
        self.lyrics.push(Lyric::new(text));
    }

    /// Get the articulations
    pub fn articulations(&self) -> &[Articulation] {
        &self.articulations
    }

    /// Add an articulation
    pub fn add_articulation(&mut self, articulation: Articulation) {
        self.articulations.push(articulation);
    }

    /// Get the expressions
    pub fn expressions(&self) -> &[Expression] {
        &self.expressions
    }

    /// Add an expression
    pub fn add_expression(&mut self, expression: Expression) {
        self.expressions.push(expression);
    }

    /// Get the volume
    pub fn volume(&self) -> &Volume {
        &self.volume
    }

    /// Set the volume
    pub fn set_volume(&mut self, volume: Volume) {
        self.volume = volume;
    }

    /// Set the velocity
    pub fn set_velocity(&mut self, velocity: u8) {
        self.volume = Volume::from_velocity(velocity);
    }

    /// Get the realized (effective) volume after applying this note's own
    /// attached articulations' velocity multipliers (see
    /// `Volume::get_realized`).
    pub fn realized_volume(&self) -> Volume {
        self.volume.get_realized(&self.articulations)
    }

    /// Get the realized (effective) quarter-note length after applying this
    /// note's attached articulations' duration multipliers (e.g. staccato
    /// shortens the sounding duration without changing the notated
    /// duration returned by `duration()`).
    pub fn realized_quarter_length(&self) -> f64 {
        let multiplier: f64 = self
            .articulations
            .iter()
            .map(|a| a.type_.duration_multiplier())
            .product();
        self.duration.quarter_length_f64() * multiplier
    }

    /// Get the notehead
    pub fn notehead(&self) -> &NoteHead {
        &self.notehead
    }

    /// Set the notehead
    pub fn set_notehead(&mut self, notehead: NoteHead) {
        self.notehead = notehead;
    }

    /// Get the stem direction
    pub fn stem_direction(&self) -> StemDirection {
        self.stem_direction
    }

    /// Set the stem direction
    pub fn set_stem_direction(&mut self, direction: StemDirection) {
        self.stem_direction = direction;
    }

    /// Check if this is a grace note
    pub fn is_grace(&self) -> bool {
        self.grace_slash.is_some()
    }

    /// Whether this grace note is slashed (acciaccatura, `Some(true)`) or
    /// unslashed (appoggiatura, `Some(false)`); `None` if this isn't a
    /// grace note at all.
    pub fn is_grace_slashed(&self) -> Option<bool> {
        self.grace_slash
    }

    /// Convert to a slashed grace note (acciaccatura, the common "short"
    /// grace note). Use `to_appoggiatura` for an unslashed grace note.
    pub fn to_grace(&self) -> Note {
        let mut grace = self.clone();
        grace.grace_slash = Some(true);
        grace.duration = Duration::zero();
        grace
    }

    /// Convert to an unslashed appoggiatura-style grace note, which
    /// traditionally steals notated time from the note it precedes (unlike
    /// a slashed acciaccatura, which is not counted against the beat).
    pub fn to_appoggiatura(&self) -> Note {
        let mut grace = self.clone();
        grace.grace_slash = Some(false);
        grace.duration = Duration::zero();
        grace
    }

    /// Transpose the note
    pub fn transpose(&self, interval: &Interval) -> Note {
        let mut transposed = self.clone();
        transposed.pitch = self.pitch.transpose(interval);
        transposed
    }

    /// Transpose by semitones
    pub fn transpose_semitones(&self, semitones: i32) -> Note {
        let mut transposed = self.clone();
        transposed.pitch = self.pitch.transpose_semitones(semitones);
        transposed
    }

    /// Transpose by an interval given as a string (e.g. "P5", "-m3"),
    /// parsed via `Interval`'s `FromStr` implementation.
    pub fn transpose_str(&self, interval: &str) -> Result<Note, super::ParseError> {
        let interval: Interval = interval.parse()?;
        Ok(self.transpose(&interval))
    }

    /// Get this note's pitch as a single-element vector, for writing code
    /// that treats `Note` and `Chord` uniformly (a `Chord` has multiple
    /// pitches; a `Note` always has exactly one).
    pub fn pitches(&self) -> Vec<&Pitch> {
        vec![&self.pitch]
    }

    /// Get a human-readable summary of this note, e.g.
    /// "C-sharp in octave 4 Quarter Note".
    pub fn full_name(&self) -> String {
        format!(
            "{} in octave {} {}",
            self.pitch.name(),
            self.pitch.implicit_octave(),
            self.duration.full_name()
        )
    }

    /// Scale the duration
    pub fn augment_or_diminish(&self, scalar: Fraction) -> Note {
        let mut scaled = self.clone();
        scaled.duration = self.duration.augment_or_diminish(scalar);
        scaled
    }
}

impl Default for Note {
    fn default() -> Self {
        Self::new(Pitch::default(), Duration::default())
    }
}

impl fmt::Display for Note {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {}", self.pitch, self.duration)
    }
}

impl PartialEq for Note {
    fn eq(&self, other: &Self) -> bool {
        self.pitch == other.pitch && self.duration == other.duration
    }
}

impl Eq for Note {}

impl PartialOrd for Note {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Note {
    fn cmp(&self, other: &Self) -> Ordering {
        self.pitch.cmp(&other.pitch)
    }
}

/// A percussion-style note with no real pitch: only a display position on
/// the staff (e.g. for a specific drum/percussion instrument line) and a
/// duration. Mirrors music21's `note.Unpitched`.
#[derive(Debug, Clone, PartialEq)]
pub struct Unpitched {
    /// Staff display position (in the same line/space units as a staff
    /// position; 0 = middle line).
    display_position: i8,
    /// Duration
    duration: Duration,
}

impl Unpitched {
    /// Create a new unpitched note with the given duration, displayed on
    /// the middle line by default.
    pub fn new(duration: Duration) -> Self {
        Self {
            display_position: 0,
            duration,
        }
    }

    /// Get the staff display position.
    pub fn display_position(&self) -> i8 {
        self.display_position
    }

    /// Set the staff display position.
    pub fn set_display_position(&mut self, position: i8) {
        self.display_position = position;
    }

    /// Get the duration.
    pub fn duration(&self) -> &Duration {
        &self.duration
    }

    /// Set the duration.
    pub fn set_duration(&mut self, duration: Duration) {
        self.duration = duration;
    }

    /// Get the quarter length.
    pub fn quarter_length(&self) -> Fraction {
        self.duration.quarter_length()
    }

    /// Human-readable display name — there's no real pitch, so this
    /// describes the staff position instead (mirroring music21's
    /// `Unpitched.displayName`).
    pub fn display_name(&self) -> String {
        format!("staff position {}", self.display_position)
    }
}

impl fmt::Display for Unpitched {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Unpitched({})", self.display_name())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::Step;

    #[test]
    fn test_note_creation() {
        let pitch = Pitch::from_parts(Step::C, Some(4), None);
        let note = Note::quarter(pitch);

        assert_eq!(note.midi(), 60);
        assert_eq!(note.quarter_length(), Fraction::new(1, 1));
    }

    #[test]
    fn test_note_transpose() {
        let pitch = Pitch::from_parts(Step::C, Some(4), None);
        let note = Note::quarter(pitch);
        let transposed = note.transpose(&Interval::perfect_fifth());

        assert_eq!(transposed.pitch().step(), Step::G);
        assert_eq!(transposed.pitch().octave(), Some(4));
    }

    #[test]
    fn test_note_articulations() {
        let pitch = Pitch::from_parts(Step::C, Some(4), None);
        let mut note = Note::quarter(pitch);
        note.add_articulation(Articulation::staccato());
        note.add_articulation(Articulation::accent());

        assert_eq!(note.articulations().len(), 2);
    }

    #[test]
    fn test_articulation_metadata_reachable_from_note() {
        // Regression test: before unifying the two Articulation enums,
        // Note::add_articulation only accepted the poorer
        // core::note::ArticulationType, so the richer metadata (symbols,
        // velocity/duration multipliers) on notation::articulation::
        // ArticulationMark was never reachable from an actual Note.
        let pitch = Pitch::from_parts(Step::C, Some(4), None);
        let mut note = Note::quarter(pitch);
        note.add_articulation(Articulation::staccato());

        assert_eq!(note.articulations()[0].type_.symbol(), ".");
        assert!(note.articulations()[0].type_.affects_duration());
    }

    #[test]
    fn test_volume_get_realized_applies_velocity_multiplier() {
        let volume = Volume::from_velocity(50);
        let accented = volume.get_realized(&[Articulation::accent()]);
        // Accent has a 1.2x velocity multiplier plus a +16 additive
        // volume_shift (50 * 1.2 = 60, + 16 = 76).
        assert_eq!(accented.velocity, 76);

        let unarticulated = volume.get_realized(&[]);
        assert_eq!(unarticulated.velocity, 50);
    }

    #[test]
    fn test_note_realized_volume_and_duration() {
        let pitch = Pitch::from_parts(Step::C, Some(4), None);
        let mut note = Note::quarter(pitch);
        note.set_velocity(100);
        note.add_articulation(Articulation::staccato());

        // Staccato: 0.9x velocity, 0.5x duration.
        assert_eq!(note.realized_volume().velocity, 90);
        assert!((note.realized_quarter_length() - 0.5).abs() < 1e-9);
    }

    #[test]
    fn test_note_lyrics() {
        let pitch = Pitch::from_parts(Step::C, Some(4), None);
        let mut note = Note::quarter(pitch);
        note.add_lyric_text("la");

        assert_eq!(note.lyrics().len(), 1);
        assert_eq!(note.lyrics()[0].text, "la");
    }

    #[test]
    fn test_note_grace() {
        let pitch = Pitch::from_parts(Step::C, Some(4), None);
        let note = Note::quarter(pitch);
        let grace = note.to_grace();

        assert!(grace.is_grace());
        assert_eq!(grace.quarter_length(), Fraction::new(0, 1));
        assert_eq!(grace.is_grace_slashed(), Some(true));
    }

    #[test]
    fn test_note_appoggiatura() {
        let pitch = Pitch::from_parts(Step::C, Some(4), None);
        let note = Note::quarter(pitch);
        let appoggiatura = note.to_appoggiatura();

        assert!(appoggiatura.is_grace());
        assert_eq!(appoggiatura.is_grace_slashed(), Some(false));

        assert_eq!(note.is_grace_slashed(), None);
    }

    #[test]
    fn test_note_full_name() {
        let pitch = Pitch::from_parts(Step::C, Some(4), Some(crate::core::Accidental::Sharp));
        let note = Note::quarter(pitch);
        assert_eq!(note.full_name(), "C# in octave 4 quarter");
    }

    #[test]
    fn test_note_transpose_str() {
        let pitch = Pitch::from_parts(Step::C, Some(4), None);
        let note = Note::quarter(pitch);
        let transposed = note.transpose_str("P5").unwrap();
        assert_eq!(transposed.pitch().step(), Step::G);

        assert!(note.transpose_str("bogus").is_err());
    }

    #[test]
    fn test_note_pitches_accessor() {
        let pitch = Pitch::from_parts(Step::D, Some(4), None);
        let note = Note::quarter(pitch);
        let pitches = note.pitches();
        assert_eq!(pitches.len(), 1);
        assert_eq!(pitches[0].step(), Step::D);
    }

    #[test]
    fn test_unpitched() {
        let mut unpitched = Unpitched::new(Duration::quarter());
        assert_eq!(unpitched.display_position(), 0);
        assert_eq!(unpitched.quarter_length(), Fraction::new(1, 1));

        unpitched.set_display_position(3);
        assert_eq!(unpitched.display_position(), 3);
        assert_eq!(unpitched.display_name(), "staff position 3");
    }

    #[test]
    fn test_lyric_from_raw_text_single_syllable() {
        let lyrics = Lyric::from_raw_text("hello", 1);
        assert_eq!(lyrics.len(), 1);
        assert_eq!(lyrics[0].syllabic, Syllabic::Single);
        assert!(!is_composite_lyric_set(&lyrics));
    }

    #[test]
    fn test_lyric_from_raw_text_multi_syllable() {
        let lyrics = Lyric::from_raw_text("con-tra-ry", 2);
        assert_eq!(lyrics.len(), 3);
        assert_eq!(lyrics[0].text, "con");
        assert_eq!(lyrics[0].syllabic, Syllabic::Begin);
        assert_eq!(lyrics[1].text, "tra");
        assert_eq!(lyrics[1].syllabic, Syllabic::Middle);
        assert_eq!(lyrics[2].text, "ry");
        assert_eq!(lyrics[2].syllabic, Syllabic::End);
        assert!(lyrics.iter().all(|l| l.number == 2));
        assert!(is_composite_lyric_set(&lyrics));
    }

    #[test]
    fn test_lyric_identifier() {
        let lyric = Lyric::new("la").with_identifier("chorus");
        assert_eq!(lyric.identifier(), Some("chorus"));
    }
}