Difference between revisions of "Custom OSC"

From TidalCycles userbase
Jump to: navigation, search
Line 27: Line 27:
  
 
<source>
 
<source>
oscplay = OSC "/play" $ ArgList [("sound", Nothing),
+
let oscplay = OSC "/play" $ ArgList [("s", Nothing),
                                ("vowel", Just $ VS "a"),
+
                                    ("vowel", Just $ VS "a"),
                                ("pan", Just $ VF 0.5),
+
                                    ("pan", Just $ VF 0.5),
                                ("volume", Just $ VF 1),
+
                                    ("cut", Just $ VI 1),
                                ("cut", Just $ VI 1)
+
                                    ("intensity", Just $ VI 0)
                                ]
+
                                  ]
 
</source>
 
</source>
  
 
To define the OSC structure, you start with <source inline>OSC</source>, followed by the OSC "address pattern", in this case "/play". Then you list the message arguments, in order. Each argument is given as a 'tuple', containing the name of the parameter, and its default value.
 
To define the OSC structure, you start with <source inline>OSC</source>, followed by the OSC "address pattern", in this case "/play". Then you list the message arguments, in order. Each argument is given as a 'tuple', containing the name of the parameter, and its default value.
  
In the example above, the first parameter called "sound" doesn't have a default, indicated by the keyword <source inline>Nothing</source>. This means that if no sound parameter is given by a pattern, no OSC message will be sent.
+
In the example above, the first parameter called "s" doesn't have a default, indicated by the keyword <source inline>Nothing</source>. This means that if no s parameter is given by a pattern, no OSC message will be sent.
  
The other arguments in the example have defaults indicated by the keyword <source inline>Just</source>, followed by the type of the argument and its default value. VS gives a default as a string, VF as a floating point number, and VI as an integer. Other available types are VB for true/false boolean values (which are converted to 1 / 0 integer values in the message) and VX for binary 'blobs'. If one or more of these arguments aren't present in a pattern, the message will be sent with these default values.
+
The other arguments in the example have defaults indicated by the keyword <source inline>Just</source>, followed by the type of the argument and its default value. VS gives a default as a string, VF as a floating point number, and VI as an integer. Other available types are VB for true/false boolean values (which are converted to 1 / 0 integer values in the message) and VX for binary 'blobs'. If one or more of these arguments-with-defaults aren't present in a pattern, the message will still be sent with these default values.
  
redspeed = OSC "/mixer/channel/1" $ ArgList [("gain")]
+
Many parameters are defined in [Sound.Tidal.Params](https://github.com/tidalcycles/Tidal/blob/master/src/Sound/Tidal/Params.hs), and available to a tidal session. If you want to send parameters which aren't already defined, you can define them yourself. For example 'intensity' used above needs to be defined, like this:
  
OSC "/{asccolour}/speed"  $ ArgList [("ascspeed", Nothing)],
+
<source>
             
+
let intensity = pF "intensity"
 +
</source>
 +
 
 +
The final thing that needs defining, is a mapping between targets and the OSC message structures they accept. In this case there's only one target that accepts a single kind of OSC message, so it's simple:
 +
 
 +
<source>
 +
let oscmap = [(target, [oscplay])]
 +
</source>
 +
 
 +
Then you can start a 'stream' for turning patterns into OSC like this:
 +
<source>
 +
stream <- startStream defaultConfig oscmap
 +
</source>
 +
 
 +
And start sending a pattern like this:
 +
<source>
 +
streamReplace stream 1 $ s "hello" # cut 1 # intensity 3
 +
</source>
 +
 
 +
You can define some shortcuts like this:
 +
<source>
 +
let x1 = streamReplace stream 1
 +
    x2 = streamReplace stream 2
 +
    x3 = streamReplace stream 3
 +
    x4 = streamReplace stream 4
 +
</source>
 +
 
 +
Then this will work:
 +
<source>
 +
x1 $ s "hello" # cut 1 # intensity 3
 +
</source>
 +
 
 +
This is how d1, d2 etc are defined in BootTidal.hs . Refer to the [the default BootTidal.hs file](https://github.com/tidalcycles/Tidal/blob/master/BootTidal.hs) to see how the other tidal functions are normally defined.
  
 
= Debugging =
 
= Debugging =
  
 
One way to debug OSC is to use a packet sniffer like [https://www.wireshark.org/ wireshark]. You can put "osc" in the filter field (no double quotes) to filter out everything except OSC packets. If you click on an OSC network packet and expand fields you can find a nicely formatted representation of your OSC message.
 
One way to debug OSC is to use a packet sniffer like [https://www.wireshark.org/ wireshark]. You can put "osc" in the filter field (no double quotes) to filter out everything except OSC packets. If you click on an OSC network packet and expand fields you can find a nicely formatted representation of your OSC message.

Revision as of 22:12, 23 May 2020

The following is where in-development documentation will appear for TidalCycles 1.5.0 and later. For older versions, please see Custom OSC - pre 1.5.0.

Open Sound Control (OSC) is a standard network protocol, ostensibly designed for music, but it's really just an easy way to send numbers and other data across a network. A range of live coding and other systems including DAWs (Digital Audio Workstations), visualisers and mixers are compatible with OSC.

Really the one and only job of TidalCycles is to send patterned OSC messages, most often to the SuperDirt audio framework. It's fairly straightforward to configure Tidal to send OSC to another system. It involves specifying and where messages should be sent to (the target) - and the structure of the OSC data that is sent (the shape or format of the message).

First, define a target:

let target =
      Target {oName = "visualiser",   -- A friendly name for the target (only used in error messages)
              oAddress = "localhost", -- The target's network address, normally "localhost"
              oPort = 5050,           -- The network port the target is listening on
              oLatency = 0.2,         -- Additional delay, to smooth out network jitter/get things in sync
              oSchedule = Live,       -- The scheduling method - see below
              oWindow = Nothing       -- Not yet used
             }

The scheduling method for oSchedule can be one of:

  • Live - send OSC messages on time. This is the simplest approach, that will work fine in most cases
  • Pre BundleStamp - send OSC message in batches, ahead of time, with a timestamp on the OSC 'bundle' so that the target can schedule them accurately (therefore avoiding potential network/processing jitter)
  • Pre MessageStamp - as above, but the timestamp applied to the OSC message itself, filling in the two integer fields "sec" and "usec" (you have to include these in the argument list of your osc format, in this case).

Then define the structure of the OSC message. It's worth first spending a bit of time familiarising yourself with the OSC spec. There are two ways to structure the OSC messages that tidal sends. Either as an argument list, or as name-value pairs.

The argument list approach is most common. It looks like this:

let oscplay = OSC "/play" $ ArgList [("s", Nothing),
                                     ("vowel", Just $ VS "a"),
                                     ("pan", Just $ VF 0.5),
                                     ("cut", Just $ VI 1),
                                     ("intensity", Just $ VI 0)
                                   ]

To define the OSC structure, you start with OSC, followed by the OSC "address pattern", in this case "/play". Then you list the message arguments, in order. Each argument is given as a 'tuple', containing the name of the parameter, and its default value.

In the example above, the first parameter called "s" doesn't have a default, indicated by the keyword Nothing. This means that if no s parameter is given by a pattern, no OSC message will be sent.

The other arguments in the example have defaults indicated by the keyword Just, followed by the type of the argument and its default value. VS gives a default as a string, VF as a floating point number, and VI as an integer. Other available types are VB for true/false boolean values (which are converted to 1 / 0 integer values in the message) and VX for binary 'blobs'. If one or more of these arguments-with-defaults aren't present in a pattern, the message will still be sent with these default values.

Many parameters are defined in [Sound.Tidal.Params](https://github.com/tidalcycles/Tidal/blob/master/src/Sound/Tidal/Params.hs), and available to a tidal session. If you want to send parameters which aren't already defined, you can define them yourself. For example 'intensity' used above needs to be defined, like this:

let intensity = pF "intensity"

The final thing that needs defining, is a mapping between targets and the OSC message structures they accept. In this case there's only one target that accepts a single kind of OSC message, so it's simple:

let oscmap = [(target, [oscplay])]

Then you can start a 'stream' for turning patterns into OSC like this:

stream <- startStream defaultConfig oscmap

And start sending a pattern like this:

streamReplace stream 1 $ s "hello" # cut 1 # intensity 3

You can define some shortcuts like this:

let x1 = streamReplace stream 1
    x2 = streamReplace stream 2
    x3 = streamReplace stream 3
    x4 = streamReplace stream 4

Then this will work:

x1 $ s "hello" # cut 1 # intensity 3

This is how d1, d2 etc are defined in BootTidal.hs . Refer to the [the default BootTidal.hs file](https://github.com/tidalcycles/Tidal/blob/master/BootTidal.hs) to see how the other tidal functions are normally defined.

Debugging

One way to debug OSC is to use a packet sniffer like wireshark. You can put "osc" in the filter field (no double quotes) to filter out everything except OSC packets. If you click on an OSC network packet and expand fields you can find a nicely formatted representation of your OSC message.