Writing a program converts guitar tabs to notes

1.1k views Asked by At

I am trying to learn what notes are where on the guitar, so I want to be able to type in what the string is tuned to and what fret I am playing and have the program tell me what note it is or type in the note and get back all the different places I can play it.

So far, I have a program that tells me what the note is based on the string and fret but I just wrote it the long way and it takes a lot of processing power. Based on what the user types in for what the string is tuned to, it opens a function for that string which asks what fret is being used, then based on the fret it runs one of the many elifs that I individually typed out.

For example:

elif fret == '3':
    print('That note is E')

I know there's a way to do it with not nearly as much code, but I'm really new to programming clearly and can't quite come up with the logic.

1

There are 1 answers

0
bgporter On BEST ANSWER

Build a pair of dicts that map note names to pitch numbers and back, and you can build a simple function to do this, like:

NOTES = {"C" : 0, "C#" : 1,  "D": 2, "D#" : 3, "E": 4, "F": 5, 
   "F#" : 6, "G":  7, "G#" : 8, "A": 9, "A#" : 10, "B": 11}


NAMES = dict([(v, k) for (k, v) in NOTES.items()])


def GetNote(stringNote, fretNum):
   baseNote = NOTES[stringNote]
   fretNoteNum = (baseNote + fretNum) % 12
   return NAMES[fretNoteNum]

>>> GetNote("E", 0)
'E'
>>> GetNote("E", 1)
'F'
>>> GetNote("A", 7)
'E'
>>> GetNote("G", 6)
'C#'