[orx-envelopes] Add trigger id and object functions

This commit is contained in:
Edwin Jakobs
2023-04-26 10:46:17 +02:00
parent a732a0559d
commit 1f6e60faa0
8 changed files with 145 additions and 40 deletions

View File

@@ -5,42 +5,62 @@ package org.openrndr.extra.envelopes
import org.openrndr.Clock
import org.openrndr.extra.parameters.DoubleParameter
class Trigger(val on: Double, var off: Double, val envelope: Envelope)
class Trigger(val id: Int, val on: Double, var off: Double, val envelope: Envelope)
class TrackerValue(val time: Double, val value: Double)
abstract class Tracker<T : Envelope>(val clock: Clock) {
val triggers = mutableListOf<Trigger>()
protected abstract fun createEnvelope(): T
fun triggerOn() {
val t = clock.seconds
triggers.removeAll { !it.envelope.isActive(t - it.on, it.off - it.on) }
triggers.add(Trigger(clock.seconds, 1E30, createEnvelope()))
class TrackerValue(
val time: Double,
val value: Double,
val position: Double,
val envelope: Envelope
) {
operator fun invoke() {
draw()
}
fun triggerOff() {
val t = clock.seconds
triggers.removeAll { !it.envelope.isActive(t - it.on, it.off - it.on) }
triggers.lastOrNull()?.let {
it.off = clock.seconds
fun draw() {
envelope.objectFunction(time, value, position)
}
}
abstract class Tracker<T : Envelope>(val clock: Clock) {
val triggers = mutableListOf<Trigger>()
protected abstract fun createEnvelope(objectFunction: (time: Double, value: Double, position: Double) -> Unit): T
fun triggerOn(
triggerId: Int = 0,
objectFunction: (time: Double, value: Double, position: Double) -> Unit = { _, _, _ -> }
) {
mppSynchronized(triggers) {
val t = clock.seconds
triggers.removeAll { !it.envelope.isActive(t - it.on, it.off - it.on) }
triggers.add(Trigger(triggerId, clock.seconds, 1E30, createEnvelope(objectFunction)))
}
}
fun triggerOff(triggerId: Int = 0) {
mppSynchronized(triggers) {
val t = clock.seconds
triggers.removeAll { !it.envelope.isActive(t - it.on, it.off - it.on) }
triggers.findLast { it.id == triggerId }?.let {
it.off = clock.seconds
}
}
}
fun values(): List<TrackerValue> {
val t = clock.seconds
return triggers.mapNotNull {
val tOn = t - it.on
val tOff = it.off - it.on
return mppSynchronized(triggers) {
triggers.mapNotNull {
val tOn = t - it.on
val tOff = it.off - it.on
if (it.envelope.isActive(tOn, tOff)) {
val v = it.envelope.value(tOn, tOff)
TrackerValue(t, v)
} else {
null
if (it.envelope.isActive(tOn, tOff)) {
val v = it.envelope.value(tOn, tOff)
TrackerValue(t, v, it.envelope.position(tOn, tOff), it.envelope)
} else {
null
}
}
}
}
@@ -48,21 +68,27 @@ abstract class Tracker<T : Envelope>(val clock: Clock) {
fun value(): Double {
return values().sumOf { it.value }
}
}
class ADSRTracker(clock: Clock): Tracker<ADSR>(clock) {
class ADSRTracker(clock: Clock) : Tracker<ADSR>(clock) {
@DoubleParameter("attack", 0.0, 20.0, order = 1)
var attack: Double = 0.1
@DoubleParameter("decay", 0.0, 20.0, order = 2)
var decay: Double = 0.1
@DoubleParameter("sustain", 0.0, 1.0, order = 3)
var sustain: Double = 0.9
@DoubleParameter("release", 0.0, 20.0, order = 4)
var release: Double = 0.9
override fun createEnvelope(): ADSR {
return ADSR(attack, decay, sustain, release)
override fun createEnvelope(objectFunction: (time: Double, value: Double, position: Double) -> Unit): ADSR {
return ADSR(attack, decay, sustain, release).apply {
this.objectFunction = objectFunction
}
}
}
}