Archive for the ‘clojure’ Category

MIDI notes and enharmonic equivalence – towards unequal temperaments in Clojure


“Positiv Division, Manila Cathedral Pipe Organ” by Cealwyn on flickr

One current ‘when-I-get-spare-time-in-the-evening’ project is to explore how different keys sounded before the advent of equal temperament. Partly out of interest and partly because whenever I hear/read discussions of how keys got their distinctive characteristics (for example in answers to this question on the Musical Practise and Performance Stack Exchange) temperament is raised as an issue or explanation.

Having recently enjoyed Karsten Schmidt‘s Clojure workshop at Resonate 2014 Clojure and Overtone seem a good place to start. My first steps are with the easiest non-equal temperament to get my head around, the Pythagorean Temperament. My (albeit limited) understanding of temperaments has been helped enormously by the amazing chapters on the subject in David Benson’s book Music, a mathematical offering.

The story, as I was told it, has Pythagoras walking down a street in ancient Greece when he hears the sound coming from blacksmiths hammering iron bars. He notes that some hammerings produce harmonious sounds and some do not. On further investigation into the pleasing sounds Pythagoras discovered the frequency ratios behind the octave (a doubling of the frequency) and the perfect fifth (adding a half to the bar length, a frequency ratio of 3:2). Thus the Pythagorean Temperament is formed solely of repeated perfect fifths and octaves.

So how might that look in Clojure? This is me working through my understanding so I’m going to keep it simple. But first up I need some helper functions

(defn keep-in-octave [ratio]
  "Shift ratio by octaves until it falls between 1 and 2 inclusive"
(> ratio 2) (keep-in-octave (/ ratio 2))
(< ratio 1) (keep-in-octave (* ratio 2))
:else ratio))

(defn apply-ratio
"Vector of ratio and pitch shifted by ratio"
([ratio] (apply-ratio ratio 440.0))
([ratio pitch] [ratio (* pitch ratio)]))

With them in hand I can define the ratios that take our root pitch up through the fifths in Pythagorean tuning:

(def pythagorean-ratios-ascending-fifths
"Lazy sequence of ascending fifth ratios in Pythagorean temperament starting from root."
(iterate (fn [x] (keep-in-octave (* x (/ 3 2)))) 1))

Inverting the ratio to take us up a fifth takes us down a fifth, which when we shift up an octave is the same as going up a fourth:

(def pythagorean-ratios-descending-fifths
"Lazy sequence of descending fifth ratios in Pythagorean temperament starting from root."
(iterate (fn [x] (keep-in-octave (* x (/ 2 3)))) 1))

I can now just write out the names of the fifths and fourths we are interested in. Starting from A the first few give us the notes we need for a scale of A major: A, B, C♯, D, E, F♯, and G♯, and if we take the first twelve of each we get to common notes before needing double sharps, double flats, etc. (N.B. We’ll come back to that “etc”!)

(def fifths-ascending-from-a
[:A :E :B :F♯ :C♯ :G♯ :D♯ :A♯ :E♯ :C :G :D])

(def fifths-descending-from-a
[:A :D :G :C :F :B♭ :E♭ :A♭ :D♭ :G♭ :C♭ :F♭])

Zipping these together gives us the ascending fifths and fourths:

(zipmap fifths-ascending-from-a
(map apply-ratio pythagorean-ratios-ascending-fifths))

=>{:A [1 440.0], 
:D♯ [729/512 626.484375], 
:C [19683/16384 528.59619140625], 
:B [9/8 495.0], 
:G [59049/32768 792.894287109375], 
:D [177147/131072 594.6707153320314], 
:E [3/2 660.0], 
:C♯ [81/64 556.875], 
:G♯ [243/128 835.3125], 
:F♯ [27/16 742.5], 
:E♯ [6561/4096 704.794921875], 
:A♯ [2187/2048 469.86328125]}

(zipmap fifths-descending-from-a
(map apply-ratio pythagorean-ratios-descending-fifths))

=>{:A [1 440.0], 
:C [32/27 521.4814814814814], 
:D♭ [8192/6561 549.3796677335771], 
:F [128/81 695.3086419753087], 
:G [16/9 782.2222222222223], 
:D [4/3 586.6666666666665], 
:C♭ [65536/59049 488.33748242984626], 
:F♭ [262144/177147 651.1166432397949], 
:B♭ [256/243 463.5390946502056], 
:G♭ [32768/19683 732.5062236447696], 
:A♭ [4096/2187 824.0695016003657], 
:E♭ [1024/729 618.0521262002745]}

That’s a fun start, but it has left me with a question. Overtone seems to use MIDI notes as the underlying note ‘name’, but MIDI notes give enharmonically equivalent notes the same name. So, for example, the MIDI note 73 is C♯ and D♭ but in Pythagorean Temperament rooted on A they have different pitches. Benson refers to this by changing the familiar “circle of fifths” to the “spiral of fifths” noting that the “Pythagorean spiral never joins up”, i.e. after double sharps there will be triple sharps etc. without ever reaching enharmonic equivalence. I’ll check on the Overtone mailing list to see how important/correct my sense of underlying MIDI notes is.