From ef99dc9a636e38f7626156c3d3e4b80e285547f8 Mon Sep 17 00:00:00 2001 From: jacksonfu14 Date: Tue, 18 Nov 2025 07:08:34 +0100 Subject: modify sequence to ly conversion --- morphology-img/src/note.rs | 15 ++++-- morphology-img/tests/file_gen.rs | 23 ++++---- morphology-img/tests/sequence_conversion.rs | 83 ++++++++++++++++------------- 3 files changed, 73 insertions(+), 48 deletions(-) diff --git a/morphology-img/src/note.rs b/morphology-img/src/note.rs index 98dbd68..c2158a3 100644 --- a/morphology-img/src/note.rs +++ b/morphology-img/src/note.rs @@ -92,20 +92,29 @@ impl Duration { /// 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 +/// 1. Convert (outer, inner) pairs to integers: pitch = (outer * multiplier) + inner +/// where multiplier = max(abs(inner values)) + 3 /// 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: &morphology::Sequence) -> Vec { - let length = sequence.length() as i32; 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; + sequence.values() .iter() .map(|value| { - let pitch_int = (value.outer * length) + value.inner; + let pitch_int = (value.outer * multiplier) + value.inner; let pitch = integer_to_pitch_name(pitch_int); LilyPondNote::new(pitch, duration) }) diff --git a/morphology-img/tests/file_gen.rs b/morphology-img/tests/file_gen.rs index e459688..167a9d8 100644 --- a/morphology-img/tests/file_gen.rs +++ b/morphology-img/tests/file_gen.rs @@ -456,13 +456,14 @@ fn test_generate_ly_files_preserves_order() { let content = fs::read_to_string(path).unwrap(); // Verify the content corresponds to the right sequence - // Sequence with Value::new(outer, 0) and length 1 gives pitch = outer * 1 + 0 = outer - // Then integer_to_pitch_name: 5→g', 3→e', 1→c' - // (5+6=11, 11%7=4→"g", 11/7=1→') (3+6=9, 9%7=2→"e", 9/7=1→') (1+6=7, 7%7=0→"c", 7/7=1→') + // Sequence with Value::new(outer, 0), max_abs_inner = 0, multiplier = 3 + // pitch = outer * 3 + 0 + // Then integer_to_pitch_name: 15→c''', 9→d'', 3→e' + // (15+6=21, 21%7=0→"c", 21/7=3→''') (9+6=15, 15%7=1→"d", 15/7=2→'') (3+6=9, 9%7=2→"e", 9/7=1→') match i { - 0 => assert!(content.contains("g'4"), "first file should have sequence 0 content"), - 1 => assert!(content.contains("e'4"), "second file should have sequence 1 content"), - 2 => assert!(content.contains("c'4"), "third file should have sequence 2 content"), + 0 => assert!(content.contains("c'''4"), "first file should have sequence 0 content"), + 1 => assert!(content.contains("d''4"), "second file should have sequence 1 content"), + 2 => assert!(content.contains("e'4"), "third file should have sequence 2 content"), _ => panic!("unexpected index"), } } @@ -522,12 +523,16 @@ fn test_complete_ly_generation_pipeline() { } // Verify specific content for first file + // Sequence: [(1,0), (-1,1), (-1,0), (0,0)] + // max_abs_inner = 1, multiplier = 4 + // Pitches: 1*4+0=4, -1*4+1=-3, -1*4+0=-4, 0*4+0=0 → f'4 f4 e4 b4 let content0 = fs::read_to_string(&ly_paths[0]).unwrap(); assert!(content0.contains("f'4 f4 e4 b4"), "first file should have correct markup"); // Verify specific content for second file - // Sequence has length 2: [(0,0), (1,0)] - // Pitches: 0*2+0=0→b4, 1*2+0=2→d'4 + // Sequence: [(0,0), (1,0)] + // max_abs_inner = 0, multiplier = 3 + // Pitches: 0*3+0=0→b4, 1*3+0=3→e'4 let content1 = fs::read_to_string(&ly_paths[1]).unwrap(); - assert!(content1.contains("b4 d'4"), "second file should have correct markup"); + assert!(content1.contains("b4 e'4"), "second file should have correct markup"); } diff --git a/morphology-img/tests/sequence_conversion.rs b/morphology-img/tests/sequence_conversion.rs index 30cd494..72d4c51 100644 --- a/morphology-img/tests/sequence_conversion.rs +++ b/morphology-img/tests/sequence_conversion.rs @@ -33,7 +33,8 @@ fn test_single_element_sequence() { let notes = sequence_to_notes(&seq); assert_eq!(notes.len(), 1); - // (0 * 1) + 0 = 0 -> "b" + // max_abs_inner = 0, multiplier = 3 + // (0 * 3) + 0 = 0 -> "b" assert_eq!(notes[0].as_str(), "b4"); } @@ -49,6 +50,7 @@ fn test_sequence_with_all_zeros() { let notes = sequence_to_notes(&seq); assert_eq!(notes.len(), 3); + // max_abs_inner = 0, multiplier = 3 // All should be (0 * 3) + 0 = 0 -> "b" assert_eq!(notes[0].as_str(), "b4"); assert_eq!(notes[1].as_str(), "b4"); @@ -66,9 +68,10 @@ fn test_sequence_with_positive_outer() { 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" + // max_abs_inner = 0, multiplier = 3 + // (1 * 3) + 0 = 3 -> "e'" + assert_eq!(notes[0].as_str(), "e'4"); + // (0 * 3) + 0 = 0 -> "b" assert_eq!(notes[1].as_str(), "b4"); } @@ -83,9 +86,10 @@ fn test_sequence_with_negative_outer() { 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" + // max_abs_inner = 0, multiplier = 3 + // (-1 * 3) + 0 = -3 -> "f" + assert_eq!(notes[0].as_str(), "f4"); + // (0 * 3) + 0 = 0 -> "b" assert_eq!(notes[1].as_str(), "b4"); } @@ -101,11 +105,12 @@ fn test_sequence_with_inner_values() { let notes = sequence_to_notes(&seq); assert_eq!(notes.len(), 3); - // (0 * 3) + 1 = 1 -> "c'" + // max_abs_inner = 1, multiplier = 4 + // (0 * 4) + 1 = 1 -> "c'" assert_eq!(notes[0].as_str(), "c'4"); - // (0 * 3) + (-1) = -1 -> "a" + // (0 * 4) + (-1) = -1 -> "a" assert_eq!(notes[1].as_str(), "a4"); - // (0 * 3) + 0 = 0 -> "b" + // (0 * 4) + 0 = 0 -> "b" assert_eq!(notes[2].as_str(), "b4"); } @@ -122,13 +127,14 @@ fn test_sequence_with_mixed_values() { 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" + // max_abs_inner = 3, multiplier = 6 + // (2 * 6) + 3 = 15 -> "c'''" + assert_eq!(notes[0].as_str(), "c'''4"); + // (-1 * 6) + (-2) = -8 -> "a," + assert_eq!(notes[1].as_str(), "a,4"); + // (1 * 6) + 1 = 7 -> "b'" + assert_eq!(notes[2].as_str(), "b'4"); + // (0 * 6) + 0 = 0 -> "b" assert_eq!(notes[3].as_str(), "b4"); } @@ -143,32 +149,36 @@ fn test_sequence_length_two() { let notes = sequence_to_notes(&seq); assert_eq!(notes.len(), 2); - assert_eq!(notes[0].as_str(), "d'4"); + // max_abs_inner = 0, multiplier = 3 + assert_eq!(notes[0].as_str(), "e'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 +fn test_max_inner_affects_pitch_calculation() { + // Test that max_abs_inner affects the pitch calculation + // Value (1, 0) should give different pitches depending on other inner values - let seq2 = Sequence::new(vec![ + // Sequence with max_abs_inner = 0 + let seq1 = 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 notes1 = sequence_to_notes(&seq1); + // max_abs_inner = 0, multiplier = 3 + // (1 * 3) + 0 = 3 + assert_eq!(notes1[0].as_str(), "e'4"); - let seq4 = Sequence::new(vec![ + // Sequence with max_abs_inner = 2 + let seq2 = Sequence::new(vec![ Value::new(1, 0), - Value::new(0, 0), - Value::new(0, 0), + Value::new(0, 2), Value::new(0, 0), ]); - let notes4 = sequence_to_notes(&seq4); - // (1 * 4) + 0 = 4 - assert_eq!(notes4[0].as_str(), "f'4"); + let notes2 = sequence_to_notes(&seq2); + // max_abs_inner = 2, multiplier = 5 + // (1 * 5) + 0 = 5 + assert_eq!(notes2[0].as_str(), "g'4"); } #[test] @@ -183,11 +193,12 @@ fn test_extreme_values() { 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" + // max_abs_inner = 5, multiplier = 8 + // (10 * 8) + 5 = 85 -> "c'''''''''''''" + assert_eq!(notes[0].as_str(), "c'''''''''''''4"); + // (-10 * 8) + (-5) = -85 -> "a,,,,,,,,,,,," + assert_eq!(notes[1].as_str(), "a,,,,,,,,,,,,4"); + // (0 * 8) + 0 = 0 -> "b" assert_eq!(notes[2].as_str(), "b4"); } -- cgit v1.2.3