summaryrefslogtreecommitdiff
path: root/morphology-img/src/note.rs
diff options
context:
space:
mode:
Diffstat (limited to 'morphology-img/src/note.rs')
-rw-r--r--morphology-img/src/note.rs156
1 files changed, 156 insertions, 0 deletions
diff --git a/morphology-img/src/note.rs b/morphology-img/src/note.rs
new file mode 100644
index 0000000..92c3310
--- /dev/null
+++ b/morphology-img/src/note.rs
@@ -0,0 +1,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(&notes)
+}