serial match

In MaxMSP there is a great object called [match]. It is specially 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...

//f.olofsson
+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;
                        }, {
                                ^nil;
                        });
                });
                ^res;
        }
}

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() {
  Serial.begin(57600);
}
void loop() {
  Serial.write(200);  //header
  Serial.write(201);
  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
  delay(UPDATERATE);
}

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

//supercollider test code - read six 10bit values
SerialPort.listDevices;
(
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({
        inf.do{|i|
                var res;
                res= port.match(arr);
                if(res.notNil, {
                        num_analog.do{|i|
                                var lsb= res[i*2+2];
                                var msb= res[i*2+3];
                                ((msb<<8)+lsb).postln;
                        };
                });
        };
});
CmdPeriod.doOnce({port.close});
)

r.stop

buffer xfader

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|
                inBuffer.loadToFloatArray(action:{|arr|
                        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);
                        interleavedFrames.do{|i|
                                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);
                });
        });
};
)

s.boot;

//edit and load a soundfile (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;
d.release;

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

b.free;
c.free;

//--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});
c.write("~/Desktop/testnoise-looped.wav".standardizePath);
c.free;
b.free;

The SuperCollider code works with any Buffer containing soundfiles, 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 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.

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

AttachmentSize
Image icon xfader-audacity.gif947.7 KB

esp mesh network with osc

With the painlessMesh library it turned out easy to set up a decentralised mesh network for a few ESP8266 modules. The example below show how I set it up and how to send and receive OpenSoundControl (OSC) messages. The code also work 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() {
  pinMode(LED_BUILTIN, OUTPUT);
  mesh.init(MESH_NAME, MESH_PASS, 5555, WIFI_AP_STA, 1, 0, MAX_CONN);
  Udp.begin(INPORT);
}

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(); //0.0.0.0 if current base station
  outmsg.add(ip[0]);
  outmsg.add(ip[1]);
  outmsg.add(ip[2]);
  outmsg.add(ip[3]);
  ip= mesh.getAPIP();
  outmsg.add(ip[0]);
  outmsg.add(ip[1]);
  outmsg.add(ip[2]);
  outmsg.add(ip[3]);
  Udp.beginPacket(outIP, OUTPORT);
  outmsg.send(Udp);
  Udp.endPacket();
}

void loop() {
  mesh.update();

  int packetSize= Udp.parsePacket();
  if (packetSize) {
    OSCMessage inMsg;
    while (packetSize--) {
      inMsg.fill(Udp.read());
    }
    if (!inMsg.hasError()) {
      outIP= Udp.remoteIP();
      inMsg.dispatch("/ping", pingFunc);
    }
  }
}

3. After that power up the nodes and a new WiFi network should show up.

4. Connect a laptop to the new mesh network and take note of the IP number assigned.

5. 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("10.214.190.255", 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 seem 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

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 allow 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).

//--requirements

* a SD card - 2 Gb is plenty
* a Raspberry pi - here a RPi 3B v1.2
* an ethernet cable
* a router with internet connection
* a laptop - here running macOS

//--preparation

* download piCore-9.0.3.zip for armv7 (or piCore-9.0.3.zip for armv6 - see notes below)
* burn the zip file to the SD card using for example balenaEtcher
* put the SD card in the RPi and connect ethernet and 5V power

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

arp -a  #figure out which IP address the RPi has (here 192.168.1.13)
ssh tc@192.168.1.13  #pass: piCore

sudo fdisk -u /dev/mmcblk0  #then press the following keys in order to delete and recreate partition2
 p  #check start of partition2 - usually 77824 (or 195693)
 d
 2
 n
 p
 2
 77824  #enter start of partition2 from above
 <RET>  #type return to accept suggestion
 w
sudo reboot

ssh-keygen -R 192.168.1.13  #remove the ssh keys to be able to log in again
ssh tc@192.168.1.13  #pass: piCore

sudo resize2fs /dev/mmcblk0p2  #resize partition2

//--jackd

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@192.168.1.13:jack2.tcz ~/Downloads  #pass: piCore

//--supercollider

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
cmake -DCMAKE_BUILD_TYPE="Release" -DBUILD_TESTING=OFF -DSUPERNOVA=OFF -DNATIVE=ON -DSC_IDE=OFF -DNO_X11=ON -DSC_QT=OFF -DSC_ED=OFF -DSC_EL=OFF -DSC_VIM=ON -DINSTALL_HELP=OFF -DNO_AVAHI=ON -DSC_ABLETON_LINK=OFF -DCMAKE_C_FLAGS="-lncurses" -DCMAKE_CXX_FLAGS="-lncurses" ..
make
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@192.168.1.13:supercollider.tcz ~/Downloads  #pass: piCore

//--sc3-plugins

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/ ..
make
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@192.168.1.13:sc3-plugins.tcz ~/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 .tgz packages)

* burn the zip file to the SD card using for example balenaEtcher
* put the SD card in the RPi and connect ethernet and 5V power

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

arp -a  #figure out which IP address the RPi has (here 192.168.1.13)
ssh-keygen -R 192.168.1.13  #remove the ssh keys to be able to log in again
ssh tc@192.168.1.13  #pass: piCore

sudo fdisk -u /dev/mmcblk0  #then press the following keys in order to delete and recreate partition2
 p  #check start of partition2 - usually 77824 (or 195693)
 d
 2
 n
 p
 2
 77824  #enter start of partition2 from above
 <RET>  #type return to accept suggestion
 w
sudo reboot

ssh-keygen -R 192.168.1.13  #remove the ssh keys to be able to log in again
ssh tc@192.168.1.13  #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@192.168.1.13:

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
        #!/bin/sh
        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
        s.waitForBoot{
                {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.

volume

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

notes

* for RPi1 and RPi Zero you should probably get the armv6 version.
* for RPi2 and newer get the armv7 version (even though the files seem identical).
* running the make command with flag -j3 will usually just result in a out-of-memory freeze.
* avahi is not activated because libavahi-client-dev is not available for piCore - maybe later.
* waf-2.0.12 seems to be the newest version that can build jack2.
* jackd and supercollider will be running as root when autostarting.
* the start up time from applying power to supercollider is making sound is ~20 seconds.
* after the first backup (filetool.sh -b) the ssh-keygen -R will not be needed any longer.
* cpu benchmarks are more or less the same as for running sc under raspbian (see here)

//TODO: usb soundcard

update 200401: simplified by using install_manifest.txt

motion-induced blindness

A fascinating illusion. Stare at the green dot for a bit.
More info here https://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="can2" width="600" height="600"></canvas>
<script>
//motion-induced blindness after https://en.wikipedia.org/wiki/Motion-induced_blindness
var rotationRate= 0.1;  //rps
var blinkRate= 2.5;  //Hz
var numCrosses= 7;
var numDots= 3;
var dotRadius= 5;
var crossWidth= 0.1;  //percent
var crossWeight= 3;
var colorBackground= '#000';
var colorCrosses= '#00F';
var colorCenter= '#0F0';
var colorDots= '#FF0';
var can2, ctx2;
(function() {
    can2= document.getElementById('can2');
    ctx2= can2.getContext('2d');
    window.requestAnimationFrame(draw2);
})();
function draw2() {
    var w2= can2.width*0.5;
    var h2= can2.height*0.5;
    var uw= can2.width/3;

    ctx2.fillStyle= colorBackground;
    ctx2.fillRect(0, 0, can2.width, can2.height);

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

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

    //--dots
    ctx2.save();
    ctx2.translate(w2, h2);
    ctx2.fillStyle= colorDots;
    ctx2.rotate(0.5*Math.PI);
    for (var i= 0; i<numDots; i++) {
        ctx2.beginPath();
        ctx2.ellipse(can2.width/4, 0, dotRadius, dotRadius, 0, 0, Math.PI*2);
        ctx2.fill();
        ctx2.rotate(Math.PI*2/numDots);
    }
    ctx2.restore();

    window.requestAnimationFrame(draw2);
}
</script>
</div>

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

AttachmentSize
Binary Data processing1.39 KB
Binary Data supercollider1.57 KB

keyboard shortcuts

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

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.

hid to osc

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 127.0.0.1 (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
./HIDtoOSC

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 show how to connect to an external generic usb keyboard with the vendor and product id 6700, 2.

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...

OSCFunc.trace;

Here is what the example above generates...

OSC Message Received:
        time: 75956.941459501
        address: a NetAddr(127.0.0.1, 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)

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

 
building

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

homebrew and python3 is required.

First install some libraries...

brew install hidapi liblo

Next create an 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 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

#--settings
version= 0.1

#--arguments
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= '127.0.0.1')
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):
        dev.detach_kernel_driver(i)

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

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

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

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

if __name__=='__main__':
  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:.'

featureCreep

Software written for and in collaboration with violinist George Kentros. Premiered at after work - this violin must die, Fylkingen Stockholm 30 mar 2019.

The program is written in SuperCollider with additional features like iPad control and video mixer made in MaxMSPJitter.

Main interface

The main window with its eight tracks (configurable) looks like this...

and here you can set an amplitude threshold for the microphone and arm tracks to automatically detect and capture sounds.

With the waveform selection (dark grey areas) you select what part of the captured sound you want your different players to use.

There are many players - all with different behaviours...
slow, medium, repetitive, scanner, sweep, nervous, fastRhythmic, slowRhythmic, limpRhythmic, fast, drill, jump, plain, half, third, quarter, pingpong, crawl, wave, waveHalf, pattern1, pattern2, pattern3, round, sync

Player behaviours include in which direction and how fast to progress in the captured sound buffer, when to play for how long, with which envelope and how loud, how much transposition, play in which audio channel etc etc.
Some of these players are using granular synthesis techniques while others are scratching and jumping around in the sound buffer in more or less unpredictable ways. hopefully the names listed here above will give a hint of what players do to the sound.

All players are pieces of code. For example this the code for the scanner player...

SynthDef(\scanner, {|buf, amp= 1, start= 0, end= 1|
        var dur= BufDur.kr(buf);
        var pha= LFNoise2.kr(In.kr(100)+0.1)+1/2;
        var pos= pha*(end-start)+start;
        var add= LFNoise2.kr(1);
        var fre= In.kr(101).linexp(0, 1, 1.1, 18)+add*SinOsc.kr(0.1).max(0.5);
        var snd= TGrains.ar(
                2,
                Impulse.ar(fre),
                buf,
                1+LFNoise2.kr(1, 0.05),
                pos*dur,
                0.75,
                LFNoise2.kr(0.06, 0.5),
                amp.lag(0.5)
        );
        SendReply.kr(Impulse.kr(\updateRate.kr+1), '/pos', pos);
        Out.ar(\syncBus.ir(40)+\index.ir, K2A.ar(pha));
        snd= CompanderD.ar(snd, \compressorThresh.kr, 0.5, 0.5, 0.005, 0.01, EnvGate());
        Out.ar(\out.kr, snd);
}).add;

and most players will look very similar to this. But there are also a few special ones like sync which just matches the playback position of the player in the track above.

In the main window one can also set track volume, bypass any global effects and read/write soundfiles.

The computer keyboard may be used for shortcuts quickly arming, playing and selecting what each track should play.

Global control

There is also an additional control gui with global volume, effects, automation and record...

The three sliders delay, sustainer, distortion are just simple wet/dry control for global effects (that can be bypassed for each track using the bp button), while the other sliders freeze, fire, industry, chaos, air are more complex and control a bunch of effects and behaviours. For example freeze will take the sound from all players and smear it out by removing transients and pitch shifting copies of the sound up/down in octaves.

When the automation button is clicked, the program itself can start and stop tracks, change effects and vary a lot of other parameters in the code. The idea here is that the software should be able to run independently and generate interesting variations over long periods of time. Also it should never go completely silent nor go wild and overload everything.

Remote iPad interface

All the above can be viewed and controlled remotely from a tablet or a phone using a web browser. We programmed a look-alike gui patch with MaxMSPJitter and the mira externals. This patch talks to the SuperCollider interface over Open Sound Control.

The interface here is even more ugly - specially the waveform display because the sound data had to be downsampled and kept at a low resolution to reduce wifi network traffic. Also it was hard to fit everything on a single screen as all widgets had to be quite large and not too close to each other to be usable on an iPad.

Our software also includes a video mixer for camera and movie playback (not shown here).

Pages

Subscribe to f0blog RSS