diff options
Diffstat (limited to 'morphology-img')
| -rw-r--r-- | morphology-img/src/file_gen.rs | 14 | ||||
| -rw-r--r-- | morphology-img/src/note.rs | 115 | ||||
| -rw-r--r-- | morphology-img/src/template.rs | 30 | ||||
| -rw-r--r-- | morphology-img/static/entry.ily | 9 | ||||
| -rw-r--r-- | morphology-img/static/template.ly | 6 | ||||
| -rw-r--r-- | morphology-img/tests/template.rs | 114 |
6 files changed, 199 insertions, 89 deletions
diff --git a/morphology-img/src/file_gen.rs b/morphology-img/src/file_gen.rs index dffb7bb..7bcae49 100644 --- a/morphology-img/src/file_gen.rs +++ b/morphology-img/src/file_gen.rs @@ -6,7 +6,7 @@ use tempfile::TempDir; use morphology::Sequence; use crate::error::RenderError; use crate::template; -use crate::note::{sequence_to_notes, notes_to_markup}; +use crate::note::{sequence_to_notes, notes_to_markup, sequence_to_staff, staff_to_scheme_string}; /// Generates a filename for a sequence with proper zero-padding. /// @@ -115,10 +115,16 @@ pub fn generate_ly_file( let notes = sequence_to_notes(sequence); // Convert notes to markup string - let markup = notes_to_markup(¬es); + let morpheme = notes_to_markup(¬es); - // Substitute into template - let ly_content = template::substitute_template(&markup)?; + // Convert sequence to staff line positions + let staff = sequence_to_staff(sequence); + + // Convert staff to Scheme list string + let rungs = staff_to_scheme_string(&staff); + + // Substitute both values into template + let ly_content = template::substitute_template(&morpheme, &rungs)?; // Write to file (UTF-8) fs::write(output_path, ly_content)?; diff --git a/morphology-img/src/note.rs b/morphology-img/src/note.rs index c2158a3..408884d 100644 --- a/morphology-img/src/note.rs +++ b/morphology-img/src/note.rs @@ -86,14 +86,33 @@ impl Duration { } } +/// Calculates the staff scaling factor for a sequence based on its inner values. +/// +/// The factor is computed as: max(abs(inner values)) + 3 +/// +/// # Arguments +/// - `sequence`: The meta-affine sequence +/// +/// # Returns +/// The factor value to use for pitch calculations +fn calculate_factor(sequence: &morphology::Sequence) -> i32 { + let max_abs_inner = sequence.values() + .iter() + .map(|value| value.inner.abs()) + .max() + .unwrap_or(0); + + max_abs_inner + 3 +} + /// 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 * multiplier) + inner -/// where multiplier = max(abs(inner values)) + 3 +/// 1. Convert (outer, inner) pairs to integers: pitch = (outer * factor) + inner +/// where factor = max(abs(inner values)) + 3 /// 2. Convert integers to pitch names using integer_to_pitch_name /// 3. Append quarter note duration to create notes /// @@ -101,26 +120,98 @@ impl Duration { /// - `sequence`: The meta-affine sequence to convert pub fn sequence_to_notes(sequence: &morphology::Sequence) -> Vec<LilyPondNote> { let duration = Duration::QUARTER; - - // Find the maximum absolute value of inner values - let max_abs_inner = sequence.values() - .iter() - .map(|value| value.inner.abs()) - .max() - .unwrap_or(0); - - let multiplier = max_abs_inner + 3; + let factor = calculate_factor(sequence); sequence.values() .iter() .map(|value| { - let pitch_int = (value.outer * multiplier) + value.inner; + let pitch_int = (value.outer * factor) + value.inner; let pitch = integer_to_pitch_name(pitch_int); LilyPondNote::new(pitch, duration) }) .collect() } +/// Converts a meta-affine sequence to staff line positions (rungs). +/// +/// This function computes the staff line positions based on the unique outer +/// values in the sequence, scaled by the same factor used for pitch calculation. +/// +/// # Algorithm +/// 1. Collect all unique outer values from the sequence +/// 2. Multiply each by the factor (max(abs(inner values)) + 3) +/// 3. Always include 0 in the result +/// 4. Return as a sorted vector +/// +/// # Arguments +/// - `sequence`: The meta-affine sequence to analyze +/// +/// # Returns +/// A sorted vector of staff line positions +/// +/// # Examples +/// ``` +/// use morphology::{Sequence, Value}; +/// use morphology_img::note::sequence_to_staff; +/// +/// let seq = Sequence::new(vec![ +/// Value::new(1, 0), +/// Value::new(-1, -1), +/// Value::new(-1, -2), +/// Value::new(-1, 0), +/// Value::new(0, 0), +/// ]); +/// +/// // Outer values: [1, -1, -1, -1, 0] -> unique: [1, -1, 0] +/// // max_abs_inner = 2, factor = 5 +/// // Rungs: [5, -5, 0] -> sorted: [-5, 0, 5] +/// let staff = sequence_to_staff(&seq); +/// assert_eq!(staff, vec![-5, 0, 5]); +/// ``` +pub fn sequence_to_staff(sequence: &morphology::Sequence) -> Vec<i32> { + use std::collections::HashSet; + + let factor = calculate_factor(sequence); + + // Collect unique outer values + let unique_outers: HashSet<i32> = sequence.values() + .iter() + .map(|value| value.outer) + .collect(); + + // Multiply each by the factor and include 0 + let mut rungs: Vec<i32> = unique_outers + .into_iter() + .map(|outer| outer * factor) + .collect(); + + // Sort the rungs + rungs.sort(); + + rungs +} + +/// Converts staff line positions to a LilyPond Scheme list string. +/// +/// # Arguments +/// - `rungs`: Vector of staff line positions +/// +/// # Returns +/// A string representing a Scheme list, e.g., "#'(-5 0 5)" +/// +/// # Examples +/// ``` +/// use morphology_img::note::staff_to_scheme_string; +/// +/// let rungs = vec![-5, 0, 5]; +/// assert_eq!(staff_to_scheme_string(&rungs), "#'(-5 0 5)"); +/// ``` +pub fn staff_to_scheme_string(rungs: &[i32]) -> String { + use itertools::Itertools; + + format!("#'({})", rungs.iter().map(|r| r.to_string()).join(" ")) +} + /// Converts a vector of LilyPond notes into a space-separated string. /// /// This function creates the LilyPond markup string that will be substituted diff --git a/morphology-img/src/template.rs b/morphology-img/src/template.rs index 15a47d5..e552843 100644 --- a/morphology-img/src/template.rs +++ b/morphology-img/src/template.rs @@ -8,7 +8,8 @@ const FORMAT_ILY: &str = include_str!("../static/format.ily"); const ENTRY_ILY: &str = include_str!("../static/entry.ily"); const ELEMENTS_ILY: &str = include_str!("../static/elements.ily"); -const PLACEHOLDER: &str = "%%%"; +const MORPHEME_PLACEHOLDER: &str = "%MORPHEME%"; +const RUNGS_PLACEHOLDER: &str = "%RUNGS%"; /// Returns the template .ly content pub fn get_template() -> &'static str { @@ -30,24 +31,35 @@ pub fn get_elements_ily() -> &'static str { ELEMENTS_ILY } -/// Substitutes the placeholder in the template with the provided markup. +/// Substitutes placeholders in the template with the provided values. /// /// # Arguments -/// - `markup`: The LilyPond markup to substitute into the template +/// - `morpheme`: The LilyPond morpheme markup to substitute +/// - `rungs`: The Scheme list string for staff line positions /// /// # Returns -/// The complete .ly file content with substitution applied +/// The complete .ly file content with substitutions applied /// /// # Errors -/// Returns `RenderError::Template` if the placeholder is not found -pub fn substitute_template(markup: &str) -> Result<String, RenderError> { +/// Returns `RenderError::Template` if placeholders are not found +pub fn substitute_template(morpheme: &str, rungs: &str) -> Result<String, RenderError> { let template = get_template(); - if !template.contains(PLACEHOLDER) { + if !template.contains(MORPHEME_PLACEHOLDER) { return Err(RenderError::Template( - "Template does not contain placeholder '%%%'".to_string() + "Template does not contain placeholder '%MORPHEME%'".to_string() )); } - Ok(template.replace(PLACEHOLDER, markup)) + if !template.contains(RUNGS_PLACEHOLDER) { + return Err(RenderError::Template( + "Template does not contain placeholder '%RUNGS%'".to_string() + )); + } + + let result = template + .replace(MORPHEME_PLACEHOLDER, morpheme) + .replace(RUNGS_PLACEHOLDER, rungs); + + Ok(result) } diff --git a/morphology-img/static/entry.ily b/morphology-img/static/entry.ily index 235e3c0..b7b1a1b 100644 --- a/morphology-img/static/entry.ily +++ b/morphology-img/static/entry.ily @@ -1,7 +1,7 @@ entry = #(define-music-function - (morpheme) - (ly:music?) + (morpheme rungs) + (ly:music? list?) #{ \transpose c c' << \new StaffContainer { @@ -10,10 +10,11 @@ entry = \override StaffContainer.LedgerLineSpanner.color = #white \override StaffContainer.StaffSymbol.ledger-positions = #'(-10 -5 -3 3 5 10) \override StaffContainer.StaffSymbol.line-count = 0 - \override StaffContainer.StaffSymbol.line-positions = #'(-5 -0.15 0 0.15 5) + \override StaffContainer.StaffSymbol.line-positions = + #(sort (append rungs '(-0.15 0.15)) <) \cadenzaOn << - \new Staff { + \new Staff { \override NoteHead.color = #white #morpheme } diff --git a/morphology-img/static/template.ly b/morphology-img/static/template.ly index f0bb7b5..e4d2969 100644 --- a/morphology-img/static/template.ly +++ b/morphology-img/static/template.ly @@ -5,8 +5,8 @@ \include "entry.ily" \score { - \entry { - %%% - } + \entry { + %MORPHEME% + } %RUNGS% } diff --git a/morphology-img/tests/template.rs b/morphology-img/tests/template.rs index 60ef20e..07a5f3b 100644 --- a/morphology-img/tests/template.rs +++ b/morphology-img/tests/template.rs @@ -13,27 +13,31 @@ fn test_embedded_templates_non_empty() { } #[test] -fn test_template_contains_placeholder() { - // Verify the template contains the placeholder +fn test_template_contains_placeholders() { + // Verify the template contains both placeholders let template = get_template(); - assert!(template.contains("%%%"), "template should contain '%%%' placeholder"); + assert!(template.contains("%MORPHEME%"), "template should contain '%MORPHEME%' placeholder"); + assert!(template.contains("%RUNGS%"), "template should contain '%RUNGS%' placeholder"); } #[test] fn test_successful_substitution() { // Test successful substitution with simple markup - let markup = "c'4 d'4 e'4"; - let result = substitute_template(markup); + let morpheme = "c'4 d'4 e'4"; + let rungs = "#'(-5 0 5)"; + let result = substitute_template(morpheme, rungs); assert!(result.is_ok(), "substitution should succeed"); let substituted = result.unwrap(); - // Verify the markup is present in the result - assert!(substituted.contains(markup), "result should contain the markup"); + // Verify both values are present in the result + assert!(substituted.contains(morpheme), "result should contain the morpheme"); + assert!(substituted.contains(rungs), "result should contain the rungs"); - // Verify the placeholder is no longer present - assert!(!substituted.contains("%%%"), "result should not contain placeholder"); + // Verify the placeholders are no longer present + assert!(!substituted.contains("%MORPHEME%"), "result should not contain morpheme placeholder"); + assert!(!substituted.contains("%RUNGS%"), "result should not contain rungs placeholder"); // Verify the template structure is preserved assert!(substituted.contains(r#"\version "2.24.0""#), "version should be preserved"); @@ -43,42 +47,30 @@ fn test_successful_substitution() { #[test] fn test_substitution_with_empty_markup() { - // Test with empty markup string - let markup = ""; - let result = substitute_template(markup); + // Test with empty morpheme string + let morpheme = ""; + let rungs = "#'(0)"; + let result = substitute_template(morpheme, rungs); - assert!(result.is_ok(), "substitution with empty string should succeed"); + assert!(result.is_ok(), "substitution with empty morpheme should succeed"); let substituted = result.unwrap(); - // Placeholder should be replaced with empty string - assert!(!substituted.contains("%%%")); + // Placeholders should be replaced + assert!(!substituted.contains("%MORPHEME%")); + assert!(!substituted.contains("%RUNGS%")); + assert!(substituted.contains(rungs)); } #[test] fn test_error_when_placeholder_missing() { - // This test simulates what would happen if the placeholder was missing - // We'll test this by creating a mock template without the placeholder - - // Note: We can't easily modify the embedded template at runtime, - // so we'll just verify the error handling works as designed - // by checking that our current template DOES have the placeholder + // Verify error handling works by checking that our current template + // has both required placeholders let template = get_template(); - if !template.contains("%%%") { - // If this branch were taken, substitute_template would return an error - let result = substitute_template("test"); - assert!(result.is_err()); - - if let Err(RenderError::Template(msg)) = result { - assert!(msg.contains("placeholder")); - } else { - panic!("Expected RenderError::Template"); - } - } else { - // Current template has placeholder, which is correct - assert!(template.contains("%%%")); - } + // Current template should have both placeholders + assert!(template.contains("%MORPHEME%"), "template should have morpheme placeholder"); + assert!(template.contains("%RUNGS%"), "template should have rungs placeholder"); } #[test] @@ -97,8 +89,9 @@ fn test_template_structure() { #[test] fn test_substitution_preserves_whitespace() { // Test that substitution preserves indentation and whitespace - let markup = "c'4 d'4"; - let result = substitute_template(markup).unwrap(); + let morpheme = "c'4 d'4"; + let rungs = "#'(0)"; + let result = substitute_template(morpheme, rungs).unwrap(); // The indented structure should be preserved // The markup should be indented within the entry block @@ -108,37 +101,44 @@ fn test_substitution_preserves_whitespace() { #[test] fn test_multiple_substitutions_independent() { // Test that multiple calls to substitute_template are independent - let markup1 = "c'4 d'4"; - let markup2 = "e'4 f'4"; - - let result1 = substitute_template(markup1).unwrap(); - let result2 = substitute_template(markup2).unwrap(); - - assert!(result1.contains(markup1)); - assert!(!result1.contains(markup2)); - - assert!(result2.contains(markup2)); - assert!(!result2.contains(markup1)); + let morpheme1 = "c'4 d'4"; + let morpheme2 = "e'4 f'4"; + let rungs1 = "#'(-3 0 3)"; + let rungs2 = "#'(-5 0 5)"; + + let result1 = substitute_template(morpheme1, rungs1).unwrap(); + let result2 = substitute_template(morpheme2, rungs2).unwrap(); + + assert!(result1.contains(morpheme1)); + assert!(result1.contains(rungs1)); + assert!(!result1.contains(morpheme2)); + assert!(!result1.contains(rungs2)); + + assert!(result2.contains(morpheme2)); + assert!(result2.contains(rungs2)); + assert!(!result2.contains(morpheme1)); + assert!(!result2.contains(rungs1)); } #[test] fn test_substitution_with_actual_sequence_markup() { // Test with markup that would come from the actual transformation pipeline - // This is the example from the development plan - let markup = "f'4 f4 e4 b4"; + let morpheme = "f'4 f4 e4 b4"; + let rungs = "#'(-4 0 4)"; - let result = substitute_template(markup); + let result = substitute_template(morpheme, rungs); assert!(result.is_ok()); let substituted = result.unwrap(); // Verify it creates valid LilyPond syntax structure - assert!(substituted.contains(r#"\entry { "#)); - assert!(substituted.contains(markup)); + assert!(substituted.contains(r#"\entry {"#)); + assert!(substituted.contains(morpheme)); + assert!(substituted.contains(rungs)); assert!(substituted.contains("}")); - // The substituted markup should be between the entry braces - let entry_start = substituted.find(r#"\entry { "#).unwrap(); - let markup_pos = substituted.find(markup).unwrap(); - assert!(markup_pos > entry_start, "markup should be inside entry block"); + // The substituted values should be in the correct positions + let entry_start = substituted.find(r#"\entry {"#).unwrap(); + let morpheme_pos = substituted.find(morpheme).unwrap(); + assert!(morpheme_pos > entry_start, "morpheme should be inside entry block"); } |