[flocking-users] combining with processingjs? and timing accuracy

enrike altern2 at gmail.com
Sun Apr 20 05:15:35 EDT 2014


couple of questions again...

is there a way to make a synth to run but not to play its sound output? 
I am trying to get a rect() in processingjs to change its width 
controlled by a sinOsc from flockingjs. The sinOsc is running at a 
frequency of 0.3. I would like to use that sinOsc only to produce contol 
data, not sound. Maybe I need to set its rate to "control"? but how do I 
do that? I see a reference in the docs but I dont find any example.

Also, the impulse I am using to trigger the callback is running at a 
frequency of 30 but I am getting pretty random updates on the processing 
side. probably ~2 fps. The processing graphics update is around 30fps so 
any changes in the graphics from the processing code are quickly 
updated. Any ideas if I am doing something or if there is some 
bottleneck in the communication between Processingjs and flockingjs? 
this is part of the flockingjs code that deals with this issue

var impulse = flock.synth({
     synthDef: {
         ugen: "flock.ugen.triggerCallback",
         source: {
		id: "carrier",
		ugen: "flock.ugen.sinOsc",
		freq: 0.3, //LFO
         	mul: 1
         },
         trigger: {
             ugen: "flock.ugen.impulse",
             freq: 30, //FPS
         },
         options: {
             callback: {
                 func: doit
             }
         }
     }
  });

/* pjs is an instance to the processingjs graphics set previously by
pjs = Processing.getInstanceById(canvasID);
moveysass() sets the width of the rect drawn by processingjs
*/
function doit(value)
{
	pjs.moveyrass(value);
}


Finally I am also calling a function in flocking from Processing but 
there is like 0.5 sec latency. In Processingjs I do
void mousePressed()
{
   playSine(mouseX);
}

and in Flockingjs I do
function playSine(freq)
{
	sine.play();
	sine.input("sine.freq", freq);
	setTimeout(	sine.pause, 1000 );
}
where the sine var contains a synth with a simple sinOsc

I attach both the scripts with the Flockingjs and Processingjs code in 
case this description is not clear enough.

thanks!

enrike

og., 2014.eko apiren 17a 01:39(e)an, Colin Clark(e)k idatzi zuen:
> Hi Enrike,
>
> I’ve added a new unit generator called “flock.ugen.triggerCallback” that allows you to trigger function calls from within the signal processing engine. Here are links to a demo and some documentation:
>
> http://flockingjs.org/demos/interactive/html/playground.html#triggerCallback
> https://github.com/colinbdclark/Flocking/blob/master/docs/ugens/triggers.md#flockugentriggercallback
>
> In comparison to SuperCollider’s SendTrig, there are some limitations. Since Flocking’s synthDef format doesn’t yet support connecting unit generators up to multiple destinations, you can can only send the “source” input’s value to the callback. If you want to access arbitrary parts of a synth’s state, you can trigger a callback and then use the get() method to get the current values of a synth’s inputs.
>
> For example, here’s how you’d access the frequencies of both the carrier and modulator unit generators in an AM synth:
>
> function logSynthValues() {
>      var values = synth.get(["carrier.freq", "mod.freq"]);
>      console.log(values);
> }
>
> var synth = flock.synth({
>      synthDef: {
> 		ugen: "flock.ugen.triggerCallback",
> 		source: {
>      		id: "carrier",
> 			ugen: "flock.ugen.sinOsc",
> 			freq: 440,
>          	mul: {
>                  id: "mod",
>                  ugen: "flock.ugen.sinOsc",
>                  freq: 1.0,
>                  mul: 0.25
>              }
>          },
>          trigger: {
> 	    	ugen: "flock.ugen.impulse",
>              freq: 1
>          },
>          options: {
>              callback: {
>                  func: logSynthValues
>              }
>          }
> 	}
> });
>
> I hope this helps,
>
> Colin
>
>
> On Apr 16, 2014, at 3:32 AM, enrike <altern2 at gmail.com> wrote:
>
>> oh thats great! thank you
>>
>> ar., 2014.eko apiren 15a 21:37(e)an, Colin Clark(e)k idatzi zuen:
>>> Hi Enrike,
>>>
>>> Yes, this makes perfect sense. There’s currently no way to make an arbitrary callback from the synthesis engine in Flocking, but I’m in the process of implementing something for you. It should be available in the next day or two.
>>>
>>> Colin
>>>
>>> On Apr 14, 2014, at 12:56 PM, enrike <altern2 at gmail.com> wrote:
>>>
>>>> sorry I am kinf of half offline...
>>>>
>>>> this code is very old I just copied and pasted I hope it works fine
>>>>
>>>> (
>>>> SynthDef ( "StereoPlayer" , {
>>>> 	arg outbus=0, buffer=0, amp=1, pan=0, mute=1, start=0, end=1, rate=1, fps=12, index=0; //, trig=1, resetpos=0;
>>>>     	var length, offset, clock, env, sound, left, right, phasor, dur;
>>>>
>>>> 	dur = BufFrames.kr(buffer);
>>>>     	length = (end - start) * dur; // scale from range 0 - 1 to 0 - buflength
>>>>     	clock = LFPulse.kr(rate.abs / length, 0); // loop
>>>>     	length = Latch.kr(length, clock);
>>>>     	offset = Latch.kr(start * dur, clock);
>>>>     	env = EnvGen.kr( Env .new([0,1,1,0], [0.01, 0.98,0.01]), clock, timeScale:length /(rate.abs)) * amp * mute;
>>>>
>>>> 	phasor = Phasor.ar( 0, rate, start*dur, end*dur);
>>>>     	SendTrig .kr( LFPulse.kr(fps, 0), index, phasor/dur); //12 times per sec playhead. Normalised to 0-1 range
>>>>     	#left, right = BufRd.ar( 2, buffer, phasor, 1 ) * env;
>>>>     	Out .ar(outbus, Balance2.ar(left, right, pan));
>>>> }).load(s)
>>>> )
>>>>
>>>> Basically this does the job
>>>> SendTrig .kr( LFPulse.kr(fps, 0), index, phasor/dur);
>>>>
>>>> In this case this is a sample player that is looping a buffer. I needed to display in the graphics a "playhead" that shows where in the buffer the player was, typically going left to right. This kind of thing
>>>>
>>>> But I was also thinking on other things such as the frequency of a sinosc controlling the radius of a circle, for instance. That kind of things, visuals responding to audio units internal states. I am not sure I am clear I am writing very quickly
>>>>
>>>> thanks!!!!
>>>>
>>>>
>>>>
>>>>
>>>> ig., 2014.eko apiren 13a 18:14(e)an, Colin Clark(e)k idatzi zuen:
>>>>> It’d definitely possible to write a unit generator to do this. Just to be sure I fully understand your use case, can you elaborate on what you want to accomplish or send an example of some SuperCollider code? Are you thinking of things like SendTrig in SuperCollider, where a message can be sent from the server to the client? That should be easy to implement.
>>>>>
>>>>> Thanks,
>>>>>
>>>>> Colin
>>>>>
>>>>> On Apr 11, 2014, at 7:22 AM, enrike <altern2 at gmail.com> wrote:
>>>>>
>>>>>> would it be possible to call a function from a ugen like a pulse for instance? or set a variable from a ugen? a bit like in Supercollider you can use triggers from synthdefs. For instance flock.ugen.xLine to control the value of a variable
>>>>>>
>>>>>>
>>>>>> og., 2014.eko apiren 10a 01:55(e)an, Colin Clark(e)k idatzi zuen:
>>>>>>> Hi Enrike,
>>>>>>>
>>>>>>> I haven’t heard of anyone yet who is using Processing.js with Flocking, but I don’t see any reason why it wouldn’t work. Web Audio in general has some risks with very sophisticated visuals because of the fact that the audio processing runs in the same thread as the visuals, but I think there’s lots of room to work with them both.
>>>>>>>
>>>>>>> In terms of the reliability of timing in Flocking, it depends on how you schedule things. At the moment, the asynchronous scheduler is a little bit “drifty,” especially in Firefox. I’m in the midst of planning out a block-accurate scheduler that will have no drift, and I can fast-track that work if it’s a priority for you. In the meantime, I do most of my high priority scheduling within the unit generator graph, by using unit generators like flock.ugen.sequence as well as triggers like flock.ugen.impulse. It’s solid and simple. Here’s an example of a piece I wrote recently that has a very accurate drum pulse in it.
>>>>>>>
>>>>>>> https://github.com/colinbdclark/compositions/blob/master/greenwich-park/js/instruments.js#L4-L60
>>>>>>>
>>>>>>> Colin
>>>>>>>
>>>>>>> On Apr 9, 2014, at 10:00 AM, enrike <altern2 at gmail.com> wrote:
>>>>>>>
>>>>>>>> hi
>>>>>>>>
>>>>>>>> two different questions...
>>>>>>>>
>>>>>>>> has anyone tried to combine Flockingjs with Processingjs?
>>>>>>>> http://processingjs.org/
>>>>>>>>
>>>>>>>> how accurate and reliable is timing using Flocking? I need to work on a percussion system that needs to be quite precise.
>>>>>>>>
>>>>>>>> thanks
>>>>>>>>
>>>>>>>> enrike
>
>

-------------- next part --------------
int yloc, xloc, wsize;
Object pjs;

void setup()
{
  size (500, 400);
  xloc = 50;
  yloc = 50;
  wsize = 200;
  
  // this line gets the instance of this sketch in the flockingjs 
  //code to be able to access functions and variable from this code
  // this could be done directly in flockinjs as long as it is not 
  // too early in the code. Must pass the name of the canvas ID from the index.html 
  pjsInstance("graphics");
 }

void draw()
{
		background(255);
		//xloc+= 5;
		//if(xloc > width) xloc = 0; //reset
  rect(xloc, yloc, wsize, 100);
}

void mousePressed()
{
		println("mousePressed!"); //processing print
		
		// this calls a function in the flocking js module
  playSine(mouseX);
}

/* this is called from processingjs thanks to 
			Processing.getInstanceById() 
*/
void moveyrass(val)
{
  //xloc+= 5;
  wsize = (val+1) * 200; 
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: flockingscripts.js
Type: application/javascript
Size: 1405 bytes
Desc: not available
URL: <http://lists.idrc.ocad.ca/pipermail/flocking/attachments/20140420/37aa5325/attachment.js>


More information about the flocking mailing list