Wednesday, 6 October 2010

Object-oriented Programming

The type of programming one we are doing with PureData is called object-oriented programming. This way of programming is comprised of objects, classes, messages and a myriad of other components. It has existed for circa 40 years, but only within the last 15 years has its functionality started to be fully appreciated.
It is safe to assume that most software that include use interfaces of menus, windows and similar icons has been designed from an object-oriented programming perspective.

Objects can take many different forms in PureData - from an audio output ([dac~]) to printing data ([print]). Objects have two important characteristics: attributes and behavior.

Attributes: aspects/characteristics of an object.
Behaviour: the actions an object knows how to do - each object has a list of messages it knows, and it responds to them.


These two characteristics are important when working out classes. A class a collection of objects that have similar attributes.


Example

Let's say we have two classes - BrassInstrument and StringInstrument - what attributes would the objects of these groups have?

BrassInstrument
1. Made of brass/similar metal.
2. Requires an airflow to create sound.
3. Sound can be controlled by valves, which direct airflow.
4. The sound produced does not resonate in the body of the instrument.

StringInstrument
1. Made of wood/mostly of wood.
2. Requires string vibration to produce sound.
3. Sound can be controlled by fretting the strings, which changes pitch produced by string.
4. The sound produced resonates in the body of the instrument.
5. Has to be played to produce sound.

What would their behaviors be?

BrassInstrument
1. Dynamics - the musician adjusts his/her playing (inputting data) to make the sound louder (brass instrument responding to data - behaving)
2. Note produced - the musician has the choice of what note to play - this is achieved by changing the direction of the airflow using the valves.

2. Articulation - the musician plays with a legato style, but changes to staccato (for example)
3. Expression - halfway through a performance, the musician starts to play with vibrato
4. No output - instrument not being played.
5. Has to be played to produce sound.

Nos. 2 & 3 are also achieved by using the valves on a brass instrument.


StringInstrument
1. Dynamics - the musician bows strings harder or softer
2. Note produced - the musician has the choice of what string to play and where to fret it (increase pitch by holding string down to neck) - if at all
3. Articulation - the musician plays notes for longer than indicated on sheet music (legato)
4. Expression - the musician bends the note he/she is playing over and over (vibrato).
5. No output - instrument is not being played.
6. Chords - with string instruments, it is possible to produce two sounds at different pitches - called a dryad (a chord). This is achieved by bowing more than one string at a time (double-
bowing)
7. Pizzicato playing - the notes can be plucked as well as bowed. This produces a different sound.

As you can see, although the above classes are similar, the behaviors of the objects within them are achieved in different ways (due to the differing nature of the objects themselves) - and as a result we get two different sounds i.e. the sound of a brass instrument and the sound of a string instrument.

States

A state of an object is determined by adding the values of that object's attributes.

For example, states for a Guitar would be:

1. Not being played.
2. A Bb note being played (for example)

And due to the fact that they are both instruments, states of a Saxophone would be the same - only the messages that control these states differ. For example, if we constructed a saxophone patch in PureData and sent it the message "palm mute", the saxophone would not know hot to behave in accordance with this message and the sound output would not be affected. However, if we sent a guitar patch the same message, the sound would change as a guitar patch would be programmed to behave in the appropriate way in response to this message.

As you can imagine, there are many different classes and objects in PureData. One of the most important is the [dac~] object - this acts as an audio output and as such has two inputs or inlets, which act as a left and right input (for use with a speaker system with stereo capability).

Another object in this class is [adc~]. This acts as the audio input. This means you can process an incoming signal through PureData.

In a seperate class are the number functions - *, /, +, -, pow.
These functions can be made into objects in PureData, and because they have similar attributes and behavior they are in the same class.

As we already know, we can send messages to objects. But what we don't know is that sending a message to an object changes its state. For example:


Above we have a simple oscillating patch, oscillating at 440 Hz. If we add a connect a message box to the oscillating object, and type a number into the message box - we can change the state of the object (in execute mode) by clicking on it. Thus:














No comments:

Post a Comment