Any function you put in the \finish
key is called just before the Event
is played (this is usually for making final modifications to the event before playback, as the name implies). \callback
is called just after the Event
is played. So, you’re best off doing your recording in the \callback
function. FWIW the function is called with the current event as the environment - nice because you can use ~environmentVariables
to access Event keys, but it also makes it harder to refer to an array from the outside. But, here’s simple example:
(
~degreeHistory = List(); // use a list so you can modify it without making a copy
Pbind(
\degree, Prand((0..6), inf),
\degreeHistory, ~degreeHistory, // add it to your event so you can access inside \callback
\callback, {
~degreeHistory.add(~degree)
}
)
)
A slighly more well-factored version -
- Split your “recorder” Pbind into a separate object and compose with your pattern, to make things more reusable. One “base” history recorder, and one version for each event stream you want to record.
- Use the environment of the Pdef to store things like the event list - this is the easiest way to keep track of objects specific to a Pdef.
- Combine your \callback function with any \callback that was set previously (in the naive case above, you’ll overwrite whatever \callback was there - this is fine for most cases, but can occasionally bite you, so I like to do it this was as a matter of best practice)
(
// This expects two keys to be defined: ~history (the list) and ~recordKey (which key to record)
Pdef(\keyRecorder, Pbind(
\callback, Pfunc({
|event|
event[\callback].addFunc({ // combine your record func with any previous callback that was there (it's okay if its nil)
~history.add(currentEnvironment[~recordKey])
}).postln;
})
));
// Now you can construct different kinds of recorders
Pdef(\degreeRecorder, Pdef(\keyRecorder)).set(\recordKey, \degree, \history, List());
Pdef(\legatoRecorder, Pdef(\keyRecorder)).set(\recordKey, \legato, \history, List());
Pdef(\durRecorder, Pdef(\keyRecorder)).set(\recordKey, \dur, \history, List());
// <> compose it with your pbind.
Pdef(\somePattern, Pdef(\degreeRecorder) <> Pbind(
\dur, 1/8,
\scale, Scale.hexSus,
\octave, 4,
\legato, Prand([0.7, 1, 2], inf),
\degree, Ptuple([
Pseq([0, 0, -1, -1, 0, 0, 2, 3], inf),
Pseq([5, 4, -2], inf).stutter(6),
], inf)
)).play;
)
Pdef(\degreeRecorder).get(\history).postln;