Building a USB MIDI controller

Figure 1: A basic 6-button footswitch USB MIDI controller. LEDs show button state.
I’m working on some pieces at the moment that require some electronics to be triggered via footpedals in performance. The problem is that I don’t have any MIDI foot pedals, and those that exist are expensive or overkill or both. All I need is a simple, passive MIDI pedal, connectable via USB that can give me a bunch of footswitch triggers (and maybe more features down the road). So, I decided that I’d build my own, and thought that if I documented the (surprisingly easy) process, others might see how easy it is to get custom hardware controllers…

There are a few simple options out there. In the first instance, an Arduino would probably cut it. I could use the serial port, or run a MIDI library to get what I need. Arduino is simple to learn, but I didn’t really need all the extras (or the board size). A Teensy is a smaller, equally affordable, and in some cases, a more versatile microcontroller that gets used a lot in USB MIDI and Music Tech related projects. And there’s a handy MIDI library already out there. So I opted for a Teensy (v3.2).

I sourced a cheap aluminium box here (a nice diecast Hammond box would have been lovely and heavier duty, but they’re expensive. This (Takachi Electric Industrial, MB-14) was 5 quid). For my current purposes, I only need 4 switches, but I got carried away and cut holes for 6 — not realising that this narrows the footprint considerably, making this a tricky thing to trigger with shoes on. It’s also a real mess of wiring inside as a result. If I’d thought about it before, I would have limited the switches to 4. As they say: measure twice, cut once. Lesson learned.

Switches are simple momentary (not-latching) switches, with some LEDs for visual confirmation. Below is a quick breadboard mock-up of the setup:
Figure 2: A breadboard mock-up of the circuit (in the finished product, I skipped the breadboard and chained the ground on each component) [made with Fritzing]
Figure 3: I took the strain off of the Teensy’s USB port by adding a panel-mount USB cable. It also allows me to use a slightly heavier-duty USB-B-type connector, as opposed to the Teensy’s micro port.
The code for the Teensy is simple given Josh Nishikawa’s MIDI controller library available here (Note, that I’m interested in MIDI controller numbers/values, not MIDI notes, Teensy can handle regular notes without a library). I’m only using the Pushbutton, assigned as MIDI control numbers (20-25) as they’re typically unassigned. This could also be serial, or MIDI note numbers rather than controller number. The code is simple: when a footswitch is pushed, fire off a MIDI control value of 127, otherwise it’s zero. When the pedal goes down, the LED pin is also lit up. In Max, all I need to read this is a [ctlin], [pak] and a [route 20 21 22 23 24 25] and I’ve got my pedal triggers into Max (see .maxpat in my git below). Easy.

In the Arduino IDE (Teensy uses the Arduino IDE, just with an additional library. There’s a guide to getting started with it here), I wanted the MIDI device to show up with it’s own name, so use the {name.c} file to set that. Set your USB Type to ‘MIDI’ in the Tools menu, and set your Board to ‘Teensy 3.2’, upload all that to the board and you’re done! A cheap, custom MIDI controller…

Improvements:

  • We’re gonna need a bigger boat. The initial plan was to include jack sockets and a stereo TRS jack so that I could plug in keyboard sustain pedals and an expression pedal to read as MIDI controllers. The code is simple, but since I didn’t leave myself enough space in the box, they won’t fit now.
  • Push-and-hold. My Max patch takes care of held buttons for flexibility, but this could easily be programmed in at the MIDI device level. Currently, if a button is held for x seconds or more, it clears a buffer.
    • Writing in at the Teensy level would let me have LED feedback too (light flashes after x seconds). I suppose I could set this anyway, but without it linking directly to the Max value (via serial?) there’s little point.

Files:

String harmonics, multiphonics and partials map {Processing}

double-bass-E1-string-harmonics-snip

There is some excellent research on performance techniques over at CelloMap.com on a variety of subjects, but the area of most interest to me lately has been string multiphonics. A criminally overlooked and beautifully fragile technique.

It seems a slightly murky area (a lot folk-lore, hearsay, speculation), but an interesting one that I hope people continue to plug away at. The two best resources I’ve found so far are Ellen Fallowfield and Thomas Resch’s aforementioned Cello Map project, and Seth Josel and Ming Tsao’s excellent new book The Techniques of Guitar Playing (2014; Bärenreiter, pp.118-24). Both map out the location of their partials in similar ways. Despite the different instruments the authors address, we are ultimately just dealing with a vibrating string and as such should not be too difficult to figure out…

double-bass-E1-string-harmonics

A recent composition of mine, yet another example of the porousness of certain borders (score .pdf, 15mb) for solo double bass uses quite a few multiphonics, and as part of the composing process I created a little Processing script which uses the Cello Map scheme. The script allows one to calculate and map out (theoretical) partials for a string of any tuning (my piece was initially going to feature some evolving scordatura but this was written out early on).

The benefit of coding this out that you can quickly generate a partial map of any tuning and as many partials as you like (though by default this is capped at 15, past then it gets tricky to find what you’re looking for). This becomes pretty helpful when searching for and identifying clustered sounding partials within multiphonics. It is worth noting however, that this is all fairly theoretical, and that real-world physics may not play as nicely on your string of choice…

You can find the script on my GitHub page. The script requires Processing, a free, open-source programming language.

My thanks to the Cello Map project for doing all the hard work on this and many other subjects.

 

Piano harmonics calculator

piano harmonics calculator

Most likely of interest to no-one, I’ve knocked up a little spreadsheet to help calculate string harmonics on a prepared piano.

If you set the partial number, it gives you the sounding pitch. If you calibrate the string length, it gives you the node location.

Download the Excel .xlsx spreadsheet

Hexahedral vertex permutations [Max]

Spent the weekend reading analyses of Xenakis’ Nomos Alpha, so I knocked up a little hexahedral permutation program instrument. Vertices are parsed out as x,y,z and can be hooked up as parametric controls based on the hexahedron’s position in 3D space! Hooray!

Xenakis uses isomorphic permutations of a cube to order elements of a set using pretty standard permutations (rotate 90º, rotate 180º, etc). His process is much more based in boolean logic and set theory, but I took the basic principal ofusing the isomorphic permutations as parametric controls, but running in real time. So you can drag the cube about, or animate it, and it spits out a bunch of numbers which you could use to control anything. (ie. bow technique, dynamic, durations, pitch, or whatever digitally!).

A Thomas DeLio article on Nomos Alpha largely ignores Xenakis’ use of hexahedral vertices and looks at the set more simply: thats worth looking at:http://www.jstor.org/discover/10.2307/843739?uid=3738032&uid=2&uid=4&sid=21102363132331

Code is here (albeit in an awkward, copy-and-paste-into-Max kinda format)

 

Beeblebrox and the Infinite Improbability Drive {Python}

Trying to deal with an issue in my first string quartet, where performers are faced with a probabilistic decision… being that musicians are not computers, I’m looking at pre-calculating these probabilities, whilst trying to find a way to keep my score system indeterminate (more on that later). Some code I knocked up today to calculate probabilities for weighted edges in a graph network.

### Weighted outcome calculator for Network #1 (String Quartet #1)
# owmtxy, feb 2013 - python, v.2.7
# Input up to 5 probability percentages, and calculate a weighted outcome

import random

elapsed = 0
duration = 0 #int(input("Enter duration: "))

while(elapsed < 840): # While the elapsed time is < 14 minutes
    print("nEnter probabilites, smallest first (0 for unused edges)...")
    # Enter each (accumulative) probability weighting (%)
    val1 = int(input("Weighting A: "))
    val2 = val1 + int(input("Weighting B: "))
    val3 = val2 + int(input("Weighting C: "))
    val4 = val3 + int(input("Weighting D: "))
    val5 = val4 + int(input("Weighting E: "))

    def makeRandom():
        randomVal = random.random() # Generate a random 'deciding' value
        randomVal = round(randomVal*100) #Scale it to a %
        return(int(randomVal))

    randomX = makeRandom()
    print(randomX)

# Compare the randomX value to the probability boundries:
    if(randomX <= val1): print("Outcome A") elif (randomX > val1) and (randomX <= val2): print("Outcome B") elif (randomX > val2) and (randomX <= val3): print("Outcome C") elif (randomX > val3) and (randomX <= val4): print("Outcome D") elif (randomX > val4) and (randomX <= val5): print("Outcome E") # Add the duration of the selected edge to the elapsed time duration = int(input("Enter duration: ")) elapsed += duration print("Elapsed time: ", elapsed/60) # Repeat until we reach > 14 minutes

Magic Squares

Sitting in on some undergraduate composition lectured this year, using it as a chance to brush up on some things I was never formally taught. This is particularly beneficial to my knowledge of acoustic contemporary composition (as opposed to the electroacoustic side of things). Yesturday I sat in on a lecture on Maxwell Davies’ use of Magic Square number patterns in Ave Maris Stella (1975; chamber ensemble). I wrote a little python script to generate any odd-order sized square (n), and a the values with modulo wrap (where modulo = n):

# MAGIC SQUARES CALCULATOR
# solves magic square for odd-ordered grid sizes
# owmtxy 17/10/2012
# for python v.2.6.6

n = int(input("Enter (odd) order size (n): "))
square = []
def calculateSquare(row,col):
val = n*((row+col-1+(n/2))%n) + ((row + (2*col)-2)%n)+1;
return val

#Create Array
print "Magic",n,"*",n,"Square Array:"
for i in range(n):
for j in range(n):
square.append(calculateSquare(i+1,j+1))

# Print array in a nice n*n grid
for k in range(n):
print square[k*n:(k*n)+n]

print "n Modulo",n,"Array:

#Create Array of modulo values
modSquare = []

for i in range(n):
for j in range(n):
modulo = (calculateSquare(i+1,j+1)%n)
modSquare.append(modulo)

# Print array in a nice n*n grid
for k in range(n):
print modSquare[k*n:(k*n)+n]

To model Maxwell Davies’ use somewhat, a regular n x n grid is drawn up, and a melody of length n is written to the top row of the grid. Each row is then transposed (using the interval of that top-line melody), the pitch of cell i comes from cell i-(n-1). In a 7×7 grid, the starting pitch of the second row (cell 8), comes from cell 2. Our magic square layout is then used to re-map these cells into a new order, giving us a magic square of pitch classes.

My plan is to use this grid in conjunction with some form of cellular automata to select or ‘activate’ cells of the magic square.

Tendency Masks & Vector-Based Amplitude Panning [Max/MSP]

Been thinking a bit about Koenig’s ‘Tendency Masks’ since reading more about them in Robert Rowe’s Machine Musicianship the other day. As a distribution method, they seem perhaps too random and arbitrary for generating or controlling pitch parameters (unless we’re talking grain pitch, or other techniques that benefit a more sporadic distribution?) To me, part of the interest in tendency masks lays in their gestural nature, and the possibility to relate this to other gestural behaviours. The patch here is a simple realisation of a tendency mask in Max, controlling some basic MIDI notes and [cycle], hooked up to Ville Pulke‘s vector-based amplitude panning object, [vbap] (just a 2D version at the moment, elevation not hooked up).

This demo is setup as a quadrophonic array using the [vbap]. In this setting, a narrower mask limits our signal to a single speaker location, whilst opening the mask up allows signal to move throughout the array more. Control of the system thereby comes from gesturally navigating narrower bands between speakers, and opening masks up to make full use of the array.

A nice little addition to the tendency mask outlined in Rowe (p.208) is the ability to control density as a separate breakpoint function, thereby changing over time (rather than even throughout). At the moment, my implementation of this is a little shaky at best (controlling the frequency of a [qmetro] object), but the result is there and gives a nice effect. Next steps with this include hooking up the [vbap]’s ‘elevation’ argument, cleaning up the mask density control, and linking it to loudspeaker spreading parameters.

Thanks to Chris Dobrain at the cycling74 forums for his advice on implementing the masks with [function].