«first 1 2 3 4 5 6 7 last»

Building SuperCollider for piCore Linux

2020-03-16 21:42 supercollider

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

requirements

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

preparation

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

//TODO: USB soundcard

Updates:

  • 200401: simplified by using install_manifest.txt

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>
<script>
//motion-induced blindness after en.wikipedia.org/wiki/Motion-induced_blindness
(function() {
  let rotationRate= 0.1;  //rps
  let blinkRate= 2.5;  //Hz
  let numCrosses= 7;
  let numDots= 3;
  let dotRadius= 5;
  let crossWidth= 0.1;  //percent
  let crossWeight= 3;
  let colorBackground= '#000';
  let colorCrosses= '#00F';
  let colorCenter= '#0F0';
  let colorDots= '#FF0';
  let can= document.getElementById('canvas');  //EDIT name of canvas to draw to

  function draw() {
    let bottom= can.getBoundingClientRect().bottom;
    if(bottom>=0 && bottom<=(innerHeight+can.height)) {  //only draw when visible in browser
      let w2= can.width*0.5;
      let h2= can.height*0.5;
      let uw= can.width/3;

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

      //--crosses
      ctx.save();
      ctx.translate(w2, h2);
      ctx.lineWidth= crossWeight;
      ctx.strokeStyle= colorCrosses;
      ctx.rotate(Date.now()/1000*Math.PI*2*rotationRate%(Math.PI*2));
      ctx.beginPath();
      for(let i= 0; i<numCrosses; i++) {
        let y= i*(uw*2)/(numCrosses-1)-uw;
        for(let j= 0; j<numCrosses; j++) {
          let 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));
        }
      }
      ctx.stroke();
      ctx.restore();

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

      //--dots
      ctx.save();
      ctx.translate(w2, h2);
      ctx.fillStyle= colorDots;
      ctx.rotate(0.5*Math.PI);
      for (let i= 0; i<numDots; i++) {
        ctx.beginPath();
        ctx.ellipse(can.width/4, 0, dotRadius, dotRadius, 0, 0, Math.PI*2);
        ctx.fill();
        ctx.rotate(Math.PI*2/numDots);
      }
      ctx.restore();
    }
    window.requestAnimationFrame(draw);
  }
  window.requestAnimationFrame(draw);
})();
</script>
</div>

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

Attachments:
motion_induced_blindness.pde
motion_induced_blindness.scd

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.


HID to OSC

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

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)

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.

building

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

2019-04-23 17:19 supercollider

This software was 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...

featureCreep screenshot 00

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.

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

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

featureCreep screenshot 01

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.

featureCreep screenshot 02

The interface here is even more ugly - especially 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).


SC: Fixed Number of Decimals

2019-04-23 11:46 supercollider

Here's a quick function for displaying float numbers as strings in SuperCollider.

(
~fixDec= {|val, numDecimals= 2|  //float to string with fixed number of decimals
    var str= val.round(0.1**numDecimals).asString;
    var num= str.size-str.indexOf($.)-1;
    str.extend(str.size+numDecimals-num, $0);
};
)


//test examples
~fixDec.value(0.1, 3)
-> 0.100
~fixDec.value(0.12345, 3)
-> 0.123

//rounds internally.  compare:
~fixDec.value(0.191, 2)
-> 0.19
~fixDec.value(0.197, 2)
-> 0.20

//can deal with negative values
~fixDec.value(-2pi, 4)
-> -6.2832

//and integers
~fixDec.value(10000, 4)
-> 10000.0000

~fixDec.value(10000, 0)
-> 10000.

Note: It does not play well with exponential notation

~fixDec.value(1e-4, 5)
-> 0.00010
~fixDec.value(1e-5, 5)
//error

anneVideotracking4

2019-01-21 22:33 visuals

A much-improved version of my old MaxMSPJitter application anneVideotracking

anneVideotracking4 screenshot

With this version, you can use 12 zones on a web camera video input to trigger MIDI, sound files, audio input (mics) and OSC messages (send to SuperCollider for example).

The zones include filters and different types of thresholds and calibration. The data can be on/off triggers and or continuous values.

(sorry for the terrible GUI design.)

Download the macOS standalone from /code/apps/#annevideotracking.

and here's the updated SuperCollider example that demonstrates how to use the OSC data to control some sine oscillators.

//to start: select all & cmd+enter
//to stop: cmd + .
(
n= 12;
s.latency= 0.05;
s.waitForBoot{
    var dlast= 0.dup(n);
    d= {Bus.control(s, 1)}.dup(n);
    e= {Bus.control(s, 1)}.dup(n);
    OSCFunc({|m|
        var index= m[1], val= m[2], diff= (val-dlast[index]).abs;
        //m.postln;
        d[index].set(val);
        e[index].set(diff);
        dlast.put(index, val);
    }, \anneVideoTracking);
    CmdPeriod.doOnce({d.do{|x| x.free}; e.do{|x| x.free}});
    SynthDef(\annetest, {
        var src= Mix({|i| SinOsc.ar(i*100+400, 0, LagUD.kr(In.kr(e[i].index), 0.01, 0.1))}.dup(n));
        Out.ar(0, Pan2.ar(src));
    }).add;
    s.sync;
    Synth(\annetest);
};
)

MIDI Pedals

2019-01-16 14:01 electronics

Here's how I built a USB foot pedal that sends out MIDI CC messages as well as note on/off messages when crossing some threshold.

To get a rugged pedal I bought a sewing machine foot pedal/speed control. For converting to MIDI and connecting to a computer I used a Digispark module. The two other parts needed are a 3.5mm socket and a 4K7 resistor.

pedal photo 1

pedal photo 0

The code for the Digispark is really simple and I programmed it from the Arduino IDE. THRESH_HI and THRESH_LO together with the state variable implement hysteresis, and lastVal is used to filter out any repeating values.

//with Digispark (Default - 16.5MHz)
//connect 4.7K resistor between tip and 5V, tip to P2 and sleeve to GND

//usb yellow or red     5V
//usb white             data-
//usb green             data+
//usb grey or black     GND

#define USB_CFG_DEVICE_NAME     'm','i','d','i','P','e','d','a','l'
#define USB_CFG_DEVICE_NAME_LEN 9
#include <DigiMIDI.h>

#define PINLED 1      //onboard led
#define PINSENSOR A1  //foot controller sensor (P2)
#define PINGND 0      //ground
#define CTRL 7        //midi controller (cc)
#define NOTE 99       //midi note
#define VELO 64       //midi velocity
#define CHAN 9        //midi channel
#define THRESH_HI 100 //0-127
#define THRESH_LO 50  //0-127

DigiMIDIDevice midi;
int lastVal = 0;
int state = 0;

void setup() {
  pinMode(PINLED, OUTPUT);
  pinMode(PINGND, OUTPUT);
  digitalWrite(PINGND, LOW);
  midi.sendNoteOff(NOTE, VELO, CHAN);
}

void loop() {
  midi.update();
  int val = analogRead(PINSENSOR);
  val = constrain(val, 9, 900);
  val = map(val, 9, 900, 127, 0);
  if (val != lastVal) {
    midi.sendControlChange(CTRL, val, CHAN);
    if (state == 0 && val > THRESH_HI) {
      midi.sendNoteOn(NOTE, VELO, CHAN);
      digitalWrite(PINLED, HIGH);
      state = 1;
    } else {
      if (state == 1 && val < THRESH_LO) {
        midi.sendNoteOff(NOTE, VELO, CHAN);
        digitalWrite(PINLED, LOW);
        state = 0;
      }
    }
    lastVal = val;
  }
  midi.delay(100);
}

Some SuperCollider test code...

MIDIClient.init;
MIDIIn.connectAll;
MIDIdef.cc(\pedalCont, {|...args| [\pedalCont, args].postln}, 7, 8);
MIDIdef.noteOn(\pedalOn, {|...args| [\pedalOn, args].postln}, 99);
MIDIdef.noteOff(\pedalOff, {|...args| [\pedalOff, args].postln}, 99);

MIDIdef.trace;

For another project, I also made a 3D printed variant. This one is not so rugged and only acts as an on/off switch. It's based on Adafruit's USB_Foot_Switch_Controller but modified to fit a Digispark.

pedal photo 2

Arduino code for this discrete pedal (only note on/off)...

//with Digispark (Default - 16.5MHz)
//modified Adafruit's USB_Foot_Switch_Controller

#define USB_CFG_DEVICE_NAME     'm','i','d','i','P','e','d','a','l'
#define USB_CFG_DEVICE_NAME_LEN 9
#include <DigiMIDI.h>

#define PINLED 1  //onboard led
#define PINSENSOR 2 //switch
#define NOTE 89  //midi note
#define VELO 64  //midi velocity
#define CHAN 9  //midi channel

DigiMIDIDevice midi;

int state = 0;

void setup() {
  pinMode(PINSENSOR, INPUT_PULLUP);
  pinMode(PINLED, OUTPUT);
  midi.sendNoteOff(NOTE, VELO, CHAN);
}

void loop() {
  midi.update();

  if (digitalRead(PINSENSOR) == 0) {
    if (state == 0) {
      digitalWrite(PINLED, HIGH);
      midi.sendNoteOn(NOTE, VELO, CHAN);
      state = 1;
    }
  } else {
    if (state == 1) {
      digitalWrite(PINLED, LOW);
      midi.sendNoteOff(NOTE, VELO, CHAN);
      state = 0;
    }
  }
  midi.delay(100);
}

«first 1 2 3 4 5 6 7 last»