Ndef, Pbind and indexes

Hi everyone. I’m still experimenting a lot with SuperCollider, trying to setup my personal live-coding system. In most live-coding environments, patterns can be updated on the fly without restarting each time a particular line is reevaluated. However, the current default behavior of Ndef → Pbind is to swap the old and the new instance each time the line is evaluated:

// Reevaluating the line is restarting the pattern to first index.
Ndef(\restart)[0] = Pbind(\legato, 0.1, \dur, 0.25, \note, Pseq([0,2,4,5,7], inf));

I understand that this is how Ndef works. There is a transition and crossfading, fadeTime long, between the old Pbind and the new Pbind.

What are your techniques to live-update a pattern that is already playing? How do you build patterns incrementally over time by adding / updating keys and values?

EventPatternProxy is the class you’ll need to swap out the definition of a Pattern while it’s playing. More specifically, you probably want to use Pdef, which is an corollary to Ndef but for Patterns, and is built on top of EventPatternProxy.

Ndef(\notes, Pdef(\notes)).play;

Pdef(\notes, Pbind(
	\octave, 3,
	\degree, Pseq([1, 3, 5, 4], inf)
Pdef(\notes, Pbind(
	\octave, 4,
	\degree, Pseq([1, 3, 5, 4], inf)

Re-evaluating this block of code multiple times will still produce only one note, even though nothing has changed inside the pattern:

Pdef(\notes, Pbind(
	\octave, 4,
	\degree, Pseq([1, 3, 5, 4], inf)

The perceived result can be described as: Pseq([1,1,1,1], inf).

u need to set the quant for the Pdef if you want it to change only on the bar lines for example…


Pdef is for replacing the whole Pbind, for replacing key streams see its subclass Pbindef. For replacing single key streams you can use Pdefn, but for replacing more than one, Pbindef is more practical. See Psym for working with Symbols.
I use Pbindef via my wrapper class PLbindef. It can also be used with Chars and Strings to shorten syntax as described in the tutorial “PLx and live coding with Strings”.

Thanks @semiquaver and @dkmayer. I think I know precisely what I am aiming for. I would like the quant value to be automatically deduced from the length of one of the streams. For a melodic pattern, it would mean that the size of key \freq or \note would be the quant value for the pattern.

Pseq([1,2,3], inf) representing an infinite stream, I don’t know if there is a way to extract the information that the pattern is in fact a repetition of three elements.

There’s a way (there’s probably many ways)
The way i know, and use a lot, may require some rethinking and refactoring of your setup.

The magic is in http://doc.sccode.org/Classes/PbindProxy.html

If you implement your Pbinds as PbindProxies you can access the list ([1, 2, 3] in your example) like this:


So assuming you’ve stored your Pbindproxy in ~mybind:

~newquant = ~mybind.at(\freq).source.list.size

I’m typing this on my phone and haven’t fully read the thread, so ymmv!


Just tried your solution. It works like a charm! For reference, I used the following code snippet:

  Ndef(\test)[0] = PbindProxy(\note, Pseq([0,5], inf), \dur, 0.25, \legato, 0.1);
  Ndef(\test).quant = Ndef(\test).source.at(\note).source.list.size;

The quant command is quite long, but that’s not a problem because it will be hidden between a layer of abstraction.

Thanks @alln4tural and thanks everyone for sharing your knowledge!

Could write a nice method for ndef to have this at your fingertips