Software » Max/MSP

Ruby Jitter Example

Since JRuby for Max runs on Java, it can access any of Cycling '74's Java APIs. So you can do things like access the Java Jitter API. Here's an example of how to modify a Jitter matrix using Ruby.

This example requires JRuby for Max.

  1. Copy the text in this box (it's the patcher source code)
  2. In Max's menu, go to File → New from Clipboard
  3. Lock the patcher to interact with it

If all goes well, you should see this patcher and no errors in the Max window. Click the 3 buttons in the order indicated. You can click the last button multiple times to flip the matrix back and forth.

How does it work? Here's the Ruby code, annotated:

# load the Java API and create a matrix
require 'java'
$matrix = com.cycling74.jitter.JitterMatrix.new

# handle matrix messages arriving at jruby object's inlet
def jit_matrix(matrix_name)

  # load the arriving matrix's data
  $matrix.setName(matrix_name)

  # get its dimensions
  width, height = $matrix.dim

  # loop over all the "pixels"
  for x in 0...width do
    for y in 0...height do
      pixel = $matrix.getcell2dInt(x,y)

      # flip the value of one of the pixel's channels
      pixel[0] = 255 - pixel[0]

      # and update it in the matrix
      $matrix.setcell2d(x,y,pixel)
    end
  end

  # send the updated matrix out the jruby object's outlet
  out0 'jit_matrix', matrix_name
end

Also note the [sprintf] object in the Max patcher. This is needed to call the Ruby jit_matrix() method. sprintf wraps the Jitter matrix name in quotes to convert it to a String. Otherwise it would be interpreted as an undefined variable name.