Basic Music Theory in ~200 Lines of Python
I&#x27;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&#x27;s hardwired. Also what if you gave it something like ♭♭♭♭♭♭44? Why shouldn&#x27;t it &quot;theoretically&quot; be able to handle that. It is &quot;theory&quot; after all. I use something like this to convert things basically to an int (if we want to for some other function):<p><pre><code>  # Assuming note values are of Jazz style.. i.e,  &#x27;1&#x27;, &#x27;b3&#x27;, &#x27;#5&#x27;, or &#x27;♭3&#x27; with unicode-sub after
  jazzAllFlats = [&#x27;1&#x27;,&#x27;b2&#x27;,&#x27;2&#x27;,&#x27;b3&#x27;,&#x27;3&#x27;,&#x27;4&#x27;,&#x27;b5&#x27;,&#x27;5&#x27;,&#x27;b6&#x27;,&#x27;6&#x27;,&#x27;b7&#x27;,&#x27;7&#x27;]
  sharpStrs = [&#x27;#&#x27;,&#x27;♯&#x27;]
  flatStrs = [&#x27;b&#x27;,&#x27;♭&#x27;]
  accidentalStrs = sharpStrs + flatStrs
  def stripAccidentals(note:str) -&gt; str:
      return &#x27;&#x27;.join([c for c in note if not c in accidentalStrs])
  def jazzToDist(jazz:str) -&gt; int:
      dist = 0
      degree = int(stripAccidentals(jazz))
      while degree &gt; 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(&#x27;bb3&#x27;)) # returns 2
  print(jazzToDist(&#x27;1&#x27;)) # returns 0
  print(jazzToDist(&#x27;♭♭♭♭♭♭44&#x27;)) # returns 68
  print(jazzToDist(&#x27;2&#x27;)) # This one is strange as it&#x27;s the   only one where input == output
</code></pre>
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&#x27;s easier to think about going into ways that are not Jazz&#x2F;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.<p>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&#x27;s name like I did and look up let&#x27;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.<p>So yes I prefer to make helper functions like this that actually kind of &quot;get it&quot; about what the languages&#x2F;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 &quot;Way Of Change&quot; algorithms I can share.<p>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&#x27;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).<p>Google thinks this site is a security threat and I literally posted it two days ago but it&#x27;s got all scales&#x2F;chords etc, and other stuff. Still in prototype phase. <a href="https:&#x2F;&#x2F;edrihan.neocities.org&#x2F;wayofchange v14.html" rel="nofollow">https:&#x2F;&#x2F;edrihan.neocities.org&#x2F;wayofchange v14.html</a>