Sunday, September 7, 2008

How I relax...

So if you know me, you know I've been working far too hard the last few weeks. On Friday, I got home and though I had a pile of work to do, I knew I couldn't do anything unless I let myself relax a bit first.

A bit of backstory — one thing I started doing this summer to relax was taking up guitar lessons again, which has been a good thing. One thing I've been working on is learning series of inversions. The thing I like about this exercise is that it allows me to generate a limitless number of new chords, which means I can now help myself get un-bored when I find myself in a guitar rut. Lately, for example, I've been taking some songs I wrote that have very simple progressions (just C and G basically) and playing with different inversions and voicings.

Anyway, I realized what I really wanted to do on Friday was to write a program to generate those series of inversions for me. Here's an example of how it works...
./ 3x043x
Printing inversions of 3x043x

In that case, there are three inversions generated because it's a three note chord. If I give it a chord with four notes, it will of course generate four inversions:
tom@hydrophax:~/Projects/chord_inverter$ ./ 3x443x
Printing inversions of 3x443x

I can also make it print out the notes so I can keep track of where the root is:
$ ./ --include-notes x3201x
Printing inversions of x3201x

And of course it can move down as well as up:
$ ./ --down xxx988
Printing inversions of xxx988

Finally, it handles alternate tunings -- here's the inversions of D in drop-D tuning for example (which, by the way, illustrate that not all inversions are pleasant to finger -- check out the second chord):
$ ./ --tuning DADGBE 0xx323
Printing inversions of 0xx323

This means it will work for any fretted instrument.

The next step would be to have it name the chords, but of course that's kind of complicated. One of the things that pops out of this sort of exercise are "oh duh" moments like when I generated this series based on Am7 and found that the 2nd chord was already fixed in my muscle memory:

It took me a second until I realized something I already knew: an Am7 has all the same notes as a C6, which is what I'm used to calling 8x798x...

The next step would be to cook up a web interface and make this into a handy tool for everyone. In the mean time, the full source code follows... (by the way, if anyone knows how to attach a generic file to a post or do the equivalent of an lj-clip on blogger, please let me know!)

#!/usr/bin/env python

import types
UP = 1
DOWN = -1
notes = ['C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B']
synonyms = {'C#':'Db','D#':'Eb','F#':'Gb','G#':'Ab','A#':'Bb'}
x = None

def get_degree (note):
'''Return the numeric degree of the note named note.
return notes.index(synonyms.get(note,note))

def get_name (degree):
'''Return the name of the note from the degree'''
abs_degree = degree % 12
return notes[abs_degree]

def add_to_note (note, steps):
return get_name(get_degree(note)+steps)

def get_interval (a,b):
"""Return the interval in chromatic steps between two notes
a = get_degree(a)
b = get_degree(b)
if a > b:
b += 12
return b - a

def pad (txt, length, pad_char = '-'):
if len(txt) > length:
raise ValueError
elif len(txt) == length:
return txt
pad_with = length - len(txt)
left = pad_with / 2
right = pad_with / 2 + pad_with % 2
return pad_char*left + txt + pad_char*right

class String:

def __init__ (self, root='A'):
self.root = root
self.nroot = get_degree(self.root)

def get_note_for_fret (self, n):
return get_name(self.get_degree_for_fret(n))

def get_degree_for_fret (self, n):
return self.nroot + n

def get_next_fret_in_chord (self, start, chord, direction=UP):
for n in chord:
assert(n in notes or n in synonyms)
raise ValueError('Chord contains invalid note %s'%n)
if start is None: return start
n = start + direction
chord = [synonyms.get(note,note) for note in chord]
while self.get_note_for_fret(n) not in chord:
n += direction
return n

def get_fingering (fingering):
if type(fingering) in types.StringTypes:
fingering_ltrs = fingering[:]; fingering = []
for char in fingering_ltrs:
if char in ('x','X'):
return fingering

def fingering_to_txt (fingering):
s = ''
for f in fingering:
if f is None: s += 'x'
else: s += str(f)
s += ' '
return s[:-1]

class FretBoard:

def __init__ (self, strings='EADGBE'):
self.strings = [String(n) for n in strings]

def fingering_to_chord (self, fingering):
fingering = get_fingering(fingering)
chord = []
for fret,string in zip(fingering,self.strings):
if fret is not None:
return chord

def invert_chord (self, fingering, direction=UP, chord=None):
fingering = get_fingering(fingering)
if not chord:
chord = self.fingering_to_chord(fingering)
next_fingering = []
for fret,s in zip(fingering,self.strings):
if fret is None:
return next_fingering

def get_inversions (self, fingering, direction=UP):
chord = self.fingering_to_chord(fingering)
n_fingerings = len(set(chord))
fingerings = [get_fingering(fingering)]
for n in range(n_fingerings - 1):
fingering = self.invert_chord(fingering,
return fingerings

def print_inversion_series (self, fingering,direction=UP,include_notes=False):
fingerings = self.get_inversions(fingering,direction)
lines = []
for n,s in enumerate(self.strings):
line = '-'
for fing in fingerings:
fret = fing[n]
if fret is not None:
note = '(' + s.get_note_for_fret(int(fret)) + ')'
fret = str(fret)
note = ''; fret='x'
if include_notes:
line += pad(fret,3) + pad(note,4)
line += pad(fret,3)
for l in lines: print l

if __name__ == '__main__':

# If called from the commandline, treat our argument as a chord
# and print the series of inversions...
import optparse

parser = optparse.OptionParser(option_list=[
help='A custom tuning'),

optparse.make_option('--down',dest='direction',default=UP,action='store_const',const=DOWN,help='Print the series of inversions moving up the neck (down in pitch). Default is to move down the neck (up in pitch'),

(options,args) = parser.parse_args()

if False:
if options.tuning:
fb = FretBoard(strings=options.tuning)
fb = FretBoard()
include_notes = options.include_notes
direction = options.direction
for a in args:
print 'Printing inversions of ',a
import traceback; traceback.print_exception()


Anonymous said...

AHA! I found it.

Tom Hinkle said...

Well found.