«first  …4 5 6 7 8 9 10 last»

serialAudio

2014-10-19 23:31:45 electronics, supercollider

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() {
  Serial.begin(9600);
}
void loop() {
  delay(1000);
  Serial.write(128);
  Serial.write(10);
  Serial.write(20);
  Serial.write(30);
  Serial.write(40);
  Serial.write(50);
}

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 analysed the sound by hand and wrote a little program in SuperCollider that could generate similar waveforms.

s.boot;
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= Duty.ar(1/9600, 0, Dseq(data), 2);  //baudrate
    OffsetOut.ar(1, src*amp);
}).add;
)
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.

serialAudio screenshot

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. SuperCollider 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 SuperCollider and Pure Data example code.

And for a more advanced (actually using a much better technique) example see /f0blog/arduino-programming-via-soundcard/.


FSK

2014-10-18 21:36:28 supercollider

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 /f0blog/soft-modem/ 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 SuperCollider busses at a very low baud rate. For good baud rate and min/max frequency settings see code.google.com/p/arms22/source/browse/trunk/SoftModem/SoftModem.h.

(
s.latency= 0.05;
s.waitForBoot{
    SynthDef(\redFSKdecode, {|in= 20, thresh= 0.1, baudrate= 126, lo= 882, hi= 1764|
        var sig= InFeedback.ar(in, 1);
        var sigActive= Amplitude.ar(sig, 0.01, 0.01)>thresh;
        var fre= 1/Timer.ar(sig*sigActive);
        var trg= Schmidt.ar(fre, lo+(baudrate*0.5), hi-(baudrate*0.5));  //0= lo, 1= hi
        var trgLo= trg<0.5;
        var trgHi= 1-trgLo;
        var trgCarr= Sweep.ar(trgHi, 1)>(1/baudrate*16);  //when found more than 16bits high in a row
        var trgData= SetResetFF.ar(trgLo*trgCarr, trgCarr);
        var imp= Phasor.ar(trgData, baudrate/SampleRate.ir);
        var writeTrg= (imp-Delay1.ar(imp))<0+Impulse.ar(0);
        var writePos= PulseCount.ar(writeTrg, trgData);
        var writeVal= Median.ar(31, trg);
        var ok, done= 1-trgData;
        var buf= LocalBuf(13).clear;
        Demand.ar(writeTrg, 0, Dbufwr(writeVal, buf, writePos, 0));
        ok= done*(1-Demand.ar(done, 0, Dbufrd(buf, 1, 0)))*Demand.ar(done, 0, Dbufrd(buf, 11, 0));
        SendReply.ar(ok, '/data', Demand.ar(ok, 0, Dbufrd(buf, (2..9), 0)));
        DC.ar(0);
    }).add;
    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= EnvGen.ar(Env(#[1, 1, 0], [invBaudrate*(16+11), 0]), doneAction:2);
        var parity= data.sum+1;
        var freq= Duty.ar(Dseq([invBaudrate*16]++invBaudrate.dup(11)), 0, Dseq(#[1, 0]++data++[parity%2, 1]));
        var src= SinOsc.ar(freq*(maxFreq-minFreq)+minFreq, 0, amp);
        OffsetOut.ar(out, src*env);
    }).add;
};
)

//test sending a simple counter
(
var baudrate= 126;
var lo= 882;
var hi= 1764;
OSCFunc({|msg|
    var byte;
    //("received:"+msg[3..]).postln;
    byte= msg[3..].sum{|x, i| 2**i*x};
    ("received:"+byte).postln;
}, '/data');
Routine.run({
    Synth(\redFSKdecode, [\in, 20]);  //bus 20
    inf.do{|i|
        var byte= (i%256).asInteger;
        var data= byte.asBinaryDigits.reverse;
        s.bind{
            ("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
        };
        (1/baudrate*(11+16)).wait;
    };
})
)

//test a string of text
(
var baudrate= 126;
var lo= 882;
var hi= 1764;
OSCFunc({|msg|
    var byte;
    //("received:"+msg[3..]).postln;
    byte= msg[3..].sum{|x, i| 2**i*x};
    ("received:"+byte.asInteger.asAscii).postln;
}, '/data');
Routine.run({
    Synth(\redFSKdecode, [\in, 20]);  //bus 20
    "hello supercollider!".do{|x|
        var byte= x.ascii.clip(0, 255);
        var data= byte.asBinaryDigits.reverse;
        s.bind{
            //("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
        };
        (1/baudrate*(11+16)).wait;
    };
})
)

This FSK technique was used in many projects to control LEDs. e.g. most of the 3rd gen projects on RHYME 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.


AndroSensor

2014-10-18 01:14:39 supercollider

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, accelerometer, mic, battery, light 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 saves 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).

// https://play.google.com/store/apps/details?id=com.fivasim.androsensor

//--read the data - edit the path to your CSV file
(
var path= "~/Desktop/Sensor_record_20141018_115750_AndroSensor.csv";  //edit here
var data= CSVFileReader.read(path.standardizePath, delimiter:$;, startRow:1);
var data2= data.flop;
var dict= ();
data2.do{|x|
    var key= x[0];
    var val= x.copyRange(1, x.size-1);
    while({key.last==Char.space 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...
~dict.keys.do{|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
)

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

//--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
(
s.waitForBoot{
    var a= {|freq= 500, amp= 0, pan= 0| Pan2.ar(Pulse.ar(freq.lag(0.25), 0.2, amp.lag(0.25)), pan.lag(0.25))}.play;
    s.sync;
    Routine.run({
        var prevTime= 0;
        ~dict['Time since start in ms'].do{|x, i|
            a.set(
                \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)
            );
            (x-prevTime*0.001).wait;
            prevTime= x;
        };
        "done".postln;
        a.release;
    });
};
)

Attached below 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...
androsensor screenshot

Attachments:
Sensor_record_20141018_115750_AndroSensor.csv_.zip

More SC Workshop Material

2014-10-16 23:54:15 supercollider

clean-up: #52

Tested, cleaned up and uploaded some material from a SuperCollider workshop I held a year ago.

Sound material recorded directly with the built-in laptop mic.

The code is available on the sc page


Keystroke Recorder

2014-10-15 23:55:37 supercollider

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= List.new;

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

//--playback of list l
Routine({
    inf.do{
        var now= Main.elapsedTime;
        if(l.size>0 and:{now-delay>l.last.time}, {
            l.pop.postln;
        });
        0.01.wait;
    };
}).play;
)

Harmonicism

2014-10-14 21:17:35 supercollider

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

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

s.boot
Pbind(\dur, 0.25, \freq, 60.midicps*Pseq(a.at((0,1..7)))).play
Pbind(\dur, 0.25, \freq, 60.midicps*Pseq(a.at((0,8..63)))).play
Pbind(\dur, 0.25, \freq, 60.midicps*Pseq(a.at((8,9..15)))).play
Pbind(\dur, 0.25, \freq, 60.midicps*Pseq(a.at((32,33..39)))).play
Pbind(\dur, 0.25, \freq, 60.midicps*Pseq(a.at((48,49..55)))).play
Pbind(\dur, 0.25, \freq, 60.midicps*Pseq(a.at((56,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


chipwave

2014-10-12 15:50:10 supercollider

clean-up: #48

Here's an old port I did of a Pure Data patch called chipwave by Phill Phelps.

Also see the updated Pd version Dan Wilcox made: rc-patches

//after http://www.zenpho.co.uk/chipwave.shtml

s.boot
(
SynthDef(\ioscs, {|out= 0, amp= 0.5, freq= 400, width= 0.25, gate= 1, atk= 0.002, dec= 0, sus= 1, rel= 0.2, ws= #[0, 2, 2, 3, 2, 2, 2, 2, 2], ps= #[0, 12, -12, 0, 7, 0, 7, 0, 7]|
    var e= EnvGen.ar(Env.adsr(atk, dec, sus, rel), gate, amp, doneAction:2);
    var w= Duty.ar(0.025, 0, Dseq(ws));
    var p= Duty.ar(0.025, 0, Dseq(ps));
    var f= freq*p.midiratio;
    var ti= LFTri.ar(f, 0, 0.5, 0.5);
    var sq= LFPulse.ar(f, 0, width*0.5, 2, -1);
    var ns= LFNoise0.ar(f*10);
    var z= Select.ar(w, [DC.ar(0), ti, sq, ns]);
    Out.ar(out, LeakDC.ar(z*e));
}).add;
)

//arp
a= Synth(\ioscs, [\freq, 60.midicps, \amp, 0.5, \width, 0.5]).setn(\ws, #[0, 2, 2, 3, 2, 2, 2, 2, 2], \ps, #[0, 12, -12, 0, 7, 0, 7, 0, 7])
a.release
a= Pbind(\instrument, \ioscs, \midinote, Pseq([60, 60, 70, 60, 65, 63], inf), \amp, 0.5, \dur, 0.25, \width, Pseg(Pseq([0, 1], inf), 5)).play
a.stop

//kick
a= Synth(\ioscs, [\freq, 60.midicps, \amp, 1, \atk, 0.002, \dec, 0, \sus, 1, \rel, 0.3, \width, 0.5]).setn(\ws, #[0, 3, 2, 2, 2, 2, 2, 2, 2], \ps, #[0, 18, -12, 0, 0, -6, -12, -24, -48])
a.release
a= Pbind(\instrument, \ioscs, \midinote, Pseq([40, 48], inf), \amp, 0.5, \rel, 0.3, \legato, 0.3, \dur, 0.5, \width, 0.5, \ws, #[[0, 3, 2, 2, 2, 2, 2, 2, 2]], \ps, #[[0, 18, -12, 0, 0, -6, -12, -24, -48]]).play
a.stop

//snare
a= Synth(\ioscs, [\freq, 60.midicps, \amp, 1, \atk, 0.002, \dec, 0, \sus, 1, \rel, 0.3, \width, 0.5]).setn(\ws, #[0, 3, 2, 3, 3, 3, 3, 3, 3], \ps, #[0, 18, 0, 3, 12, 18, 24, 36, 36])
a.release
a= Pbind(\instrument, \ioscs, \midinote, 70, \amp, Pseq([0, 0.5, 0, 0.25], inf), \rel, 0.3, \dur, 0.5, \width, 0.5, \ws, #[[0, 3, 2, 3, 3, 3, 3, 3, 3]], \ps, #[[0, 18, 0, 3, 12, 18, 24, 36, 36]]).play
a.stop

//bass
a= Synth(\ioscs, [\freq, 60.midicps, \amp, 1, \atk, 0.002, \dec, 0, \sus, 1, \rel, 0.3, \width, 0.5]).setn(\ws, #[0, 1, 1, 1, 1, 1, 1, 1, 1], \ps, #[0, 24, 0, 12, 0, -1, 1, -1, 0])
a.release
a= Pbind(\instrument, \ioscs, \midinote, Pseq([40, 48, 48, 60], inf), \amp, 0.5, \rel, 0.3, \dur, 0.25, \width, 0.5, \ws, #[[0, 1, 1, 1, 1, 1, 1, 1, 1]], \ps, #[[0, 24, 0, 12, 0, -1, 1, -1, 0]]).play
a.stop

(
Ppar([
    Pbind(\instrument, \ioscs, \amp, 0.5, \midinote, Pseq([64, 66, 52], inf), \dur, 0.25),
    Pbind(\instrument, \ioscs, \amp, 0.5, \midinote, Pseq([60, 70], inf), \dur, 0.5),
    Pbind(\instrument, \ioscs, \amp, 0.5, \midinote, 100, \dur, 1/3)
]).play;
)

«first  …4 5 6 7 8 9 10 last»