From 8e18a8fcd7bc79fb62c23675dc1fc78f30b9eda8 Mon Sep 17 00:00:00 2001 From: Hasinjato Date: Wed, 11 Nov 2026 15:37:57 +0300 Subject: [PATCH] Fix transpose to C notes when partition have modulation {c: --- .../mg/dot/feufaro/solfa/TimeUnitObject.kt | 51 +++++++++++++++++-- .../kotlin/mg/dot/feufaro/solfa/Transpose.kt | 16 ++++-- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt index ed23ce0..3b476b5 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt @@ -280,6 +280,40 @@ class TimeUnitObject (val pTemplate: PTemplate, val prevTUO: TimeUnitObject?, co return finalLines } + fun getCumulativeModulationInterval(): Int { + val notes = noteAsMultiString() + val MODULATION_REGEX = Regex(">([drmfslt][ia]?)([₄₃₂₁¹²³⁴',]*)>([drmfslt][ia]?)([₄₃₂₁¹²³⁴',]*)") + val tokens = notes.split(Regex("(?<=[:|•\\s])|(?=[:|•\\s])")) + var modDelta = 0 + + for (token in tokens) { + val trimmed = token.trim() + val modMatch = MODULATION_REGEX.matchEntire(trimmed) + if (modMatch != null) { + val oldNoteCore = modMatch.groupValues[1] + val oldOctaveStr = modMatch.groupValues[2] + val newNoteCore = modMatch.groupValues[3] + val newOctaveStr = modMatch.groupValues[4] + + val oldIdx = Transpose.noteToNumber.indexOf(oldNoteCore) + val newIdx = Transpose.noteToNumber.indexOf(newNoteCore) + + if (oldIdx != -1 && newIdx != -1) { + val oldOctaveVal = Transpose.octaveToInt(oldOctaveStr) + val newOctaveVal = Transpose.octaveToInt(newOctaveStr) + + val oldTotalSemitones = oldIdx + (oldOctaveVal * 12) + val newTotalSemitones = newIdx + (newOctaveVal * 12) + + modDelta = oldTotalSemitones - newTotalSemitones + break + } + } + } + + val prevDelta = prevTUO?.getCumulativeModulationInterval() ?: 0 + return prevDelta + modDelta + } } private val REGEX_CLEAN_PREFIX = Regex("(\\d+\\.)+") @@ -322,13 +356,17 @@ fun TimeUnitComposable( val focusRequesters = remember { List(4) { FocusRequester() } } val isLitany = tuo.isLitany() - + val notes = tuo.noteAsMultiString() + val initialMod = tuo.prevTUO?.getCumulativeModulationInterval() ?: 0 // Mettre en cache les annotations et les chaînes transposées val rawText = remember(tuo, transpositionInterval) { - Transpose.transposeText(text = tuo.noteAsMultiString(), interval = transpositionInterval) + Transpose.transposeText( + text = notes, interval = transpositionInterval, + initialModulationInterval = initialMod + ) } val rawLines = remember(rawText) { rawText.lines() } - val multiLineText = remember(rawText, isLitany) { + val multiLineText = remember(tuo, transpositionInterval, isLitany) { if (isLitany) SolfaLabels.toLabels(rawText) else rawText } @@ -486,8 +524,11 @@ fun bestTUOWidth( // println("=== [DEBUG TUO] Début mesure de ${items.size} blocs TUO ===") items.forEach { tuo -> - val rawNotes = Transpose.transposeText( - text = tuo.noteAsMultiString(), interval = transpositionInterval + val prevMod = tuo.prevTUO?.getCumulativeModulationInterval() ?: 0 + val rawNotes = Transpose.transposeText( + text = tuo.noteAsMultiString(), + interval = transpositionInterval, + initialModulationInterval = prevMod ) val transposedNotes = if (tuo.isLitany()) SolfaLabels.toLabels(rawNotes) else rawNotes val noteLines = transposedNotes.lines() diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Transpose.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Transpose.kt index 716aa30..e0f5b1c 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Transpose.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Transpose.kt @@ -25,10 +25,10 @@ class Transpose { val noteToNumber = listOf("d", "di", "r", "ri", "m", "f", "fi", "s", "si", "l", "ta", "t") val keyToNumber = listOf("C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B" ) val keyToArmature = listOf(0, -5, 2, -3, 4, -1, -6, 1, -4, 3, -2, 5 ) - fun transposeText(text: String, interval: Int): String { + fun transposeText(text: String, interval: Int, initialModulationInterval: Int = 0): String { if (text.isEmpty()) return text - var currentModulationInterval = 0 + var currentModulationInterval = initialModulationInterval val MODULATION_REGEX = Regex(">([drmfslt][ia]?)([₄₃₂₁¹²³⁴',]*)>([drmfslt][ia]?)([₄₃₂₁¹²³⁴',]*)") val NOTE_UNIT_REGEX = Regex("([drmfslt][ia]?)([₄₃₂₁¹²³⁴]*)") val tokens = text.split(Regex("(?<=[:|•\\s])|(?=[:|•\\s])")) @@ -49,7 +49,16 @@ class Transpose { val oldIdx = noteToNumber.indexOf(oldNoteCore) val newIdx = noteToNumber.indexOf(newNoteCore) - //currentModulationInterval = newIdx - oldIdx + val oldOctaveVal = octaveToInt(oldOctave) + val newOctaveVal = octaveToInt(newOctave) + + val oldTotal = if (oldIdx != -1) oldIdx + (oldOctaveVal * 12) else 0 + val newTotal = if (newIdx != -1) newIdx + (newOctaveVal * 12) else 0 + + if (oldIdx != -1 && newIdx != -1) { + val modDelta = oldTotal - newTotal + currentModulationInterval += modDelta + } val transposedOld = transpose(note = oldNoteCore + oldOctave, fromKey = "C", toKey = "C", alter = interval) val transposedNew = transpose(note = newNoteCore + newOctave, fromKey = "C", toKey = "C", alter = interval) @@ -74,6 +83,7 @@ class Transpose { } } } +// println("--- [DEBUG transposeText] Résultat final : \"$finalResult\" (Modulation finale : $currentModulationInterval) ---") return finalResult }