diff --git a/composeApp/src/androidMain/kotlin/mg/dot/feufaro/midi/MidiPlayer.kt b/composeApp/src/androidMain/kotlin/mg/dot/feufaro/midi/MidiPlayer.kt index 718060c..20e1d15 100644 --- a/composeApp/src/androidMain/kotlin/mg/dot/feufaro/midi/MidiPlayer.kt +++ b/composeApp/src/androidMain/kotlin/mg/dot/feufaro/midi/MidiPlayer.kt @@ -181,6 +181,8 @@ actual class FMediaPlayer actual constructor( private val navigationSteps = mutableListOf() private val prefs: Settings = provideSettings() + + @Volatile private var userCustomTempo: Boolean = false init { midiDriver.start() loadVoiceVolumes() @@ -196,7 +198,9 @@ actual class FMediaPlayer actual constructor( val usPerQuarter = ((md[0].toInt() and 0xFF) shl 16) or ((md[1].toInt() and 0xFF) shl 8) or (md[2].toInt() and 0xFF) - targetBpm = 60_000_000f / usPerQuarter + val parsedBpm = 60_000_000f / usPerQuarter + targetBpm = parsedBpm + initialBpm = parsedBpm } usPerTick = (60_000_000.0 / targetBpm) / resolution @@ -292,8 +296,10 @@ actual class FMediaPlayer actual constructor( private fun resetTempoToInitial() { tempoChangeJob?.cancel() isInTempoChange = false - targetBpm = initialBpm - boundModel?.setBpmFlow(initialBpm) + //targetBpm = initialBpm + userCustomTempo = false + usPerTick = (60_000_000.0 / targetBpm.toDouble()) / resolution.toDouble() + boundModel?.setBpmFlow(targetBpm) } private fun extractDynamic(marker: String) = Dynamic.entries.firstOrNull { @@ -438,9 +444,13 @@ actual class FMediaPlayer actual constructor( currentTickPos = gridIndex.toLong() * resolution lastEventNano = System.nanoTime() - val initialOrPreviousBpm = navigationSteps - .filter { it.newBpm != null && it.gridIndex <= gridIndex } - .maxByOrNull { it.gridIndex }?.newBpm ?: initialBpm + val initialOrPreviousBpm = if (gridIndex == 0 && userCustomTempo) { + targetBpm + } else { + navigationSteps + .filter { it.newBpm != null && it.gridIndex <= gridIndex } + .maxByOrNull { it.gridIndex }?.newBpm ?: initialBpm + } targetBpm = initialOrPreviousBpm sharedScreenModel.setBpmFlow(initialOrPreviousBpm) @@ -691,9 +701,14 @@ actual class FMediaPlayer actual constructor( //♩ step.newBpm != null -> { step.alreadyDone = true - println("Exécution changement de tempo : ${step.newBpm} BPM à la grille $currentIndex") - targetBpm = step.newBpm - sharedScreenModel.setBpmFlow(step.newBpm) + if (step.gridIndex == 0 && userCustomTempo) { + + } + else { + println("Exécution changement de tempo : ${step.newBpm} BPM à la grille $currentIndex") + targetBpm = step.newBpm + sharedScreenModel.setBpmFlow(step.newBpm) + } } // Modulation @@ -922,7 +937,9 @@ actual class FMediaPlayer actual constructor( allNotesOff(); currentTickPos = 0L resetNavigationFlags(); navigationSteps.clear(); clearLoop() currentDynamicFactor = Dynamic.MF.factor - resetTempoToNormal() + //resetTempoToNormal() + currentPlaybackBpm = targetBpm + usPerTick = (60_000_000.0 / targetBpm.toDouble()) / resolution.toDouble() resetTempoToInitial() } actual fun release() { @@ -943,7 +960,9 @@ actual class FMediaPlayer actual constructor( actual fun setTempo(bpm: Float) { targetBpm = bpm currentPlaybackBpm = bpm + userCustomTempo = true usPerTick = (60_000_000.0 / bpm.toDouble()) / resolution.toDouble() + boundModel?.setBpmFlow(bpm) boundModel?.let { prepareNavigation(it) } needsClockSync = true }