2 summary::an environment that represents an action
3 related::Classes/Pattern
4 categories::Collections>Unordered, Streams-Patterns-Events>Events
7 An Event is an Environment that specifies an action to be taken in response to a link::#-play:: message. The key/value pairs within the Event specify the parameters of that action. Most methods, Event inherits from its superclasses, especially from link::Classes/Dictionary::.
13 subsection::Class variables
16 An IdentityDictionary of useful parent events.
18 An IdentityDictionary of Events that define the default values and functions for different aspects of note generation (timing, volume, pitch, server to use, etc).
20 subsection::Creation methods
23 create an event with initial size strong::n::.
27 May be provided as another event which is used to override keys in the event.
29 May be provided as another event which is used to provide default keys for the event without modifying it.
31 If strong::know:: is set to link::Classes/True::, the event will respond to appropriate message calls. See link::Classes/Environment:: for more details.
34 Returns an empty event with link::#.defaultParentEvent:: as parent.
37 Returns an event that describes a pause of strong::dur:: duration.
40 Event types define alternate play functions that are selected by the value of strong::~type::.
42 A name (usually a symbol) for the event type, which can be used to select it
45 A function which optionally takes the server as a first argument
48 Event.addEventType(\happyEvent, { ("I am so happy to be silent sometimes, says" + ~who).postln; });
49 Pbind(\type, \happyEvent, \who, Prand(["Alice", "Bob", "Eve"], inf), \dur, Pwhite(0.1, 1.0, inf)).play;
51 // To a certain degree, it is possible to reuse some of another event type's functionality:
53 Event.addEventType(\happyEvent, { |server|
54 ~octave = [5, 6, 7]; // always play three octaves
55 ~detune = 10.0.rand2; // always play a bit out of tune
56 ~type = \note; // now set type to a different one
57 currentEnvironment.play;
59 Pbind(\type, \happyEvent, \degree, Pseq([0, 1, 2, 3, 4, 4, 5, 5, 5, 5, 4, 2, 3, 2, 3, 1], inf), \dur, Pwhite(0.1, 1.0, inf)).play;
65 method::makeDefaultSynthDef
66 This method is called in order to build the default SynthDef, which is stored under the key strong::\default::
68 SynthDef(\default, { Out.ar(0, Line.kr(0.3, 0, 0.5) * SinOsc.ar(Rand(300, 500.0)) ) }).add; // overwrite default
70 Event.makeDefaultSynthDef; // reset default
77 Play the event. This evaluates the function at strong::\play::.
80 (play: { "I rather do something else: ".post; ~x.postln; }, x: 800.rand).play;
84 Returns the inter onset time - the time delay until the next event in a sequence. This usually depends on strong::\dur:: and strong::\stretch::, but may be overridden by specifying strong::\delta:: directly.
86 Pn((dur: 2, freq:8000)).play;
90 Combines an event given in the argument with the current event. This is used to enable events to be composed.
92 (a: 6, b: 7).next((c: 100));
96 Used by link::Classes/EventStreamPlayer:: to play Events and obtain a time increment.
99 Returns strong::true:: if the event will be played as a rest, and strong::false:: otherwise. See link::Classes/Rest:: for a more complete discussion of rests in event patterns.
102 Calls link::#-asControlInput::.
104 method::asControlInput
105 Enables events to represent the server resources they created in an Event.
107 subsection::Methods that allow Events to provide user control for Synths on Groups
110 Makes the event a control interface to the resultant link::Classes/Synth:: when played.
113 Makes the event a control interface to the resultant link::Classes/Group:: when played. This is experimental, does not work consistently yet.
116 Free the link::Classes/Synth:: or link::Classes/Group::.
119 Pause the link::Classes/Synth:: or link::Classes/Group::.
122 Resume the link::Classes/Synth:: or link::Classes/Group::.
125 Release the link::Classes/Synth:: or link::Classes/Group::.
128 Set a control value in the link::Classes/Synth:: or link::Classes/Group::.
139 Events can be written as a series of key value pairs enclosed in parentheses. Empty parentheses will create an empty event. They may be also used for object prototyping - see link::Classes/Environment:: for more details.
141 subsection::Event as a name space for keeping objects
143 Because of this simple syntax, Events are often used as name space for keeping objects:
145 // using an event to store values
146 q = (n: 10, x: [1, 2, 3]);
147 q[\y] = q[\x] * q[\n]; // similar to ~y = ~x * ~n, but in a separate name space
148 q.y = q.x * q.n; // shorter way to do the same (pseudo-methods)
149 q.y; // [ 100, 200, 300 ]
152 subsection::Event for specifying different things to happen
154 Event provides a link::#.defaultParentEvent:: that defines a variety of different event types and provides a complete set of default key/value pairs for each type. The type is determined by the value of the key strong::\type:: which defaults to strong::\note::. Note events create synths on the server.
156 ( ).play; // the default note
157 (freq: 500, pan: -1) .play; // 500 Hz, panned left
158 (play: { ~what.postln }, what: "hello happening").play; // something else
160 Per default, the play message derives its behaviour from the link::#.defaultParentEvent::, which provides many default values, such as default instrument (\default), note (0), legato (0.8) and so on. Depending on the event type, these may differ completely and need not even represent a sound.
162 subsection::Events and SynthDefs
164 The key used to select what synthdef is to be played is strong::\instrument::. In order to use a link::Classes/SynthDef:: with an Event, send it an strong::add:: message. This creates a description of the SynthDef that the event can consult to determine its control names. The values of these names in the event are used when the event is played. (See link::Classes/SynthDesc:: for details.)
167 SynthDef(\pm, { |out=0, freq=440, amp=0.1, pan=0, gate=1, ratio = 1, index = 1, ar = 0.1, dr = 0.1|
170 PMOsc.ar(freq, freq * ratio, Linen.kr(gate, ar,index, dr), 0, 0.3),
171 XLine.kr(Rand(4000, 5000), Rand(2500, 3200), 1)
172 ) * Linen.kr(gate, 0.01, 0.7, dr, 2);
173 OffsetOut.ar(out, Pan2.ar(z, pan, amp));
177 (instrument: \pm).play;
179 (instrument: \pm, ratio: 3.42, index: 12, freq: 150, ar: 8, dr: 3, sustain: 10).play;
183 The use of link::Classes/OffsetOut:: in the SynthDef prevents irregularities that can result from the interaction of the timing of a sequence of notes and the control rate of the Server.
186 subsection::Multi-channel Expansion
188 If a key relevant to the action is assigned an link::Classes/Array::, the action is repeated on each element of the array:
190 (degree: (0..12)).play; // a diatonic cluster
192 If several keys are assigned arrays, the action is repeated for each element of the largest array.
193 Smaller arrays are rotated through repeatedly. Here are some examples:
195 // every other note of the diatonic cluster: stacked thirds
196 (degree: (0..12), amp: [0, 0.1]).play;
198 // every other note of the semitone cluster: a wholetone cluster again
199 (note: (0..12), amp: [0, 0.1]).play;
201 // every third note of the semitone cluster: a diminished chord
202 (note: (0..12), amp: [0, 0, 0.1]).play;
204 // the same with different sustain times
205 (note: (0..12), amp: [0, 0, 0.1], sustain:[0.1, 0.3, 1.3, 2.5]).play;
207 // timingOffset gives a tempo-relative offset time to each synth
208 (instrument: \pm, ratio: [2.3, 4.5, 1.7], timingOffset: [0, 1.2, 3], sustain: [0.2, 2, 1]).play;
211 In the \note event, all keys multichannel expand apart from: \instrument, \dur, \delta, \strum.
213 subsection::Arrayed Arguments
215 It is possible to assign an array to one of a link::Classes/SynthDef::'s control names. For example:
218 SynthDef(\test, { | out = 0, amp = 0.01, freq = #[300,400,400], pan, gate = 1 |
220 audio = Mix.ar(Pulse.ar(freq, 0.5)); // this is a mixture of three oscillators
221 env = Linen.kr(gate, susLevel: amp , doneAction: 2); // envelope deletes the synt when done
223 OffsetOut.ar(0, audio );
227 Within an event, arrayed arguments of this sort must be enclosed within an additional array to distinguish them from arguments intended for multi-channel expansion.
229 // one synth, use enclosing array to prevent multi-channel expansion
230 (instrument: \test, note: [[0, 2, 4]]).play;
233 (instrument: \test, note: [[0, 2, 4], [6, 8, 10]]).play;
236 subsection::Events and Patterns
238 Events are closely integrated with the Patterns library. Different patterns can be bound to different keys (or collections of keys) to specify the resultant music. See the help files link::Classes/Pattern:: and link::Classes/Pbind:: and the tutorials link::Tutorials/Streams-Patterns-Events4:: and link::Tutorials/Streams-Patterns-Events5:: for more details on Patterns.
240 Patterns that return events may be played on a clock: dur specifies the time between two subsequent events.
242 // Pseq returns one item in the list after the other
245 (note: 2, sustain: 1, dur: 1.5),
246 (note: [5, 7], sustain: 0.5, dur: 0.8),
247 (note: [2, 6], sustain: 1, dur: 0.8)
251 // Pbind binds parameters to events:
254 \note, Pseq([2, [5, 7], [2, 6]]),
255 \sustain, Pseq([1, 0.5, 1]),
256 \dur, Pseq([1.5, 0.8, 0.8])
260 // per-event timing may be specified:
263 \note, Pseq([[0, 9], [5, 7], [2, 6]], inf),
264 \sustain, Pseq([1, 0.5, 1], inf),
265 \dur, Pseq([1.5, 0.8, 0.8], inf),
266 \timingOffset, Pseq([[0, 0.3], [0, 0.01]], inf)
271 Here is an example that illustrates some more of the keys defined by the link::#.defaultParentEvent::. Note that it is equivalent to write code::Pbind(\key, val, ...):: and code::Pbind(*[key: val, ...])::.
275 stepsPerOctave: Pstep(Pseq((2..12).mirror, inf),12), // 3 - 12 tone e.t. scales
276 note: Pseq((0..12).mirror, inf), // play full notes up and down
277 ctranspose: Pwhite(-0.2, 0.2), // detune up to +-20 cents
278 detune: Pwhite(-1.0, 1.0), // detune up to 1 Hz
279 sustain: Prand([0.2, 0.2, 0.2, 4], inf), // notes last 0.2 or 4 seconds
280 // 1 in 6 chance waits 0.8 seconds:
281 dur: Prand([0.2, 0.2, 0.2, 0.2, 0.2, 0.8], inf),
282 db: Pstep(Pseq([-15, -25, -20, -25], inf), 0.8)// 4 beat accent structure
287 subsection::Event's play method
289 When an Event (or any other link::Classes/Environment::) receives a code::use(function):: message, it sets itself to be currentEnvironment, evaluates the function, and restores the original value of currentEnvironment. This allows the function to access and alter the contents of the event using the following shortcuts:
290 code::~keyName:: which is equivalent to code::currentEnvironment.at(keyName):: and
291 code::~keyName = value:: which is equivalent to code::currentEnvironment.put(keyName, value)::.
293 We will write code::~keyName:: whenever referring to the value stored at the key keyName in the event.
295 Here is the definition of Event's play method:
298 if (parent.isNil) { parent = defaultParentEvent };
299 this.use { ~play.value };
302 Thus we can see that the link::#.defaultParentEvent:: is used unless otherwise specified and the function stored in code::~play:: is executed in the context of the Event. It can be replaced in a given event for different behavior:
304 (a: 6, b: 7, play: { (~a * ~b).postln }).play;
307 subsection::Timing control with Event's delta method
309 Events also specify timing within a link::Classes/Pattern::. Event's code::delta:: method returns the value of code::~delta:: or, if that is nil, code::~dur * ~stretch::.
311 Patterns are played by link::Classes/TempoClock::s, which have their own tempo controls. This tempo which can be controlled through code::~tempo:: in the event. Changes to the tempo affect everything else scheduled by the TempoClock, so code::tempo:: provides a global tempo control while code::stretch:: provides a control limited to the one pattern.
313 subsection::The structure of defaultParentEvent
315 method::defaultParentEvent
316 The default event used in most cases. This is a private class variable. See link::#*default::.
318 The default parent event provides the collection of default values and functions needed for the different uses of an Event. These defaults are defined in partialEvents that specify distinct aspects of default parent Event:
320 playerEvent // defines ~play, ~type and ~eventTypes
321 serverEvent // server, group, addAction
322 durEvent // duration, tempo and articulation
323 ampEvent // volume, pan, MIDI velocity
324 pitchEvent // pitch specified in many different ways
325 bufferEvent // buffers on the server
326 midiEvent // defines the sending of midi messages
329 subsection::Useful keys for notes
331 Using Events is largely a matter of overwriting keys. Here is a list of keys useful for defining notes with their default values, grouped by the partialEvent within which they are defined.
335 ## strong::serverEvent keys:::
337 The keys in serverEvent provide the values needed to identify the server to be used and where in the tree
338 of nodes to place the group.
340 server: nil, // if nil, Server.default is used
341 instrument: \default, // this is the name of a SynthDef
342 group: 1, // nodeID of group on the server
343 // whening adding before or after a node
344 // this could be the nodeID of a synth instead of a group
345 addAction: 0, // 0, 1, 2, 3 or \addToHead, \addToTail, \addBefore, \addAfter
346 out: 0, // usually an output bus number, but depends on the SynthDef
349 ## strong::ampEvent keys:::
351 The ampEvent determines volume. Notice that code::~amp:: is a function that determines its value from code::~db::. The user can choose to specify the amplitude directly by overwriting code::~amp:: or to use a decibel specification by overwriting code::~db::.
353 amp: #{ ~db.dbamp }, // the amplitude
354 db: -20.0, // the above described in decibel
355 pan: 0.0, // pan position: -1 left 1 right
356 velocity: 64 // midi velocity
357 trig: 0.5 // trigger value
360 ## strong::durEvent keys:::
362 The durEvent has keys that determine the timing of a note. Notice that code::~sustain:: is a function that uses code::~legato:: to determine the sustain. Like code::~amp:: this can be overwritten to set the sustain directly.
364 tempo: nil, // changes tempo of a TempoClock
365 dur: 1.0, // time until next note (inter-onset time)
366 stretch: 1.0, // inverse of tempo control, specific to the Event's stream
367 legato: 0.8, // ratio of sustain to duration
368 sustain: #{ ~dur * ~legato * ~stretch },
369 lag: 0.0, // delay (in seconds) relative to current time position of Stream
370 timingOffset: 0.0, // delay (in beats) relative to current time position of Stream
371 strum: 0.0 // "breaks" a chord. May be negative, playing the chord backward
372 strumEndsTogether: false // if true, the strummed notes end together (with gated synths)
375 ## strong::pitchEvent keys:::
377 The pitchEvent has the most complex system of functions that provide a variety of useful ways to determine pitch:
379 freq (->440) // determines the pitch directly as a frequency in Hertz
380 midinote (-> 60) // determines pitch as a fractional MIDI note (69 -> 440)
381 note (-> 0) // determines pitch as a scale degree in an ~stepsPerOctave equal tempered scale
382 degree: 0 // determines pitch as a scale degree within the scale ~scale
384 The event also provides a set of transposition keys:
386 mtranspose: 0 // modal transposition of degree within a scale
387 root: 0.0 // transposes root of the scale
388 gtranspose: 0.0 // gamut transposition within the ~stepsPerOctave equal tempered scale
389 ctranspose: 0.0 // chromatic transposition within the 12 tone equal tempered scale
390 harmonic: 1.0 // multiplies the frequency determined by ~midinote, typically to an overtone
391 detune: 0.0 // directly offsets frequency by adding this value
392 midiToCps // a function taking a MIDI note number and turning it into frequency
393 // Normally this is _.midicps, but you can override it for non-ET tunings
395 mtranspose: 0, // modal transposition of degree
396 gtranspose: 0.0 // gamut transposition of note within a ~stepsPerOctave e.t. scale
397 ctranspose: 0.0 // chromatic transposition of midinote within 12 tone e.t. scale
399 octave: 5.0 // octave offest of note
400 root: 0.0 // root of the scale
401 degree: 0 // degree in scale
402 scale: #[0, 2, 4, 5, 7, 9, 11] // diatonic major scale
403 stepsPerOctave: 12.0 //
404 detune: 0.0, // detune in Hertz
405 harmonic: 1.0 // harmonic ratio
406 octaveRatio: 2.0 // size of the octave (can be used with the Scale class)
408 The event calculates with these keys to derive parameters needed for the synth:
410 note: #{ // note is the note in halftone steps from the root
411 (~degree + ~mtranspose).degreeToKey(~scale, ~stepsPerOctave);
413 midinote: #{ // midinote is the midinote (continuous intermediate values)
414 ((~note.value + ~gtranspose + ~root) / ~stepsPerOctave + ~octave) * 12.0;
417 (~midinote.value + ~ctranspose).midicps * ~harmonic;
419 detunedFreq: #{ // finally sent as "freq" to the synth as a parameter, if given
420 ~freq.value + ~detune
425 subsection::Event types
427 An Event responds to a play message by evaluating code::~play:: in the event, which by default uses the event's type to define the action to be performed. See link::Overviews/Event_types::.