summaryrefslogtreecommitdiff
path: root/morphology-img/src
diff options
context:
space:
mode:
Diffstat (limited to 'morphology-img/src')
-rw-r--r--morphology-img/src/file_gen.rs14
-rw-r--r--morphology-img/src/note.rs115
-rw-r--r--morphology-img/src/template.rs30
3 files changed, 134 insertions, 25 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(&notes);
+ let morpheme = notes_to_markup(&notes);
- // 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)
}