Pbind realt time changes without launching another instance

I’ve read and saw on multiple tutorials that pbinds can be realtime manipulated and launched without invoking another stream.
Whenever I do that , another instance of pbind is launched .
I am also having issues with the variable assigned to a pbind , in the first example it works , var q is used to launch and stop the pbind
In the second example it doesn’t
The differnce is that in the first example I just used a Pbind , the second is a Pbind within a Pdef ,

(
SynthDef(\twoop,
{
|fmamt=2,modoffset=24,moatt=0.001,modrel=0.500,ampatt=0.001,amprel=0.800,pitch=48,outt=0.5|
var mod,modenv,ampenv,carr;

		mod=SinOsc.ar((pitch+modoffset).midicps)*fmamt;
		modenv=EnvGen.ar(Env.perc(moatt,modrel),doneAction:0);
		ampenv=EnvGen.ar(Env.perc(ampatt,amprel),doneAction:2);
		carr=SinOsc.ar(pitch.midicps,(mod*modenv))*ampenv*outt!2;
		Out.ar(0,carr);
}).add
)
//////////
(
q=(
Pbind(
	\instrument,
	\twoop,
	\pitch,Prand([48,53,46,65],inf),
	\dur,Pseq([1,0.5,0.25,0.5,0.25,0.25,0.25,0.5],inf),
	\fmamt,Pseq([2,20,2,1,2,1,8,4],inf),
	\modrel,Prand([0.425,0.525,0.500,0.550,1,0.625,0.500,0.5,0.8],inf),
	\modoffset,Prand([-0.6,24.3,0.2,12.5,-12.2,12.5,0.3,0.6,0.3],inf),
	).play
)
)
q;
q.play;
q.stop;

(
SynthDef (\simple,
{
|att=0.001,dec=0.200,mod=500,pitch=0,filterfreq=400,reso=0.5|
var sig,env;

		env=EnvGen.ar(Env([0,1,0],[att,dec],[0,-5]),doneAction:2);
		sig=Pulse.ar(pitch.midicps,mul:0.5)+Saw.ar((pitch+0.1).midicps,mul:0.5);///////I an
		sig=RLPF.ar(sig,filterfreq+(env*mod).clip(40,20000),rq:reso);
		sig=sig*env!2;
		Out.ar(0,sig)
}).add
)




(
~ward=(
Pdef(\qwe,
	Pbind(\instrument,\simple,
		\dur,Pseq([0.12,0.12,0.25],inf),
		\pitch,Pseq([60,67,71,48,50,55],inf)-3,
		\filterfreq,Prand([400,440,600,400,2000,400,900,350,3000],inf),
		\dec,Prand([0.125,0.125,0.125,0.7],inf),
		\att,Prand([0.001,0.1,0.001,0.001],inf),
		\reso,Pseq([0.5,0.5,0.5,0.2,0.5,0.5,0.1],inf),

	).play
)
)
)

~ward.play;
~ward.stop;
/////

Ahh , this was it
assigning the variable to Pbind within Pdef
Still curious how to apply realtime changes without restarting the pbind


(
SynthDef (\simple,
{
|att=0.001,dec=0.200,mod=500,pitch=0,filterfreq=400,reso=0.5|
var sig,env;

		env=EnvGen.ar(Env([0,1,0],[att,dec],[0,-5]),doneAction:2);
		sig=Pulse.ar(pitch.midicps,mul:0.5)+Saw.ar((pitch+0.1).midicps,mul:0.5);///////I an
		sig=RLPF.ar(sig,filterfreq+(env*mod).clip(40,20000),rq:reso);
		sig=sig*env!2;
		Out.ar(0,sig)
}).add
)





(
Pdef(\qwe,
	~ward=Pbind(\instrument,\simple,
		\dur,Pseq([0.12,0.12,0.25],inf),
		\pitch,Pseq([60,67,71,48,50,55],inf)-3,
		\filterfreq,Prand([400,440,600,400,2000,400,900,350,3000],inf),
		\dec,Prand([0.125,0.125,0.125,0.7],inf),
		\att,Prand([0.001,0.1,0.001,0.001],inf),
		\reso,Pseq([0.5,0.5,0.5,0.2,0.5,0.5,0.1],inf),

	).play
)
)


~ward.play;
~ward.stop;
/////

You should call play on the Pdef, not the Pbind. And also, now you don’t have to provide a variable name for the Pbind. The Pdef creates the namespace.

(
Pdef(\qwe, 
	Pbind(
		\instrument,\simple,
		\dur,Pseq([0.12,0.12,0.25],inf),
		\pitch,Pseq([60,67,71,48,50,55],inf)-3,
		\filterfreq,Prand([400,440,600,400,2000,400,900,350,3000],inf),
		\dec,Prand([0.125,0.125,0.125,0.7],inf),
		\att,Prand([0.001,0.1,0.001,0.001],inf),
		\reso,Pseq([0.5,0.5,0.5,0.2,0.5,0.5,0.1],inf),
		
	)
)
)

Pdef(\qwe).play
Pdef(\qwe).stop

Also, you may want to check out Pwrand - it’s a weighted random that’ll save you from typing the same number over and over in your Prands when you want a higher probability of one choice over another.

Sorry but it doesn’t work over here
At least when exucuting .play ,
When launched for the first time (selecting all code ) and making changes to some Pseq’s then executing play , another instance is Pseq is launched



(
SynthDef (\simple,
{
|att=0.001,dec=0.200,mod=500,pitch=0,filterfreq=400,reso=0.5|
var sig,env;

		env=EnvGen.ar(Env([0,1,0],[att,dec],[0,-5]),doneAction:2);
		sig=Pulse.ar(pitch.midicps,mul:0.5)+Saw.ar((pitch+0.1).midicps,mul:0.5);///////I an
		sig=RLPF.ar(sig,filterfreq+(env*mod).clip(40,20000),rq:reso);
		sig=sig*env!2;
		Out.ar(0,sig)
}).add
)



(
Pdef(\whatever,
Pbind(\instrument,\simple,
		\dur,Pseq([0.12,0.12,0.25]/0.5,inf),
		\pitch,Pseq([60,72,71,48,50,74,55,67],inf),
		\filterfreq,Prand([400,440,600,400,2000,400,900,350,3000],inf),
		\dec,Pseq([0.125,0.125,4.125,0.7,0.25],inf),
		\att,Prand([0.001,0.1,0.001,0.001],inf),
		\reso,Pseq([0.5,0.5,0.5,0.2,0.5,0.5,0.1],inf),

	).play
)
)
////
Pdef(\whatever).play
Pdef(\whatever).stop
//////////

Same issue , I don’t get any console errors but play and stop don’t work when executed for the first time .


////
(
SynthDef(\twoop,
{
|fmamt=2,modoffset=24,moatt=0.001,modrel=0.500,ampatt=0.001,amprel=0.800,pitch=48,outt=0.5|
var mod,modenv,ampenv,carr;

		mod=SinOsc.ar((pitch+modoffset).midicps)*fmamt;
		modenv=EnvGen.ar(Env.perc(moatt,modrel),doneAction:0);
		ampenv=EnvGen.ar(Env.perc(ampatt,amprel),doneAction:2);
		carr=SinOsc.ar(pitch.midicps,(mod*modenv))*ampenv*outt!2;
		Out.ar(0,carr);
}).add
);
//////////

(
Pdef(
	\crt,
	Pbind(
	\instrument,
	\twoop,
	\pitch,Prand([48,53,46,65],inf),
	\dur,Pseq([1,0.5,0.25,0.5,0.25,0.25,0.25,0.5],inf),
	\fmamt,Pseq([2,8,2,1,2,1,8,4],inf),
	\modrel,Prand([0.425,0.525,0.500,0.550,1,0.625,0.500,0.5,0.8],inf),
	\modoffset,Prand([-0.6,24.3,0.2,12.5,-12.2,12.5,0.3,0.6,0.3],inf),
	).play
)
)
/////////////////
Pdef(\crt).play;
Pdef(\crt).free;
Pdef(\crt).stop;
///////////

You are having a problem because you are calling .play on the Pbind and the Pdef

remove the .play from just after the closing paren of the Pbind.

This is because when you call .play. on the Pbind sclang returns an EventStreamPlayer.

See below to see .play in the correct position

////
(
SynthDef(\twoop,
{
|fmamt=2,modoffset=24,moatt=0.001,modrel=0.500,ampatt=0.001,amprel=0.800,pitch=48,outt=0.5|
var mod,modenv,ampenv,carr;

		mod=SinOsc.ar((pitch+modoffset).midicps)*fmamt;
		modenv=EnvGen.ar(Env.perc(moatt,modrel),doneAction:0);
		ampenv=EnvGen.ar(Env.perc(ampatt,amprel),doneAction:2);
		carr=SinOsc.ar(pitch.midicps,(mod*modenv))*ampenv*outt!2;
		Out.ar(0,carr);
}).add
);
//////////

(
Pdef(
	\crt,
	Pbind(
	\instrument,
	\twoop,
	\pitch,Prand([48,53,46,65],inf),
	\dur,Pseq([1,0.5,0.25,0.5,0.25,0.25,0.25,0.5],inf),
	\fmamt,Pseq([2,8,2,1,2,1,8,4],inf),
	\modrel,Prand([0.425,0.525,0.500,0.550,1,0.625,0.500,0.5,0.8],inf),
	\modoffset,Prand([-0.6,24.3,0.2,12.5,-12.2,12.5,0.3,0.6,0.3],inf),
	)
)
)
/////////////////
Pdef(\crt).play;
Pdef(\crt).free;
Pdef(\crt).stop;
///////////

Thanks a million times !