diff options
| author | jacksonfu14 <jackson@fashionunited.com> | 2025-11-16 16:21:37 +0100 |
|---|---|---|
| committer | Jackson Moore <jacksonmoore@tuta.io> | 2025-11-16 16:21:37 +0100 |
| commit | 3505cc56367feabc60aaffe5e2b899d4d5d00531 (patch) | |
| tree | 923921a2113ccb8ad9bac1af0a7c8ab13fa5cd8e /morphology-img/src | |
| parent | 6aeb5a91c42446b5ea68ec81dd01079a74787c96 (diff) | |
feature: convert sequences to ly markup
Diffstat (limited to 'morphology-img/src')
| -rw-r--r-- | morphology-img/src/lib.rs | 31 | ||||
| -rw-r--r-- | morphology-img/src/note.rs | 156 | ||||
| -rw-r--r-- | morphology-img/src/pitch.rs | 43 |
3 files changed, 184 insertions, 46 deletions
diff --git a/morphology-img/src/lib.rs b/morphology-img/src/lib.rs index 28dfc58..a8a4acd 100644 --- a/morphology-img/src/lib.rs +++ b/morphology-img/src/lib.rs @@ -1,9 +1,34 @@ +//! Converts meta-affine sequences to SVG visualizations via LilyPond markup. +//! +//! This library provides a public API for converting structures from the +//! morphology crate into SVG images. All LilyPond-specific implementation +//! details are kept internal. +//! +//! # Public API +//! +//! The primary function is [`sequence_to_markup`] which converts a +//! meta-affine `Sequence` directly to a LilyPond markup string. +//! +//! # Architecture +//! +//! - **Input**: Only meta-affine types (`Sequence`, `Value`) +//! - **Output**: File paths or markup strings +//! - **Hidden**: All LilyPond-specific types and implementation details + pub mod error; -pub mod pitch; + +#[doc(hidden)] +pub mod note; + +#[doc(hidden)] pub mod template; + +#[doc(hidden)] pub mod file_gen; + +#[doc(hidden)] pub mod render; -// Re-export key types +// Public API - only export what external clients should use pub use error::RenderError; -pub use pitch::{LilyPondPitch, LilyPondNote, Duration}; +pub use note::sequence_to_markup; 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(¬es) +} diff --git a/morphology-img/src/pitch.rs b/morphology-img/src/pitch.rs deleted file mode 100644 index dd7d87e..0000000 --- a/morphology-img/src/pitch.rs +++ /dev/null @@ -1,43 +0,0 @@ -/// 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 - } -} - -/// 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() - } -} |