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.rs115
1 files changed, 103 insertions, 12 deletions
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