@@ -43,11 +43,9 @@ import tags.pickItemToGenerate
4343import tags.presetTagsMapping
4444import ui.screens.imgSpritesMap
4545import ui.screens.presetsMap
46- import utils.HistoryNotNull
4746import utils.prettyPrint
4847import utils.runningHistory
4948import utils.runningHistoryNotNull
50- import kotlin.math.roundToInt
5149import kotlin.time.Duration.Companion.milliseconds
5250import kotlin.time.Duration.Companion.seconds
5351
@@ -183,23 +181,6 @@ class Deck(
183181 pulseWidth.startFlows()
184182 waveForm.startFlows()
185183 enabled.startFlows()
186-
187- // Link.bpm
188- // .combine(bpmSyncMultiplier) { bpm, multiplier ->
189- // val beatsPerSecond = bpm / 60.0f
190- // beatsPerSecond / multiplier
191- // }
192- // .map {
193- // (it * 100).roundToInt() / 100f
194- // }
195- // .combine(bpmSyncEnabled) { a, b -> a to b }
196- // .distinctUntilChanged()
197- // .onEach { (value, toggle) ->
198- // if (toggle) {
199- // effectSpeed.value = value
200- // }
201- // }
202- // .launchIn(flowScope)
203184 }
204185 }
205186
@@ -252,45 +233,10 @@ class Deck(
252233
253234 @Immutable
254235 inner class PresetSwitching () {
255- val transitionTime =
256- MutableStateFlow (1f ) // OscSynced.Value("/deck$N/transitionTime", 1.0f, target = Target.TouchOSC)
257- val triggerTime =
258- MutableStateFlow (0.75f ) // OscSynced.Value("/deck$N/triggerTime", 0.75f, target = Target.TouchOSC)
259- // val hasSwitched = MutableStateFlow(false)
260- private val hasSwitchedNew = MutableStateFlow (false )
261- val hasSwitched = hasSwitchedNew.asStateFlow()
262-
263- suspend fun beatFlow (
264- flow : Flow <HistoryNotNull <Double >>
265- ) = combine(
266- flow,
267- beatFrame,
268- triggerTime,
269- isEnabled,
270- ) { (currentBeat, lastBeat), beatFrame, triggerTime, isEnabled ->
271- val triggerAt = triggerTime * beatFrame
272- // logger.info { "currentBeat: $currentBeat" }
273- // logger.info { "triggerAt: $triggerAt" }
274- // logger.info { "lastBeat: $lastBeat" }
275- // TODO: reset hasSwitched after transition time has passed
276- if (isEnabled && ! hasSwitchedNew.value) {
277- val shouldTrigger = (lastBeat < triggerAt && currentBeat >= triggerAt) || (lastBeat > beatFrame && currentBeat >= triggerAt)
278-
279- if (shouldTrigger) {
280- flowScope.launch {
281- logger.info { " $deckName triggered at $currentBeat ($triggerAt )" }
282- }
283- hasSwitchedNew.value = true
284- flowScope.launch {
285- val transitionTime = ndTime.transitionTime.value.toDouble().seconds
286- delay(transitionTime)
287- hasSwitchedNew.value = false
288- }
289- doSwitch()
290- }
291- }
292- currentBeat
293- }
236+ val transitionTime = MutableStateFlow (1f )
237+ val triggerTime = MutableStateFlow (0.75f )
238+ private val switchingLocked = MutableStateFlow (false )
239+ val isLocked = switchingLocked.asStateFlow()
294240
295241 private suspend fun doSwitch () {
296242 // change preset queue
@@ -330,17 +276,17 @@ class Deck(
330276 isEnabled,
331277 ) { (currentBeat, lastBeat), beatFrame, triggerTime, isEnabled ->
332278 val triggerAt = triggerTime * beatFrame
333- if (isEnabled && ! hasSwitchedNew .value) {
279+ if (isEnabled && ! switchingLocked .value) {
334280 val shouldTrigger =
335281 (lastBeat < triggerAt && currentBeat >= triggerAt) || (lastBeat > beatFrame && currentBeat >= triggerAt)
336282
337283 if (shouldTrigger) {
338- logger.info { " $deckName triggered at $currentBeat $triggerAt " }
339- hasSwitchedNew .emit(true )
284+ logger.debug { " $deckName triggered at $currentBeat $triggerAt " }
285+ switchingLocked .emit(true )
340286 flowScope.launch {
341287 val transitionTime = ndTime.transitionTime.value.toDouble().seconds
342288 delay(transitionTime)
343- hasSwitchedNew .emit(false )
289+ switchingLocked .emit(false )
344290 }
345291 flowScope.launch {
346292 doSwitch()
0 commit comments