Quantcast

How to get value from an array, based on an argument, within SynthDef

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to get value from an array, based on an argument, within SynthDef

LightRate
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get value from an array, based on an argument, within SynthDef

ddw_music
LightRate wrote
However, I am able to do stuff like 'rate + 100' or 'rate * 1.5'... causing me to wonder why a SynthDef won't let me access arrays in the first place.

At one point, I even tried ~global_scale[~global_scale.indexOf(rate.source.values[0]) + 1] ...no such luck.

So can someone please be the one to tell me that what I'm trying to do is impossible, so I can just give up and move on?
It's a fairly standard confusion, between client (language) and server operations.

The catch is: arrays literally do not exist in the server. No such thing.

You can't index an array in a SynthDef, based on a synth argument, because array indexing is a client-only operation. Because it's client-only, you have to know the index in advance. But a synth argument is a value that you don't know Right Now while building the SynthDef. So that approach won't work.

You'll see some SynthDefs that use arrays internally, and index them based on, say, looping arguments ("array.do { |item, i| ... otherarray[i] }") -- this is ok because the index doesn't depend on any signals -- it comes only from the loop. In that case, the indexing happens during SynthDef building, to determine synth structure, and it's hard-wired into the SynthDef and never changes after that.

If you want "soft" indexing, you need a UGen that fetches one value from an array. That's Select.kr:

Select.kr(index, ~global_scale)

"rate + 100" works because + is not client-only. Math operators calculate Right Now if both operands can do the math Right Now. But if either side is a "later" value (like "rate," if it's a synth arg), then the math operator becomes a "later" operation. If you post the result of rate + 100 in your SynthDef, you will not get a number -- you'll get a BinaryOpUGen.

It takes time to wrap your head around this. It isn't immediately clear in code examples which operations can only be "eager" and which might be "lazy."

hjh
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get value from an array, based on an argument, within SynthDef

LightRate
CONTENTS DELETED
The author has deleted this message.
Loading...