supercollider firmata

clean-up: #57

just cleaned up an example for supercollider and arduino that i found on my computer. it is demonstrating the SCFirmata class by Eirik Arthur Blekesaune.

//how to read pin A0 with SCFirmata...

//for Arduino1.0.6 and SC3.6.6
//first in Arduino IDE:
//  * select File / Examples / Firmata / StandardFirmata
//  * upload this example to an arduino
//then in SC install the SCFirmata classes
//  * download zip file
//  * extract files and put them in your sc application support directory
//  * recompile sc

d= SerialPort.devices[0]; // or d= "/dev/tty.usbserial-A1001NeZ" - edit number (or string) to match your arduino
f= FirmataDevice(d);//if it works it should post 'Protocol version: 2.3' after a few seconds

Ndef(\snd, {|freq= 400, amp= 0.5|[freq, freq+4].lag(0.08), 0, amp.lag(0.08)).tanh}).play;
f.reportAnalogPin(0, true)      //start reading A0
f.analogPinAction= ({|num, val| [num, val].postln; Ndef(\snd).set(\freq, val.linexp(0, 1023, 400, 800))})//control freq
f.analogPinAction= ({|num, val| [num, val].postln; Ndef(\snd).set(\amp, val.linexp(0, 1023, 0.001, 1))})//control amp instead

f.reportAnalogPin(0, false)     //stop reading A0


clean-up: #56

compared to generating a serial bitstream in audio, analyzing and extract serial data from audio is much harder. the supercollider code below does it, but the program has limitations and is quite sensitive for noise.

the code takes a string, chops it up into groups of six 8bit bytes and generates a serial audio bitstream from that. another part listens to this sound and tries to decode it. if it finds six full bytes it sends the result back to sclang via osc where it is printed.
to test the example connect an audio cable directly from your computer's output to its input (preferably via a small mixer), or change the audioSerial synthdef to use an internal audio bus. i can also imagine it could function with a mic next to the speakers - but i didn't test this.
if it only prints gibberish try with a different threshold setting, different volume on you computer or use a lower baud rate.

        var baudrate= 9600;
        SynthDef(\serialAudio, {|out= 0, amp= -0.5|                     //for sending out serial via audio
                var data= Control.names([\data]).kr(Array.fill(60, 0)); //max 6 bytes
                var src=, 0, Dseq(data), 2);
      , src*amp);
        SynthDef(\audioSerial, {|in= 0, thresh= 0.05|           //for receiving serial via audio
                var raw=; //here change to if trying internal audio bus
                var src= raw>thresh;
                var reading=, 1/baudrate*9), 1/baudrate/2, 1/baudrate/2);
                var osc=, baudrate/;
                var clock= (<;
                var index=, reading);
                var stopTrig= index>7;
                var data=, index>=#[7, 6, 5, 4, 3, 2, 1]);
                var byte= (1-data).sum{|x, i| 2**(6-i)*x};
      , '/data', byte);
        OSCFunc({|msg| msg[3]}, '/data');

var str= "hello supercollider!";
var baudrate= 9600;
                var data= bytes.collect{|x| [1]++(1-x.asBinaryDigits.reverse)++[0]}.flat;
                        Synth(\serialAudio, [\data, data]);

one can use this technique to communicate with another computer via audio. to communicate with a microcontroller (e.g. an arduino), one needs additional electronics (amplification, rectification). here's schematics for a bi-directional circuit for talking to a 5v arduino.

this audio-to-serial technique was used to get input from rfid, touch and bend sensors in our reflect installation. i.e. sc is running on an ipod touch and receives all sensor data via audio from an atmega168 microcontroller.


clean-up: #55

another way (compared to fsk in my previous blog entry) of sending data via audio is to directly generate the serial bit stream using supercollider.

to test and learn about these things i first wrote and uploaded a very simple program to an arduino board. the program just transmitted the bytes 128, 10, 20, 30, 40 and 50.

//arduino testcode
void setup() {
void loop() {

then i connected the arduino serial tx pin (pin1) to the audio line-in of my laptop (via a 1k + 10k voltage divider) and recorded the sound of the serial transmission.

i then analyzed the sound by hand and wrote a little program in sc that could generate similar waveforms.

o= {|chr| [1]++(1-chr.asBinaryDigits.reverse)++[0]};
SynthDef(\serialAudio, {|amp= -0.5|     //for sending out serial via audio
        var data= Control.names([\data]).kr(Array.fill(60, 0));//max 6 bytes
        var src=, 0, Dseq(data), 2);     //baudrate, src*amp);
Synth(\serialAudio, [\data, [128, 10, 20, 30, 40, 50].collect{|c| o.value(c)}.flat, \amp, -0.5]);

this screenshot show the signal recorded from the arduino in the first channel, and the supercollider generated one in the second.

after all this i could reverse the process, generate any serial data and send it back to the arduino rx pin (pin0). a small amplifier circuit in between helped to get a more stable communication going.

this serial-to-audio technique was used to control the 24 leds (6 pwm channels) in our reflect installation. i.e. sc is running on an ipod touch and sends out serial audio to an atmega168 microcontroller.

here is another example that can fade a single led by sending serial commands over audio. includes schematics for an amplifier circuit plus sc and pd example code.

and for a more advanced (actually using a much better technique) example see here


clean-up: #54

for a few projects in the past i had to communicate data bidirectionally via sound. it involved for example hooking up a microcontroller to an ipod touch (running supercollider) so that the device could read sensors and/or control leds. one successful method was to do frequency-shift keying. see here for another blog entry on that.
i've published fsk pure data code for that before, and also some supercollider code for generating/encoding fsk, but never the decoding part i think.
so below is some old code i've cleaned up a bit. it just demonstrates sending audio via internal sc busses at a very low baudrate. for good baudrate and min/max frequency settings see here

s.latency= 0.05;
        SynthDef(\redFSKdecode, {|in= 20, thresh= 0.1, baudrate= 126, lo= 882, hi= 1764|
                var sig=, 1);
                var sigActive=, 0.01, 0.01)>thresh;
                var fre= 1/*sigActive);
                var trg=, lo+(baudrate*0.5), hi-(baudrate*0.5)); //0= lo, 1= hi
                var trgLo= trg<0.5;
                var trgHi= 1-trgLo;
                var trgCarr=, 1)>(1/baudrate*16); //when found more than 16bits high in a row
                var trgData=*trgCarr, trgCarr);
                var imp=, baudrate/;
                var writeTrg= (<;
                var writePos=, trgData);
                var writeVal=, trg);
                var ok, done= 1-trgData;
                var buf= LocalBuf(13).clear;
      , 0, Dbufwr(writeVal, buf, writePos, 0));
                ok= done*(, 0, Dbufrd(buf, 1, 0)))*, 0, Dbufrd(buf, 11, 0));
      , '/data',, 0, Dbufrd(buf, (2..9), 0)));
        SynthDef(\redFSKencode, {|out= 0, amp= 0.6, minFreq= 4900, maxFreq= 7350, invBaudrate= 0.008|
                var data= Control.names([\data]).ir(Array.fill(8, 0));
                var env=[1, 1, 0], [invBaudrate*(16+11), 0]), doneAction:2);
                var parity= data.sum+1;
                var freq=[invBaudrate*16]++invBaudrate.dup(11)), 0, Dseq(#[1, 0]++data++[parity%2, 1]));
                var src=*(maxFreq-minFreq)+minFreq, 0, amp);
      , src*env);

//test sending a simple counter
var baudrate= 126;
var lo= 882;
var hi= 1764;
        var byte;
        byte= msg[3..].sum{|x, i| 2**i*x};
}, '/data');{
        Synth(\redFSKdecode, [\in, 20]); //bus 20{|i|
                var byte= (i%256).asInteger;
                var data= byte.asBinaryDigits.reverse;
                        ("sending :"+byte).postln;
                        Synth(\redFSKencode, [\minFreq, lo, \maxFreq, hi, \data, data, \out, 20, \invBaudrate, 1/baudrate]); //bus 20
                        Synth(\redFSKencode, [\minFreq, lo, \maxFreq, hi, \data, data, \out, 0, \invBaudrate, 1/baudrate]); //monitor

//test a string of text
var baudrate= 126;
var lo= 882;
var hi= 1764;
        var byte;
        byte= msg[3..].sum{|x, i| 2**i*x};
}, '/data');{
        Synth(\redFSKdecode, [\in, 20]); //bus 20
        "hello supercollider!".do{|x|
                var byte= x.ascii.clip(0, 255);
                var data= byte.asBinaryDigits.reverse;
                        //("sending :"+x+byte+data).postln;
                        Synth(\redFSKencode, [\minFreq, lo, \maxFreq, hi, \data, data, \out, 20, \invBaudrate, 1/baudrate]); //bus 20
                        Synth(\redFSKencode, [\minFreq, lo, \maxFreq, hi, \data, data, \out, 0, \invBaudrate, 1/baudrate]); //monitor

this fsk technique was used in many projects to control leds. e.g. most of the 3rd gen projects listed here are running supercollider on an ipod touch with the 6 channels led brightness data being sent via fsk on one of the audio output channels.


clean-up: #53

below some code for reading and parsing data from the android app AndroSensor.
with this free app you can record a lot of different sensors in your android phone (like gps, accel, mic, battery, light etc etc) and save it to a comma separated file (csv). later one can copy over the data file from the android phone to a computer and read it in supercollider. the csv files are normally stored on the sd card in a folder called AndroSensor.

by default the app only save and display data at slow update rates, so go into the AndroSensor's settings and change update interval to very fast and recording interval to for example 0.05 seconds (the fastest).


//--read the data - edit the path to your csv file
var path= "~/Desktop/Sensor_record_20141018_115750_AndroSensor.csv"; //edit here
var data=, delimiter:$;, startRow:1);
var data2= data.flop;
var dict= ();{|x|
        var key= x[0];
        var val= x.copyRange(1, x.size-1);
        while({ or:{key.last==$:}}, {
                key= key.copyRange(0, key.size-2);
        if(val[0].any{|x| #[$:, $/].includes(x)}.not, {
                val= val.asFloat; //make single numbers into floats
        //}, { //else do nothing - keep date and satellites as strings
        dict.put(key.asSymbol, val);
~dict= dict; //handle

//--list all stored keys...{|x| x.postln}

//--access data stored at keys...
~dict['ACCELEROMETER X (m/s²)']
~dict['ACCELEROMETER Y (m/s²)']
~dict['ACCELEROMETER Z (m/s²)']
~dict['GRAVITY X (m/s²)']
~dict['GRAVITY Y (m/s²)']
~dict['GRAVITY Z (m/s²)']
~dict['GYROSCOPE X (rad/s)']
~dict['GYROSCOPE Y (rad/s)']
~dict['GYROSCOPE Z (rad/s)']
~dict['LIGHT (lux)']
~dict['MAGNETIC FIELD X (μT)']
~dict['MAGNETIC FIELD Y (μT)']
~dict['MAGNETIC FIELD Z (μT)']
~dict['ORIENTATION X (°)']
~dict['ORIENTATION Y (°)']
~dict['ORIENTATION Z (°)']
~dict['PROXIMITY (i)']
~dict['SOUND LEVEL (dB)']
~dict['LOCATION Latitude']
~dict['LOCATION Longitude']
~dict['LOCATION Altitude ( m)']
~dict['LOCATION Speed ( Kmh)']
~dict['LOCATION Accuracy ( m)']
~dict['Satellites in range'] //note as strings
~dict['Temperature (F)']
~dict['Level (%)']
~dict['Voltage (Volt)']
~dict['Time since start in ms'] //timestamps in milliseconds
~dict['YYYY-MO-DD HH-MI-SS_SSS'] //absolute timestamps - note as strings

//--plot the xyz accelerometer data...
        ~dict['ACCELEROMETER X (m/s²)'],
        ~dict['ACCELEROMETER Y (m/s²)'],
        ~dict['ACCELEROMETER Z (m/s²)']

//--plot the xyz orientation data...
        ~dict['ORIENTATION X (°)'],
        ~dict['ORIENTATION Y (°)'],
        ~dict['ORIENTATION Z (°)']

//--plot a lot of data (but not all)...
        'ACCELEROMETER X (m/s²)',
        'ACCELEROMETER Y (m/s²)',
        'ACCELEROMETER Z (m/s²)',
        'GRAVITY X (m/s²)',
        'GRAVITY Y (m/s²)',
        'GRAVITY Z (m/s²)',
        'GYROSCOPE X (rad/s)',
        'GYROSCOPE Y (rad/s)',
        'GYROSCOPE Z (rad/s)',
        'LIGHT (lux)',
        'MAGNETIC FIELD X (μT)',
        'MAGNETIC FIELD Y (μT)',
        'MAGNETIC FIELD Z (μT)',
        'ORIENTATION X (°)',
        'ORIENTATION Y (°)',
        'ORIENTATION Z (°)',
        'SOUND LEVEL (dB)'
].collect{|x| ~dict[x]}.plot;

//--sound example mapping accelerometer to simple sound
        var a= {|freq= 500, amp= 0, pan= 0|, 0.2, amp.lag(0.25)), pan.lag(0.25))}.play;
                var prevTime= 0;
                ~dict['Time since start in ms'].do{|x, i|
                                \freq, ~dict['ACCELEROMETER Y (m/s²)'][i].linexp(-20, 20, 100, 1000),
                                \amp, ~dict['ACCELEROMETER Z (m/s²)'][i].linlin(-20, 20, 0, 1),
                                \pan, ~dict['ACCELEROMETER X (m/s²)'][i].linlin(-20, 20, -1, 1)
                        prevTime= x;

attached is a demo csv file of me first keeping the phone still, and then shaking it a bit + turning it around. the data recording is only about eight seconds long.

if you plot for example the 3d accelerometer data it will look like this...

keystroke recorder

clean-up: #51

today a very simple piece of code that takes whatever you're typing in a document and posts it back 2 seconds later. see it as a demonstration. the list is treated as a fifo buffer.
note: only works in supercollider versions with Document support (not 3.6, but 3.4, 3.5, 3.7)

var delay= 2; //post 2 seconds later
var l=;

//--record in list l
Document.current.keyDownAction= {|doc, key|
                (key: key, time: Main.elapsedTime)

//--playback of list l
                var now= Main.elapsedTime;
                if(l.size>0 and:{now-delay>l.last.time}, {


clean-up: #50

found more old supercollider code laying around... this little lambdoma experiment was based on this Cymatic Music video.

//--8x8 lambdoma matrix
a= [
        1/1, 2/1, 3/1, 4/1, 5/1, 6/1, 7/1, 8/1,
        1/2, 2/2, 3/2, 4/2, 5/2, 6/2, 7/2, 8/2,
        1/3, 2/3, 3/3, 4/3, 5/3, 6/3, 7/3, 8/3,
        1/4, 2/4, 3/4, 4/4, 5/4, 6/4, 7/4, 8/4,
        1/5, 2/5, 3/5, 4/5, 5/5, 6/5, 7/5, 8/5,
        1/6, 2/6, 3/6, 4/6, 5/6, 6/6, 7/6, 8/6,
        1/7, 2/7, 3/7, 4/7, 5/7, 6/7, 7/7, 8/7,
        1/8, 2/8, 3/8, 4/8, 5/8, 6/8, 7/8, 8/8

a= {|x| {|y| y+1/(x+1)}.dup(8)}.dup(8).flat;

Pbind(\dur, 0.25, \freq, 60.midicps*Pseq(,1..7)))).play
Pbind(\dur, 0.25, \freq, 60.midicps*Pseq(,8..63)))).play
Pbind(\dur, 0.25, \freq, 60.midicps*Pseq(,9..15)))).play
Pbind(\dur, 0.25, \freq, 60.midicps*Pseq(,33..39)))).play
Pbind(\dur, 0.25, \freq, 60.midicps*Pseq(,49..55)))).play
Pbind(\dur, 0.25, \freq, 60.midicps*Pseq(,57..63)))).play

Pbind(\dur, 0.125, \freq, 60.midicps*Pseq(a)).play

//--16x16 lambdoma matrix
b= {|x| {|y| y+1/(x+1)}.dup(16)}.dup(16).flat;
Pbind(\dur, 0.125, \freq, 60.midicps*Pseq(b)).play


Subscribe to f0blog RSS