Upgrade to OPENRNDR 0.4 snapshot
This commit is contained in:
24
orx-jvm/orx-osc/README.md
Normal file
24
orx-jvm/orx-osc/README.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# orx-osc
|
||||
|
||||
Open Sound Control makes it possible to send and receive messages
|
||||
from other OSC enabled programs in the same or a different computer.
|
||||
Used to create multi-application or multi-device software.
|
||||
|
||||
Can be used to remote control your program from a mobile device,
|
||||
to produce sound in a audio programming environment, make games
|
||||
and networked experiments.
|
||||
|
||||
Orx-osc is a wrapper around javaOSC
|
||||
|
||||
## Usage
|
||||
|
||||
```kotlin
|
||||
// PORT IN and OUT: 57110
|
||||
val osc = OSC()
|
||||
|
||||
osc.listen("/live/track2") {
|
||||
// do something
|
||||
}
|
||||
|
||||
osc.send("/maxmsp/filter", 500, "hz")
|
||||
```
|
||||
5
orx-jvm/orx-osc/build.gradle
Normal file
5
orx-jvm/orx-osc/build.gradle
Normal file
@@ -0,0 +1,5 @@
|
||||
dependencies {
|
||||
def withoutSlf4j = { exclude group: 'org.slf4j' }
|
||||
|
||||
implementation "com.illposed.osc:javaosc-core:0.6", withoutSlf4j
|
||||
}
|
||||
85
orx-jvm/orx-osc/src/main/kotlin/OSC.kt
Normal file
85
orx-jvm/orx-osc/src/main/kotlin/OSC.kt
Normal file
@@ -0,0 +1,85 @@
|
||||
package org.openrndr.extra.osc
|
||||
|
||||
import com.illposed.osc.OSCMessage
|
||||
import com.illposed.osc.OSCMessageListener
|
||||
import com.illposed.osc.messageselector.OSCPatternAddressMessageSelector
|
||||
import com.illposed.osc.transport.udp.OSCPort
|
||||
import com.illposed.osc.transport.udp.OSCPortIn
|
||||
import com.illposed.osc.transport.udp.OSCPortOut
|
||||
import mu.KotlinLogging
|
||||
import java.net.InetAddress
|
||||
import java.net.PortUnreachableException
|
||||
import kotlin.reflect.KMutableProperty0
|
||||
|
||||
private typealias OSCListener = Pair<OSCPatternAddressMessageSelector, OSCMessageListener>
|
||||
|
||||
private val logger = KotlinLogging.logger {}
|
||||
|
||||
@Suppress("unused")
|
||||
class OSC (
|
||||
val address: InetAddress = InetAddress.getLocalHost(),
|
||||
val portIn: Int = OSCPort.DEFAULT_SC_OSC_PORT,
|
||||
val portOut: Int = portIn
|
||||
) {
|
||||
private val receiver: OSCPortIn = OSCPortIn(portIn)
|
||||
private val sender: OSCPortOut = OSCPortOut(address, portOut)
|
||||
private val listeners: MutableMap<String, OSCListener> = mutableMapOf()
|
||||
|
||||
fun <T> send(channel: String, vararg message: T) {
|
||||
if (!sender.isConnected) sender.connect()
|
||||
|
||||
val msg = OSCMessage(channel, message.toList())
|
||||
|
||||
try {
|
||||
sender.send(msg)
|
||||
} catch (ex: PortUnreachableException) {
|
||||
logger.error(ex) { "Error: Could not connect to OUT port" }
|
||||
} catch (ex: IllegalStateException) {
|
||||
logger.error(ex) { "Error: Couldn't send message to channel: $channel" }
|
||||
}
|
||||
}
|
||||
|
||||
fun listen(channel: String, callback: (List<Any>) -> Unit) {
|
||||
val selector = OSCPatternAddressMessageSelector(channel);
|
||||
|
||||
val cb = OSCMessageListener {
|
||||
callback(it.message.arguments)
|
||||
}
|
||||
|
||||
receiver.dispatcher.addListener(selector, cb)
|
||||
|
||||
listeners[channel] = Pair(selector, cb)
|
||||
|
||||
if (!receiver.isListening) this.startListening()
|
||||
}
|
||||
|
||||
infix fun String.bind(prop: KMutableProperty0<Double>) {
|
||||
val channel = this
|
||||
|
||||
listen(channel) {
|
||||
when (val message = it.first()) {
|
||||
is Double -> prop.set(message)
|
||||
is Float -> prop.set(message.toDouble())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun listen(function: OSC.() -> Unit) = function()
|
||||
|
||||
// Cannot be called inside a listener's callback
|
||||
fun removeListener(channel: String?) {
|
||||
val listener = listeners[channel]
|
||||
|
||||
if (listener != null) {
|
||||
receiver.dispatcher.removeListener(listener.first, listener.second)
|
||||
}
|
||||
}
|
||||
|
||||
private fun startListening() {
|
||||
receiver.dispatcher.isAlwaysDispatchingImmediately = true;
|
||||
|
||||
receiver.startListening()
|
||||
|
||||
if (receiver.isListening) logger.info("OSC is listening on port: $portIn")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user