fluid simulator in supercollider

clean-up: #44

this is a supercollider port of Memo Akten's processing library MSAFluid. i looked at his version 1.3.0 and rewrote it for sc. i also optimized it as much as i could but still it is slow and the framerate is very low. i blame the slow sc graphics. the fluid system itself should perform ok - it's just the drawing that doesn't live up to being useful.
the good news is that one doesn't need use the fancy graphics. sc is anyway made for sound synthesis and the fluid simulator can run and particles can be mapped to sound without displaying anything.

classes, helpfiles and examples are attached as a zip archive below.

msafluid2d from redFrik on Vimeo.

Package icon MSAFluidSolver2D.zip15.74 KB

failed tweet

clean-up: #43

during today's clean-up-of-old-projects i got stuck shrinking an old piece of supercollider code down to under 140 characters (sctweet). the shortest i could get it down to was 165 characters. still i thought i'd post the results of my efforts here - both if someone have suggestions or if someone want to study the process. it's the same code, just rewritten over and over to be shorter - using every trick i could think of.

of course one could imagine other goals than briefness - better sounding or less cpu taxing are two (probably better and more) common goals when optimizing code in this manner.

n=Pbind(\tempo,2.5,\freq,Pseq([Pseq([103.8],14),Pseq([82.4],14)],inf),\amp,Pseq([2,1,1,2,2,1,1]/2,inf),\dur, 0.5);Ptpar([0,n,5,Pmul(\dur,2,Pmul(\freq,2,n)),15,Pmul(\freq,3,n),30,Pmul(\freq,3.5,n),80,Pmul(\dur,3,Pmul(\freq,5.035, n)),140,Pmul(\dur,1,Pmul(\freq,8,n))]).play

n=Pbind(\tempo,2.5,f=\freq,Pseq([Pseq([103.8],14),Pseq([82.4],14)],inf),\amp,Pseq([2,1,1,2,2,1,1]/2,inf),d=\dur, 0.5);Ptpar([0,n,5,Pmul(d,2,Pmul(\freq,2,n)),15,Pmul(f,3,n),30,Pmul(f,3.5,n),80,Pmul(d,3,Pmul(f,5.035, n)),140,Pmul(d,1,Pmul(f,8,n))]).play

n=Pbind(\tempo,2.5,f=\freq,Pseq([Pseq([103.8],14),Pseq([82.4],14)],inf),\amp,Pseq([2,1,1,2,2,1,1]/2,inf),d=\dur, 0.5);Ptpar([0,n,5,Pmul(d,2,Pmul(f,2,n)),15,Pmul(f,3,n),30,Pmul(f,3.5,n),80,Pmul(d,3,Pmul(f,5.035, n)),140,Pmul(d,1,Pmul(f,8,n))]).play

n=Pbind(\tempo,2.5,f=\freq,Pseq([Pseq([103.8],14),Pseq([82.4],14)],inf),\amp,Pseq([2,1,1,2,2,1,1]/2,inf),d=\dur, 0.5);Ptpar([0,n,5,Pmul(d,2,Pmul(f,2,n)),15,Pmul(f,3,n),30,Pmul(f,3.5,n),80,Pmul(d,3,Pmul(f,5.035, n)),140,Pmul(f,8,n)]).play







TempoClock.tempo= 1 //need to reset default clock tempo





n={|x,y=1|Pbind(\freq,Pseq([104,82.5] stutter:14,99)*x,\amp,Pseq([2,1,1,2,2,1,1]/2,99),\dur,y/5)};Ptpar([0,n.(1),2,n.(2,2),6,n.(3),12,n.(3.5),32,n.(5.035,3),56,n.(8)]).play

n={|x,y=1|Pbind(\freq,Pseq([104,82.5] stutter:14,99)*x,\amp,Pseq([2,1,1,2,2,1,1]/2,99),\dur,y/5)};Ptpar([0,n.(1),2,n.(2,2),6,n.(3),12,n.(3.5),32,n.(5.04,3),56,n.(8)]).play

a=[0,1,1,2,2,2,6,3,1,12,3.5,1,32,5.04,3,56,8,1];{|i|Pbind(\lag,a[b=i*3],\freq,Pseq([104,82.5] stutter:14,99)*a[b+1],\amp,Pseq([2,1,1,2,2,1,1]/2,99),\dur,a[b+2]/5).play}!6

[0,1,1,2,2,2,6,3,1,12,3.5,1,32,5.04,3,56,8,1].clump(3).do{|x|Pbind(\freq,Pseq([104,82.5] stutter:14,99)*x[1],\amp,Pseq([2,1,1,2,2,1,1]/2,99),\dur,x[2]/5,\lag,x[0]).play}

deleted quarks

clean-up: #42

started deleting my red quarks

it failed.

update 141024:
i did delete all my red quarks on the 4th of october (because of my 3rd october transition to green phase) but failed with the svn commit. the error i got was Transmitting file data ...svn: E160028: Commit failed (details follow): svn: E160028: Directory '/redSys' is out of date. i saw it as a sign that they shouldn't go.
but today i figured out that i had checked out the http version and that i needed the https to be able to commit. so after the command... svn relocate i can now commit changes to the quarks svn repository again.


clean-up: #41

yet another small piece of code i found in my supercollider extensions folder.

it's very basic and just generates a pwm signal (pulse-width modulation). you could do the same thing quicker with the standard LFPulse, but sometimes it's good to roll your own just to see another approach.

Pwm {
        *ar {|freq= 100, width= 0.5|
                ^>(width* -2+1);
        *kr {|freq= 100, width= 0.5|
                ^>(width* -2+1);
//mouse controls pwm duty cycle
{,, 1))!2*0.2}.play
//simulating voltage
{,, 1))).poll;}.play


clean-up: #40

found an old extension to Wouter Snoei's nice MasterEQ class (it's in his wslib quark). my extension just takes the current setting and post code that recreates that. it's handy for when you want to hardcode a equalizer into some piece of code and when you don't want to be dependent on wslib being installed.

save as in your extensions folder.

//redFrik 120901
//post synthdef code for current preset
//requires wslib from quarks

+MasterEQ {
        *manual {|strip= true|
                var params;
                if(eq.notNil, {

                        params= eq[\frdb][0][[0, 2, 1]];
                        if(params[2]!=0 or:{strip.not}, {
                                "input=, %, %, %);".format(*params).postln;

                        params= eq[\frdb][1][[0, 2, 1]];
                        if(params[2]!=0 or:{strip.not}, {
                                "input=, %, %, %);".format(*params).postln;

                        params= eq[\frdb][2][[0, 2, 1]];
                        if(params[2]!=0 or:{strip.not}, {
                                "input=, %, %, %);".format(*params).postln;

                        params= eq[\frdb][3][[0, 2, 1]];
                        if(params[2]!=0 or:{strip.not}, {
                                "input=, %, %, %);".format(*params).postln;

                        params= eq[\frdb][4][[0, 2, 1]];
                        if(params[2]!=0 or:{strip.not}, {
                                "input=, %, %, %);".format(*params).postln;
                }, {
                        "start MasterEQ first".warn;

MasterEQ.manual(false) //keep zero gain poles


clean-up: #39

since long i had this idea of sonifying what my laptop is doing. before, when laptops had spinning harddrives, one could at least hear the faint little noises when there was disk activity. but today with the sd disks, this insight is gone.

below is a draft of how it could work. it tracks activity in the system.log file. to try it start supercollider and run the code. then launch some apps, save files, disconnect network etc etc. it's quite fun to have it running in the background for a while.

one day i plan to do a more thorough sonification, tracking many more things. here is a good reference for what to tap into in the future.

sudo iosnoop
sudo execsnoop -v
sudo opensnoop -ve
sudo dtruss -n SuperCollider
sudo errinfo -c
sudo iotop -CP 1
man -k dtrace

//osx only
s.latency= 0.05;
var num= 10;
var delta= 0.1;
var maxlen= 256;
var rate= 0.5;
var curr=;
var mySplit= {|str|
        var res= "";
        var arr= [];
        var separator=;
        var tab=;{|chr, i|
                if(chr.ascii!=separator or:{str.clipAt(i+1).ascii==tab}, {
                        res= res++chr;
                }, {
                        arr= arr.add(res);
                        res= "";
var read= Routine({
        var res, arr, last, new;{
                res= ("tail -n"+num+"/var/log/system.log").unixCmdGetStdOut;
                if(res!=last, {
                        last= res;
                        arr= mySplit.value(res);
                        new={|x| curr.indexOfEqual(x).isNil};
                delta.wait;     //time between checking system log
var play= Routine({
        var data;{
                if(curr.size>0, {
                        data= curr.pop;
                        ("buf"++curr.size++": ").post;
                                Synth(\log, [\data, data.ascii.extend(maxlen, 0)]);
                }, {
SynthDef(\log, {|out= 0, amp= 0.5, minFreq= 300, maxFreq= 14400|
        var data= Control.names([\data]).ir(Array.fill(maxlen, 0));
        var freq=*rate, 0, Dseq(data), 2);
        var src=, 255, minFreq, maxFreq), 0, (freq>0).lag(0.02));,, 0, amp));

separate noisy and pitched sounds

clean-up: #38

here’s a little fun trick how to use the not so well known clarity mode of supercollider’s built in Pitch tracker. set the clar argument to 1 and then grab the second value returned. by default this is a binary hasFreq flag, but with clar=1 it becomes a continuous value 0.0 to 1.0.
the example below includes a one second delay to be able to hear the sounds you make being panned left-right depending on how ’clear’ they are. there’s also a short lag and a curved mapping to make the example work better.

//example: noise in left ear, clear tones in right
//use headphones
        var src=, 1, 1);
        var clarity=, clar:1)[1].lag(0.1);, clarity.lincurve(0.1, 0.9, -1, 1, 4).poll);

and as variations on the above one could easily mute noises…

//example: mute noise, only play pitched sounds
//use headphones
        var src=, 1, 1);
        var clarity=, clar:1)[1].lag(0.1);*clarity.lincurve(0.1, 0.9, 0, 1, 4);

and of course by just flipping a value around we only play the noises…

//example: mute pitched sounds, only play noises
//use headphones
        var src=, 1, 1);
        var clarity=, clar:1)[1].lag(0.1);*(1-clarity.lincurve(0.1, 0.9, 0, 1, 4));

last an example to demonstrate how to route signals to effects depending on clarity…

//example: pitched sounds with reverb
//use headphones
        var src=, 1, 1)*0.5;
        var clarity=, clar:1)[1].lag(2);,, 50), clarity.lincurve(0.3, 0.9, -1, 1, -2).poll);

quartz composer in sc

clean-up: #37

thanks to scott wilson it is possible to load and embed quartz composer sketches in supercollider.
below is an example that demonstrate how to use it with a camera input. you'll need the attached .qtz file and edit the path in the sc code.

cam qtz from redFrik on Vimeo.

note1: this is mac osx only.
note2: this works on sc3.7 (or sc3.5 if you replace QuartzComposerView with SCQuartzComposerView).
note3: i didn't test but you could maybe make it run under sc3.6 with GUI.cocoa.

var width= 1024, height= 576;
w= Window("qc", Rect(100, 100, width, height)).front;
m= QuartzComposerView(w, Rect(0, 0, width, height));
//m.path= "~/Desktop/cam.qtz".standardizePath;  //edit to match filepath
m.path= thisProcess.nowExecutingPath.dirname+/+"cam.qtz";
m.inputKeys.postln;     //should return [X, Y]
        ~speedx= 0.1;
        ~speedy= 0.11;
        a= {|x= 0, y= 0|*99+99), y.lag(0.1)*199+99, 3), x.lag(0.1)*2-1)}.play;
        r= Routine({
                                var x= sin(i*~speedx)*0.5+0.5;
                                var y= cos(i*~speedy)*0.5+0.5;
                                m.setInputValue(\X, x);
                                m.setInputValue(\Y, y);
                                a.set(\x, x, \y, y);

~speedx= 0.02
~speedx= 0.4
~speedy= 0.05
~speedy= 0.5


in the above example frequency is mapped to video effect vertical movement, and panning to video effect horizontal movement.

Binary Data cam.qtz35.9 KB


Subscribe to RSS - supercollider