Popular Posts

Please at least read this, or at least the section that describes controls, or DO NOT download Mugician. Contact me if you like Mugician, b...

Wednesday, December 5, 2012

An OSC / ChucK Windows8 instrument

OSC

Up until now, I had avoided OSC because getting synths that understand it setup correctly was very inconsistent, if not difficult. At the time, I was wringing all I could out of MIDI, or rather unhappily building internal audio engines - knowing that the result would not be as good as a battle hardened synth. I have tinkered with Pd (hard to do non-trivial programming in brute-force visual spaghetti code), SuperCollider (a rather horrible language, but more complete programming capability), and ChucK (a little weird at first, but a great language - but performance is not necessarily great). The other main issue was that before I found myself on an x86 tablet, the GPL license for SuperCollider and ChucK were problematic. On iOS, you end up having to bake everything into a monolithic app.

But I really wanted to offload all signal processing into one of these environments somehow, and found out that ChucK does OSC really nicely. It's pointless (and ridiculous) for me to spend my time implementing an entire windowing system because the native (or just common) toolkits have too much latency, and it's just stupid for me to try to compete with the greatest synthesizers out there. So, I offloaded absolutely everything that's not in my area of expertise or interest. The synthesizer behind it?

Here is a ChucK server that implements a 10 voice OSC synth with a timbre on the y axis (implemented a few minutes after the video above). It's just sending tuples of (voice, amplitude, frequency, timbre):

The two main things about ChucK you need to decipher it are that assignment is syntactically backwards, "rhs => type lhs" rather than the traditional C "type lhs = rhs"; where the "@=>" operator is just assignment by reference. The other main thing is the special variable "now". Usually "now" is a read-only value. But in ChucK, you setup a graph of oscillators and advance time forward explicitly (ie: move forward by 130ms, or move time forward until an event comes in). So, in this server, I just made an array of oscillators such that incoming messages will use one per finger. When the messages come in, they select the voice and set volume, frequency, and timbre. It really is that simple. Here is a client that generates a random orchestra that sounds like microtonal violinists going kind of crazy (almost all of the code is orthogonal to the task of simply understanding what it does; as the checks against random variables just create reasonable distributions for jumping around by fifths, octaves, and along scales):

What is important is the xmit code. So, when I went to implement OSC manually in my windows instrument, I had to work out a few nits in the spec to get things to work. The main thing is that OSC messages are just as simple as can be imagined (although a bit inefficient compared to MIDI). The first thing to know is that all elements of OSC messages must be padded to be multiples of 4 bytes. In combination with Writer stream APIs that don't null terminate for you, you just need to be careful to pad so that there is at least a null terminator with up to 3 extra useless null bytes to pad the data. So, OSC is like a remote procedure call mechanism where the function name is an URL in ASCII, followed by a function signature in ASCII, followed by the binary data (big-endian 32bit ints and floats, etc).

There is no other messaging ceremony required. The set of methods defined is up to the client and server to agree on.

Note that the method signature and null terminators tell the parser exactly how many bytes to expect. Note also, that the major synths generally use UDP(!!!). So, you have to write out things as if messages are randomly dropped (they are. they will be.). For instance, you will get a stuck note if you only sent volume zero once to turn off voice, or would have leaks if you expected the other end to reliably follow all of the messages. So, when you design your messages in OSC, you should make heartbeats double as mechanisms to explicitly zero out notes that are assumed to be dead (ie: infrequently send 'note off' to all voices to cover for packet losses). If you think about it, this means that even though OSC is agnostic about the transport, in practice you will need to at least design the protocol as if UDP is the transport.

OSC Ambiguity

So, the protocol defines little more than a verbose RPC syntax, where the names look like little file paths (where parents are the scope and bottom most file in the directory is the method name to invoke). You can make a dead simple instrument that only sends tuples to manipulate the basic voice spline changes (voiceid, volume, frequency, timbre). It will work, and will be a million times easier than trying to do this in MIDI. Everything, including units is up to you (literal hz frequencies? floating point 'midi numbers' which are log frequencies?, etc.). That's where the madness comes in.

If you use OSC, you must be prepared to ship with a recommendation for a freeware synth (ie: ChucK or SuperCollider), instructions on exactly how to setup and run them, and an actual protocol specification for your synth (because some synth you don't have doesn't know your custom protocol). This is the biggest stumbling block to shipping with OSC over MIDI. But I have finally had enough of trying to make MIDI work for these kinds of instruments. So, here is an OSC version. It really is a custom protocol. The OSC "specification" really just defines a syntax (like Json, etc). Implementing it means nothing for compatibility, as it's just one layer of compatibility. But if you plan on using SuperCollider or ChucK for the synth, it's a pretty good choice. You can scale OSC implementations down to implement exactly and only what you need.