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
|
/// Represents a LilyPond absolute pitch name (e.g., "c'", "bes,", "fis")
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LilyPondPitch(String);
impl LilyPondPitch {
pub fn new(s: impl Into<String>) -> Self {
Self(s.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
/// Converts an integer to a LilyPond absolute pitch name.
///
/// The mapping uses B (integer 0) as the reference pitch in the middle octave.
/// Positive integers represent diatonic steps upward, negative integers downward.
///
/// # Diatonic Scale
/// - 0 → b (reference)
/// - 1 → c' (next octave)
/// - -1 → a (same octave)
/// - 7 → b' (one octave up)
/// - -7 → b, (one octave down)
///
/// # Algorithm
/// The diatonic scale is C, D, E, F, G, A, B (7 notes).
/// B is at index 6, corresponding to n=0.
/// We adjust the input by 6 to convert to C-based indexing,
/// then use modular arithmetic to find the pitch class and octave.
pub fn integer_to_pitch_name(n: i32) -> LilyPondPitch {
const PITCH_NAMES: [&str; 7] = ["c", "d", "e", "f", "g", "a", "b"];
// B is at index 6, corresponds to n=0
// Adjust to C-based indexing
let adjusted = n + 6;
// Calculate pitch class (0-6) and octave
// rem_euclid and div_euclid handle negative numbers correctly
let pitch_index = adjusted.rem_euclid(7) as usize;
let octave = adjusted.div_euclid(7);
// Get base pitch name
let pitch_name = PITCH_NAMES[pitch_index];
// Add octave markers
let octave_marker = if octave > 0 {
"'".repeat(octave as usize)
} else if octave < 0 {
",".repeat((-octave) as usize)
} else {
String::new()
};
LilyPondPitch::new(format!("{}{}", pitch_name, octave_marker))
}
/// Represents a complete LilyPond note with duration (e.g., "c'4")
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LilyPondNote(String);
impl LilyPondNote {
pub fn new(pitch: LilyPondPitch, duration: Duration) -> Self {
Self(format!("{}{}", pitch.as_str(), duration.as_str()))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
/// Represents a note duration in LilyPond (e.g., "4", "8", "2")
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Duration(u8);
impl Duration {
pub const QUARTER: Duration = Duration(4);
pub fn new(value: u8) -> Self {
Self(value)
}
pub fn as_str(&self) -> String {
self.0.to_string()
}
}
/// Converts a meta-affine sequence to a vector of LilyPond notes.
///
/// All notes are assigned quarter note duration internally.
/// In future iterations, duration may be derived from additional meta-affine structures.
///
/// Transformation steps:
/// 1. Convert (outer, inner) pairs to integers: pitch = (outer * length) + inner
/// 2. Convert integers to pitch names using integer_to_pitch_name
/// 3. Append quarter note duration to create notes
///
/// # Arguments
/// - `sequence`: The meta-affine sequence to convert
pub fn sequence_to_notes(sequence: &meta_affine::Sequence) -> Vec<LilyPondNote> {
let length = sequence.length() as i32;
let duration = Duration::QUARTER;
sequence.values()
.iter()
.map(|value| {
let pitch_int = (value.outer * length) + value.inner;
let pitch = integer_to_pitch_name(pitch_int);
LilyPondNote::new(pitch, duration)
})
.collect()
}
/// Converts a vector of LilyPond notes into a space-separated string.
///
/// This function creates the LilyPond markup string that will be substituted
/// into the template .ly file.
pub fn notes_to_markup(notes: &[LilyPondNote]) -> String {
use itertools::Itertools;
notes.iter()
.map(|n| n.as_str())
.join(" ")
}
/// Converts a meta-affine sequence directly to LilyPond markup string.
///
/// This is the main public API for converting sequences to LilyPond markup.
/// It combines sequence_to_notes and notes_to_markup into a single operation.
///
/// # Arguments
/// - `sequence`: The meta-affine sequence to convert
///
/// # Returns
/// A space-separated string of LilyPond notes ready for template substitution
///
/// # Examples
/// ```
/// use meta_affine::{Sequence, Value};
/// use morphology_img::sequence_to_markup;
///
/// let seq = Sequence::new(vec![
/// Value::new(1, 0),
/// Value::new(-1, 1),
/// Value::new(-1, 0),
/// Value::new(0, 0),
/// ]);
///
/// let markup = sequence_to_markup(&seq);
/// assert_eq!(markup, "f'4 f4 e4 b4");
/// ```
pub fn sequence_to_markup(sequence: &meta_affine::Sequence) -> String {
let notes = sequence_to_notes(sequence);
notes_to_markup(¬es)
}
|