|
I've worked on music theory coding for a while. I originally used a dict-lookup style like you have done, but found a simpler way (for me). The problem with that approach is you have to maintain values for enharmonics of note names. It's hardwired. Also what if you gave it something like ♭♭♭♭♭♭44? Why shouldn't it "theoretically" be able to handle that. It is "theory" after all. I use something like this to convert things basically to an int (if we want to for some other function): # Assuming note values are of Jazz style.. i.e, '1', 'b3', '#5', or '♭3' with unicode-sub after
jazzAllFlats = ['1','b2','2','b3','3','4','b5','5','b6','6','b7','7']
sharpStrs = ['#','♯']
flatStrs = ['b','♭']
accidentalStrs = sharpStrs + flatStrs
def stripAccidentals(note:str) -> str:
return ''.join([c for c in note if not c in accidentalStrs])
def jazzToDist(jazz:str) -> int:
dist = 0
degree = int(stripAccidentals(jazz))
while degree > 7:
dist += 12
degree -= 7
dist += jazzAllFlats.index(str(degree))
for c in jazz:
if c in sharpStrs:
dist += 1
elif c in flatStrs:
dist -= 1
#Here you could add support for double sharps and double flats if you want.. although unlikely as font support for these glyphs is horrible overall.
else:
break
return dist
print(jazzToDist('bb3')) # returns 2
print(jazzToDist('1')) # returns 0
print(jazzToDist('♭♭♭♭♭♭44')) # returns 68
print(jazzToDist('2')) # This one is strange as it's the only one where input == output
I started making stuff more like this as it just saves a lot of trouble in the long run. Once you have things made generic like these it's easier to think about going into ways that are not Jazz/Dist (which is semitone distance, or set notation), like Keys for example.. because it turns out the logic for that is really similar to what is in the jazz.The shape of the jazz system is the same shape as a change in the key of C. You would just separate the accidental part of the note's name like I did and look up let's say the index in all keys, giving you distance from C instead of what I showed there which is like distance from what is called 1 in Jazz. So yes I prefer to make helper functions like this that actually kind of "get it" about what the languages/ways like Jazz or note names are actually saying.. then you can go one to another, or different keys really easily. If interested in more of my "Way Of Change" algorithms I can share. I think your article is cool and I could comment more.. maybe if you want you could read my repo I could pm it to you. But it's long. In the meantime I have a new website using some of this type of logic. unfortunately js instead of python (where my bigger codebase resides). Google thinks this site is a security threat and I literally posted it two days ago but it's got all scales/chords etc, and other stuff. Still in prototype phase. https://edrihan.neocities.org/wayofchange%20v14.html |
https://lotushelix.bandcamp.com/
Wow, I'm very captivated by it, such high musical weirdness! Excellent stuff.