diff options
Diffstat (limited to 'generate_chord_tones.py')
-rw-r--r-- | generate_chord_tones.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/generate_chord_tones.py b/generate_chord_tones.py new file mode 100644 index 0000000..dbc990e --- /dev/null +++ b/generate_chord_tones.py @@ -0,0 +1,57 @@ +from pychord import Chord +from constants import notes, strings, enharmonic_map, html_string_ids + + +class ChordToneGenerator: + def __init__(self, chords): + self.chords = chords + self.chord_map = {} + self.note_to_fret_map = {} + self.prepped_chord_map = {} + self.all_frets = [] + self.counts = None + + def main(self): + self.generate_chord_map() + self.prepare_chord_map() + self.prepare_counts() + + def generate_chord_map(self): + for chord in self.chords: + if chord not in self.chord_map: + self.chord_map[chord] = {} + c = Chord(chord) + for note in c.components(): + self.chord_map[chord][note] = self.determine_frets(note) + + def determine_frets(self, note): + if note not in self.note_to_fret_map: + frets = [] + for string in strings: + current_string = (notes[notes.index(string):] + notes[:notes.index(string)]) * 2 + if note in enharmonic_map: + note = enharmonic_map[note] + frets.append([i for i, x in enumerate(current_string) if x == note and i <= 12]) + + self.note_to_fret_map[note] = frets + return self.note_to_fret_map[note] + + def prepare_chord_map(self): + for chord, data in self.chord_map.items(): + self.prepped_chord_map[chord] = [] + for note, strings in data.items(): + for idx, string in enumerate(strings): + for fret in string: + html_fret = f'{html_string_ids[idx]}{fret}' + self.prepped_chord_map[chord].append(html_fret) + self.all_frets.append(html_fret) + + def prepare_counts(self): + self.counts = {x:self.all_frets.count(x) for x in self.all_frets} + + +if __name__ == '__main__': + t = ChordToneGenerator(["Ab", "Dsus2"]) + t.main() + + |