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 8a61996..a1164a3 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt @@ -909,20 +909,24 @@ fun LazyVerticalGridTUO( selectedIndex = globalIndex showEditDialog = true } else { + val enableSmartP = prefs.getBoolean("smartBpoint", true) when (abModeState) { ABModeState.WAITING_FOR_A -> { - sharedScreenModel.mediaPlayer?.setPointA(globalIndex) - sharedScreenModel.setABModeState(ABModeState.WAITING_FOR_B) - } - ABModeState.WAITING_FOR_B -> { - val enableSmartB = prefs.getBoolean("smartBpoint", true) - val smartIndex = if (enableSmartB) { - sharedScreenModel.calculateSmartPointB(globalIndex, isZeroIndexed = true) + val smartIndexA = if (enableSmartP) { + sharedScreenModel.calculateSmartPointA(globalIndex) } else { globalIndex } - sharedScreenModel.mediaPlayer?.setPointB(smartIndex) - + sharedScreenModel.mediaPlayer?.setPointA(smartIndexA) + sharedScreenModel.setABModeState(ABModeState.WAITING_FOR_B) + } + ABModeState.WAITING_FOR_B -> { + val smartIndexB = if (enableSmartP) { + sharedScreenModel.calculateSmartPointB(globalIndex) + } else { + globalIndex + } + sharedScreenModel.mediaPlayer?.setPointB(smartIndexB) sharedScreenModel.setABModeState(ABModeState.LOOP_ACTIVE) } ABModeState.IDLE, ABModeState.LOOP_ACTIVE -> { diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/viewmodel/SharedScreenModel.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/viewmodel/SharedScreenModel.kt index 8ffab14..ae47133 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/viewmodel/SharedScreenModel.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/viewmodel/SharedScreenModel.kt @@ -482,7 +482,8 @@ class SharedScreenModel(private val fileRepository: FileRepository) : ScreenMode fun setABModeState(state: ABModeState) { _abModeState.value = state } - fun calculateSmartPointB(rawPointB: Int, isZeroIndexed: Boolean = false): Int { + + fun calculateSmartPointA(rawPointA: Int): Int { val currentMeasure = _measure.value val numerator = currentMeasure.substringBefore("/").toIntOrNull() ?: 4 @@ -492,18 +493,31 @@ class SharedScreenModel(private val fileRepository: FileRepository) : ScreenMode else -> 2 } - val remainder = rawPointB % multiple - var finalPointB = if (remainder == 0) { - rawPointB - } else { - rawPointB + (multiple - remainder) - } - if (isZeroIndexed) { - finalPointB -= 1 + val remainder = rawPointA % multiple + return rawPointA - remainder + } + + fun calculateSmartPointB(rawPointB: Int): Int { + val currentMeasure = _measure.value + val numerator = currentMeasure.substringBefore("/").toIntOrNull() ?: 4 + + val multiple = when (numerator) { + 2, 4, 8, 10 -> 4 + 3, 6, 9, 12 -> 3 + else -> 2 } - return finalPointB + val offset = rawPointB % multiple + val distToPrev = offset + 1 + val distToCurr = multiple - 1 - offset + + return if (distToPrev <= distToCurr) { + (rawPointB - offset - 1).coerceAtLeast(0) + } else { + rawPointB + (multiple - 1 - offset) + } } + private val _gridCount = MutableStateFlow(0) val gridCount: StateFlow = _gridCount.asStateFlow() @@ -823,6 +837,7 @@ class SharedScreenModel(private val fileRepository: FileRepository) : ScreenMode setTranspToC(0) setSilentDurBefr(0) updateLoopingState(0, 0) + _abModeState.value = ABModeState.IDLE _midiMarkersList.value = emptyList() _tuoTimestamps.value = emptyList() _searchTitle.value = ""