From 3505cc56367feabc60aaffe5e2b899d4d5d00531 Mon Sep 17 00:00:00 2001 From: jacksonfu14 Date: Sun, 16 Nov 2025 16:21:37 +0100 Subject: feature: convert sequences to ly markup --- morphology-img/src/lib.rs | 31 +++- morphology-img/src/note.rs | 156 +++++++++++++++++++ morphology-img/src/pitch.rs | 43 ------ morphology-img/tests/note_types.rs | 77 ++++++++++ morphology-img/tests/notes_to_markup.rs | 128 +++++++++++++++ morphology-img/tests/pitch.rs | 85 ---------- morphology-img/tests/pitch_conversion.rs | 192 +++++++++++++++++++++++ morphology-img/tests/sequence_conversion.rs | 231 ++++++++++++++++++++++++++++ 8 files changed, 812 insertions(+), 131 deletions(-) create mode 100644 morphology-img/src/note.rs delete mode 100644 morphology-img/src/pitch.rs create mode 100644 morphology-img/tests/note_types.rs create mode 100644 morphology-img/tests/notes_to_markup.rs delete mode 100644 morphology-img/tests/pitch.rs create mode 100644 morphology-img/tests/pitch_conversion.rs create mode 100644 morphology-img/tests/sequence_conversion.rs 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) -> 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 { + 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) -> 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() - } -} diff --git a/morphology-img/tests/note_types.rs b/morphology-img/tests/note_types.rs new file mode 100644 index 0000000..dad9753 --- /dev/null +++ b/morphology-img/tests/note_types.rs @@ -0,0 +1,77 @@ +use morphology_img::note::{LilyPondPitch, LilyPondNote, Duration}; + +#[test] +fn test_lilypond_pitch_creation() { + let pitch = LilyPondPitch::new("c'"); + assert_eq!(pitch.as_str(), "c'"); +} + +#[test] +fn test_lilypond_pitch_from_string() { + let pitch = LilyPondPitch::new("bes,".to_string()); + assert_eq!(pitch.as_str(), "bes,"); +} + +#[test] +fn test_duration_quarter() { + let duration = Duration::QUARTER; + assert_eq!(duration.as_str(), "4"); +} + +#[test] +fn test_duration_new() { + let duration = Duration::new(8); + assert_eq!(duration.as_str(), "8"); +} + +#[test] +fn test_lilypond_note_creation() { + let pitch = LilyPondPitch::new("c'"); + let duration = Duration::QUARTER; + let note = LilyPondNote::new(pitch, duration); + assert_eq!(note.as_str(), "c'4"); +} + +#[test] +fn test_lilypond_note_with_different_duration() { + let pitch = LilyPondPitch::new("d"); + let duration = Duration::new(8); + let note = LilyPondNote::new(pitch, duration); + assert_eq!(note.as_str(), "d8"); +} + +#[test] +fn test_pitch_equality() { + let pitch1 = LilyPondPitch::new("c'"); + let pitch2 = LilyPondPitch::new("c'"); + let pitch3 = LilyPondPitch::new("d'"); + assert_eq!(pitch1, pitch2); + assert_ne!(pitch1, pitch3); +} + +#[test] +fn test_note_equality() { + let note1 = LilyPondNote::new( + LilyPondPitch::new("c'"), + Duration::QUARTER + ); + let note2 = LilyPondNote::new( + LilyPondPitch::new("c'"), + Duration::QUARTER + ); + let note3 = LilyPondNote::new( + LilyPondPitch::new("d'"), + Duration::QUARTER + ); + assert_eq!(note1, note2); + assert_ne!(note1, note3); +} + +#[test] +fn test_duration_equality() { + let dur1 = Duration::new(4); + let dur2 = Duration::QUARTER; + let dur3 = Duration::new(8); + assert_eq!(dur1, dur2); + assert_ne!(dur1, dur3); +} diff --git a/morphology-img/tests/notes_to_markup.rs b/morphology-img/tests/notes_to_markup.rs new file mode 100644 index 0000000..c540691 --- /dev/null +++ b/morphology-img/tests/notes_to_markup.rs @@ -0,0 +1,128 @@ +use morphology_img::note::{LilyPondPitch, LilyPondNote, Duration, notes_to_markup}; + +#[test] +fn test_empty_vector() { + let notes: Vec = vec![]; + assert_eq!(notes_to_markup(¬es), ""); +} + +#[test] +fn test_single_note() { + let notes = vec![ + LilyPondNote::new(LilyPondPitch::new("c'"), Duration::QUARTER), + ]; + assert_eq!(notes_to_markup(¬es), "c'4"); +} + +#[test] +fn test_multiple_notes() { + let notes = vec![ + LilyPondNote::new(LilyPondPitch::new("c'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("d'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("e'"), Duration::QUARTER), + ]; + assert_eq!(notes_to_markup(¬es), "c'4 d'4 e'4"); +} + +#[test] +fn test_example_from_preliminary_overview() { + // From overview: ["f'4", "f4", "e4", "b4"] + let notes = vec![ + LilyPondNote::new(LilyPondPitch::new("f'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("f"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("e"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("b"), Duration::QUARTER), + ]; + assert_eq!(notes_to_markup(¬es), "f'4 f4 e4 b4"); +} + +#[test] +fn test_notes_with_octave_markers_up() { + let notes = vec![ + LilyPondNote::new(LilyPondPitch::new("c'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("c''"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("c'''"), Duration::QUARTER), + ]; + assert_eq!(notes_to_markup(¬es), "c'4 c''4 c'''4"); +} + +#[test] +fn test_notes_with_octave_markers_down() { + let notes = vec![ + LilyPondNote::new(LilyPondPitch::new("b"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("b,"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("b,,"), Duration::QUARTER), + ]; + assert_eq!(notes_to_markup(¬es), "b4 b,4 b,,4"); +} + +#[test] +fn test_notes_with_different_durations() { + let notes = vec![ + LilyPondNote::new(LilyPondPitch::new("c'"), Duration::new(4)), + LilyPondNote::new(LilyPondPitch::new("d'"), Duration::new(8)), + LilyPondNote::new(LilyPondPitch::new("e'"), Duration::new(2)), + ]; + assert_eq!(notes_to_markup(¬es), "c'4 d'8 e'2"); +} + +#[test] +fn test_mixed_pitches_and_durations() { + let notes = vec![ + LilyPondNote::new(LilyPondPitch::new("f'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("a,"), Duration::new(8)), + LilyPondNote::new(LilyPondPitch::new("c''"), Duration::new(2)), + LilyPondNote::new(LilyPondPitch::new("b"), Duration::QUARTER), + ]; + assert_eq!(notes_to_markup(¬es), "f'4 a,8 c''2 b4"); +} + +#[test] +fn test_long_sequence() { + let notes = vec![ + LilyPondNote::new(LilyPondPitch::new("c'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("d'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("e'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("f'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("g'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("a'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("b'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("c''"), Duration::QUARTER), + ]; + assert_eq!(notes_to_markup(¬es), "c'4 d'4 e'4 f'4 g'4 a'4 b'4 c''4"); +} + +#[test] +fn test_duplicate_notes() { + // Test with repeated notes + let notes = vec![ + LilyPondNote::new(LilyPondPitch::new("c'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("c'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("c'"), Duration::QUARTER), + ]; + assert_eq!(notes_to_markup(¬es), "c'4 c'4 c'4"); +} + +#[test] +fn test_spacing_is_single_space() { + // Verify that notes are separated by exactly one space + let notes = vec![ + LilyPondNote::new(LilyPondPitch::new("c'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("d'"), Duration::QUARTER), + ]; + let markup = notes_to_markup(¬es); + assert_eq!(markup, "c'4 d'4"); + // Count spaces + assert_eq!(markup.matches(' ').count(), 1); +} + +#[test] +fn test_no_leading_or_trailing_spaces() { + let notes = vec![ + LilyPondNote::new(LilyPondPitch::new("c'"), Duration::QUARTER), + LilyPondNote::new(LilyPondPitch::new("d'"), Duration::QUARTER), + ]; + let markup = notes_to_markup(¬es); + assert!(!markup.starts_with(' ')); + assert!(!markup.ends_with(' ')); +} diff --git a/morphology-img/tests/pitch.rs b/morphology-img/tests/pitch.rs deleted file mode 100644 index 986cebc..0000000 --- a/morphology-img/tests/pitch.rs +++ /dev/null @@ -1,85 +0,0 @@ -use morphology_img::pitch::{LilyPondPitch, LilyPondNote, Duration}; - -#[test] -fn test_lilypond_pitch_creation() { - let pitch = LilyPondPitch::new("c'"); - assert_eq!(pitch.as_str(), "c'"); -} - -#[test] -fn test_lilypond_pitch_from_string() { - let pitch = LilyPondPitch::new("bes,".to_string()); - assert_eq!(pitch.as_str(), "bes,"); -} - -#[test] -fn test_duration_quarter() { - let duration = Duration::QUARTER; - assert_eq!(duration.as_str(), "4"); -} - -#[test] -fn test_duration_new() { - let duration = Duration::new(8); - assert_eq!(duration.as_str(), "8"); -} - -#[test] -fn test_lilypond_note_creation() { - let pitch = LilyPondPitch::new("c'"); - let duration = Duration::QUARTER; - let note = LilyPondNote::new(pitch, duration); - assert_eq!(note.as_str(), "c'4"); -} - -#[test] -fn test_lilypond_note_with_different_duration() { - let pitch = LilyPondPitch::new("d"); - let duration = Duration::new(8); - let note = LilyPondNote::new(pitch, duration); - assert_eq!(note.as_str(), "d8"); -} - -#[test] -fn test_lilypond_note_with_flat() { - let pitch = LilyPondPitch::new("bes,"); - let duration = Duration::QUARTER; - let note = LilyPondNote::new(pitch, duration); - assert_eq!(note.as_str(), "bes,4"); -} - -#[test] -fn test_pitch_equality() { - let pitch1 = LilyPondPitch::new("c'"); - let pitch2 = LilyPondPitch::new("c'"); - let pitch3 = LilyPondPitch::new("d'"); - assert_eq!(pitch1, pitch2); - assert_ne!(pitch1, pitch3); -} - -#[test] -fn test_note_equality() { - let note1 = LilyPondNote::new( - LilyPondPitch::new("c'"), - Duration::QUARTER - ); - let note2 = LilyPondNote::new( - LilyPondPitch::new("c'"), - Duration::QUARTER - ); - let note3 = LilyPondNote::new( - LilyPondPitch::new("d'"), - Duration::QUARTER - ); - assert_eq!(note1, note2); - assert_ne!(note1, note3); -} - -#[test] -fn test_duration_equality() { - let dur1 = Duration::new(4); - let dur2 = Duration::QUARTER; - let dur3 = Duration::new(8); - assert_eq!(dur1, dur2); - assert_ne!(dur1, dur3); -} diff --git a/morphology-img/tests/pitch_conversion.rs b/morphology-img/tests/pitch_conversion.rs new file mode 100644 index 0000000..667bf17 --- /dev/null +++ b/morphology-img/tests/pitch_conversion.rs @@ -0,0 +1,192 @@ +use morphology_img::note::integer_to_pitch_name; + +// Zero case +#[test] +fn test_zero_maps_to_b() { + assert_eq!(integer_to_pitch_name(0).as_str(), "b"); +} + +// Positive cases - basic +#[test] +fn test_positive_one_maps_to_c_prime() { + assert_eq!(integer_to_pitch_name(1).as_str(), "c'"); +} + +#[test] +fn test_positive_two_maps_to_d_prime() { + assert_eq!(integer_to_pitch_name(2).as_str(), "d'"); +} + +#[test] +fn test_positive_three_maps_to_e_prime() { + assert_eq!(integer_to_pitch_name(3).as_str(), "e'"); +} + +#[test] +fn test_positive_four_maps_to_f_prime() { + assert_eq!(integer_to_pitch_name(4).as_str(), "f'"); +} + +#[test] +fn test_positive_five_maps_to_g_prime() { + assert_eq!(integer_to_pitch_name(5).as_str(), "g'"); +} + +#[test] +fn test_positive_six_maps_to_a_prime() { + assert_eq!(integer_to_pitch_name(6).as_str(), "a'"); +} + +#[test] +fn test_positive_seven_maps_to_b_prime() { + assert_eq!(integer_to_pitch_name(7).as_str(), "b'"); +} + +// Negative cases - basic +#[test] +fn test_negative_one_maps_to_a() { + assert_eq!(integer_to_pitch_name(-1).as_str(), "a"); +} + +#[test] +fn test_negative_two_maps_to_g() { + assert_eq!(integer_to_pitch_name(-2).as_str(), "g"); +} + +#[test] +fn test_negative_three_maps_to_f() { + assert_eq!(integer_to_pitch_name(-3).as_str(), "f"); +} + +#[test] +fn test_negative_four_maps_to_e() { + assert_eq!(integer_to_pitch_name(-4).as_str(), "e"); +} + +#[test] +fn test_negative_five_maps_to_d() { + assert_eq!(integer_to_pitch_name(-5).as_str(), "d"); +} + +#[test] +fn test_negative_six_maps_to_c() { + assert_eq!(integer_to_pitch_name(-6).as_str(), "c"); +} + +#[test] +fn test_negative_seven_maps_to_b_comma() { + assert_eq!(integer_to_pitch_name(-7).as_str(), "b,"); +} + +// Multiple octaves up +#[test] +fn test_eight_maps_to_c_double_prime() { + assert_eq!(integer_to_pitch_name(8).as_str(), "c''"); +} + +#[test] +fn test_fourteen_maps_to_b_double_prime() { + assert_eq!(integer_to_pitch_name(14).as_str(), "b''"); +} + +#[test] +fn test_fifteen_maps_to_c_triple_prime() { + assert_eq!(integer_to_pitch_name(15).as_str(), "c'''"); +} + +#[test] +fn test_twenty_one_maps_to_b_triple_prime() { + assert_eq!(integer_to_pitch_name(21).as_str(), "b'''"); +} + +// Multiple octaves down +#[test] +fn test_negative_eight_maps_to_a_comma() { + assert_eq!(integer_to_pitch_name(-8).as_str(), "a,"); +} + +#[test] +fn test_negative_fourteen_maps_to_b_double_comma() { + assert_eq!(integer_to_pitch_name(-14).as_str(), "b,,"); +} + +#[test] +fn test_negative_fifteen_maps_to_a_double_comma() { + assert_eq!(integer_to_pitch_name(-15).as_str(), "a,,"); +} + +#[test] +fn test_negative_twenty_one_maps_to_b_triple_comma() { + assert_eq!(integer_to_pitch_name(-21).as_str(), "b,,,"); +} + +// Edge cases across octave boundaries +#[test] +fn test_octave_boundary_b_to_c_ascending() { + // B at reference octave + assert_eq!(integer_to_pitch_name(0).as_str(), "b"); + // C in next octave + assert_eq!(integer_to_pitch_name(1).as_str(), "c'"); +} + +#[test] +fn test_octave_boundary_c_to_b_descending() { + // C at reference octave + assert_eq!(integer_to_pitch_name(-6).as_str(), "c"); + // B in octave below + assert_eq!(integer_to_pitch_name(-7).as_str(), "b,"); +} + +#[test] +fn test_complete_ascending_scale_from_b() { + let expected = vec!["b", "c'", "d'", "e'", "f'", "g'", "a'", "b'"]; + for (i, expected_pitch) in expected.iter().enumerate() { + assert_eq!( + integer_to_pitch_name(i as i32).as_str(), + *expected_pitch, + "Failed at index {}", i + ); + } +} + +#[test] +fn test_complete_descending_scale_from_b() { + let expected = vec!["b", "a", "g", "f", "e", "d", "c", "b,"]; + for (i, expected_pitch) in expected.iter().enumerate() { + assert_eq!( + integer_to_pitch_name(-(i as i32)).as_str(), + *expected_pitch, + "Failed at index -{}", i + ); + } +} + +#[test] +fn test_example_from_preliminary_overview() { + // From the preliminary overview: [4, -3, -4, 0] becomes ["f'", "f", "e", "b"] + assert_eq!(integer_to_pitch_name(4).as_str(), "f'"); + assert_eq!(integer_to_pitch_name(-3).as_str(), "f"); + assert_eq!(integer_to_pitch_name(-4).as_str(), "e"); + assert_eq!(integer_to_pitch_name(0).as_str(), "b"); +} + +#[test] +fn test_symmetry_property() { + // Test that going up 7 and down 7 from reference gives B in adjacent octaves + assert_eq!(integer_to_pitch_name(7).as_str(), "b'"); + assert_eq!(integer_to_pitch_name(-7).as_str(), "b,"); +} + +#[test] +fn test_large_positive_value() { + // Test a very large value to ensure no overflow + // 100 + 6 = 106; 106 / 7 = 15 remainder 1; pitch_index = 1 (d), octave = 15 + assert_eq!(integer_to_pitch_name(100).as_str(), "d'''''''''''''''"); +} + +#[test] +fn test_large_negative_value() { + // Test a very large negative value + // -100 + 6 = -94; -94.div_euclid(7) = -14; -94.rem_euclid(7) = 4 (g) + assert_eq!(integer_to_pitch_name(-100).as_str(), "g,,,,,,,,,,,,,,"); +} diff --git a/morphology-img/tests/sequence_conversion.rs b/morphology-img/tests/sequence_conversion.rs new file mode 100644 index 0000000..9889448 --- /dev/null +++ b/morphology-img/tests/sequence_conversion.rs @@ -0,0 +1,231 @@ +use meta_affine::{Sequence, Value}; +use morphology_img::note::sequence_to_notes; + +#[test] +fn test_example_from_preliminary_overview() { + // Example from preliminary overview: [(1,0), (-1,1), (-1,0), (0,0)] + // Length = 4 + // Expected integers: [4, -3, -4, 0] + // Expected pitches: ["f'", "f", "e", "b"] + // Expected notes: ["f'4", "f4", "e4", "b4"] + + let seq = Sequence::new(vec![ + Value::new(1, 0), + Value::new(-1, 1), + Value::new(-1, 0), + Value::new(0, 0), + ]); + + let notes = sequence_to_notes(&seq); + + assert_eq!(notes.len(), 4); + assert_eq!(notes[0].as_str(), "f'4"); + assert_eq!(notes[1].as_str(), "f4"); + assert_eq!(notes[2].as_str(), "e4"); + assert_eq!(notes[3].as_str(), "b4"); +} + +#[test] +fn test_single_element_sequence() { + // Single origin value + let seq = Sequence::new(vec![Value::origin()]); + + let notes = sequence_to_notes(&seq); + + assert_eq!(notes.len(), 1); + // (0 * 1) + 0 = 0 -> "b" + assert_eq!(notes[0].as_str(), "b4"); +} + +#[test] +fn test_sequence_with_all_zeros() { + // All origin values + let seq = Sequence::new(vec![ + Value::origin(), + Value::origin(), + Value::origin(), + ]); + + let notes = sequence_to_notes(&seq); + + assert_eq!(notes.len(), 3); + // All should be (0 * 3) + 0 = 0 -> "b" + assert_eq!(notes[0].as_str(), "b4"); + assert_eq!(notes[1].as_str(), "b4"); + assert_eq!(notes[2].as_str(), "b4"); +} + +#[test] +fn test_sequence_with_positive_outer() { + // Sequence with positive outer values + let seq = Sequence::new(vec![ + Value::new(1, 0), + Value::new(0, 0), + ]); + + let notes = sequence_to_notes(&seq); + + assert_eq!(notes.len(), 2); + // (1 * 2) + 0 = 2 -> "d'" + assert_eq!(notes[0].as_str(), "d'4"); + // (0 * 2) + 0 = 0 -> "b" + assert_eq!(notes[1].as_str(), "b4"); +} + +#[test] +fn test_sequence_with_negative_outer() { + // Sequence with negative outer values + let seq = Sequence::new(vec![ + Value::new(-1, 0), + Value::new(0, 0), + ]); + + let notes = sequence_to_notes(&seq); + + assert_eq!(notes.len(), 2); + // (-1 * 2) + 0 = -2 -> "g" + assert_eq!(notes[0].as_str(), "g4"); + // (0 * 2) + 0 = 0 -> "b" + assert_eq!(notes[1].as_str(), "b4"); +} + +#[test] +fn test_sequence_with_inner_values() { + // Sequence with various inner values + let seq = Sequence::new(vec![ + Value::new(0, 1), + Value::new(0, -1), + Value::new(0, 0), + ]); + + let notes = sequence_to_notes(&seq); + + assert_eq!(notes.len(), 3); + // (0 * 3) + 1 = 1 -> "c'" + assert_eq!(notes[0].as_str(), "c'4"); + // (0 * 3) + (-1) = -1 -> "a" + assert_eq!(notes[1].as_str(), "a4"); + // (0 * 3) + 0 = 0 -> "b" + assert_eq!(notes[2].as_str(), "b4"); +} + +#[test] +fn test_sequence_with_mixed_values() { + // Sequence with mix of outer and inner values + let seq = Sequence::new(vec![ + Value::new(2, 3), + Value::new(-1, -2), + Value::new(1, 1), + Value::new(0, 0), + ]); + + let notes = sequence_to_notes(&seq); + + assert_eq!(notes.len(), 4); + // (2 * 4) + 3 = 11 -> "f''" + assert_eq!(notes[0].as_str(), "f''4"); + // (-1 * 4) + (-2) = -6 -> "c" + assert_eq!(notes[1].as_str(), "c4"); + // (1 * 4) + 1 = 5 -> "g'" + assert_eq!(notes[2].as_str(), "g'4"); + // (0 * 4) + 0 = 0 -> "b" + assert_eq!(notes[3].as_str(), "b4"); +} + +#[test] +fn test_sequence_length_two() { + // Test a simple length-2 sequence + let seq = Sequence::new(vec![ + Value::new(1, 0), + Value::new(0, 0), + ]); + + let notes = sequence_to_notes(&seq); + + assert_eq!(notes.len(), 2); + assert_eq!(notes[0].as_str(), "d'4"); + assert_eq!(notes[1].as_str(), "b4"); +} + +#[test] +fn test_length_affects_pitch_calculation() { + // Test that sequence length affects the pitch calculation + // Value (1, 0) with length 2 vs length 4 should give different pitches + + let seq2 = Sequence::new(vec![ + Value::new(1, 0), + Value::new(0, 0), + ]); + let notes2 = sequence_to_notes(&seq2); + // (1 * 2) + 0 = 2 + assert_eq!(notes2[0].as_str(), "d'4"); + + let seq4 = Sequence::new(vec![ + Value::new(1, 0), + Value::new(0, 0), + Value::new(0, 0), + Value::new(0, 0), + ]); + let notes4 = sequence_to_notes(&seq4); + // (1 * 4) + 0 = 4 + assert_eq!(notes4[0].as_str(), "f'4"); +} + +#[test] +fn test_extreme_values() { + // Test with extreme positive and negative values + let seq = Sequence::new(vec![ + Value::new(10, 5), + Value::new(-10, -5), + Value::new(0, 0), + ]); + + let notes = sequence_to_notes(&seq); + + assert_eq!(notes.len(), 3); + // (10 * 3) + 5 = 35 -> "b'''''" + assert_eq!(notes[0].as_str(), "b'''''4"); + // (-10 * 3) + (-5) = -35 -> "b,,,," + assert_eq!(notes[1].as_str(), "b,,,,,4"); + // (0 * 3) + 0 = 0 -> "b" + assert_eq!(notes[2].as_str(), "b4"); +} + +#[test] +fn test_empty_sequence() { + // Edge case: empty sequence + let seq = Sequence::new(vec![]); + + let notes = sequence_to_notes(&seq); + + assert_eq!(notes.len(), 0); +} + +#[test] +fn test_notes_preserve_sequence_order() { + // Verify that notes maintain the same order as input sequence + let seq = Sequence::new(vec![ + Value::new(3, 1), + Value::new(2, 2), + Value::new(1, 3), + Value::new(0, 4), + Value::new(-1, 5), + Value::new(0, 0), + ]); + + let notes = sequence_to_notes(&seq); + + assert_eq!(notes.len(), 6); + // Verify they're all different (order matters) + for i in 0..notes.len() { + for j in (i+1)..notes.len() { + // Most should be different, though some might coincidentally match + // The important thing is the order is preserved + let note_i = notes[i].as_str(); + let note_j = notes[j].as_str(); + // Just verify we got valid notes for all positions + assert!(note_i.ends_with('4')); + assert!(note_j.ends_with('4')); + } + } +} -- cgit v1.2.3