Sunday, March 25, 2012

MicroOSC: a Ruby DSL for OSC

Being on my way to the west coast and out of arm's reach of the synths, I've had a few hours to switch gears and put together a quick Ruby gem, MicroOSC. It's a utility and DSL for dealing with OSC messaging.

gem install micro-osc

I applied the same principle to OSC that I applied to MIDI with MicroMIDI: distilling the simplest messenger interface that I could think of.

Unlike MIDI, OSC deals with generic user-defined messages. This eliminates the need for describing different concrete message types, something that added a lot complexity to MicroMIDI.

MicroOSC can function as a server, a client or both. In this example, I'll demonstrate them separately, having the two programs talk to each other over a local network.

Here is a server:

require "osc"

OSC.using(:input_port => 8000) do

  receive("/greeting") { |val| p "received #{val}" }

  p "Ready to receive OSC messages on port(s) #{input_ports.join(', ')}..."

  wait_for_input

end

Once you have this running, you should see "Ready to receive OSC messages..." in your Ruby console. Switch to another window and run this client program:

require "osc"

o = OSC.using(:output => { :host => "localhost", :port => 8000 })
o.out("/greeting", "hullo!")

After running it, flip back to your server program and see “received hullo!”, confirming that your two programs were in fact talking to each other!

Notice that in the second program I didn't use a Ruby block style. Doing it this way would generally be better for live coding, and probably some other scenarios as well.

That's it!

Look for another post with real-world examples, advanced techniques and combining with MicroMIDI soon.

http://github.com/arirusso/micro-osc