- Multimedia Programming with Pure Data
- Bryan WC Chung
- 1119字
- 2021-08-13 16:53:20
Performing arithmetic calculation
Besides the print
object, Pure Data includes numerous objects catered to various purposes. You can check out those objects from the Help menu. To access the general help for Pure Data, you can choose Help | Browser from the menu bar.
Within the help browser, you can further select the topics you would like to reference. Double-click on the title; you will call up the help menu for that topic.
The first two items, Manuals and Pure Data are for general Pure Data reference. The rest are for external libraries shipped with the pd-extended software.
As all digital media elements are represented in numbers, the objects to perform numeric operations are essential. This section will introduce the use of common arithmetic computation. They are addition, subtraction, multiplication, and division. The programming symbols are: +
, -
, *
, /
.
Create an empty patch as described in the previous section and save it with name calculation.pd
in your folder. Put a print
object in the patch window. Put another object on the patch window. Type a plus sign +
inside this object. Connect the outlet of this 'plus' object to the inlet of the print
object.
Note that the plus
object has two inlets, one on the left, one on the right. It is intuitive to think that addition involves two numbers. You need to have two numbers supplied through the two inlets for the calculation. Now, we put two message boxes with two numbers onto the patch and connect them to the inlets .of the plus
object box.
Now, switch to Run Mode by selecting Edit | Edit mode. Click upon the right message box first and then click on the left message box. Note the message from the console window. The order of clicking matters, which we shall be covering in the coming section.
This example always adds the two numbers 7 and 11 together. Nevertheless, message boxes don't offer much flexibility when it comes to changing the numbers in real time for addition. We will replace the two message boxes with number boxes.
First, we delete the two message boxes. To delete items in Pure Data, we click-and-drag the left mouse button to draw a selection rectangle including the two message boxes and press the Delete key on the computer keyboard to delete them.
If you want to delete a connection without deleting the item, you can click upon the connection link. The cursor will change into a cross shape. Press the Delete key and you can delete the link without touching the two other objects.
Going back to our example, we can also delete the print
object and replace it with a number
box. The next step is to create three number boxes from Put | Number (Command + 3 or Ctrl + 3). Connect two of them to the plus
object box inlets and the last one to its outlet. Note that the initial value for the number box is zero.
Now the addition program is ready. We can switch to Run Mode to test it. First, you click on the number box connecting to the right inlet of the plus
object. Type in any number inside the number box; say 11
. Note there are three dots after the number. Once you are fine with the number, press Return/Enter to confirm the number. The three dots will disappear. Note that the number box connecting to the outlet remains zero.
Next, you repeat the step with the left inlet number box. Type in a number and press Return/Enter. Note that once you press Return/Enter, the result number box will show you the correct result, 18 in this case.
We can conclude that the plus
object performs an addition operation between two numbers, one in the left inlet and one in the right inlet. The result will be in the number box connected to the outlet. Nevertheless, the behaviors of the two inlets are different, the sequence is extremely important in Pure Data programming. In Pd terminology, the left inlet is the hot inlet. All other inlets are cold inlets. Only change of values in the hot inlet will trigger the operation, which in this example is an addition process. Change of values in the cold inlet (the right one in this case) will only store the new value in the inlet without initiating an addition process.
Sometimes, it is counter-intuitive to change the number value of the right number box and wait for the left number to change too. Pure Data has another object to tackle this behavior. It is the trigger
object.
If you do not know how to use the trigger
object, you can always right-click or Ctrl-click on it to display the help message. A trigger
object will pass the message it receives to its outlets from a strictly right to left order.
The two float after the trigger are parameters indicating that the trigger
object will pass numeric values to its outlets. All numbers in Pure Data are floating point numbers, that is with decimal. Integer or natural number is a special kind of floating point number that it does not have digits after the decimal point. When you key in a number in the inlet number box, you will see the output from the console window. The message right: 7 appears before the left: 7.
You can also change the value of the number box by clicking-and-dragging it upward or downward to increase or decrease the value by 1. If you press the Shift key and click-and-drag, you can increase or decrease its value by 0.01. In all cases, the right outlet always comes before the left one. With this function, we can enhance our addition operation.
The only difference is the right number box. It goes through a trigger
object with two parameters: bang
and float
. Whenever we change the value of the right number box, its numeric value (float
) will pass to the right outlet first (right to left order) and a bang
message will then pass to its left outlet. The right outlet goes to the cold inlet of the plus
object first with the new number value. After that, the left outlet sends a bang
message to the hot inlet of the plus
object to trigger the addition process to start. It will then perform immediately without waiting for the value change in the left number box.
Once you are familiar with the object, you can write trigger b f
or even t b f
instead of the long form trigger bang float
. As an exercise, you can replace the plus sign with the minus -
, multiplication *
, or division /
signs to try them out.