clean-up: #56

compared to generating a serial bitstream in audio, analysing 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.