Possible to do data sharing within patterns dependent on a selected buffer

I am trying make a key dependent on a selected buffer. Haven’t found any information about this so far.

\buf,  Prand([d['hat'],d['kick']],inf),
\legato, Pfunc{|ev| if(ev[\buf].detect({|item| item == d['kick'][3]})) {t.beatDur*Pkey(\stretch)*0.01} {t.beatDur*Pkey(\stretch)*0.25}}

detect returns an item from the collection – its result is not Boolean, so you can’t use it in if in that way.

Also Pkey inside Pfunc is a nope.

I guess perhaps you’re trying to apply one legato to kicks and the other to hats.

Wouldn’t it be clearer to produce a symbol first, and reduce to the single buffer later?

\bufset, Prand([\kick, \hat], inf),
\buf, Pfunc { |ev| d[ev[\bufset]].choose },
\legato, Pif(Pkey(\bufset) |==| \kick,

\legato could also use Pfunc, but you have to substitute ev[\something] in place of Pkey all three times, not only the one time in your original.


1 Like

I was hesitant posting the original code is something like this but I didn’t post it because it is kind of a mess. The selection of the buf is dependent of the /dur pattern so it is maybe more complicated?

  \buf, Pgate(Pseq([d['dubstep_kick'][3]]++ Pxrand(d['voices'][0..30].flat,1),inf),inf,\step1),
	\crv, -15,
	/*\sustain, Pfunc{|ev| if(ev[\buf].detect({|item| item == d['dubstep_kick'][3]})) {t.beatDur*Pkey(\stretch)*0.01} {t.beatDur*Pkey(\stretch)*0.25}},*/
  \rel, Pkey(\dur)*Pkey(\stretch)*0.25,
  \out, Ptuple([Pwrand([~bus[\plateReverb],0],[20,90].normalizeSum,1),~bus[\audio1]],inf),
	\group, Pfuncn({ ~lowGrp},inf),
).collect({arg ev; ~eventScr=ev })).play(t)

My main suggestion is: You have a nested data structure for buffers: main collection → categories → buffers. Your pattern is choosing from the lowest level in the hierarchy, and then trying to determine from that which category it belongs to. IMO you could streamline that logic by choosing the category first, then choosing the leaf node (the buffer).

… because, the legato calculation depends on the category. It’s complicated now because you don’t have access to the category in the event. If you did have a key for the buffer category in the event, it would be super simple (no need to check array items).

\sustain, Pfunc{|ev| if(ev[\buf].detect({|item| item == d['dubstep_kick'][3]}))

In parentheses here, the expression must return a Boolean. detect does not – it returns either a collection element or nil. So you will get “non-Boolean” errors here.

Perhaps you mean any but IMO it’s better to restructure so that you don’t have to do any array searching here.


In the same Pfunc, you correctly used ev[\buf] but then here you switch to Pkey, which will not work in this context. These need to be ev[\stretch].


Thanks for the help! I got it to work!
Your suggestion on how to order the structure of the buffers is on top of my list. My current folder structure of my library is tidy but it quickly becomes messy inside of SC. Will definitely start putting favorite sounds in folder by categories.

Many thanks!