CtkEvent a event prototyping system for use with the Ctk objects.

Part of the CompositionToolKit (Ctk) system. See Ctk help for more details

Class Methods

*new(starttime, condition, amp, function, addAction, target, server) - Creates a new instance of CtkEvent.

starttime - an offset from the current time (in play mode) or the starttime of the event (if NRT rendering is

used).

condition - a control for note event creation. If this is a number, the function will be executed that many

times. If an Env, the function will be executed until the duration is satisfied (or, if the Env has a

releaseNode, until .relase is called and the Env has finished). condition can also be a Boolean, or

a function to be evaluated on each execution of the function. If a function is given for the condition,

the internal CtkTimer and instance var 'inc' are available as arguments. The function should return

a Boolean.

amp - an amplitude scaler. Defaults to 1.0.

function - a function to evaluate for every occurance in the CtkEvent (see below for more details).

addAction - a symbol (\head, \tail, \before, \after or \replace) or an addAction id

(see Server-Command-Reference) for and the /s_new section for a more in-depth description)

target - a node ID (synth or group) or an instance of CtkGroup or CtkNote

server - the server to execute the node on. Defaults to Server.default for real-time performance.

collect - a CtkObject to be performed by this event.

next_ - a duration in beats for rescheduling the execution of the CtkEvent function. Defaults to 'nil'. If the value

remains 'nil', no further execution will be allowed, regardless of condition. 0.0 is an allowable value,

but if this is the case, your condition must terminate the execution of the event to avoid an infinite loop.

More details below.

inc - an instance variable telling the user how many times a function has been run.

score(sustain) - returns an instance of CtkScore with notes that are collected from the function while condition

is true. If the condition is a sustaining envelope, the sustain duration can be passed into the method.

play - plays the CtkEvent while the condition is true.

record - plays the CtkEvent, and records the performance into a CtkScore. The score is returned from the

method.

Some notes on CtkEvent functions:

functions in CtkEvent will have five parameters passed into them: theCtkEvent itself, this instances

CtkGroup, the CtkControl where global amplitude data is written to, the current 'inc' value and the server.

While the condition is true AND the CtkEvent's 'next' parameter is notNil, the function will be repeatedly

called. CtkObjects can then be collected and used by the CtkEvent. See the examples below.

Examples:

(

z = CtkNoteObject(

SynthDef(\grain, {arg globenv = 1, outbus, freq, dur, amp = 1;

Out.ar(outbus, Pan2.ar(SinOsc.ar(freq, 0,

Line.kr(amp, 0, dur) * globenv)))

})

);

// 5 iterations

a = CtkEvent(0.0, 5, 5, server: s);

a.function_({arg event, group, envbus, inc, server;

[event, group, envbus, inc, server].postln; // see the args that are passed in

// collect a new CtkNote

event.collect(

z.new(0.0, 1.0, target: group).globenv_(envbus).freq_(440.rrand(880))

.dur_(1.0).amp_(0.01);

);

// set the 'next' time

event.next_(0.2.rrand(1.0));

});

)

a.play; // play it

b = a.record; // play it AND record the performance in a CtkScore

b.items;

b.write("~/Desktop/trashme.aif".standardizePath, options: ServerOptions.new.numOutputBusChannels_(1));

// using an Env to control global amplitudes and as a control

(

a = CtkEvent(0.0, Env([0, 1, 0], [1, 1], \sin, 1), 5, server: s);

a.function_({arg event, group, envbus, inc, server;

// collect a new CtkNote

event.collect(

z.new(0.0, 1.0, target: group).globenv_(envbus).freq_(440.rrand(880))

.dur_(1.0).amp_(0.01);

);

// set the 'next' time

event.next_(0.2.rrand(1.0));

});

)

a.play; // play it

a.release; // release it

b = a.record; // play it AND record the performance in a CtkScore

a.release;

b = a.score(10); // make a score, and use sustain portion of 10 seconds

b.items;

b.play;

b.saveToFile("~/Desktop/trashme.sc".standardizePath);

b.write("~/Desktop/trashme.aif".standardizePath, options: ServerOptions.new.numOutputBusChannels_(1));

// merge the score of a CtkEvent with another CtkScore

y = CtkScore.new;

y.merge(a.score(10)); // create a CtkScore from a, and merge it with another score

y.items;

// Additive synthesis example - Risset Bell

(

var noteob;

noteob = CtkNoteObject(

SynthDef(\bellpar, {arg outbus, freq, dur, amp = 1;

Out.ar(outbus, Pan2.ar(SinOsc.ar(freq, 0, amp) *

XLine.kr(amp, 0.0001, dur, doneAction: 0)))

})

);

f = {arg starttime = 0.0, basefreq = 440, basedur = 10, baseamp = 1;

var ev, amps, dur, freq, freqoff, parfreqs;

amps = [0.37, 0.25, 0.37, 0.67, 1.0, 0.62, 0.55, 0.5, 0.37, 0.5] * baseamp;

dur = [1.0, 0.9, 0.65, 0.55, 0.325, 0.35, 0.25, 0.2, 0.1, 0.075] * basedur;

freq = [0.56, 0.56, 0.92, 0.92, 1.19, 1.7, 2.0, 2.74, 3.76, 4.07];

freqoff = [0, 1, 0, 1.7, 0, 0, 0, 0, 0, 0];

parfreqs = basefreq * freq + freqoff;

parfreqs.postln;

// creates a single bell

ev = CtkEvent(starttime, 1, server: s);

ev.function_({arg event, group, envbus, inc, server;

10.do({arg i;

event.collect(

noteob.new(duration: dur[i], target: group).freq_(parfreqs[i])

.dur_(dur[i]).amp_(amps[i]);

)

})

});

};

g=f.value(baseamp: 0.25);

)

g.play;

// or, add it directly to a new score

a = CtkScore.new;

a.add(f.value(1.0, 660, 20, 0.5))

a.play;

// save the osc Score out to a file

h.saveToFile("~/Desktop/test.sc".standardizePath)

(

// algorithmically create many instances of CtkEvent and add the to a CtkScore;

a = CtkScore.new;

10.do({arg i;

a.add(f.value(1.0.rrand(4.0), 440 * (i + 1), i + 2.0.rand, 0.3));

});

)

// play the CtkScore

a.play;

// an example of running the CtkEvent structure by hand (bypassing the function)

z = CtkNoteObject(

SynthDef(\grain, {arg globenv = 1, outbus, freq, dur, amp = 1;

Out.ar(outbus, Pan2.ar(SinOsc.ar(freq, 0,

Line.kr(amp, 0, dur) * globenv)))

})

);

a = CtkEvent(0.0, Env([0, 1, 0], [1, 1], \sin, 1), 5, server: s);

a.play;

( // run as may times as you want

a.collect(

z.new(0.0, 15.0, target: a.group).globenv_(a.envbus).freq_(440.rrand(880))

.dur_(15.0).amp_(0.01);

);

)

a.release; // closes the env, kills all synths running in the group

// with .record

a = CtkEvent(0.0, Env([0, 1, 0], [1, 1], \sin, 1), 5, server: s);

b = a.record;

( // run as may times as you want

a.collect(

z.new(0.0, 15.0, target: a.group).globenv_(a.envbus).freq_(440.rrand(880))

.dur_(15.0).amp_(0.01);

);

)

a.release; // closes the env, kills all synths running in the group

b.items

b.saveToFile("~/Desktop/test.sc".standardizePath)