Specifications

3.7 Common idioms 35
Boolean logical objects
There are a whole bunch of logical objects in Pd including bitwise operations
that work exactly like C code. Most of them aren’t of much interest to us in this
book, but we will mention the two important ones
||
and
&&
. The output of
||
, logical OR, is true if either of its inputs are true. The output of
&&
, logical
AND, is true only when both its inputs are true. In Pd any non-zero number is
“true”, so the logical inverter or “not” function is unnecessary because there are
many ways of achieving this using other objects. For example, you can make a
logical inverter by using
!=
with 1 as its argument.
SECTION 3.7
Common idioms
There are design patterns that crop up frequently in all types of program-
ming. Later we w ill look at abstraction and how to encapsulate code into new
objects so you don’t find yourself writing the same thing again a nd again. Here
I will introduce a few very co mmo n patterns.
Constrained counting
metro 500
f
mod 8
+ 1
mod 4
mod 3
13
trigger f f f
7
fig 3.28:
Constrained
counter.
We have alr e ady se e n how to make a counter by repeatedly in-
crementing the value stored in a float box. To turn an increasing
or decreasing counter into a cycle for repeated sequences there
is an easier way than resetting the counter when it matches
an upper limit, we wra p the numbers using
mod
. By inserting
mod
into the feedback path befor e the increment we can ensure
the counter stays bounded. Further
mod
units c an be added to
the number stream to generate polyrhythmic sequences. You
will frequently see variations on the idiom shown in Fig. 3.28.
This is the way we produce multi-rate timebases for musical
sequencers, rolling objects or machine sounds that have complex repetitive pa t-
terns.
Accumulator
+ f
0
1 -1
fig 3.29:
Accumu-
lator.
A similar construct to a counter is the accumula tor or integrator.
This reverses the positions of
f
and
+
to create an integrator that
stores the sum of all previous number messages sent to it. Such an
arrangement is useful for turning “up and down” mes sages from an
input controller into a position. Whether to use a counter or acc u-
mulator is a subtle choice. Although you can change the increment
step of the counter by placing a new value on the right inlet of
+
it will not
take effect until the pr e vious value in
f
has been used. An accumulator on the
other hand can be made to jump different interva ls immediately by the value
sent to it. Note the important difference, an accumulator takes floats as an
input while a c ounter ta kes bang messages.