«  …7 8 9 10 11 12 13 »

Fluid Simulator in SuperCollider

2014-10-08 20:16 supercollider

clean-up: #44

This is a SuperCollider port of Memo Akten's Processing library MSAFluid. I looked at his MSAFluidSolver2D.java version 1.3.0 and rewrote it for SuperCollider. I also optimised it as much as I could but still it is slow and the framerate is very low. I blame the slow SuperCollider graphics. The fluid system itself should perform ok - it's just the drawing that doesn't live up to be useful.

The good news is that one doesn't need to use the fancy graphics. SuperCollider is anyway made for sound synthesis and the fluid simulator can run and particles can be mapped to sound without displaying anything.

Classes, help files and examples are attached as a zip archive below.

Updates:

Attachments:
MSAFluidSolver2D.zip

Failed Tweet

2014-10-07 23:36 supercollider

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 has suggestions or if someone wants 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 optimising 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

Ptpar([0,n=Pbind(\tempo,2.5,f=\freq,Pstutter(14,Pseq([103.8,82.4],inf)),\amp,Pseq([2,1,1,2,2,1,1]/2,inf),d=\dur,0.5),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

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

a={|a,b|Pmul(a,b,n)};Ptpar([0,n=Pbind(\tempo,5,f=\freq,Pstutter(14,Pseq([103.8,82.4],99)),\amp,Pseq([2,1,1,2,2,1,1]/2,inf)),10,Pmul(d,2,a.(f,2)),30,a.(f,3),60,a.(f,3.5),160,Pmul(d,3,a.(f,5.035)),280,a.(f,8)]).play

a={|a,b|Pmul(a,b,n)};Ptpar([0,n=Pbind(\tempo,5,f=\freq,Pstutter(14,Pseq([103.8,82.4],99)),\amp,Pseq([2,1,1,2,2,1,1]/2,inf)),10,Pmul(d=\dur,2,a.(f,2)),30,a.(f,3),60,a.(f,3.5),160,Pmul(d,3,a.(f,5.035)),280,a.(f,8)]).play

a={|x|Pmul(f,x,n)};Ptpar([0,n=Pbind(\tempo,5,f=\freq,Pstutter(14,Pseq([103.8,82.4],99)),\amp,Pseq([2,1,1,2,2,1,1]/2,inf)),10,Pmul(d=\dur,2,a.(2)),30,a.(3),60,a.(3.5),160,Pmul(d,3,a.(5.035)),280,a.(8)]).play

a=Pmul(f,_,n);b={|y,x|Pmul(\dur,y,a.(x))};Ptpar([0,n=Pbind(\tempo,5,f=\freq,Pstutter(14,Pseq([103.8,82.4],99)),\amp,Pseq([2,1,1,2,2,1,1]/2,inf)),10,b.(2,2),30,a.(3),60,a.(3.5),160,b.(3,5.035),280,a.(8)]).play

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

a=Pmul(f,_,n);b={|y,x|Pmul(d,y,a.(x))};Ptpar([0,n=Pbind(f=\freq,Pstutter(14,Pseq([103.8,82.4],99)),\amp,Pseq([2,1,1,2,2,1,1]/2,inf),d=\dur,0.2),2,b.(2,2),6,a.(3),12,a.(3.5),32,b.(3,5.035),56,a.(8)]).play

a=Pmul(f,_,n);Ptpar([0,n=Pbind(f=\freq,Pstutter(14,Pseq([103.8,82.4],99)),\amp,Pseq([2,1,1,2,2,1,1]/2,inf),d=\dur,0.2),2,Pmul(d,2,a.(2)),6,a.(3),12,a.(3.5),32,Pmul(d,3,a.(5.035)),56,a.(8)]).play

a=Pmul(f,_,n);Ptpar([0,n=Pbind(f=\freq,Pseq([103.8,82.4],99).stutter(14),\amp,Pseq([2,1,1,2,2,1,1]/2,99),d=\dur,0.2),2,Pmul(d,2,a.(2)),6,a.(3),12,a.(3.5),32,Pmul(d,3,a.(5.035)),56,a.(8)]).play

n={|x,y=1|Pbind(\freq,Pseq([103.8,82.4],99).stutter(14)*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.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

2014-10-04 20:34 supercollider

clean-up: #42

Started deleting my red quarks

It failed.

Updates:


Pwm

2014-10-03 11:53 supercollider

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|
    ^LFSaw.ar(freq)>(width* -2+1);
  }
  *kr {|freq= 100, width= 0.5|
    ^LFSaw.kr(freq)>(width* -2+1);
  }
}
/*
s.scope
//mouse controls pwm duty cycle
{Pwm.ar(300, MouseX.kr(0, 1))!2*0.2}.play
//simulating voltage
{Amplitude.ar(Pwm.ar(300, MouseX.kr(0, 1))).poll; DC.ar(0)}.play
*/

extMasterEQ

2014-10-03 02:25 supercollider

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 posts code that recreates that. It's handy for when you want to hardcode an equaliser into some piece of code and when you don't want to be dependent on wslib being installed.

Save as extMasterEQ.sc 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= BLowShelf.ar(input, %, %, %);".format(*params).postln;
      });

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

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

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

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

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

system.log

2014-10-01 21:01 supercollider

clean-up: #39

Since long I had this idea of sonifying what my laptop is doing. Before, when laptops had spinning hard drives, 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. 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;
s.waitForBoot{
  var num= 10;
  var delta= 0.1;
  var maxlen= 256;
  var rate= 0.5;
  var curr= List.new;
  var mySplit= {|str|
    var res= "";
    var arr= [];
    var separator= Char.nl.ascii;
    var tab= Char.tab.ascii;
    str.do{|chr, i|
      if(chr.ascii!=separator or:{str.clipAt(i+1).ascii==tab}, {
        res= res++chr;
      }, {
        arr= arr.add(res);
        res= "";
      });
    };
    arr.add(res);
  };
  var read= Routine({
    var res, arr, last, new;
    inf.do{
      res= ("tail -n"+num+"/var/log/system.log").unixCmdGetStdOut;
      if(res!=last, {
        last= res;
        arr= mySplit.value(res);
        new= arr.select{|x| curr.indexOfEqual(x).isNil};
        new.do{|x|
          curr.addFirst(x);
        };
      });
      delta.wait;  //time between checking system log
    };
  });
  var play= Routine({
    var data;
    inf.do{
      if(curr.size>0, {
        data= curr.pop;
        ("buf"++curr.size++": ").post;
        data.postln;
        s.bind{
          Synth(\log, [\data, data.ascii.extend(maxlen, 0)]);
        };
        (data.size/maxlen*rate).wait;
      }, {
        delta.wait;
      });
    };
  });
  SynthDef(\log, {|out= 0, amp= 0.5, minFreq= 300, maxFreq= 14400|
    var data= Control.names([\data]).ir(Array.fill(maxlen, 0));
    var freq= Duty.ar(1/maxlen*rate, 0, Dseq(data), 2);
    var src= SinOsc.ar(freq.linexp(0, 255, minFreq, maxFreq), 0, (freq>0).lag(0.02));
    OffsetOut.ar(out, Pan2.ar(src, 0, amp));
  }).add;
  s.sync;
  read.play(AppClock);
  play.play;
};
)

Separate Noisy and Pitched Sounds

2014-10-01 11:14 supercollider

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 flag (hasFreq), 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 the left ear, clear tones in the right
//use headphones
{
  var src= DelayN.ar(SoundIn.ar, 1, 1);
  var clarity= Pitch.kr(src, clar:1)[1].lag(0.1);
  Pan2.ar(src, clarity.lincurve(0.1, 0.9, -1, 1, 4).poll);
}.play

As variations on the above one could easily mute noises...

//example: mute noise, only play pitched sounds
//use headphones
{
  var src= DelayN.ar(SoundIn.ar, 1, 1);
  var clarity= Pitch.kr(src, clar:1)[1].lag(0.1);
  Pan2.ar(src)*clarity.lincurve(0.1, 0.9, 0, 1, 4);
}.play

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= DelayN.ar(SoundIn.ar, 1, 1);
  var clarity= Pitch.kr(src, clar:1)[1].lag(0.1);
  Pan2.ar(src)*(1-clarity.lincurve(0.1, 0.9, 0, 1, 4));
}.play

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

//example: pitched sounds with reverb
//use headphones
{
  var src= DelayN.ar(SoundIn.ar, 1, 1)*0.5;
  var clarity= Pitch.kr(src, clar:1)[1].lag(2);
  XFade2.ar(src, GVerb.ar(src, 50), clarity.lincurve(0.3, 0.9, -1, 1, -2).poll);
}.play

Quartz Composer in SC

2014-09-30 01:08 supercollider

clean-up: #37

Thanks to Scott Wilson it is possible to load and embed Quartz Composer sketches in SuperCollider.

Below is an example that demonstrates how to use it with a camera input. You'll need the attached .qtz file and edit the path in the SuperCollider code.

note1: this is macOS only.

note2: this works on SC 3.7 and newer (or SC 3.5 if you replace QuartzComposerView with SCQuartzComposerView).

(
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";
CmdPeriod.doOnce({w.close});
m.start;
m.inputKeys.postln;  //should return [X, Y]
s.waitForBoot{
  ~speedx= 0.1;
  ~speedy= 0.11;
  a= {|x= 0, y= 0| Pan2.ar(MoogFF.ar(Saw.ar(1-y.lag(0.1)*99+99), y.lag(0.1)*199+99, 3), x.lag(0.1)*2-1)}.play;
  r= Routine({
    inf.do{|i|
      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);
      (1/60).wait;
    };
  }).play(AppClock);
};
)

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

r.stop;
a.free;
m.stop;
w.close;

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

update 200116: I found an older example that could play movie files and added it here as well. It will play back the smoothest if the movie file is compressed using a codec that compresses each frame individually. e.g. photoJPEG.

//macOS only
(
var x= 10, y= 10;
var width= 1280, height= 720;
var path= "somefolder/somefilm.mov";

var qtz= thisProcess.nowExecutingPath.dirname+/+"filmplayer2.qtz";
var win= Window("filmplayer", Rect(x, y, width, height), false, false);
var view= QuartzComposerView(win, Rect(0, 0, width, height));
view.path= qtz;
view.setInputValue('Movie_Location', path);
view.start;
view.front;
win.front;
CmdPeriod.doOnce({view.close});
)
//macOS only
(
var qtz= thisProcess.nowExecutingPath.dirname+/+"filmplayer2.qtz";
var win= Window("filmplayer2", Rect(100, 100, 640, 260));
var readButton= Button(win, Rect(10, 10, 100, 30)).states_([["read"]]);
var pathText= StaticText(win, Rect(10, 50, 300, 30));
var playButton= Button(win, Rect(10, 100, 100, 30)).states_([["enable"], ["disable"]]);
var durText= StaticText(win, Rect(10, 130, 300, 30)).string_("Dur:");
var posText= StaticText(win, Rect(10, 150, 300, 30)).string_("Pos:");
var qc= QuartzComposerView(win, Rect(310, 10, 320, 240));
qc.path= qtz;
readButton.action= {
  playButton.valueAction= 0;
  Dialog.openPanel({|path|
    pathText.string= path;
    qc.setInputValue('Movie_Location', path);
    {
      playButton.valueAction= 1;
      durText.string= "Dur:"+qc.getOutputValue('Movie_Duration');
    }.defer(0.5);
  });
};
playButton.action= {|view|
  qc.setInputValue('Enable', view.value);
};
Routine({
  var lastPos;
  inf.do{
    var pos= qc.getOutputValue('Movie_Position');
    if(pos!=lastPos, {
      posText.string= "Pos:"+pos;
    });
    (1/25).wait;
  };
}).play(AppClock);
qc.front;
win.front;
CmdPeriod.doOnce({win.close});
)
filmplayer2 screenshot
Attachments:
cam.qtz.zip
filmplayer2.qtz.zip

«  …7 8 9 10 11 12 13 »