Skip to content

Commit a085006

Browse files
committed
updates and fixes
1 parent 3ad7fe3 commit a085006

32 files changed

+7122
-7585
lines changed

src/desktopMain/kotlin/Main.kt

Lines changed: 368 additions & 369 deletions
Large diffs are not rendered by default.

src/desktopMain/kotlin/ScanPresets.kt

Lines changed: 265 additions & 265 deletions
Large diffs are not rendered by default.

src/desktopMain/kotlin/nestdrop/NestdropControl.kt

Lines changed: 292 additions & 291 deletions
Large diffs are not rendered by default.
Lines changed: 137 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,137 @@
1-
package nestdrop
2-
3-
import com.illposed.osc.OSCMessage
4-
import com.illposed.osc.OSCPacket
5-
import flowScope
6-
import io.github.oshai.kotlinlogging.KotlinLogging
7-
import kotlinx.coroutines.channels.Channel
8-
import kotlinx.coroutines.delay
9-
import kotlinx.coroutines.flow.consumeAsFlow
10-
import kotlinx.coroutines.flow.launchIn
11-
import kotlinx.coroutines.flow.onEach
12-
13-
sealed interface PresetIdState {
14-
val queue: Queue<out Preset>? get() = null
15-
// val index: Int? get() = null
16-
val index: Int? get() = null
17-
data class Data(
18-
// override val index: Int,
19-
override val index: Int,
20-
override val queue: Queue<out Preset>,
21-
val force: Boolean = false,
22-
// val hardCut: Boolean = false,
23-
) : PresetIdState {
24-
// val id get() = queue.presets.getOrNull(index)?.id
25-
}
26-
27-
data object Unset : PresetIdState
28-
}
29-
30-
31-
class NestdropSpriteQueue(
32-
private val nestdropSendChannel: Channel<OSCPacket>,
33-
private val onChange: suspend (Int) -> Unit = {}
34-
) {
35-
companion object {
36-
private val logger = KotlinLogging.logger { }
37-
}
38-
39-
private val channel: Channel<PresetIdState> = Channel()
40-
41-
suspend fun send(
42-
state: PresetIdState
43-
) {
44-
channel.send(state)
45-
}
46-
47-
private suspend fun presetId(queue: Queue<out Preset>, index: Int?, overlay: Boolean = false) {
48-
if(index == null) {
49-
logger.warn { "failed to find sprite id" }
50-
return
51-
}
52-
logger.debug { "setting index $index on ${queue.name} (/Queue/${queue.name}/ActIdx/$index)" }
53-
// logger.debug { "setting presetId $index on ${queue.name} (\"/PresetID/${queue.name}/$id\")" }
54-
nestdropSendChannel.send(
55-
OSCMessage(
56-
// /PresetID/spout_1/15879 or /Queue/spout_1/ActIdx/0
57-
"/Queue/${queue.name}/ActIdx/$index",
58-
// "/PresetID/${queue.name}/$id",
59-
// "/PresetID/$id",
60-
listOf(
61-
if (overlay) 0 else 1
62-
)
63-
)
64-
)
65-
}
66-
67-
suspend fun startFlows() {
68-
var previous: PresetIdState? = null
69-
channel
70-
.consumeAsFlow()
71-
// .runningHistory(PresetIdState.Unset)
72-
// .filterNotNull()
73-
.onEach { current ->
74-
// logger.warnF { "previous: $previous" }
75-
// logger.warnF { "current: $current" }
76-
// if (previous != null) {
77-
when (current) {
78-
is PresetIdState.Unset -> {
79-
when (val previous = previous) {
80-
is PresetIdState.Data -> {
81-
if (previous.queue.type == QueueType.SPRITE) {
82-
// to unset: send last index again
83-
logger.debug { "unsetting previous sprite" }
84-
presetId(
85-
queue = previous.queue,
86-
index = previous.index,
87-
overlay = false
88-
)
89-
} else {
90-
logger.error { "previous queue was not a spout/sprite queue: previous: $previous" }
91-
}
92-
}
93-
94-
is PresetIdState.Unset -> {
95-
// do nothing
96-
}
97-
98-
null -> {}
99-
}
100-
101-
}
102-
103-
is PresetIdState.Data -> {
104-
if (current.force) {
105-
logger.debug { "force setting sprite" }
106-
presetId(
107-
queue = current.queue,
108-
index = current.index,
109-
// current.queue.presets.first { it.id != current.id }.id,
110-
// (current.index + 1) % current.queue.presets.size,
111-
overlay = false
112-
)
113-
delay(25)
114-
presetId(current.queue, current.index, overlay = false)
115-
} else {
116-
117-
val presetName = current.queue.presets.getOrNull(current.index)
118-
val previousId = previous?.index
119-
val previousPresetName = previousId?.let {
120-
previous?.queue?.presets?.getOrNull(it)
121-
}
122-
if (current.index == previousId) {
123-
logger.info { "ND: received same preset id again, resetting ${current.queue.name} to $presetName" }
124-
presetId(
125-
queue = current.queue,
126-
index = current.index,
127-
// current.queue.presets.first { it.id != current.id }.id,
128-
overlay = false
129-
)
130-
// presetId(current.queue, current.index)
131-
delay(50)
132-
presetId(current.queue, current.index, overlay = false)
133-
onChange(current.index)
134-
} else {
135-
logger.info { "ND: switching ${current.queue.name} to '$presetName' (before: $previousPresetName)" }
136-
presetId(current.queue, current.index, overlay = false)
137-
onChange(current.index)
138-
}
139-
140-
}
141-
}
142-
}
143-
// } else {
144-
// logger.debug { "not switching after initializing program" }
145-
//
146-
// //TODO switch to another preset and back to ensure it is set correctly
147-
// }
148-
previous = current
149-
150-
}
151-
.launchIn(flowScope)
152-
}
153-
154-
}
155-
1+
package nestdrop
2+
3+
import com.illposed.osc.OSCMessage
4+
import com.illposed.osc.OSCPacket
5+
import flowScope
6+
import io.github.oshai.kotlinlogging.KotlinLogging
7+
import kotlinx.coroutines.channels.Channel
8+
import kotlinx.coroutines.delay
9+
import kotlinx.coroutines.flow.StateFlow
10+
import kotlinx.coroutines.flow.combine
11+
import kotlinx.coroutines.flow.consumeAsFlow
12+
import kotlinx.coroutines.flow.filterNotNull
13+
import kotlinx.coroutines.flow.launchIn
14+
import nestdrop.deck.Deck
15+
16+
sealed interface PresetIdState {
17+
// val queue: Queue<out Preset>? get() = null
18+
val index: Int? get() = null
19+
20+
data class Data(
21+
override val index: Int,
22+
val force: Boolean = false,
23+
// val hardCut: Boolean = false,
24+
) : PresetIdState
25+
26+
data object Unset : PresetIdState
27+
}
28+
29+
30+
class NestdropSpriteQueue(
31+
private val nestdropSendChannel: Channel<OSCPacket>,
32+
private val spoutStateMap: StateFlow<Map<String, Deck.SpriteKey>>,
33+
private val queue: StateFlow<Queue<out Preset>?>,
34+
) {
35+
companion object {
36+
private val logger = KotlinLogging.logger { }
37+
}
38+
39+
private val channel: Channel<PresetIdState> = Channel()
40+
41+
suspend fun send(
42+
state: PresetIdState
43+
) {
44+
channel.send(state)
45+
}
46+
47+
private suspend fun presetId(queue: Queue<out Preset>, index: Int?, overlay: Boolean = false) {
48+
if (index == null) {
49+
logger.warn { "failed to find sprite id" }
50+
return
51+
}
52+
logger.debug { "setting index $index on ${queue.name} (/Queue/${queue.name}/ActIdx/$index)" }
53+
// logger.debug { "setting presetId $index on ${queue.name} (\"/PresetID/${queue.name}/$id\")" }
54+
nestdropSendChannel.send(
55+
OSCMessage(
56+
// /PresetID/spout_1/15879 or /Queue/spout_1/ActIdx/0
57+
"/Queue/${queue.name}/ActIdx/$index",
58+
// "/PresetID/${queue.name}/$id",
59+
// "/PresetID/$id",
60+
listOf(
61+
if (overlay) 0 else 1
62+
)
63+
)
64+
)
65+
}
66+
67+
suspend fun startFlows() {
68+
channel
69+
.consumeAsFlow()
70+
// .runningHistory(PresetIdState.Unset)
71+
// .filterNotNull()
72+
.combine(
73+
queue.filterNotNull()
74+
) { current, queue ->
75+
val currentActive = spoutStateMap.value.values.toSet()
76+
// logger.warnF { "previous: $previous" }
77+
// logger.warnF { "current: $current" }
78+
// if (previous != null) {
79+
when (current) {
80+
is PresetIdState.Unset -> {
81+
currentActive.forEach { key ->
82+
logger.debug { "unsetting previous sprite $key" }
83+
presetId(
84+
queue = queue,
85+
index = queue.presets.indexOfFirst { preset ->
86+
preset.name == key.name && when(preset) {
87+
is Preset.Sprite -> {
88+
preset.effects == key.fx
89+
}
90+
else -> true
91+
}
92+
93+
},
94+
overlay = false
95+
)
96+
}
97+
}
98+
99+
is PresetIdState.Data -> {
100+
// require(current.queue.name == queue.name) { "queue does not match" }
101+
if (current.force) {
102+
logger.debug { "force setting sprite" }
103+
presetId(
104+
queue = queue,
105+
index = (current.index + 1) % queue.presets.size,
106+
overlay = false
107+
)
108+
delay(25)
109+
presetId(queue, current.index, overlay = false)
110+
} else {
111+
val presetName = queue.presets.getOrNull(current.index)
112+
if (currentActive.any { it.id == current.index }) {
113+
logger.info { "ND: received same preset id again, doing nothing" }
114+
// logger.info { "ND: received same preset id again, resetting ${queue.name} to $presetName" }
115+
// presetId(
116+
// queue = queue,
117+
// index = (current.index + 1) % queue.presets.size,
118+
//// current.queue.presets.first { it.id != current.id }.id,
119+
// overlay = false
120+
// )
121+
// delay(50)
122+
// presetId(queue, current.index, overlay = false)
123+
} else {
124+
logger.info { "ND: switching ${queue.name} to '$presetName'" }
125+
presetId(queue, current.index, overlay = false)
126+
}
127+
128+
}
129+
}
130+
}
131+
132+
}
133+
.launchIn(flowScope)
134+
}
135+
136+
}
137+

0 commit comments

Comments
 (0)