« 1 2 3 4 5 6 7 »

Under the Hood Changes 3

2020-07-01 22:10 other

Big remake of this blog as well as my homepage. I decided to rewrite everything using the wonderful Zola engine www.getzola.org - a static site generator.

On the surface it all should look and function about the same as before. I worked hard to keep backward compatibility and to make sure URLs stay the same. I used aliases and a few .htaccess RewriteRules.

Up until now, my site was a mixture of plain HTML and Drupal (the monster). It took quite some time to change everything over, but I think it was worth the trouble.

Also now my homepage finally should look at least decent on mobile.

Serial Match

2020-04-06 17:37 supercollider

In MaxMSP there is a great object called [match]. It is especially useful for parsing data from a serial port. In SuperCollider there is nothing like that build-in, but one can do the same with the class extension below.

Bascially this method will match an incoming array of specific bytes where nil means match any value. For example port.match([1, 2, nil, 3]); will return a match for 1, 2, 55, 3 but not for 1, 4, 55, 10.

Save the following in a file called extSerial.sc, move the file to your SuperCollider extensions folder and recompile...

+SerialPort {
  match {|arr|
    var byte, index= 0;
    var res= arr.copy;
    while({index<arr.size}, {
      byte= this.read;
      if(arr[index].isNil or:{byte==arr[index]}, {
        res[index]= byte;
        index= index+1;
      }, {

To test it upload the following code to an Arduino...

//Arduino test code - send six 10-bit values
#define NUM_ANALOG 6
#define UPDATERATE 20  //ms to wait between each reading

void setup() {
void loop() {
  Serial.write(200);  //header
  for (byte i = 0; i < NUM_ANALOG; i++) {
    int data = analogRead(i);
    Serial.write(data & 255); //lsb
    Serial.write(data >> 8); //msb
  Serial.write(202);  //footer

and then run this code in SuperCollider (adapt serial port name where it says EDIT)...

//SuperCollider test code - read six 10bit values
var num_analog= 6;
var port= SerialPort("/dev/cu.SLAB_USBtoUART", 57600);  //EDIT
var arr= [200, 201, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 202];  //200, 201 header, 202 footer and nil the data
r= Routine.run({
    var res;
    res= port.match(arr);
    if(res.notNil, {
        var lsb= res[i*2+2];
        var msb= res[i*2+3];


Buffer xFader

2020-03-29 13:42 supercollider

This SuperCollider function automates the process of making seamless loops as shown in this animation...

i.e. creating continuous loops from buffers following this recipe...

  1. select a few seconds from the end.
  2. fade out the selection.
  3. cut the selection.
  4. paste the selection at time 0.
  5. select a few seconds from the beginning (usually same length as in #1).
  6. fade in the selection.
  7. mix the two tracks.
~xfader= {|inBuffer, duration= 2, curve= -2, action|
  var frames= duration*inBuffer.sampleRate;
  if(frames>inBuffer.numFrames, {
    "xfader: crossfade duration longer than half buffer - clipped.".warn;
  frames= frames.min(inBuffer.numFrames.div(2)).asInteger;
  Buffer.alloc(inBuffer.server, inBuffer.numFrames-frames, inBuffer.numChannels, {|outBuffer|
      var interleavedFrames= frames*inBuffer.numChannels;
      var startArr= arr.copyRange(0, interleavedFrames-1);
      var endArr= arr.copyRange(arr.size-interleavedFrames, arr.size-1);
      var result= arr.copyRange(0, arr.size-1-interleavedFrames);
        var fadeIn= i.lincurve(0, interleavedFrames-1, 0, 1, curve);
        var fadeOut= i.lincurve(0, interleavedFrames-1, 1, 0, 0-curve);
        result[i]= (startArr[i]*fadeIn)+(endArr[i]*fadeOut);
      outBuffer.loadCollection(result, 0, action);


//edit and load a sound file (or use an already existing buffer)
b.free;  b= Buffer.read(s, "~/Desktop/testnoise.wav".standardizePath);

//evaluate the function to create the seamless cross fade
c= ~xfader.value(b);

//try looping it - should loop smoothly and without discontinuities
d= {PlayBuf.ar(c.numChannels, c, loop:1)}.play;

//compare with the input file - this will probably have a hickup
d= {PlayBuf.ar(b.numChannels, b, loop:1)}.play;


//--save to disk example with shorter cross fade and done action function.
b.free;  b= Buffer.read(s, "~/Desktop/testnoise.wav".standardizePath);
c= ~xfader.value(b, 0.5, -3, action:{|buf| ("done with buffer"+b).postln});

The SuperCollider code works with any Buffer containing sound files, live sampled or generated sounds. The duration argument set the cross fade length in seconds and with the curve argument one can set curvature.

Note that duration can not be longer than half the duration of the input buffer and a curve of 0.0 or greater will mean that the amplitude will dip in the middle of the crossfade. So it is recommended to bend the curve a bit to get more of an equal power crossfade. -2.0 to -4.0 seem like sensible values. The function will allocate and return a new Buffer instance and not touch the buffer passed in as an argument.

An action function is optional. The function is asynchronous. Also, note that the resulting file will be shorter than the original because of the crossfade.

I remember learning this trick from Peter Lundén ~20 years ago. He showed it to me on an SGI machine running IRIX, possibly using the Snd sound editor.

ESP Mesh Network with OSC

2020-03-23 19:32 electronics

With the painlessMesh library, it turned out easy to set up a decentralised mesh network for a few ESP8266 modules. The example below shows how I set it up and how to send and receive OpenSoundControl (OSC) messages. The code also works on an ESP32.

  1. Install the painlessMesh and the OSC libraries for Arduino.
  2. Program a few nodes (ESP8266) so that they all run this code...
//req. libraries: OSC, painlessMesh

#include <Arduino.h>
#include <painlessMesh.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCData.h>

#define MESH_NAME "networkname" //EDIT mesh name
#define MESH_PASS "networkpass" //EDIT password
#define MAX_CONN 4 //EDIT ESP32 can have more than ESP8266

#define INPORT 19998 //OSC in port
#define OUTPORT 57120 //OSC out port (SC)

IPAddress outIP;
WiFiUDP Udp;
painlessMesh mesh;

void setup() {
  mesh.init(MESH_NAME, MESH_PASS, 5555, WIFI_AP_STA, 1, 0, MAX_CONN);

void pingFunc(OSCMessage &inMsg) {
  digitalWrite(LED_BUILTIN, 1 - digitalRead(LED_BUILTIN));  //toggle led
  OSCMessage outmsg("/pong");
  outmsg.add(mesh.getNodeId()); //uint32
  IPAddress ip;
  ip= mesh.getStationIP(); // if current base station
  ip= mesh.getAPIP();
  Udp.beginPacket(outIP, OUTPORT);

void loop() {

  int packetSize= Udp.parsePacket();
  if (packetSize) {
    OSCMessage inMsg;
    while (packetSize--) {
    if (!inMsg.hasError()) {
      outIP= Udp.remoteIP();
      inMsg.dispatch("/ping", pingFunc);
  1. After that power up the nodes and a new WiFi network should show up.
  2. Connect a laptop to the new mesh network and take note of the IP number assigned.
  3. Run the test code below on the laptop. It will broadcast a \ping OSC message and listen for \pong replies.

The test code is for SuperCollider but any program that can send OSC should work.

OSCFunc({|msg| msg.postln}, \pong);
NetAddr.broadcastFlag= true;
NetAddr("", 19998).sendMsg(\ping);  //EDIT laptop ip number but leave 255 as the last number

The painlessMesh library will for sure come in handy when I need a network without a WiFi router, or for when trying to cover a larger area.

A major drawback though seems to be that the maximum number of nodes that can be used is really low. Apparently, an ESP8266 can only handle 5 (TCP/IP) connections at the same time and an ESP32 about three times that. And that is not very many.

Building SuperCollider for piCore Linux

2020-03-16 21:42 supercollider

NOTE: there is a newer and updated version available... /f0blog/building-supercollider-for-picore-linux-2/

These instructions show how to build, package and install SuperCollider, sc3-plugins and jackd for piCore - a variant of TinyCoreLinux for the Raspberry Pi.


piCore has many advantages over the common Raspbian system. It will boot a lot faster, is extremely light-weight and is easy to customise. And because the whole system always resides in RAM, SD card wear is minimal.

Its immutable-by-default design means one can unplug the power to the Raspberry Pi without performing and waiting for a proper shutdown nor risking corrupting the SD card. It also allows one to experiment without being afraid of messing up. A simple reboot will take the system back to a known state. For changes to be persistent, one must deliberately write them to the SD card (using the command filetool.sh -b).

Some drawbacks are that piCore is more advanced to install and configure and that much common Linux software is missing from the built-in package manager (one will have to compile it oneself - hence this guide).



On the laptop, open a terminal and run the following commands:

ping  #first broadcast ping (stop with ctrl+c)
arp -a  #figure out which IP address the RPi has (here
ssh tc@  #default password: piCore

sudo fdisk -u /dev/mmcblk0  #then press the following keys in order to delete and recreate partition2
 p  #note start of partition2 - StartLBA - usually 77824
 77824  #enter start of partition2 from above
 <RET>  #type return to accept suggestion
 w  #write

filetool.sh -b
sudo reboot

ssh tc@  #pass: piCore

sudo resize2fs /dev/mmcblk0p2  #resize partition2


Assuming piCore is now installed and partition2 resized like above...

#download and install build dependencies
tce-load -wil cmake compiletc python squashfs-tools libudev-dev libsndfile-dev readline-dev libsamplerate-dev fftw-dev git

#download and compile jackd
cd /tmp
git clone git://github.com/jackaudio/jack2 --depth 1
cd jack2
wget https://waf.io/waf-2.0.12
chmod +x waf-2.0.12
./waf-2.0.12 configure --alsa
./waf-2.0.12 build
sudo ./waf-2.0.12 install > /tmp/jack2_tmp.list

#create the jackd tcz extension package
cd /tmp
cat jack2_tmp.list | grep "/usr/local/" | grep -v "/share/man/\|.h \|.pc " | awk '{print $3}' > jack2.list
tar -T /tmp/jack2.list -czvf /tmp/jack2.tar.gz
mkdir /tmp/pkg && cd /tmp/pkg
tar -xf /tmp/jack2.tar.gz
cd ..
mksquashfs pkg/ jack2.tcz
sudo mv jack2.tcz ~
rm -rf /tmp/pkg
tce-load -i ~/jack2.tcz
jackd  #check that it is working

On the laptop, open another terminal window and download the resulting compressed jackd package:

scp tc@ ~/Downloads  #pass: piCore


Assuming jackd is installed like above...

#download and compile SuperCollider
cd /tmp
git clone --recurse-submodules https://github.com/supercollider/supercollider.git
cd supercollider
mkdir build && cd build
sudo make install
cat install_manifest.txt | grep -v "/usr/local/include/\|/usr/local/share/pixmaps/\|/usr/local/share/mime/" > /tmp/sc.list

#create the SuperCollider tcz extension package
cd /tmp
tar -T /tmp/sc.list -czvf /tmp/sc.tar.gz
mkdir /tmp/pkg && cd /tmp/pkg
tar -xf /tmp/sc.tar.gz
cd ..
mksquashfs pkg/ supercollider.tcz
sudo mv supercollider.tcz ~
rm -rf /tmp/pkg
tce-load -i ~/supercollider.tcz
sclang -h  #just to check that it is working

On the laptop, open another terminal window and download the resulting compressed SuperCollider package:

scp tc@ ~/Downloads  #pass: piCore


Assuming SuperCollider is installed like above...

#download and compile sc3-plugins
cd /tmp
git clone --recursive https://github.com/supercollider/sc3-plugins.git
cd sc3-plugins
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE="Release" -DSUPERNOVA=OFF -DNATIVE=ON -DSC_PATH=../../supercollider/ ..
sudo make install
cat install_manifest.txt | grep -v "/HelpSource\|.html\|/Help" > /tmp/scplugs.list

#create the sc3-plugins tcz extension package
cd /tmp
tar -T /tmp/scplugs.list -czvf /tmp/scplugs.tar.gz --exclude=*/HelpSource --exclude=*.html --exclude=*/Help
mkdir /tmp/pkg && cd /tmp/pkg
tar -xf /tmp/scplugs.tar.gz
cd ..
mksquashfs pkg/ sc3-plugins.tcz
sudo mv sc3-plugins.tcz ~
rm -rf /tmp/pkg

On the laptop, open another terminal window and download the resulting compressed sc3-plugins package:

scp tc@ ~/Downloads  #pass: piCore

Now that the three tcz packages are created and downloaded to the laptop, we can erase the SD card and start afresh. (It is possible to continue working with the same piCore install, but unused build dependencies would waste some space).

restart and install

(for future installs you can skip all of the above and start here assuming you have kept the .tcz packages)

On the laptop, open a terminal and run the following commands:

ping  #first broadcast ping (stop with ctrl+c)
arp -a  #figure out which IP address the RPi has (here
ssh-keygen -R  #resets the ssh keys to be able to log in again
ssh tc@  #default password: piCore

sudo fdisk -u /dev/mmcblk0  #then press the following keys in order to delete and recreate partition2
 p  #note start of partition2 - StartLBA - usually 77824
 77824  #enter start of partition2 from above
 <RET>  #type return to accept suggestion
 w  #write

filetool.sh -b
sudo reboot

ssh tc@  #pass: piCore

sudo resize2fs /dev/mmcblk0p2  #resize partition2

#install dependencies
tce-load -wi nano alsa alsa-utils libsamplerate libudev readline git fftw

On the laptop, open another terminal window and upload the three compressed packages:

cd ~/Downloads
scp jack2.tcz supercollider.tcz sc3-plugins.tcz tc@

Back on the Raspberry Pi...

cd ~
mv jack2.tcz supercollider.tcz sc3-plugins.tcz /mnt/mmcblk0p2/tce/optional/
echo jack2.tcz >> /mnt/mmcblk0p2/tce/onboot.lst
echo supercollider.tcz >> /mnt/mmcblk0p2/tce/onboot.lst
echo sc3-plugins.tcz >> /mnt/mmcblk0p2/tce/onboot.lst
echo -e "\nsudo /usr/local/sbin/alsactl -f /home/tc/mysound.state restore" >> /opt/bootlocal.sh

#autostart - optional
nano autostart.sh  #add the following lines
  jackd -P75 -p16 -dalsa -dhw:0 -r44100 -p1024 -n3 &
  sclang /home/tc/mycode.scd
chmod +x autostart.sh

nano mycode.scd  #add the following lines
    {SinOsc.ar([400, 404], 0, 0.5)}.play;

nano /opt/bootlocal.sh  #add the following lines to the end
  echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
  /home/tc/autostart.sh &

#IMPORTANT - make the changes permanent
filetool.sh -b

sudo reboot

The piCore system should now have SuperCollider installed and (optionally) start at boot.


To adjust the volume log in and run the following commands...

alsamixer  #set volume with arrow keys, ESC to exit
alsactl -f /home/tc/mysound.state store  #save in custom alsa settings file
filetool.sh -b  #make permanent


//TODO: USB soundcard


Motion Induced Blindness

2020-01-27 21:16 visuals

A fascinating illusion. Stare at the green dot for a bit.

More info here en.wikipedia.org/wiki/Motion-induced_blindness.

This is just a remake of the wikipedia GIF animation, but this JavaScript code and its variables/settings opens up for experimentation.

<div style="background-color:black;">
<canvas id="canvas" width="600" height="600"></canvas>
// motion-induced blindness after en.wikipedia.org/wiki/Motion-induced_blindness
(function () {
  const UPDATERATE = 1000.0 / 60.0 // 60fps
  const rotationRate = 0.1 // rps
  const blinkRate = 2.5 // Hz
  const numCrosses = 7
  const numDots = 3
  const dotRadius = 5
  const crossWidth = 0.1 // percent
  const crossWeight = 3
  const colorBackground = '#000'
  const colorCrosses = '#00F'
  const colorCenter = '#0F0'
  const colorDots = '#FF0'
  let lastTime = -1
  const can = document.getElementById('canvas') // EDIT name of canvas to draw to

  function draw () {
    const currTime = Date.now()
    if (currTime >= (lastTime + UPDATERATE)) {
      lastTime = currTime
      const bottom = can.getBoundingClientRect().bottom
      if (bottom >= 0 && bottom <= (innerHeight + can.height)) { // only draw when visible in browser
        const w2 = can.width * 0.5
        const h2 = can.height * 0.5
        const uw = can.width / 3

        const ctx = can.getContext('2d')
        ctx.fillStyle = colorBackground
        ctx.fillRect(0, 0, can.width, can.height)

        // --crosses
        ctx.translate(w2, h2)
        ctx.lineWidth = crossWeight
        ctx.strokeStyle = colorCrosses
        ctx.rotate(Date.now() / 1000 * Math.PI * 2 * rotationRate % (Math.PI * 2))
        for (let i = 0; i < numCrosses; i++) {
          const y = i * (uw * 2) / (numCrosses - 1) - uw
          for (let j = 0; j < numCrosses; j++) {
            const x = j * (uw * 2) / (numCrosses - 1) - uw
            ctx.moveTo(x - (crossWidth * uw), y)
            ctx.lineTo(x + (crossWidth * uw), y)
            ctx.moveTo(x, y - (crossWidth * uw))
            ctx.lineTo(x, y + (crossWidth * uw))

        // --center
        if ((Date.now() / 1000 * blinkRate) % 1 > 0.5) {
          ctx.fillStyle = colorCenter
          ctx.ellipse(w2, h2, dotRadius, dotRadius, 0, 0, Math.PI * 2)

        // --dots
        ctx.translate(w2, h2)
        ctx.fillStyle = colorDots
        ctx.rotate(0.5 * Math.PI)
        for (let i = 0; i < numDots; i++) {
          ctx.ellipse(can.width / 4, 0, dotRadius, dotRadius, 0, 0, Math.PI * 2)
          ctx.rotate(Math.PI * 2 / numDots)

Also attached is the same code ported to Processing and SuperCollider.


Keyboard Shortcuts

2020-01-19 14:14 other

MacBook Pro mid-2015 keyboard after some hard use of the cmd key. My habits shining through.

keyboard shortcuts

The left side shift and the three up/down/right arrow keys also have this nice transparency look. But strangely enough, the left arrow key must not have been used much.


2019-12-06 17:04 supercollider

A Human Interface Device (HID) to Open Sound Control (OSC) converter for macOS written in Python3.

The program can send OSC to any IP and port but by default, it will send to (localhost) on port 57120 (SuperCollider).

Here is a binary build... HIDtoOSC.zip (macOS, 64bit, 5.3MB)

To run it start Terminal.app and type...

cd ~/Downloads

That should list available HID devices on your system. After that, you will probably see a warning that it failed to open the default device.

So to open and start reading data from any of your own devices you will need to give the correct vendor id and product id as arguments.

usage: HIDtoOSC [-h] [-V] [--vid VID] [--pid PID] [--ip IP] [--port PORT]
                [--rate RATE] [--debug]

optional arguments:
  -h, --help     show this help message and exit
  -V, --version  show program version
  --vid VID      set HID vendor id
  --pid PID      set HID product id
  --ip IP        set OSC destination IP
  --port PORT    set OSC destination port
  --rate RATE    update rate in milliseconds
  --debug        post incoming HID data

example - USB keyboard

Here is an example that shows how to connect to an external generic USB keyboard with the vendor and product id 6700, 2.

hidtoosc terminal screenshot

NOTE: for security reasons sudo is needed when accessing keyboards but not for accessing most other devices (gamepads, joysticks, mice)

Some times you will need to run the program a few times before the HID is claimed. Stop the program with ctrl+c

In the example, the key A is pressed and released, but all HID will use their own data format. The --debug flag makes the program print out the incoming data.

receiving OSC

In SuperCollider we can receive the data as an OSC message. Test it by running the line...


Here is what the example above generates...

OSC Message Received:
  time: 75956.941459501
  address: a NetAddr(, 56073)
  recvPort: 57120
  msg: [ /hid, 6700, 2, Int8Array[ 0, 0, 4, 0, 0, 0, 0, 0 ] ]

example - USB mouse

And here is another example connecting to an optical mouse. (no sudo needed)

hidtoosc terminal screenshot 2

This mouse example sends OSC to port 9999 and the data format is slightly different than in the keyboard example above.


If you do not trust the binary above (and you should not - especially when running it with sudo) you can run the Python code directly.

Homebrew and Python3 are required.

First, install some libraries...

brew install hidapi liblo

Next, create a virtual environment...

cd ~/python3
mkdir HIDtoOSC && cd HIDtoOSC
python3 -m venv env
source env/bin/activate  #to later leave the virtual environment type: deactivate

Then install some Python libraries...

pip install Cython
pip install pyliblo
pip install hid pyusb
pip install pyinstaller

Finally copy the main Python program below, put it in a file called HIDtoOSC.py and test it with for example python HIDtoOSC.py -h (stop with ctrl+c)

#f.olofsson 2019

# required Python libraries: pyliblo, hid, pyusb
# example: sudo python HIDtoOSC.py --vid 6700 --pid 2 --port 12002 --debug
# note: macOS must be running this as root if the device is a keyboard

import sys, argparse
import usb.core, hid, liblo

version= 0.1

parser= argparse.ArgumentParser()
parser.add_argument('-V', '--version', action= 'store_true', help= 'show program version')
parser.add_argument('--vid', type= int, dest= 'vid', help= 'set HID vendor id')
parser.add_argument('--pid', type= int, dest= 'pid', help= 'set HID product id')
parser.add_argument('--ip', dest= 'ip', help= 'set OSC destination IP')
parser.add_argument('--port', type= int, dest= 'port', help= 'set OSC destination port')
parser.add_argument('--rate', type= int, dest= 'rate', help= 'update rate in milliseconds')
parser.add_argument('--debug', dest= 'debug', action= 'store_true', help= 'post incoming HID data')

parser.set_defaults(vid= 1452)
parser.set_defaults(pid= 517)
parser.set_defaults(ip= '')
parser.set_defaults(port= 57120)
parser.set_defaults(rate= 100)
parser.set_defaults(debug= False)
args= parser.parse_args()

if args.version:
  sys.exit('HIDtoOSC version %s'%version)

print('Available HID devices:')
for d in hid.enumerate():
  print('  device: %s, %s'%(d['manufacturer_string'], d['product_string']))
  print('  vid: %s, pid: %s'%(d['vendor_id'], d['product_id']))

def main():
  dev= usb.core.find(idVendor= args.vid, idProduct= args.pid)

  if dev is None:
    sys.exit('Could not find device %s, %s'%(args.vid, args.pid))

  for config in dev:
    for i in range(config.bNumInterfaces):
      if dev.is_kernel_driver_active(i):

  except usb.core.USBError as e:
    sys.exit('Could not set configuration: %s'%str(e))

  endpoint= dev[0][(0, 0)][0]
    dev= hid.Device(args.vid, args.pid)
    print('Device %s, %s open'%(args.vid, args.pid))
    sys.exit('Could not open device %s, %s'%(args.vid, args.pid))

  target= (args.ip, args.port)
  noerror= True
  while noerror:
      data= dev.read(endpoint.wMaxPacketSize, args.rate)
      noerror= False
        print('Closed the hid device')

    if data:
      if args.debug:
        print([x for x in data])
        msg= liblo.Message('/hid', args.vid, args.pid, ('b', data))
        liblo.send(target, msg)
        print('WARNING: Could not send OSC to %s'%(target, ))

if __name__=='__main__':

Later you can build your own binary with the command...

pyinstaller HIDtoOSC.py --onefile --hidden-import=libhidapi --add-binary='/usr/local/lib/libhidapi.dylib:.'

« 1 2 3 4 5 6 7 »