@@ -3,7 +3,9 @@ package nestdrop.deck
33import androidx.compose.runtime.Immutable
44import androidx.compose.ui.graphics.Color
55import androidx.compose.ui.graphics.compositeOver
6+ import beatCounter
67import beatFrame
8+ import com.illposed.osc.OSCMessage
79import configFolder
810import decks
911import flowScope
@@ -92,7 +94,7 @@ class Deck(
9294 val horizontalMotion = NestdropControl .SliderWithResetButton (id, " HorizonMotion" , - 0.5f .. 0.5f , 0.0f )
9395 val verticalMotion = NestdropControl .SliderWithResetButton (id, " VerticalMotion" , - 0.5f .. 0.5f , 0.0f )
9496 val stretchSpeed = NestdropControl .SliderWithResetButton (id, " StretchSpeed" , 0.5f .. 1.5f , 1.0f )
95- val waveMode = NestdropControl .SliderWithResetButton (id, " WaveMode" , - 15f .. 15f , 0. 0f )
97+ val waveMode = NestdropControl .SliderWithResetButton (id, " WaveMode" , - 15f .. 15f , 0f )
9698
9799 suspend fun startFlows () {
98100 transitionTime.startFlows()
@@ -258,47 +260,6 @@ class Deck(
258260 private val hasSwitchedNew = MutableStateFlow (false )
259261 val hasSwitched = hasSwitchedNew.asStateFlow()
260262
261- suspend fun resetLatch () {
262- // hasSwitched.value = false
263- }
264-
265- // suspend fun beatFlowOldOld(
266- // flow: Flow<HistoryNotNull<Double>>
267- // ) = flow
268- // .combine(
269- // triggerTime.combine(beatFrame) { triggerTime, b ->
270- // logger.info { "$deckName triggerTime: ${triggerTime * b} ($triggerTime * $b)" }
271- // triggerTime * b
272- // }
273- // ) { (currentBeat, lastBeat), triggerAt ->
274- // Triple(currentBeat, lastBeat, triggerAt)
275- // }.onEach { (currentBeat, lastBeat, triggerAt) ->
276- //
277- // // logger.info { "currentBeat: $currentBeat" }
278- // // logger.info { "triggerAt: $triggerAt" }
279- // if (!hasSwitched.value && lastBeat < triggerAt && currentBeat >= triggerAt) {
280- // logger.info { "$deckName triggered at ${(currentBeat * 1000).roundToInt() / 1000f} ${(triggerAt * 1000).roundToInt() / 1000f}" }
281- // hasSwitched.value = true
282- // doSwitch()
283- // }
284- // }
285- suspend fun beatFlowOld (
286- flow : Flow <HistoryNotNull <Double >>
287- ) = combine(
288- flow,
289- triggerTime.combine(beatFrame) { triggerTime, b ->
290- logger.info { " $deckName triggerTime: ${triggerTime * b} ($triggerTime * $b )" }
291- triggerTime * b
292- },
293- isEnabled
294- ) { (currentBeat, lastBeat), triggerAt, isEnabled ->
295- if (isEnabled && ! hasSwitched.value && lastBeat < triggerAt && currentBeat >= triggerAt) {
296- logger.info { " $deckName triggered at ${(currentBeat * 1000 ).roundToInt() / 1000f } ${(triggerAt * 1000 ).roundToInt() / 1000f } " }
297- // hasSwitched.value = true
298- doSwitch()
299- }
300- }
301-
302263 suspend fun beatFlow (
303264 flow : Flow <HistoryNotNull <Double >>
304265 ) = combine(
@@ -316,7 +277,9 @@ class Deck(
316277 val shouldTrigger = (lastBeat < triggerAt && currentBeat >= triggerAt) || (lastBeat > beatFrame && currentBeat >= triggerAt)
317278
318279 if (shouldTrigger) {
319- logger.info { " $deckName triggered at $currentBeat ($triggerAt )" }
280+ flowScope.launch {
281+ logger.info { " $deckName triggered at $currentBeat ($triggerAt )" }
282+ }
320283 hasSwitchedNew.value = true
321284 flowScope.launch {
322285 val transitionTime = ndTime.transitionTime.value.toDouble().seconds
@@ -358,6 +321,36 @@ class Deck(
358321 .joinToString(" , " , " {" , " }" ) { (k, v) -> " $k : \" $v \" " }
359322 appendWarn(" SKIPPED $deckName preset: \" ${preset.currentPreset.value.name} \" all decks: $otherDecks " )
360323 }
324+
325+ suspend fun startFlows () {
326+ combine(
327+ beatCounter.runningHistoryNotNull(),
328+ beatFrame,
329+ triggerTime,
330+ isEnabled,
331+ ) { (currentBeat, lastBeat), beatFrame, triggerTime, isEnabled ->
332+ val triggerAt = triggerTime * beatFrame
333+ if (isEnabled && ! hasSwitchedNew.value) {
334+ val shouldTrigger =
335+ (lastBeat < triggerAt && currentBeat >= triggerAt) || (lastBeat > beatFrame && currentBeat >= triggerAt)
336+
337+ if (shouldTrigger) {
338+ logger.info { " $deckName triggered at $currentBeat $triggerAt " }
339+ hasSwitchedNew.emit(true )
340+ flowScope.launch {
341+ val transitionTime = ndTime.transitionTime.value.toDouble().seconds
342+ delay(transitionTime)
343+ hasSwitchedNew.emit(false )
344+ }
345+ flowScope.launch {
346+ doSwitch()
347+ }
348+ }
349+ }
350+ currentBeat
351+ }
352+ .launchIn(flowScope)
353+ }
361354 }
362355
363356 val presetSwitching = PresetSwitching ()
@@ -371,6 +364,7 @@ class Deck(
371364 ndStrobe.startFlows()
372365 ndAudio.startFlows()
373366 ndOutput.startFlows()
367+ presetSwitching.startFlows()
374368
375369// transitionTime
376370// //TODO: combine with trigger flow
@@ -450,12 +444,15 @@ class Deck(
450444
451445 @Immutable
452446 inner class Preset {
453- val currentPreset = OscSynced .FlowCustom (
447+ val currentPreset = object : OscSynced .FlowBase < PresetData > (
454448 " /Deck$id /Preset" ,
455449// initialValue = PresetData(-1, "unitialized"),
456450 target = OscSynced .Target .Nestdrop
457- ) { address, arguments ->
458- PresetData (arguments[0 ] as Int , arguments[1 ] as String )
451+ ) {
452+ override fun convertMessage (message : OSCMessage ): PresetData {
453+ val input = message.arguments
454+ return PresetData (input[0 ] as Int , input[1 ] as String )
455+ }
459456 }
460457 .stateIn(flowScope, SharingStarted .Eagerly , initialValue = PresetData (- 1 , " unitialized" ))
461458
@@ -517,19 +514,22 @@ class Deck(
517514
518515 val spoutTarget = MutableStateFlow (emptySet<SpriteKey >())
519516 val spoutStates = MutableStateFlow <Map <String , SpriteKey >>(mapOf ())
520- private val spriteStateFlow = OscSynced .FlowCustom (
517+ private val spriteStateFlow = object : OscSynced .FlowBase < SpriteData > (
521518 " /Deck$id /Sprite" ,
522519// SpriteData(),
523520 target = OscSynced .Target .Nestdrop
524- ) { _, args ->
525- SpriteData (
526- id = args[0 ] as Int ,
527- name = args[1 ] as String ,
528- mode = ImgMode .valueOf(args[2 ] as String ),
529- fx = args[3 ] as Int ,
530- mystery = args[4 ] as Int ,
531- enabled = (args[5 ] as Int ) == 1 ,
532- )
521+ ) {
522+ override fun convertMessage (message : OSCMessage ): SpriteData {
523+ val args = message.arguments
524+ return SpriteData (
525+ id = args[0 ] as Int ,
526+ name = args[1 ] as String ,
527+ mode = ImgMode .valueOf(args[2 ] as String ),
528+ fx = args[3 ] as Int ,
529+ mystery = args[4 ] as Int ,
530+ enabled = (args[5 ] as Int ) == 1 ,
531+ )
532+ }
533533 }
534534
535535// suspend fun enabledSprites() = imgStates.value.values
@@ -544,12 +544,11 @@ class Deck(
544544// .launchIn(flowScope)
545545// val imgSpriteIds = imgSpritesMap.map { it.values.map { it.id }.toSet() } //.stateIn(flowScope)
546546 spriteStateFlow
547- .onEach {
548- logger.info { " $deckName sprite: $it " }
549- }
550-
547+ // .onEach {
548+ // logger.info { "$deckName sprite: $it" }
549+ // }
551550 .combine(
552- imgSpritesMap.map { it .values.map { it .id }.toSet() }
551+ imgSpritesMap.map { map -> map .values.map { img -> img .id }.toSet() }
553552 ) { spriteData, imgSpriteIds ->
554553 spriteData.copy(isImg = spriteData.id in imgSpriteIds)
555554 }
0 commit comments