Evaluating Code


#1

Hello Forum, entirely newb question but not entirely clear to me reading various resources so am asking here:

say I just have a simple bit of code to evaluate:
( //a couple of sines with different arguments
{
SinOsc.ar(100, 0, 0.5)
+ SinOsc.ar(400, 0, 0.4)
+ SinOsc.ar(900, 0, 0.3)
* SinOsc.ar(3, 0, 1)
}.play
)

I trigger it with cmd+enter -> it plays
If I change a value, evaluate it again it triggers again on the original evaluation etc, perpetually building up. I understand if you stop the original and start again this evaluates one at a time but is there a simple key combo to do this for each evaluation? so the changes can be triggered without overlapping evaluations

many thanks


#2

Do you mean the following way using a unique variable for each synth?

(
x = {
SinOsc.ar(100, 0, 0.5)
+ SinOsc.ar(400, 0, 0.4)
+ SinOsc.ar(900, 0, 0.3)
* SinOsc.ar(3, 0, 1)
}.play
)

(
y = {
SinOsc.ar(200, 0, 0.5)
+ SinOsc.ar(800, 0, 0.4)
+ SinOsc.ar(1800, 0, 0.3)
* SinOsc.ar(4, 0, 1)
}.play
)

(
z = {
SinOsc.ar(300, 0, 0.5)
+ SinOsc.ar(1200, 0, 0.4)
+ SinOsc.ar(2100, 0, 0.3)
* SinOsc.ar(5, 0, 1)
}.play
)

x.free // stop
y.release // fade-out
z.release(10) // fade-out in 10 sec.

s.freeAll // stops all synths. You can do it by pressing cmd + . in macOS and Ctrl + . in Linux and Windows.

You could simplify this processes as follows:
(Please refer to regarding the syntaxes in the codes above:
< NamedControl: a better way to write SynthDef arguments >)

// 1. defining a function or defining a SynthDef.

// 1.1. defining a function
(
~chordTrm = {
var freq = \freq.kr(100), amp = \amp.kr(0.2); // I modified the value for amp because I think your value could be too loud.
var trmFrq = \trmFrq.kr(3), trmAmp = \trmAmp.kr(1);
var freqOsc = SinOsc.ar(freq * [1, 4, 9], 0, amp * [0.5, 0.4, 0.3]).sum;
var trmOsc = SinOsc.ar(trmFrq, 0, trmAmp);
freqOsc * trmOsc
}
)

// 1.2. defining a Synth
(
SynthDef(\chordTrm, {
var freq = \freq.kr(100), amp = \amp.kr(0.2);
var trmFrq = \trmFrq.kr(3), trmAmp = \trmAmp.kr(1);
var freqOsc = SinOsc.ar(freq * [1, 4, 9], 0, amp * [0.5, 0.4, 0.3]).sum;
var env = Env.asr.kr(gate: \gate.kr(1)); // to use release in the SynthDef
var trmOsc = SinOsc.ar(trmFrq, 0, trmAmp) * env;
var sig = LinPan2.ar(freqOsc * trmOsc, \pos.kr(0)); // stereo panning is added. Often we use Pan2.ar.
Out.ar(0, sig) // In SynthDef, we must always use Out.ar or OffesetOut.ar. Otherwise, in specific cases ReplaceOut.ar or XOut.ar
}
).add
)

// 2. execute various synths

x = ~chordTrm.play // default values are used
y = ~chordTrm.play(args:[\freq, 200])
z = Synth(\chordTrm, [\freq, 300])

// 3. change the arguments of synths

x.set(\freq, 150);
y.set(\freq, 250, \trmFrq, 5)
z.set(\freq, 350, \trmFrq, 1)

// 4. algorithmic changes of

fork{ 100.do{ arg nthExecution; z.set(\freq, 325, \trmFrq, 2, \pos, sin(nthExecution/10)); 0.05.wait} }

// 5. fade-out
x.release(2); y.release(4); z.release(8)

Have I correctly understood your question in the previous post?


#3

yes! that’s super helpful thanks.

I guess my question more clearly put is: if I simply have only the single recipe, can I stop/start the changes to it with a single evaluation command? [rather than cmd+ender which then overlaps the changes etc]

But your options seem the best logical steps to build something that avoids my problem?

thanks


#4

Look into JITLib:

( //a couple of sines with different arguments
Ndef(\additive, {
SinOsc.ar(100, 0, 0.5)
+ SinOsc.ar(400, 0, 0.4)
+ SinOsc.ar(900, 0, 0.3)
* SinOsc.ar(3, 0, 1)
}).play)

#5

@cian that looks like the ticket thanks!


#6

You are right! JitLib is the best solution for it!


#7

And actually just to correct my own code, this is probably better:

Ndef(\additive).play;
( //a couple of sines with different arguments
Ndef(\additive, {
SinOsc.ar(100, 0, 0.5)
+ SinOsc.ar(400, 0, 0.4)
+ SinOsc.ar(900, 0, 0.3)
* SinOsc.ar(3, 0, 1)
}))
Ndef(\additive).stop

One of the nice things about the JITLib library is that you can start your synth even if it doesn’t do anything.


#8

many thanks @prko @cian this has been a super helpful start : )