use crate::frac::Q;
pub fn is_music(c: u32) -> bool {
(0xE000..=0xF8FF).contains(&c)
}
pub fn head(c: u32) -> Option<Q> {
match c {
0xE0A0 => Some(Q::int(8)),
0xE0A2 => Some(Q::int(4)),
0xE0A3 => Some(Q::int(2)),
0xE0A4 => Some(Q::ONE),
0xF4BC => Some(Q::int(4)),
0xF4BD => Some(Q::int(2)),
0xF4BE => Some(Q::ONE),
0xE0A7 => Some(Q::int(4)),
0xE0A8 => Some(Q::int(2)),
0xE0A9 => Some(Q::ONE),
_ => None,
}
}
pub fn head_shape(c: u32) -> Option<&'static str> {
match c {
0xE0A7..=0xE0A9 => Some("x"),
_ => None,
}
}
pub fn rest(c: u32) -> Option<Q> {
match c {
0xE4E2 => Some(Q::int(8)),
0xE4E3 => Some(Q::int(4)),
0xE4E4 => Some(Q::int(2)),
0xE4E5 => Some(Q::ONE),
0xE4E6 => Some(Q::new(1, 2)),
0xE4E7 => Some(Q::new(1, 4)),
0xE4E8 => Some(Q::new(1, 8)),
0xE4E9 => Some(Q::new(1, 16)),
_ => None,
}
}
pub fn flag(c: u32) -> Option<u32> {
match c {
0xE240..=0xE247 => Some((c - 0xE240) / 2 + 1),
_ => None,
}
}
pub fn is_added_flag(c: u32) -> bool {
c == 0xE250 || c == 0xE251
}
pub fn accidental(c: u32) -> Option<i32> {
match c {
0xE260 => Some(-1),
0xE261 => Some(0),
0xE262 => Some(1),
0xE263 => Some(2),
0xE264 => Some(-2),
_ => None,
}
}
pub fn is_paren(c: u32) -> bool {
c == 0xE26A || c == 0xE26B
}
pub const DOT: u32 = 0xE1E7;
pub fn clef(c: u32) -> Option<&'static str> {
match c {
0xE050 | 0xE07A => Some("treble"),
0xE052 | 0xE055 | 0xE056 | 0xE057 => Some("treble8"),
0xE053 => Some("treble8va"),
0xE062 | 0xE07C => Some("bass"),
0xE064 => Some("bass8"),
0xE065 => Some("bass8va"),
0xE05C | 0xE07B => Some("alto"),
0xE069 | 0xE06A => Some("percussion"),
_ => None,
}
}
pub fn clef_on(c: u32, line: f64) -> Option<&'static str> {
let usual = clef(c)?;
let on = |n: f64| (line - n).abs() < 0.25;
Some(match usual {
"alto" if on(1.0) => "soprano",
"alto" if on(2.0) => "mezzo",
"alto" if on(4.0) => "tenor",
"alto" if on(5.0) => "baritoneC",
"treble" if on(1.0) => "french",
"bass" if on(3.0) => "baritone",
u => u,
})
}
pub fn clef_bottom(clef: &str) -> i32 {
let (letter, octave) = match clef {
"treble" | "percussion" => (2, 4), "treble8" => (2, 3),
"treble8va" => (2, 5),
"french" => (4, 4), "bass" => (4, 2), "bass8" => (4, 1),
"bass8va" => (4, 3),
"baritone" | "baritoneC" => (6, 2), "alto" => (3, 3), "soprano" => (0, 4), "mezzo" => (5, 3), _ => (1, 3), };
octave * 7 + letter
}
pub fn time_digit(c: u32) -> Option<u32> {
(0xE080..=0xE089).contains(&c).then(|| c - 0xE080)
}
pub const COMMON: u32 = 0xE08A;
pub const CUT: u32 = 0xE08B;
pub fn is_time(c: u32) -> bool {
time_digit(c).is_some() || c == COMMON || c == CUT
}
pub fn dynamic_letter(c: u32) -> Option<char> {
match c {
0xE520 => Some('p'),
0xE521 => Some('m'),
0xE522 => Some('f'),
0xE523 => Some('r'),
0xE524 => Some('s'),
0xE525 => Some('z'),
0xE526 => Some('n'),
_ => None,
}
}
pub fn dynamic_word(c: u32) -> Option<&'static str> {
Some(match c {
0xE529 => "pppp",
0xE52A => "ppp",
0xE52B => "pp",
0xE52C => "mp",
0xE52D => "mf",
0xE52E => "pf",
0xE52F => "ff",
0xE530 => "fff",
0xE531 => "ffff",
0xE534 => "fp",
0xE535 => "fz",
0xE536 => "sf",
0xE537 => "sfp",
0xE538 => "sfpp",
0xE539 => "sfz",
0xE53B => "sffz",
0xE53C => "rf",
0xE53D => "rfz",
_ => return None,
})
}
pub fn dynamic_text(c: u32) -> Option<String> {
dynamic_letter(c)
.map(String::from)
.or_else(|| dynamic_word(c).map(String::from))
}
pub fn fermata(c: u32) -> Option<&'static str> {
match c {
0xE4C0 => Some("-fermata"),
0xE4C1 => Some("-fermatabelow"),
0xE4C4 | 0xE4C5 => Some("-shortfermata"),
0xE4C6 | 0xE4C7 => Some("-longfermata"),
_ => None,
}
}
pub fn is_arpeggio(c: u32) -> bool {
matches!(c, 0xE63C | 0xE63D | 0xE634 | 0xE635 | 0xEAA9..=0xEAAE)
}
pub fn articulation(c: u32) -> Option<&'static str> {
match c {
0xE4A2 | 0xE4A3 => Some("-."),
0xE4A0 | 0xE4A1 => Some("->"),
0xE4A4 | 0xE4A5 => Some("-_"),
0xE4AC | 0xE4AD => Some("-^"),
0xE4A6..=0xE4A9 => Some("-'"),
_ => None,
}
}
pub fn ornament(c: u32) -> Option<&'static str> {
match c {
0xE566 => Some("-tr"),
0xE567 | 0xE568 => Some("-turn"),
0xE56C => Some("-prall"),
0xE56D => Some("-mordent"),
0xE4CE | 0xE4CF => Some("-breath"),
0xE63C | 0xE63D | 0xE634 | 0xE635 | 0xEAA9..=0xEAAE => Some("-arp"),
0xE220 => Some("-trem1"),
0xE221 => Some("-trem2"),
0xE222 => Some("-trem3"),
0xE223 => Some("-trem4"),
_ => None,
}
}
pub fn is_repeat_dot(c: u32) -> bool {
c == 0xE043 || c == 0xE044
}
pub fn is_tuplet_digit(c: u32) -> bool {
(0xE880..=0xE889).contains(&c)
}
pub const STEPS: [char; 7] = ['c', 'd', 'e', 'f', 'g', 'a', 'b'];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bravura_s_own_heads_are_heads() {
assert_eq!(head(0xF4BC), Some(Q::int(4)));
assert_eq!(head(0xF4BD), Some(Q::int(2)));
assert_eq!(head(0xF4BE), Some(Q::ONE));
}
#[test]
fn a_treble_clef_with_an_eight_under_it_is_an_octave_down() {
assert_eq!(clef(0xE052), Some("treble8"));
assert_eq!(clef(0xE057), Some("treble8"));
}
#[test]
fn a_c_clef_is_named_by_the_line_it_is_set_on() {
assert_eq!(clef_on(0xE05C, 3.0), Some("alto"));
assert_eq!(clef_on(0xE05C, 4.02), Some("tenor"));
assert_eq!(clef_on(0xE07B, 3.98), Some("tenor"));
assert_eq!(clef_on(0xE05C, 1.0), Some("soprano"));
assert_eq!(clef_on(0xE05C, 2.0), Some("mezzo"));
for (name, line) in [("soprano", 1), ("mezzo", 2), ("alto", 3), ("tenor", 4)] {
assert_eq!(clef_bottom(name) + 2 * (line - 1), 4 * 7, "{name}");
}
assert_eq!(clef_on(0xE05C, 2.5), Some("alto"));
assert_eq!(clef_on(0xE050, 2.0), Some("treble"));
assert_eq!(clef_on(0xE062, 4.0), Some("bass"));
assert_eq!(clef_on(0xE0A4, 2.0), None);
}
#[test]
fn clefs_are_read_at_their_pitches_by_their_own_names() {
assert_eq!(clef_on(0xE050, 1.0), Some("french"));
assert_eq!(clef_bottom("french"), 4 * 7 + 4);
assert_eq!(clef_on(0xE062, 3.0), Some("baritone"));
assert_eq!(clef_bottom("baritone") + 4, 3 * 7 + 3);
assert_eq!(clef(0xE053), Some("treble8va"));
assert_eq!(clef_bottom("treble8va"), clef_bottom("treble") + 7);
assert_eq!(clef_bottom("bass8"), clef_bottom("bass") - 7);
assert_eq!(clef_on(0xE069, 3.0), Some("percussion"));
assert_eq!(clef_bottom("percussion"), clef_bottom("treble"));
let drums = "part D \"Drums\"
time 4/4
clef D percussion
bar 1
D: c5/4 f4 g5-head:x c5
";
assert!(fm_transcribe::compile(drums)
.unwrap()
.musicxml
.contains("<unpitched>"));
for name in [
"treble",
"treble8",
"treble8va",
"bass",
"bass8",
"bass8va",
"alto",
"tenor",
"soprano",
"mezzo",
"baritoneC",
"french",
"baritone",
] {
let text = format!(
"part V \"V\" voice\ntime 4/4\nclef V {name}\nbar 1\nV: \\clef:{name} c4/1\n"
);
fm_transcribe::compile(&text).unwrap();
}
}
#[test]
fn fermatas_and_arpeggios_by_their_glyphs() {
assert_eq!(fermata(0xE4C0), Some("-fermata"));
assert_eq!(fermata(0xE4C1), Some("-fermatabelow"));
assert_eq!(fermata(0xE4C6), Some("-longfermata"));
assert_eq!(fermata(0xE4A2), None);
assert!(is_arpeggio(0xEAA9) && is_arpeggio(0xEAAD) && is_arpeggio(0xE63C));
assert_eq!(ornament(0xEAA9), Some("-arp"));
assert!(!is_arpeggio(0xEAA4));
for m in ["-fermatabelow", "-shortfermata", "-longfermata", "-arp"] {
let text = format!("part V \"V\" voice\ntime 4/4\nbar 1\nV: <c4 e4>/1{m}\n");
fm_transcribe::compile(&text).unwrap();
}
}
#[test]
fn x_heads_are_heads() {
assert_eq!(head_shape(0xE0A9), Some("x"));
assert_eq!(head_shape(0xE0A4), None);
assert_eq!(head(0xE0A7), Some(Q::int(4)));
assert_eq!(head(0xE0A8), Some(Q::int(2)));
assert_eq!(head(0xE0A9), Some(Q::ONE));
}
}