I come from a musical background, so I was interested in implementing music set theory into a Python script.
Basically the variables of musical notes are assigned to numbers (C = 0, C sharp = 1 etc.). However music set theory only works up to the number 11, since B = 11 and the next C would be = 0 again.
I've assigned some variables already, they look like this.
# pitch classes
Bs = C = 0
Cs = Db = 1
D = 2
Ds = Eb = 3
E = Fb = 4
F = Es = 5
Fs = Gb = 6
G = 7
Gs = Ab = 8
A = 9
As = Bb = 10
B = Cb = 11
# intervals
m2 = 1
mj2 = 2
m3 = 3
mj3 = 4
P4 = 5
T = 6
P5 = 7
m6 = 8
mj6 = 9
m7 = 10
mj7 = 11
I want to be able to add notes and intervals together, for instance B plus a perfect 5. This would normally be 11 + 7 = 18, but I want it to equal 6 (since 6 = F sharp, and B and F sharp are a perfect 5th apart).
I think I need something like this, but I have no idea how to implement it.
if answer >= 12:
answer - 12
Does anyone have any ideas? Is there a better way of going about doing this?
There's a modulo operator, %
which does exactly this (see also here):
print((11 + 7) % 12)
or more generally:
def add_wrap_overflow(x,y):
return (x+y) % 12