Pwm

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

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 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

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;
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

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= 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

and 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

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";
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.

AttachmentSize
Binary Data cam.qtz35.9 KB

extMultiSliderView

clean-up: #36

in november 2011 there was some discussion on the sc-users mailinglist about a multislider point scroll similar to the one in maxmsp. there were many nice suggestions in the thread, but as so often nothing got decided/implemented. below is my suggestion - a simple fifo.

extMultiSliderView from redFrik on Vimeo.

here is the class extension...

//for sc3.6 or earlier
//copy and save as extSCMultiSliderView.sc in extensions folder
+SCMultiSliderView {
        add {|val|
                this.value= this.value.copyRange(1, this.value.size-1)++val;
        }
        addFirst {|val|
                this.value= val.asArray++this.value.copyRange(0, this.value.size-2);
        }
}
+QMultiSliderView {
        add {|val|
                this.value= this.value.copyRange(1, this.value.size-1)++val;
        }
        addFirst {|val|
                this.value= val.asArray++this.value.copyRange(0, this.value.size-2);
        }
}

note that if you're using the latest sc3.7, then the GUI redirect classes are gone and you need to write the class extensions like the following...

//for sc3.7 or newer
//copy and save as extMultiSliderView.sc in extensions folder
+MultiSliderView {
        add {|val|
                this.value= this.value.copyRange(1, this.value.size-1)++val;
        }
        addFirst {|val|
                this.value= val.asArray++this.value.copyRange(0, this.value.size-2);
        }
}

and here are the examples from the video...

//--basic example
(
var n= 100; //number of sliders
var w= Window("scroll test").front;
var a= MultiSliderView(w, Rect(10, 10, w.bounds.width-20, w.bounds.height-20));
a.elasticMode= 1;
a.value= Array.fill(n, {0});
Routine({
        inf.do{|i|
                0.05.wait; //scroll speed
                a.addFirst(sin(i/10)*0.5+0.5); //try .add instead of .addFirst
        };
}).play(AppClock);
CmdPeriod.doOnce({w.close});
)

//--example with sound input
(
s.waitForBoot{
        var n= 100; //number of sliders
        var w= Window("scroll test audio").front;
        var a= MultiSliderView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
        var b= {
                var src= SoundIn.ar; //mic input
                SendReply.kr(Impulse.kr(60), '/trk', Amplitude.kr(src, 0.1, 0.1));
                DC.ar(0); //silence
        }.play;
        a.elasticMode= 1;
        a.valueThumbSize= 8;
        a.indexThumbSize= 1;
        a.drawLines= true;
        a.value= Array.fill(n, {0});
        OSCFunc({|msg| {a.addFirst(msg[3])}.defer}, '/trk');
        CmdPeriod.doOnce({w.close});
};
)

also see Marije's SWPloterMonitor from her same day clean-up.

Linear feedback shift register

clean-up: #35

over a year ago i was playing with lfsr to generate pseudo-random noise. below i cleaned up and published the results.

lfsr from redFrik on Vimeo.

with inspiration from Sebastian Tomczak's lfsr-in-maxmsp and this video.

//4-bit Fibonacci LFSR
//https://en.wikipedia.org/wiki/Linear_feedback_shift_register
(
b= 2r1000; //seed
a= [];
15.do{
        a= a++b.asBinaryDigits(4);
        b= (b>>1)|((b&1).bitXor(b&2>>1)<<3);
        b.postln;
};
"";
)

s.boot
c= Buffer.loadCollection(s, a)
c.plot
{PlayBuf.ar(1, c, MouseX.kr(0, 1), loop:1)!2}.play

//4-bit Fibonacci LFSR with local in/out
(
a= {|rate= 4, iseed= 2r1000|
        var b, trig;
        var in= LocalIn.kr(1, iseed);
        trig= Impulse.kr(rate);
        b= Latch.kr(in, trig);//read
        b= (b>>1)|((b&1).bitXor(b&2>>1)<<3);//modify
        LocalOut.kr(b);
        b.poll(trig);
        DC.ar(0);
}.play;
)
a.set(\rate, 30)
a.free

//4-bit Fibonacci LFSR as single sample feedback with dbufrd/dbufwr
(
a= {|rate= 4, iseed= 2r1000|
        var b, trig;
        var buf= LocalBuf(1);
        buf.set(iseed);
        trig= Impulse.ar(rate);
        b= Demand.ar(trig, 0, Dbufrd(buf));//read
        b= (b>>1)|((b&1).bitXor(b&2>>1)<<3);//modify
        Demand.ar(trig, 0, Dbufwr(b, buf));//write
        b.poll(trig);
        DC.ar(0);
}.play;
)
a.set(\rate, 30)
a.free

//lfsr sound example (not sure this is correct but sounds ok)
(
a= {|rate= 400, iseed= 2r1000, tap1= 1, tap2= 3, tap3= 5, length= 16|
        var l, b, trig, o;
        var buf= LocalBuf(1);
        buf.set(iseed);
        trig= Impulse.ar(rate);
        l= Demand.ar(trig, 0, Dbufrd(buf));//read
        b= l.bitXor(l>>tap1).bitXor(l>>tap2).bitXor(l>>tap3)&1;//modify
        l= (l>>1)|(b<<15);//lfsr
        Demand.ar(trig, 0, Dbufwr(l, buf));//write
        o= PulseCount.ar(Impulse.ar(rate*length), trig);//bits
        l>>o&1!2;//output
}.play;
)

a.set(\rate, 300)
a.set(\rate, 100)
a.set(\length, 3)
a.set(\length, 14)
a.set(\tap1, 14.rand)
a.set(\tap2, 14.rand)
a.set(\tap3, 14.rand)
a.set(\length, 32)
a.set(\rate, 1000)
a.free

//lfsr sound with gui  (not sure this is correct but sounds ok)
(
var w, a;
w= Window("lfsr", Rect(100, 100, 520, 200)).front;
Slider(w, Rect(10, 10, 500, 25)).action_({|view| a.set(\rate, view.value*2000)}).value= 400/2000;
Slider(w, Rect(10, 40, 500, 25)).action_({|view| a.set(\length, view.value*32)}).value= 16/32;
a= {|rate= 400, iseed= 2r1000, tap1= 1, tap2= 3, tap3= 5, length= 16|
        var l, b, trig, o;
        var buf= LocalBuf(1);
        buf.set(iseed);
        trig= Impulse.ar(rate);
        l= Demand.ar(trig, 0, Dbufrd(buf));//read
        b= l.bitXor(l>>tap1).bitXor(l>>tap2).bitXor(l>>tap3)&1;//modify
        l= (l>>1)|(b<<15);//lfsr
        Demand.ar(trig, 0, Dbufwr(l, buf));//write
        o= PulseCount.ar(Impulse.ar(rate*length), trig);//bits
        l>>o&1!2;//output
}.play;
CmdPeriod.doOnce({w.close});
)

Pages

Subscribe to f0blog RSS