Modular Arithmetic and Polyrhythms

Recently I have been exploring polyrhythms. Polyrhythms have been used for thousands of years and are a core part of music in many cultures around the world. In Western music they are used less often. Even here, drummers will often play two or more different rhythms simultaneously, and a piano player might play triplets over quarter notes (commonly called 3 over 2).

In justly tuned music, the notes a major fifth apart are also played in the (exact) ratio of 3:2. Equal temperament, the system used in the West for about the past two hundred years, changes this and all other musical ratios to be “tempered” slightly. This makes them a bit out of tune, but because each half-step interval is equal, allows for easy transposition between keys. Just intonation uses only the pure ratios, thus trading the ability to change keys easily for a much better-sounding harmony. Choral music is often sung in just intonation, which is why the harmony that you might hear in a barbershop quartet or a choir sounds so much fuller. The notes in a just major chord have the pure ratios of 4:5:6, but an ET major chord is 4 : 5.039 : 5.993.

Because I am interested in the simple ratios of just intonation, I’ve been wondering how harmony might be seen as being equivalent to a very fast polyrhythm. Then I discovered bass player Adam Neely’s fascinating video exploring the hidden links between tempo, harmony, and rhythm. (Spoiler alert: for the TLDR version go to 23 1/2 minutes where he actually speeds up a 4:5:6 polyrhythm until it blends into a major chord! Well worth a listen!)

As Adam suggests in his video, you can write out the beats of a 3:2 poly on paper. I like to draw it out the following way, by marking the zero-modulo points with 1’s and OR’ing the resulting bit patterns:

A (index mod 2 == 0):    101010
B (index mod 3 == 0):    100100
                         ------
Combination (A OR B):    101110

The combination of these two beats (tap-rest-tap-rest-tap-rest, and tap-rest-rest-tap-rest-rest) has its own unique sound — tap taptaptap, tap taptaptap. For any two rhythms, the length of the combination rhythm will be the product of the two original pattern lengths. Here we are not interested in identifying which sound is playing on a beat, just the sum total — in other words, just the composite beat pattern.

Creating polyrhythms in code

Start counting the beats, with an index i starting at 0. The pattern at A is then equal to 1 if i is divisible by 2, and is 0 otherwise. In Javacript, we could write this using the remainder operator %:

var patternA = '';
for (i=0; i<size; i++) {
   patternA += (i % 2 == 0) ? '1' : '0';
}

Pattern B would be similar but using a modulus of 3. To create the combination pattern A+B, you need to take into account the length of both patterns.

function polyrhythm(a, b) {
  var pattern = '';
  for (i=0; i<a*b; i++) {
     if ((i % a == 0) || (i % b == 0)) {
        pattern += '1';
     } else {
        pattern += '0';
     }
  }
  return pattern;
}

A more complex pattern based on the relationship of notes in the barbershop 7th (harmonic 7th) chord of 4:5:6:7, which has a total length of 840 beats, starts off like this: 1000111110101011101011001100101010011000. Try playing that yourself, it does have an interesting beat to it.

Using JFugue

I generated the 4:5:6:7 pattern experiment in Java, using the JFugue music API, with the following simple program. The JFugue API uses music description (“Staccato”) Strings, and in this language the ‘S’ is a snare and ‘^’ is a hi-hat. I use the hi-hat rather than a rest on each empty beat so that the pattern is easier to follow. See the JFugue documentation for more details on Staccato.

import org.jfugue.player.Player;
import org.jfugue.rhythm.Rhythm;

public class Demo {
  public static String harmonic7th() {
    StringBuilder s = new StringBuilder();
    for (int i=0; i<4*5*6*7; i++) {
      s.append(
        (i%4==0 || i%5==0 || i%6==0 || i%7==0) ? 'S' : '^'
      );
    }
    return s.toString();
  }

  public static void main(String[] args) {
     new Player().play("T120 " + 
       new Rhythm().addLayer(harmonic7th()).getPattern());
  }
}

Listen to it here: 4567

Rhythm Implies PolyRhythm

Consider this: for any rhythm that you could imagine, there would be a polyrhythm (many, actually) that contains that sequence. Convert the pattern to a binary format, for example 1000110111, and you can always find some values that would generate the sequence somewhere in the result. Might each rhythm pattern have a polyrhythm most naturally associated with it? This could be an interesting source for songwriting material.

A Toroidal Honeycomb of 32 Close Packed Spheres

Some shapes can fill space (form a “honeycomb”) with an infinite number of copies of themselves, using only coordinate translations (no rotations). The  obvious and most commonly seen one is a lattice of cubes. Another is based on the rhombic dodecahedron, which is the polyhedron corresponding to a sphere in fcc close packing (described in my last post, and seen below). You can connect as many of these shapes as you want, and they will fill space without gaps.

Rhombic Dodecahedral Honeycomb

If a given polyhedron can fill space, that does not necessarily hold for a similarly shaped collection of spheres (and vice versa). Consider the tetrahedron. As a convex shape it cannot fill space by itself, however, a tetrahedral configuration of spheres can. This is because the shared edges and faces which would normally be infinitely thin in a polyhedral lattice will need to take up space in a close packing. After all, the “edges” in a close packing lattice are made up of spheres, not lines.

32-ball configuration

This configuration of 32 balls is made up of 8 copies of a tetrahedral ball configuration. It’s an interesting object, looking sort of like a cube but not a completely symmetric one. It also has the fcc close packing. This configuration fills space, and if you look closely you can see a Vector Equilibrium embedded in there.

The opposite faces interlock to ensure a close packing, as the following figure shows. This object is made from 8 of the 32-sphere units for a total of 256 spheres.

Another neat thing you can do with this object is to create a toroidal lattice by joining the opposite faces (you’ll have to imagine you’re doing this in another dimension, since you can’t fold the cube space onto itself in 3D). You might have experienced a similar torus world while playing a 2D or 3D video game where objects going off the screen will reappear on the opposite side. That becomes very useful when you want to run a cellular automata (CA) or another simulation in a closed universe. You can make a torus world like this of any size that you want. We’ll revisit this idea later when we explore a CA built around the fcc lattice.

This 32 sphere object, when connected as a torus, is related to the generalized quaternion group. This is also true of any larger toroidal environments made up of these.

Navigating the Lattice

Sphere packing in 3D has some very interesting symmetry. The Bravais lattice known as face centered cubic (fcc) is the one that I find to be the most fascinating of all the 3D lattices. This one was studied in great detail by R. Buckminster Fuller, who called it the isotropic vector matrix (IVM).

Most people think of 3D space as a lattice of cubes with XYZ coordinates, with each direction orthogonal to the other two. The IVM lattice is built differently. Instead, its core vectors are the three directions that you would move from a given point in order to form a tetrahedron (using the origin as one of its vertices). I usually think of these vectors as A, B, and C to distinguish them from XYZ (cartesian) coordinates.

In the image below, the black sphere is the origin, and the other vertices of the tetrahedron are colored red, green and blue (along the A, B, and C basis vectors, respectively).

IVM Basis Vectors as Tetrahedron

In the lattice, there are 12 directions you can go in order to reach a neighbor cell. You can build these 12 from the three basis vectors as follows: A, B, C, A-B, A-C, B-A, B-C, C-A, C-B, -A, -B, -C. Together with the center sphere, this collection of spheres is often called the Vector Equilibrium (VE). The outer spheres have the same relationship to each other as the vertices of a cuboctahedron. The image below shows this, but a static image doesn’t give a very good idea how they fit together.

Vector Equilibrium

Explore furtherOpen an X3D view of the VE (opens in new window).

The ordinary 3D cartesian coordinates that we use in everyday life do not work as well for navigation between the cells of this space. That’s because each IVM basis vector is a combination of cartesian coordinates. In my explorations, I’ve been using my own navigation system based on vectors A, B, and C.

I’ve been experimenting with cellular automata (CA) on the IVM, and created a mini-language for placing the spheres (somewhat like turtle graphics). In this language, I use upper case letters A, B, and C to move in a positive direction; and the lower case letters a, b, and c to move in a negative direction relative to the current position (or from the origin). Examples:

aBBBBCC: -1, 4, 2
AAAbc: 3, -1, -1

This system helps me to visualize the relative locations better. The images here were created this way (but more on that and the IVM CA some other time).

Bring A Little Aloha Back Home

Last year I released my first single, Bring A Little Aloha Back Home. The idea for this song grew out of the fond memories that I have of  living in Hawai’i for a few years, and bittersweet memories of leaving the islands behind. BALABH expresses the hope that we can evoke the Aloha spirit at any time, and bring it “back home” to wherever we happen to be.

While I was there I fell in love with the Aloha spirit that fills the Hawai’ian islands and their people. Although it was sad to leave the place that I had grown to love so much, I remember waiting for my plane (with leis around my neck, of course!) and a friend telling me that I would bring some of the Aloha spirit with me when I left. This idea stuck with me and many years later I decided to write a song that expressed the feelings I had when I thought about my time there – the joy, the sadness, and the idea that by calling upon my experiences in Hawai’i, I could actually bring the best of the islands into my life and the lives of those around me.

Aloha is a word that doesn’t translate well – some combination of hello, love, spirit, and goodbye. The lyrics explore all of these concepts, and I hope that this music brings some of the Aloha spirit into your home – wherever that may be.

“Every time you think of Hawai’i, you’ll bring a little Aloha, bring a little Aloha back home.”

Lead Vocals: Brian D. Eubanks
Backing Vocals: Brian D. Eubanks, Gokul Chalasani
Ukulele: Brian D. Eubanks
Steel Guitar: Gokul Chalasani
Ocarina: Gokul Chalasani
Effects: Gokul Chalasani
Synthesizer: Brian D. Eubanks
Mixing: Gokul Chalasani

Bring A Little Aloha Back Home  (CD Baby page)

The song is also available on Spotify, iTunes, Google Music, and other music streaming and download sites.