-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmkchordchart
executable file
·140 lines (118 loc) · 6.51 KB
/
mkchordchart
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#! /usr/bin/env python3
"""Simple script to generate a chort-chartfor a given instrument, or tuning. Outputs HTML"""
import argparse
import os
import pypandoc
import chordprobook
import chordprobook.chords
import chordprobook.instruments
class charter:
""" Placeholder for functions to generate chord charts and chord definitions"""
variants = [("", "black"), ("7", "red"), ("m", "blue"), ("m7", "purple"), ("maj7", "pink"), ("6", "green"), ("sus4", "cadetblue"),("9", "darkorchid"),("add9", "darkorchid"), ("dim", "goldenrod")]
def generate_chord_defs(instrument, reach, fingers, unplayed):
""" Make up chord definitions for tuning that has been passed in"""
transposer = chordprobook.chords.transposer()
for note_index in range(0,12):
note = transposer.get_note(note_index)
for (variant, colour) in charter.variants:
chord_name = note + variant
chord = chordprobook.chords.Chord(chord_name)
chord.find_fingerings(instrument, reach=reach, fingers=fingers, unplayed=unplayed)
chord.add_to_chordchart(instrument.chart)
def page_for_instrument(chart, name):
"""Single page chart"""
transposer = chordprobook.chords.transposer()
page_text = "# %s chords\n\n" % name
page_text += "<table>"
#page_text += "<tr>"
#for variant in variants:
# page_text += "<td style='text-align:center'>%s</td>" % variant
#page_text += "</tr>\n"
for note_index in range(0,12):
#TODO: Get rid of this dodgy code
note = transposer.get_note(note_index)
page_text += "<tr>"
#page_text += "<td>%s</td>" % note
for (variant, colour) in charter.variants:
chord_name = note + variant
chord_name = chart.normalise_chord_name(chord_name)
if chord_name in chart.grids:
chord = chart.grids[chord_name]
page_text += "<td style='text-align:center;text-color: %s id='%s'>" % (colour, chord_name)
page_text += chord.voicings[0].to_md()
page_text += "<br />" + chord_name
page_text += "</td>"
else:
page_text += "<td></td>"
page_text += "</tr>\n"
output_file = "%s-chords.html" % name.replace(" ","")
pypandoc.convert(page_text, "html", format="md", outputfile=output_file)
print("Created %s" % output_file)
def book_for_instrument(chart, name):
"""All chords for a particular instrument"""
transposer = chordprobook.chords.transposer()
page_text = "# %s chords\n\n" % name
variants = [("", "black"), ("7", "red"), ("m", "blue"), ("m7", "purple"), ("maj7", "pink"), ("6", "green"), ("sus4", "cadetblue"),("9", "darkorchid"), ("dim", "goldenrod")]
for note_index in range(0,12):
note = transposer.get_note(note_index)
for (variant, colour) in charter.variants:
chord_name = note + variant
chord_name = chart.normalise_chord_name(chord_name)
if chord_name in chart.grids:
chord = chart.grids[chord_name]
#page_text += "\n\n## %s\n\n" % chord_name
for voicing in chord.voicings:
page_text += "<div style='float: left; text-align:center; color: %s ' id='%s'>" % (colour, chord_name)
page_text += voicing.to_md()
page_text += "<br />" + chord_name
page_text += "</div>"
output_file = "%s-all-chords.html" % name.replace(" ","")
#pypandoc.convert(page_text, "html", format="md", outputfile=output_file)
with open(output_file, 'w') as out:
out.write(page_text)
print("Created %s" % output_file)
def make_book():
parser = argparse.ArgumentParser()
parser.add_argument('files', type=argparse.FileType('r'), nargs="*", default=None, help='List of files')
parser.add_argument('-a', '--all', action='store_true', help='Print all chords (default is just one)')
parser.add_argument('-i', '--instrument', default=None, help='Show chord grids for the given instrument. Eg --instrument "Soprano Ukulele"')
parser.add_argument('--instruments', action='store_true', help='chord grids for the given instrument, then quit use any of the names or aliases listed under AKA')
parser.add_argument('-t', '--tuning', default="None", help='A string representing the tuning of an instrument. Eg guitar DADBAG or uke GCEA')
parser.add_argument('-r', '--reach', type=int, default=4, help='Maximum reach: largest number frets spanned by a chord')
parser.add_argument('-f', '--fingers',type=int, default=4, help='Maximum number of fingers allowed')
parser.add_argument('-u', '--unplayed',type=int, default=4, help='Maximum number of unplayed strings allowed')
parser.add_argument('-c', '--chordpro', action='store_true', help='Output chordpro format (for use with --tuning)')
args = vars(parser.parse_args())
instruments = chordprobook.instruments.Instruments()
if args["instruments"]:
instruments.describe()
exit()
out_dir = "."
os.makedirs(out_dir, exist_ok=True)
instrument = None
if args["tuning"] != None:
name = args["instrument"] if args["instrument"] else args["tuning"]
instrument = chordprobook.instruments.Instrument(data={"name": name, "tuning": args['tuning']})
charter.generate_chord_defs(instrument, args['reach'], args['fingers'], args['unplayed'])
if args["chordpro"]:
filename = "{tuning}.chordpro.txt".format(tuning=args["tuning"])
with open(filename, "w") as f:
f.write(instrument.chart.all_to_chordpro())
return
elif args["instrument"] != None:
instrument = instruments.get_instrument_by_name(args['instrument'])
name = args['instrument']
instrument.load_chord_chart()
if instrument:
chart = instrument.chart
print(chart.grids)
if chart:
if args['all']:
charter.book_for_instrument(chart, name)
else:
charter.page_for_instrument(chart, name)
else:
print("You need to supply a valid instrumment eg\n mkchordchart --instrument 'Tenor Guitar'\nTry one of these:\n")
instruments.describe()
if __name__ == "__main__":
make_book()