Lab 3: Pd programming, control of synthesis, OSC communication

Assigned: Wednesday, Jan 19
Due date: Weds, Jan 26 (before the start of Lab 4).

Your lab writeup should contain notes about what you did (other than simply following these instructions), what you learned, what things didn't work and how you fixed them. It should also contain the answers to the questions posed in this writeup. You must turn in a copy of your writeup to receive credit for this lab.

Before you begin this lab, check this short introductory guide to Pd and OSC; this will give you enough information to start this lab. Make sure you are familiar with its contents.

The goal of this lab is to start learning Pd and how to get information from the AVR into Pd on Linux. We will begin with a simple sinusoidal oscillator, then control its frequency using the potentiometer value from AVR board. We will also try some of the "physical models" available in Pd, and consider issues of control and mapping.

0. Setting up your environment for Pd

Make yourself a copy of the .pdrc file for setting up your Pd environment:

~> cd
~> rm .pdrc
~> cp /usr/ccrma/courses/250a/pd/class.pdrc .pdrc

Also, copy the flash demo:

~> cp -r /usr/ccrma/web/html/courses/250a/avrlib/osctest avrlib-demos

1. Controlling the frequency of a simple sinusoidal oscillator from Pd.

1a. Make yourself a copy of the flash demo:

~> cp -r /usr/ccrma/web/html/courses/250a/avrlib/osctest avrlib-demos

1b. Create a new Pd patch of sinusoidal oscillator.

To make this, you might want to review the instructions of the Pd and OSC guide.

  1. Start pd: type "pd" in a terminal window
  2. This should print some info in the terminal window (about how Pd is configured) and open a new window titled "Pd". Pd will continue to print important message into the terminal window from which you launched it, so make sure to keep that terminal visible as you add more pd windows.
  3. Create a new patch: "File" menu/"New" or control-n
  4. Now you should have another window titled "Untitled-1".
  5. Note that this window is in "Edit" mode because the mouse cursor looks like a hand with a pointing finger instead of an arrow.
  6. Put a new object into the window: "Put" menu/"Object" or control-1. Now there should be a blue rectangle (your new object) attached to the mouse cursor.
  7. Click anywhere in your patch to place the new object there. The object is blue to indicate that it's currently selected, and there is a flashing cursor on the left of the object to indicate that you should now type the name of the object.
  8. Type the name of the object you wish to create, "osc~"
  9. Click anywhere in the patch outside the object box. Pd should now instantiate a osc~ object and you should see two inlets and one outlet.
  10. Right-click on the osc~ object to bring up a contextual menu.
  11. Scroll down to "Help" and let go of the right mouse button. This should open the help patch for osc~. Note that the help patch is not in "Edit" mode.
  12. Put another object named "dac~" into the window. See its contextual help patch and try to understand its function.
  13. Connect the outlet of "osc~" to the left inlet of "dac~".
  14. Now, put a "Number" ("Put" menu/"Number", or control-3) to specify the frequency. Connect its output to the frequency input (left inlet) of the "osc~". Notice the difference between the two connections you made.
  15. Get out of the edit mode ("Edit" mode, or control-e), and change the value of the number object by dragging the mouse over it.

1c. Play sound with the patch you created:

  1. Put on the headphones connected to the machine you're using.
  2. To protect your ears, turn down the volume of your headphones (using the "level" knob immediately to the left of where your headphones plug into the omni i/o box) to the 12 o'clock position or lower.
  3. Turn the audio on; you can either push the "compute audio" button in the Pd main panel, or press control-/. Adjust your frequency to audible range. Make sure you hear a pure sine wave and you can control its frequency.
  4. To turn the audio off, toggle the "compute audio" button or press control-.(dot).

2. Controlling the STK marimba~ synthesizer from Pd.

2a. Open the help patch for the marimba~ object:

  1. Create a new patch: "File" menu/"New" or control-n.
  2. Put a new object anmed "marimba~" into the window. You should see six inlets and one outlet.
  3. Right-click on the marimba~ object to bring up a contextual menu.

2b. Play sound with the marimba~ help patch:

  1. At the bottom of the help patch is an object called "pd output". (You can tell it's an object box because it's perfectly rectangular.) The middle inlet to this object comes from a number box which defaults to zero. (You can recognize the number box by the diagonal "cut-out" in the upper right-hand corner.) This is the volume (in dB) of the output, with 100 dB defined to mean the maximum digital amplitude available to Pd. Turn the volume up all the way to 100 by clicking on it and dragging upwards.
  2. At the top of the help patch, underneath the text "try these presets" is a horizontal radio button. Click on the leftmost square to set all of the synthesis parameters to predetermined values. Now click on the message box with the text "noteon" to play a note with the current parameters. (You can tell it's a message box because of the semicircular "cut-out" on the right side.) You should hear a note. (Don't bother trying the other two squares: if you look inside the "pd mypresets" object you'll see that only one preset is defined.)
  3. Click on the "noteon" message box again" to play another note with the current parameters.
  4. Change one of the parameters by clicking and dragging up or down on one of the vertical sliders. Notice that the number box underneath the slider changes to display the value you've selected.
  5. Click on "noteon" again to play a new note with the changed parameter.

2c. Get to know the marimba~ object.

  1. Try to understand the relationship between the control inputs and the resulting sounds. There are six handles to control the STK marimba~ patch, and buttons to mAke it play a note and stop playing a note. Say something qualitative about the effect of each handle. Which handles change the effect of other handles? What are the units of the parameters that the handles control?
  2. Find a set of parameters that produce a note that sounds reasonably like a real marimba. (Don't spend more than a couple of minutes fine-tuning the parameters. If you don't know what a real marimba sounds like, just try to find a note that sounds "realistic" to you.)
  3. Find a second set of parameters that produce a note that sounds nothing like a marimba.
  4. Play these two sounds for a TA.

2d. Open the help patch for osc-marimba~

Matt Wright has written a patch called osc-marimba~ that is just a "wrapper" for the real marimba~ object. In other words, the osc-marimba~ patch has a marimba~ object inside that does all the synthesis work; osc-marimba~ exists only to provide an OpenSoundControl (OSC) interface to the object. By "OSC interface" we mean that you control the osc-marimba~ object entirely by sending it OSC messages, for example, the OSC message "/pitch 65" sets the pitch parameter of the object to 65 (which is the F above middle C).

  1. Open the help patch for osc-marimba~ (following the same procedure as above). Note that osc-marimba~ has only one inlet, not six. This inlet takes OSC messages; since each OSC message has a symbolic address indicating what parameter is being controlled the object can take all control messages through a single inlet.
  2. Turn up the volume and play with this help patch. You'll see that there's nothing new in terms of what sounds you can make, but that there are some new ideas about how you can control these sounds.

2e. Design a new control structure for the marimba patch

Make up your own interface in Pd for control of the marimba patch. We recommend that you make your own copy of help-osc-marimba~.pd with "Save as...", and use that as a starting point. Don't forget that you'll need to type Control-E to toggle the patch between edit mode and regular mode.

Your interface should include one or more of these ideas:

  1. Dimensionality reduction: some of the user's handles control more than one of the marimba~ object's handles. A very simple example would be a single "vibrato amount" slider that affected both the vibrato frequency and the vibrato gain.
  2. Reduced choices: Instead of making every possibility of marimba~ available to the user, select a smaller "space" of parameters to work in. Examples include limiting the range of a parameter (e.g., a /hardness control that goes only between .3 and .5), making the input discrete (e.g., the C harmonic minor scale example in the help patch), or eliminating a parameter entirely from the interface (e.g., making /stick-pos always 0.5).
  3. Giving over control: One user action causes multiple sonic results over time. Look at the delay, metro, and line objects.
  4. Modes: some user actions have different results depending on some kind of mode that's set with other user actions. An example would be the ability to select different scales for the horizontal radio button interface.
  5. Polyphony: put multiple osc-marimba~ objects in your patch, wire all their outputs together (so their outputs will be mixed to your headphones), and send different control messages to them.

3. Controlling Pd from a sensor via the AVR and OSC

3a. Reading continuous sensors with the a2d converter

  1. Compile and load the A2DTEST demo program:

    ~> cd avrlib-demos/a2dtest
    ~/avrlib-demos/a2dtest> make load

  2. Connect a varying voltage to PA1 (the second protected pin from the left on port A). You should see the voltage displayed on the AVR's LCD screen.
  3. Make sure you understand the use of the a2dConvert10bit() procedure.

3b. Testing your AVR/Pd OSC connection

The AVR can send OSC messages to the Linux machine via the same serial connection that you use to program it.

  1. Compile and load the OSCTEST program onto the AVR:

    ~> cd avrlib-demos/osctest
    ~/avrlib-demos/osctest> make load

  2. Open the osctest.pd patch in Pd. The easiest way to do this is to quit Pd if it's already open and then start Pd from the Unix command line with the name of the patch as an argument:

    ~/avrlib-demos/osctest> pd osctest.pd

    Make sure you understand the use of dumpOSCSerial and OSCRoute (remember that OSC supports URL-like, hierarchical address space structure).
  3. You should see that the AVR is communicating with the Pd patch.
  4. Study the osctest.c program to see how to send OSC messages from the AVR. Pay attention to the use of oscSendMessageInt and oscSendMessageIntInt. For more information, refer to the osc.h file which is located in ~/avrlib/ccrma/.

3c. Sending A/D converted data through OSC

  1. Download, compile and load the OSCA2D program onto the AVR (Note: this hasn't been added to the avrlib-demo directory yet. Please copy from the following path):

    ~> cd
    ~> cp -r /usr/ccrma/web/html/courses/250a/lab3/osca2d 250a/lab3-3c
    ~> cd 250a/lab3-3c
    ~/250a/lab3-3c> make load

  2. Open the osca2d.pd patch in Pd.
  3. Use a potentionmeter as an input (as done in lab 1) to the pin 1 of port A. See if Pd can receive the message from the AVR as you vary the potentiometer value. Try every pin of port A, and see if they work as expected.
  4. Now, make unused 3 inputs of port A grounded and see the input values. Can you see the difference?
  5. In the Pd patch, connect the incoming potentiometer value to the left inlet of osc~ in the bottom. Turn on sound, and make sure you can change the pitch of the oscillator using the potentiometer.
  6. Study the osca2d.c program to see how to send A/D converted valuess as OSC messages.

3d. Control Pd with continuous sensors

  1. Design a way to control the marimba synthesizer with two handles and zero to four buttons. You can use the sensors you already have or try others including the alternative sensors available in the MaxLab and described here.
  2. Wire up two continuous sensors to A0 and A1.
  3. You can use the buttons built into the AVRmini board, or, if you prefer, you can wire up other buttons and modify the C program to read them and send them via OSC.
  4. Make a Pd patch that takes in the sensor values, scales and maps them appropriately, and uses them to control osc-marimba~. You may want to implement some features of your "instrument" on the AVR by changing the C program, but feel free to use osctest.c unchanged and do everything in Pd.
  5. Demonstrate this to a TA.
  6. Extra Credit: this program is not as efficient as possible, because it sends the state of each button (which is only one single bit of information) as a 32-bit integer. Instead of having separate OSC messages /button/4, /button/5, etc., change your C program to read the entire byte from PINB (using the inb() procedure to read an entire byte) and then make the corresponding change in your Pd program to separate the individual bits of the single incoming integer so that they control individual toggle objects as before. (Hint: use the & object.)
  7. Note: if you want to load a new version of your program to AVR, make sure you close any open Pd patch that is using dumpOSCSerial; otherwise the serial port won't be available for downloading.

4. Controlling a different physical model

Look at the brass~ and osc-brass~ Pd objects. This synthesizer has totally different parameters from marimba~; even the pitch control works differently. Explore this synthesizer and design a way to play it.

Demonstrate this to a TA.