1 0
output~
volume dsp 60
mtof
Figure 3—Patch with mtof Object
In the patch we’re creating here we’re not going to use an external MIDI device.
We’re just going to use the facility that mtof provides, which is to turn a MIDI note message into a floating-point number that an osc~ accepts as a value to its inlet for frequency. That way we can catch a glimpse of Pd’s MIDI capabil-ities and more easily program the patch. The result is a surprise for now, but here’s a hint: the number 60 corresponds to the MIDI note message for C on a keyboard.
If you want to test what we’ve got so far, switch to Run mode with CE and press the 1 message connected to the *~, and then the 60 message. You should hear a tone, a little lower than the A we produced in previous patches. To stop hearing the tone press the 0 message. Now switch back to Edit mode.
We’ll add two more objects. Add one right above the 0 message and name it delay 500, and connect it to the 0 message. A delay object, when activated, will wait a certain amount of time and then cause something else to happen.
Next, above the delay, create a new object and type in trigger float bang bang. You read that right. Once you stop laughing, just do it, and I’ll explain.
Using Triggers
A trigger is an object that can cause multiple things to happen at one time.
Now select and delete the connection between the 60 message and mtof, and instead connect the 60 message to the trigger. Connect the leftmost outlet of
the trigger to mtof, the middle output to the delay object, and the rightmost output to the 1 message. Notice that there are as many outlets as there are parameters to the trigger. Objects in Pd can have a dynamic set of outlets, so if we had trigger float bang bang float, for instance, there would be four outlets.
Before we move on, check that your patch looks like this figure.
osc~
mtof
*~
1 60
0
delay 500
trigger float bang bang
output~
volume dsp
Now that we have something to look at, let’s talk about this, starting with trigger. A trigger takes any number of initialization arguments and, when acti-vated, sends its received input through its outlets. Whatever type of input activates the trigger will be sent along all of the trigger’s dynamic outlets and converted according to the type of the outlet.
In our case the triggering event is the message sending its value, which is 60.
The trigger receives that number and sends it as a float to outlet one. It converts the number to a bang and sends a bang to outlets two and three. A bang is a type of event that simply activates an object. It’s like telling an object, “Do whatever it is you do now.” It’s like a little nudge to an object. So now when you press the 60 message, the trigger will send 60 as a float to mtof. It will now also send a bang to the delay and the 1 message.
Let’s talk about the order in which the trigger sends these events, though, because it’s subtle and important. The trigger sends the events right to left.
That means that in this case the 1 message gets a chance to do its thing first,
which is to send a message to the *~ object. When a message receives a bang it’s the same as if you had clicked it. The delay receives its input second, and then the mtof.
Now that you see how a trigger works, consider the delay. If the delay is initial-ized with a value, which in our case is 500, and it receives a bang to the left inlet, it will wait that number of milliseconds and then send a bang to its outlet.
When the trigger sends a bang to the delay it will wait 500 milliseconds, and then send a bang to the 0 message, which will send 0 to the *~ object.
If you think this through, you can construct the reasoning behind this patch.
Here’s what we want to happen:
1. The message box “60” is pressed.
2. The note C will play.
3. After 500 milliseconds the note will stop.
To make that happen, when the message is pressed, telling the osc~ to play the MIDI note, we use a trigger to pass that along to the osc~ via the mtof converter. We also turn up the volume to full by passing 1 to the *~. Then we trigger a delay to count down 500ms and turn the volume off by passing 0 to
*~.
With this deceptively simple patch, we’ve now gained some significant power over event timing in Pd. Bask in the glow of success for a little bit, but let’s not stop there. Add six more messages with the values 58, 56, 58, 60, 60, 60 and connect them each to the left inlet of the trigger. Your patch should look like Figure 4, Mary Had a Little Lamb, on page 25.
Now press the messages in sequence from left to right. If you set up your patch correctly you should hear the notes to “Mary Had a Little Lamb.” Enjoy!
Things to Think About
When the delay turns the multiplier to 0 and the sound stops, do you notice a click or pop from your speakers? Can you reason about why that happens?
Exercise: Using a trigger and delays, make a patch that plays the MIDI notes 60, 58, and 56 one second apart for 500ms. Play around with it; there’s probably more than one way to do it.
Next Up
In this chapter we covered a lot. We created our first Pd patch and discussed objects and how to create them. We saw the difference between scalar values
osc~
mtof
*~
1 60
0
delay 500
trigger float bang bang 58 56 58 60 60 60
output~
volume dsp
Figure 4—Mary Had a Little Lamb
and signals, and used a signal to make our first sounds with Pd. Then we covered a few ways of controlling a signal’s volume and talked about messages and how they could work together with an oscillator to play musical notes.
Next we’ll build some useful tools that will help us visualize the sound we’re generating and control how sound happens over time. These tools will be integral to patches we build in the chapters after that.