## 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 " +