«[beginner question] how to transmit parameter value to a function inside a SynthDef» by pictosonic

on 17 Feb'24 10:26 in parameter

In this code, I have a list of frequencies. When I am calling \synthexample, I provide some parameters, the usual ones (amp, etc) and other like bb and threshold. The bb parameter is taken into account and works. And the threshold parameter is passed to a function which goal is to pick a frequency on a list of frequencies that is greater or equal to the threshold value. The problem is that the threshold value is not taken into account. It works only if I pass to the function a litteral value, such as 200. Also passing \threshold.ir(100) raises an exception at Command+Enter time.

So how to pass a parameter to a function called inside a SynthDef ?

Thank you !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
~freqgold = List[ 29.034441847481, 58.068883694963, 116.13776738993, 232.27553477985, 464.5510695597, 929.1021391194, 1858.2042782388, 3716.4085564776, 7432.8171129552, 14865.63422591, 29731.268451821 ];


~getFirstFreq = { | array = #[0], seuil = 0 |
	var rfreq=0;
	var i=0;
	while({ (rfreq==0) && (i < array.size()) }, { if( array[i] >= seuil , { rfreq=array[i];  }, { } ); i = i+1 });
	rfreq.postln;
	if( rfreq == 0,
		{ rfreq=array[array.size()-1] },
		{ rfreq }
		)
};


SynthDef(\synthexample, { |out, sustain = 1, amp = 0.5, bb = 5, threshold = 500|
	var env = Env.adsr(\atk.ir(0.05), \dec.ir(0.5), \slev.ir(0.4), \rel.ir (1) ).ar(Done.freeSelf, \gate.kr (1));
	var modHz = XLine.kr(1, bb, 10);
	var mod = SinOsc.ar(modHz, mul: 3);
	var f  = ~getFirstFreq.value(~freqgold, \threshold);
	var sig = SinOsc.ar([f, f+mod], mul: 0.2  ) * amp * env;
    Out.ar(out, sig );
}).add;


(instrument: \synthexample,  atk: 0.01, rel: 40, amp: 0.5, bb: 2, threshold: 500).play;
raw 996 chars (focus & ctrl+a+c to copy)
reception
comments
56228375 user 17 Feb'24 18:18

First, you may want to join the scsynth.org forum with questions. This site (sccode.org) is not very suitable for typing explanations. This is more a place to post working snippets. Having said that, what you currently try to do will not work because you mixed up two worlds (don't worry, it's beginner mistake no#1 :) ). The code inside the synthdef runs on the real-time audio server, whereas the function you defined runs in the language (i.e. potentially on a different pc from the audio-server). You cannot call a function like that inside a synthdef because both worlds are separated from each other. There is usually another way to accomplish what you want, but this site is not the best place to dive into the details.

pictosonic user 17 Feb'24 18:42

Thank you for your answer. And sorry about posting at the wrong place, I was not aware about a dedicated forum for questions. May be this post could be deleted.