supercollider

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

supercolliderArduino_sendAndReceive

clean-up: #33

below is some supercollider code for sending and receiving data from an arduino. it is a good example i think and worth sharing. it can be used as a template for when you want to read and write at the same time.

as the example is written supercollider writes data to six pwm channels (0-255), and reads data from six analog inputs (also 0-255). the sliders are the pwm output controllers, and the sensor input data is just posted as an array.

//supercolliderArduino_sendAndReceive /f0
//  controlling 6 pwm channels and reading 6 analog inputs at the same time
//  use with sc code supercolliderArduino_sendAndReceive.scd

//--serial protocol
// pwm sc->arduino: 10 nn nn nn nn nn nn 11 (nn bytes 1-6= pwm channels 0-5)
// adc arduino->sc: 20 nn nn nn nn nn nn 21 (nn bytes 1-6= analog channels 0-5)

//--settings
byte pwmPins[]= {5, 3, 9, 6, 11, 10};
byte adcPins[]= {0, 1, 2, 3, 4, 5};  //A0-A5

//--variables
boolean changed;
byte val, cnt= 0;
byte pwmData[]= {0, 0, 0, 0, 0, 0, 0, 0};
byte adcData[]= {0, 0, 0, 0, 0, 0, 0, 0};  //for detecting change and filter out repetitions

void setup() {
  Serial.begin(38400);    //baudrate must match in sc
  for(byte i= 0; i<6; i++) {
    pinMode(pwmPins[i], OUTPUT);
    pinMode(adcPins[i], INPUT);
  }
}
void loop() {
 
  //--from sc
  while(Serial.available()) {
    val= Serial.read();
    if(cnt==0) {
      if(val==10) {  //beginning of message found
        cnt= 1;  //start counter
      }
    } else if(cnt<7) {  //between 1 and 6 means message started and that bytes should be saved
      pwmData[cnt-1]= val;  //saving incoming bytes in temporary data array
      cnt++;
    } else {
      if(val==11) {
        for(byte i= 0; i<6; i++) {
          analogWrite(pwmPins[i], pwmData[i]);  //output read message to pwm pins
        }
      } else {
        //serial read error
      }
      cnt= 0;  //reset byte counter
    }
  }
 
  //--to sc
  changed= false;
  for(byte i= 0; i<6; i++) {
    val= analogRead(adcPins[i])>>4;  //scale from 10 to 8 bits
    if(val!=adcData[i]) {
      adcData[i]= val;
      changed= true;
    }
  }
  if(changed) {  //check if any sensor changed
    Serial.write(20);
    Serial.write(adcData[0]);
    Serial.write(adcData[1]);
    Serial.write(adcData[2]);
    Serial.write(adcData[3]);
    Serial.write(adcData[4]);
    Serial.write(adcData[5]);
    Serial.write(21);
  }
  delay(10);  //wait 10 milliseconds
}

//--use with supercolliderArduino_sendAndReceive.ino
//supercollider code for reading 6 analog sensors and sending out to 6 pwm channels

SerialPort.listDevices; //run this and see post window for name of serial device

//--gui code for sending 6 pwm
(
var name= "/dev/tty.usbserial-A101NB76";        //edit to match your serial device
var port= SerialPort(name, 38400, crtscts: true);
var pwm= [10, 0, 0, 0, 0, 0, 0, 11];
var win= Window("pwm", Rect(99, 99, 260, 200), false);
Slider(win, Rect(10, 10, 30, 170)).action_{|view| pwm.put(1, (view.value*255).asInteger); port.putAll(pwm.postln)};
Slider(win, Rect(50, 10, 30, 170)).action_{|view| pwm.put(2, (view.value*255).asInteger); port.putAll(pwm.postln)};
Slider(win, Rect(90, 10, 30, 170)).action_{|view| pwm.put(3, (view.value*255).asInteger); port.putAll(pwm.postln)};
Slider(win, Rect(130, 10, 30, 170)).action_{|view| pwm.put(4, (view.value*255).asInteger); port.putAll(pwm.postln)};
Slider(win, Rect(170, 10, 30, 170)).action_{|view| pwm.put(5, (view.value*255).asInteger); port.putAll(pwm.postln)};
Slider(win, Rect(210, 10, 30, 170)).action_{|view| pwm.put(6, (view.value*255).asInteger); port.putAll(pwm.postln)};
win.front;
CmdPeriod.doOnce({port.putAll([10, 0, 0, 0, 0, 0, 0, 11]); port.close; win.close});
//press cmd+. to stop and close window and serial port

s.waitForBoot{
        var byte, num= 6, index= 0, data= Array.newClear(num);
        Ndef(\arduino, {|data= #[0, 0, 0, 0, 0, 0]| Splay.ar(SinOsc.ar(data.lag(0.02)+500, 0, 0.4))}).play; //temp sound
        Routine.run({
                inf.do{
                        while({byte= port.read; byte.notNil}, {
                                //byte.postln;  //debug
                                if(index==0 and:{byte==20}, {//check if first byte is 20
                                        index= 1;
                                        }, {
                                                if(index>=1 and:{index<(num+1)}, {//ok, now start collecting bytes
                                                        data[index-1]= byte;
                                                        index= index+1;
                                                        }, {
                                                                if(index==(num+1) and:{byte==21}, {//until last data byte
                                                                        data.postln;    //debug
                                                                        Ndef(\arduino).setn(\data, data);
                                                                        index= 0;//done. reset index to prepare for new message
                                                                        }, {
                                                                                //something broke or beginning - restart
                                                                                "restart".postln;       //debug
                                                                                index= 0;
                                                                });
                                                });
                                });
                        });
                };
        });
};

)

i also attached the complete example as a zip file.

sc2 nostalgia

clean-up #32:

supercollider 2 (sc2) was such a great piece of software. thank you James McCartney. i plan to go back one day and i already have two old mac ti-books that still boot os9 natively. i also have a lot of patches for max/nato that i still think produces great graphics/video under os9 - so that’s another reason for ’downgrading’.

below is one of my first posts to sc-users. i gathered some courage and sent this mail in aug 2001.
(here is the full thread.) if you play the below sound in the newer supercollider versions it doesn’t sound nearly as good (in my ears). to get a few steps closer to that sc2 sound, try replacing the RHPF with GlitchRHPF from sc3-plugins. still it isn’t the same. listen to the attached mp3 recorded with sc2 to hear these five lines of code go wild in sc2.

------------------------------

Date: Thu, 16 Aug 2001 21:12:27 +0200
From: Fredrik Olofsson <---@---.--->
Subject: noisy instr.

hello

this works... (sc 2.2.11)

({
        RHPF.ar(
                BrownNoise.ar([1,1]),
                LFSaw.ar(1,0.99,2),
                0.24).clip2(0.5)
}.scope
)

this doesn't...

({
        RHPF.ar(
                BrownNoise.ar([1,1]),
                LFSaw.ar(1,0.99,1),             //note add difference
                0.24).clip2(0.5)
}.scope
)

how come the latter example runs fine for about 40 seconds and then
both channels 'get stuck' at +1, one after the other?  i've had the
former playing for at least 9 min.

confused/fredrik

AttachmentSize
Audio icon noisy_instr_ex.mp3629.5 KB

redUniform2

for an upcoming performance i've revisited the electronics for my redUniform piece. my old setup used a nordic nRF24L01 wireless chip but now i changed to wifi and the adafruit cc3000 module.

the circuit is really minimal and simple. basically it's just the cc3000 wifi module, an atmega328, a 16Mhz chrystal, an on/off switch, one 1000mAh Li-ion and last two 6p connectors for the sensors.

the sensors are two modified minIMU-9.


with the battery fully charged i had it sending osc data at 50hz for more than a whole day.

attached are schematics and arduino code for reading sensors via spi. the code also show how to talk to the cc3000 and send sensor data via osc.
there are also two classes for supercollider called RedUniform2 and RedUniform2GUI.

arduino programming via soundcard

with lots of study of the AudioBoot_V2_0 java code by chris at roboterclub-freiburg.de, i managed to write supercollider code for uploading sketches to an arduino via the soundcard. no ftdi chip needed!
it's a very cheap solution for programming barebone arduinos (well, atmega168 microcontrollers really). you only need a few resistors, a capacitor and a mega168. the only difficult part is to 'initialize' this microcontroller by burning the special bootloader on to it. this requires an avr programmer of some sort (STK500, USBtinyISP etc).

the preparation steps are as follows...
1. burn the bootloader on to a mega168
2. build the barebone circuit
3. prepare the arduino ide
4. install the RedArduino class

after that one can compile hex files in the arduino ide and upload them using supercollider and a standard audio cable.

1. burn the bootloader on to a mega168

the trick behind all this is the special 'sound enabled' bootloader that i found here... http://www.hobby-roboter.de/forum/viewtopic.php?f=4&t=127. i downloaded the AudioBoot_V2_0.zip file and used my stk500 avr programmer together with the great avr crosspack. the terminal command i used for burning the bootloader was the following...

avrdude -v -p m168 -b 115200 -P /dev/tty.PL2303-000013FA -c stk500v2 -U flash:w:/Users/Stirling/arbeten/sc/scAudioino/AudioBoot_V2_0/Atmega_Source/chAudioBoot_ATMEGA168_IN_PD1_LED_PB5.hex -U lfuse:w:0xE2:m -U hfuse:w:0xDF:m -U efuse:w:0xFA:m

2. build the barebone circuit

then i built a minimal and barebone arduino circuit after the schematics found here... http://www.hobby-roboter.de/forum/viewtopic.php?f=4&t=128&p=531. again credit to chris.

this is the schematics i drew...

and here the resulting circuit...


i run it off 4,5v (3 batteries) but it could also be powered from the usb port.

3. prepare the arduino ide

to compile hex files for this barebone arduino, i needed to set up a custom board in the arduino ide. one way to do this is to create a new text file called boards.txt and put it inside a new folder in the arduino/hardware folder. on mac osx that could be something like ~/Documents/Arduino/hardware/BareBones/boards.txt. the boards.txt should contain the following...

minimal168.name=ATmega168 bare bone (internal 8 MHz clock)
minimal168.upload.speed=115200
minimal168.bootloader.low_fuses=0xE2
minimal168.bootloader.high_fuses=0xDD
minimal168.bootloader.extended_fuses=0×00
minimal168.upload.maximum_size=16384
minimal168.build.mcu=atmega168
minimal168.build.f_cpu=8000000L
minimal168.build.core=arduino:arduino
minimal168.build.variant=arduino:standard

then restart the arduino ide and under boards there should be a new option with mega168 and 8mhz internal clock. i make sure this board is selected every time before compiling hex files for sound uploading.
another thing that needs to be done is to enable 'verbose compile' in the arduino ide preferences. that will print out the file path of the hex file each time you compile a sketch.

4. install the RedArduino class

i wrote a couple of classes for supercollider to help with the encoding and signal generation of hex files. they're found in my redSys quark (under redTools) and are most easily installed from within supercollider itself with these commands...

Quarks.checkoutAll;             //update
Quarks.install("redSys");       //install. recompile after this

there are two helper classes and one main class. the RedIntelHex class parses hex files and RedDifferentialManchesterCodeNegative helps to encode the signal as differential manchester code. the main class RedArduino figures out the paging of data and generates a bit stream that is played back using demand rate ugens.

uploading

i upload sketches by compiling them in arduino ide (click verify - not upload) and copy&paste the file path of the resulting hex file into supercollider and the RedArduino's read method. i connect the left sound output channel to the barebone arduino, put my mac volume to ~80%. last i press the reset button on the circuit and quickly (within seconds) call the upload method in sc. the led should blink slowly directly after a reset, and fast when receiving data.

here's a video demonstrating how to do it. i'm just uploading the simple Blink example. near the end you will also hear how that sounds.

audioino from redFrik on Vimeo.

there's also some sc code i wrote here that will do the same thing but without the need of the redSys quark classes.

traer physics library for supercollider

a while ago i started porting the java/processing library TRAER.PHYSICS 3.0 by Jeffrey Traer Bernstein to supercollider. it's a simple and elegant particle system and a physics engine all in one. there are already ports to actionscript3, javascript and c++ (cinder), but i haven't seen anyone working with it in sc yet. so i had a go - both to learn more and to have an alternative to my own physics library quark redUniverse.

it is now finished and released as a quark. this is the initial version and there might still be bugs. i _did see sc crash once in a strange way after spawning lots of particles, so watch out for memory leaks.
to install it run the following code and recompile sc...

Quarks.checkoutAll;
Quarks.install("TraerPhysics");

i also wrote a few simple examples to go along with the helpfiles. here's a screenshot of one...

Pages

Subscribe to RSS - supercollider