## Tuesday, August 03, 2010

### Event

The most important subclass of Dictionary is Event:

(
var evt;
evt = Event.new;
evt.play;
)


Much like Arrays can be declared just by square brackets, Events can be declared just with parenthesees:

( ).play;


The sonic results of that line of code might sound familiar to you:

Pbind.new.play


This is not a coincidence! Patterns create Events and play them sequentially. Compare:

Pbind(\freq, 660, \pan, -1).play


and

(freq: 660, pan: -1).play


Note that the syntax for the Event is (key: value, key2: value2) . You can have as many key: value pairs as you want. In the example above, freq and pan are both symbols, both in the Pbind and the event.

Events are Dictionaries and they are created with a bunch of default keys and values. Although, you can use any sort of object as the key for a Dictionary, you should only use symbols as keys for an Event.

The Pbind modifies elements of a default Event and then plays the results of that. To find out what interesting thigns can be changed in an Event, (and therefore also in a Pbind), check out it's helpfile, specifically the section called "Useful keys for notes."

(
Pbind(
\freq, Pseq([550], 1),
\event, Pfunc({ |evt|
evt[\freq].postln;
})
).play
)


The current event is passed as an argument to the function in Pfunc and also the one in Prout. Here, we have created a key, \event, that doesn't have meaning to the Synth, nor to the Event. This is allowed. For example:

(
Pbind(
\freq, Pseq([550], 1),
\foo, \bar
).play
)


Every time the Pbind makes an event, it goes through the list of keys and values in the order that it recieves them. For example:

(
Pbind(
\event, Pfunc({ |evt|
"Pan before: %".format(evt[\pan]).postln;
}),

\pan, Pseq([-1], 1),

\event2, Pfunc({ |evt|
"Pan after: %".format(evt[\pan]).postln;
})
).play
)

The first postln prints out 0, because that's the default value for \pan. The second postln prints out -1. Then it goes back to the first one because the Pbind does not run out the Pseq returns a nil.

Let's make the duration depend on the frequency:

(
Pbind(
\freq, Prand([440, 550, 660, 770], 10),
\dur, Pfunc({ |evt|

var dur;

dur = evt[\freq] / 1000
})
).play
)


Events are one way you can tie together two keys in an event. You can also do this with arrays:

(
Pbind(

[\freq, \dur],
Prout({
var arr, freq, dur;

arr = [440, 550, 660, 770];

10.do({

freq = arr.choose;
dur = freq / 1000;

[freq, dur].yield;
})
})
).play
)


Two or more keys can be grouped together in an array. Then, the stream, which can be a Prout, a Pfunc or any other P-thing, must return an array which is the same length as the key array. The first item in the returned array gets paired with the first item in the key array, the second item goes with the second key and so forth.

#### Summary

• Event is a subclass of Dictionary.
• You can declare an event by just using parenthesis.
• You should only use Symbols as keys for Events.
• Patterns work by creating events over and over again.
• You can access events in Pfunc. It's passed to the function as an argument.
• You can use keys in your Event or Pbind that are not meaningful to the Pbind, the Even or the Synth.
• Events can be used to tie together two different elements in a Pbind.
• Arrays can also be used to tie together two different elements in a Pbind.