sending custom OSC messages with Tidal (or rather, hosc)
TidalCycles communicates with its audio engine (normally SuperDirt) via OSC. message construction and socket I/O is handled behind the scenes, freeing the musician from having to think about it. but sometimes you may want to send custom, one-shot OSC messages from the Tidal environment. maybe you want to activate a complex effects chain in SuperCollider, or tell a visualizer to change scenes, or something, without leaving the Tidal code window.
the way to do this actually doesn't really involve Tidal at all. but Tidal runs in a regular Haskell interpreter, so we can invoke other Haskell libraries, in particular hosc. Tidal itself uses hosc, so it doesn't need to be installed separately.
let's say we want to send messages to the localhost IPv4 address on port 12345. the following code opens a UDP socket for this:
import qualified Sound.Osc.Fd as O
import qualified Sound.Osc.Transport.Fd.Udp as O
sock <- O.openUdp "127.0.0.1" 12345
this could be added to BootTidal.hs to make it happen automatically on Tidal startup. note that sock is an arbitrary variable name, you are free to call it whatever.
with the socket open, we are ready to send messages. the following code sends a message to the OSC address /hello, with integer, float and string arguments:
O.sendMessage sock (O.message "/hello" [O.int32 1, O.float 2.5, O.string "beep"])
a receiver for such messages in SuperCollider might look something like this:
(
OSCdef(\myReceiver, { |msg|
var addr, args;
#addr ... args = msg;
"addr=%, args=%\n".postf(addr, args);
}, "/hello", recvPort: 12345);
)
this splits incoming messages into separate variables for address and arguments, then prints them, producing output like:
addr=/hello, args=[1, 2.5, beep]