summaryrefslogtreecommitdiff
path: root/generate_chord_tones.py
blob: dbc990ef5ee059fe093b37110609985f43b72ea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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()