Basic looping with Quartz Composer
2008-02-08
Quartz composer is a visual programming tool from Apple that ships as part of the Developer tools with Mac OS X 10.4 or later. It presents a visual object-oriented programming metaphor around Quartz and Core Graphics that allows you to simply compose graphical effects by connecting inputs and outputs of different objects together, graphically.
You can use QC to build pipelines that respond to a variety of inputs, local or via peripheral interfaces to construct visualisers for a variety of source signals, such as MIDI, audio from the built in mic, video signals from an iSight camera, or even networked events from computers on your internet or LAN. It also can be used to procedurally generate graphics, which you can use to build fancy displays or screen savers. Some of the system screen savers that ship with OS X, like the 'word of the day' or the 'rss visualiser', are actually simple Quartz Composer scripts.
It's an impressive tool, and ships with documentation and some examples of what you can do. You can achieve nice effects quite quickly, but there is still a learning curve to climb. As an example, a common thing you might want to do when constructing simple animating displays, is loop over a set of possible outcomes. Iterators are a common piece of the vocabulary of programming languages, but it took me a little while to figure out how to achieve this with the 'box and string' interface of this tool.
Here is a simplistic solution solution I came up with. This sample patch demonstrates cycling over a fixed set by rendering live video from an iSight onto the surfaces of a 3d spinning cube, applying a cycle of realtime video filter effects to the image.
You can follow the patch from left to right. The brains of the procedure is the multiplexer , which is a patch that selects one out of a set of possible numbered inputs, depending on the value fed into it's Source Index field. In order to generate a periodic iteration over the right integers, I'm employing a linear interpolator, with a range of 0 - 3 , over a duration of 20 seconds. Because this is generating floats, I'm plugging it through a Round patch, that grossly rounds it to the nearest integer, before feeding it to the multiplexer. To get an even rounded cycle, tweak the interpolater range down a step, -0.5 to 2.5. The rest of the sequence is simple - the video input is split through three filters, one of these paths is selected via the looping mechanism, and that output is connected to the Image property of the built-in cube patch.
Here is the 7.1Kb .qtz file . It's not a terribly pretty end result, but it is quite impressive considering that it's such a tiny source file. The looping construct it illustrates is very simple, but could be used to build up any sort of repeated cycle over a set of different input paths, such as image files, or colour tones that you could connect to other patches to build cyclic displays.