@@ -10,22 +10,13 @@ import kotlinx.coroutines.flow.sample
1010import kotlinx.coroutines.launch
1111import kotlinx.datetime.Clock
1212import osc.OscSynced
13- import utils.runningHistoryNotNull
1413import kotlin.math.roundToInt
1514import kotlin.time.Duration.Companion.milliseconds
15+ import kotlin.time.Duration.Companion.seconds
1616
1717val beatFrame = MutableStateFlow (64 ) // OscSynced.Value("/beats", 64, target = OscSynced.Target.TouchOSC)
1818val beatProgress = MutableStateFlow (0f )
1919
20- // OscSynced.Value("/beatProgress", 0.0f, receive = false, target = OscSynced.Target.TouchOSC).apply {
21- // logSending = false
22- // }
23- val bpmRounded = MutableStateFlow (120f )
24-
25- // OscSynced.Value("/bpmRounded", 120.0f).apply {
26- // logSending = false
27- // }
28- val bpmRoundedInt = MutableStateFlow (120 )
2920
3021private val logger = KotlinLogging .logger { }
3122
@@ -36,36 +27,12 @@ val bpmSynced = OscSynced.ValueSingle<Float>(
3627).also {
3728 it.logReceived = false
3829}
39- // val beatCountSynced = OscSynced.ValueSingle<Int>(
40- // "/Controls/sBpmCnt",
41- // 0, send = false,
42- // target = OscSynced.Target.Nestdrop,
43- // ).also {
44- // it.logReceived = false
45- // }
30+ val bpmRounded = MutableStateFlow (120f )
31+ val bpmInt = MutableStateFlow (120 )
4632
4733val beatCounter = MutableStateFlow (0.0 )
4834
49- suspend fun startBeatCounter (
50- // vararg decks: Deck
51- ) {
52- // @OptIn(FlowPreview::class)
53- // Link.bpm
54- // .sample(100.milliseconds)
55- // .map { bpm ->
56- // (bpm * 10).roundToInt() / 10.0f
57- // }
58- // .onEach {
59- // bpmRounded.value = it
60- // bpmRoundedInt.value = it.roundToInt()
61- // }
62- // .launchIn(flowScope)
63-
64- // beatCountSynced
65- // .onEach {
66- // logger.info { "beat: $it" }
67- // }
68- // .launchIn(flowScope)
35+ suspend fun startBeatCounter () {
6936
7037 @OptIn(FlowPreview ::class )
7138 bpmSynced
@@ -75,7 +42,7 @@ suspend fun startBeatCounter(
7542 }
7643 .onEach {
7744 bpmRounded.value = it
78- bpmRoundedInt .value = it.roundToInt()
45+ bpmInt .value = it.roundToInt()
7946 }
8047 .launchIn(flowScope)
8148 beatProgress
@@ -91,14 +58,11 @@ suspend fun startBeatCounter(
9158
9259
9360 run {
94- // val beatCounter = MutableStateFlow(0.0)
9561
9662 beatCounter.combine(beatFrame) { beats, beatFrame ->
9763 beats to beatFrame
9864 }.onEach { (beats, totalBeats) ->
9965 if (beats > totalBeats) {
100- // beats -= totalBeats
101- // start += ((bpmFromLink.value / 60_000.0) * totalBeats).milliseconds
10266
10367 logger.trace { " reached $totalBeats beats, resetting to ${beats % totalBeats} " }
10468// var newBeatCounter = beatCounter.value
@@ -119,41 +83,16 @@ suspend fun startBeatCounter(
11983 }
12084 .launchIn(flowScope)
12185
122- // beatCountSynced.onEach {
123- // beatCounter.value = (beatCounter.value+1) % beatFrame.value
124- // }
125- // .launchIn(flowScope)
126-
127- // beatCounter.combine(beatFrame) { beats, beatFrame ->
128- // beats.toFloat() / beatFrame
129- // }.onEach {
130- // beatProgress.value = it
131- // }
132- // .launchIn(flowScope)
133-
134- // decks.forEach { deck ->
135- // deck.presetSwitching.beatFlow(beatCounter.runningHistoryNotNull())
136- // .launchIn(flowScope)
137- // }
138-
139- // decks.forEach { deck ->
140- // deck.presetSwitching
141- // .beatFlow(
142- // beatCountSynced.runningHistoryNotNull()
143- // )
144- // .launchIn(flowScope)
145- // }
146-
14786 flowScope.launch {
14887 var lastLoop = Clock .System .now()
14988// var beats = 0.0
15089// var deck1Switched = false
15190// var deck2Switched = false
15291 while (true ) {
153- delay(10 )
92+ delay(1 .seconds / 60 )
15493 val now = Clock .System .now()
15594 val timeDelta = now - lastLoop
156- lastLoop = Clock . System . now()
95+ lastLoop = now
15796 val beatsPerMillisecond = bpmSynced.value / 60_000.0
15897 val beatsInDuration = beatsPerMillisecond * timeDelta.inWholeMilliseconds
15998 beatCounter.value + = beatsInDuration
0 commit comments