Send out frequency and amplitude via OSC?

Hi folks!
Does anyone know about getting frequencies and amplitudes send out of SC via OSC?

My test code:

(
SynthDef(\cello, {
arg out=1,amp = 0.4,ampVal,freq=220,hasFreq,high=4000,low=40,gate=1;
var osc1,osc2,lfo,env,sig;
osc1 = SinOsc.ar(freq);
osc2 = Saw.ar(freq);
lfo = 0.1SinOsc.kr(7.5);
sig = (osc1+osc2)0.2;
env = EnvGen.ar(Env.asr(0.06, 1, 0.3), gate, doneAction: 2);
sig = (sig
env)!2;
#freq,hasFreq = Pitch.kr(sig,high,low,high);
freq.poll;
ampVal = Amplitude.ar(sig);
ampVal.poll;
Out.ar(out,sig
(1+lfo));
}).add
)

(
~s1=Pbind(
\instrument,\cello,
\degree, Prand([0,1,3,5,7], inf),
\octave, Prand([3,4],inf),
\dur, Prand([1,2,4],inf),
\amp, 0.5,
).play
)

b = NetAddr.new(“127.0.0.1”, 7771); // create the NetAddr

HERE I WOULD LIKE TO SEND IT: But how to get it out of PBind or UGen Array? :thinking:

b.sendMsg("/freq", freq.asFloat);
b.sendMsg("/amp", ampVal.asFloat);

Warning: the code fragments below are not tested, but should hopefully lead you towards a possible solution.

Pbind pattern is a language construct (i.e. it doesn’t run on the server). You can add an extra key (any not-yet-used-name will do) and use it send OSC every time a new event is generated, e.g.

(
~s1=Pbind(
\instrument,\cello,
\degree, Prand([0,1,3,5,7], inf),
\octave, Prand([3,4],inf),
\dur, Prand([1,2,4],inf),
\amp, 0.5,
\myfunnykey, b.sendMsg("/hello", "there"); 
).play

or if you need access to the pattern keyvalues:

(
~s1=Pbind(
\instrument,\cello,
\degree, Prand([0,1,3,5,7], inf),
\octave, Prand([3,4],inf),
\dur, Prand([1,2,4],inf),
\amp, 0.5,
\myfunnykey, Pfunc({ |event| b.sendMsg("/hello", event[\degree].asString); })
).play;

To send data from a synth (which lives on the server, potentially running on a different computer) you can first send it from the synth to the language using SendReply, then in the language forward it to some other application.

{SendReply.kr(Impulse.kr(1.0), 'the_answer', [40, 41, 42, 43] + SinOsc.kr, 1905)}.play(s); // runs on server
// Impulse.kr(1.0) causes values to be sent to the language once per second; you can increase the number to 
// make it send data faster

// register to receive this message in the language
(
r = OSCresponderNode(nil, 'the_answer', { |t, r, msg| "received!".postln; [t, r, msg].postln; b.sendMsg(...); }).add;
)
r.remove; 

Thanks! Both work. I decided to use the first for now:

~scale=Scale.major;

(
~s1=Pbind(
\instrument,\cello,
\degree, Prand(~scale.as(Array), inf),
//\freq,~scale.degreeToFreq(0.5,60.midicps,1),
\octave, Prand([3,4],inf),
\dur, Prand([1,2,4],inf),
\amp, Prand([0.3,0.4,0.5,0.6,0.7,0.8,0.9],inf),
\msg, Pfunc({ |event| n.sendMsg("/soundVis1", event[\degree].asFloat,event[\amp].asFloat); })
).play;
)

However, I only can get degree and amp working. I want to use degree for live coding in SC, but would like to get frequencies transferred via OSC for visualizations.

I ll update this post, after I find an answer.

The subtlties of Event, which is indeed a complicated class – have to look up this stuff myself over and over again … the Event help file and, if you are brave, the Event source code shed some light on this.

The conversion mechanism is implemented via Functions. The Function ~freq has to be evaluated within the Environment which is passed as Pfunc arg

~scale=Scale.major;
b = NetAddr.new("127.0.0.1", 7771); // create the NetAddr

(
~s1 = Pbind(
    \degree, Prand(~scale.as(Array), inf),
    \octave, Prand([3,4],inf),
    \dur, Prand([1,2,4],inf),
    \amp, Prand([0.3,0.4,0.5,0.6,0.7,0.8,0.9],inf),
    \msg, Pfunc({ |event| 
        var fr = event.use { ~freq.value.postln };
        b.sendMsg("/soundVis1", fr, event[\amp].asFloat); 
    })
).play;
)

See also this thread

https://www.listarc.bham.ac.uk/lists/sc-users/msg59742.html

Daniel

… and also this one concerning a similar question from yesterday

https://www.listarc.bham.ac.uk/lists/sc-users/msg62350.html