forked from lanky/ukebook-md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chordgen.py
executable file
·277 lines (223 loc) · 8.83 KB
/
chordgen.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import re
import yaml
import codecs
from jinja2 import Environment, FileSystemLoader
import argparse
from progress.bar import Bar
# two-way mapping of equivalent non-naturals, to allow a chord to be
# defined in more than one way (possibly to reduce duplication)
alt_names = {
'A#': 'Bb',
'Bb': 'A#',
'C#': 'Db',
'Db': 'C#',
'D#': 'Eb',
'Eb': 'D#',
'F#': 'Gb',
'Gb': 'F#',
'G#': 'Ab',
'Ab': 'G#',
}
def parse_cmdline(argv):
"""
process commandline options and arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument("chord", nargs="*", help="chord names (from configuration) to render")
parser.add_argument("-c", "--chordlist", default="chords.yml", help="chord configuration file (YAML)")
parser.add_argument("-t", "--template", default="external_chord.svg.j2", help="chord template (jinja2( - used for rendering chords as SVG")
parser.add_argument("-d", "--destdir", default="chords", help="output directory for chord (SVG) files")
bgrp = parser.add_argument_group("Fretboard Layout", "Customise frets, strings and spacing")
bgrp.add_argument("-s", "--strings", help="Number of strings to draw", type=int, default=4)
bgrp.add_argument("-f", "--frets", help="number of frets to draw (not including nut)", type=int, default=5)
bgrp.add_argument("-w", "--spacing", help="spacing (fret/string spacing)", type=float, default=18.0)
opts = parser.parse_args()
if not os.path.isfile(opts.chordlist):
raise argparse.ArgumentError("no such file or directory: {}".format(opts.chordlist))
return opts
def symbolise(name):
"""
replace pretend symbols with real ones (unicode ftw)
"""
translations = {
'b': '♭',
'#': '♯',
}
tt = { ord(k): v for k, v in list(translations.items()) }
return str(name).translate(tt)
def safe_name(chord):
"""
Translate unsafe characters for filenames (on Linux, at least. May need more for windows)
"""
transtable = {
'#': '_sharp_',
'/': '_on_',
}
return chord.translate({ ord(k): v for k, v in transtable.items() })
def merge_ctx(base, **kwargs):
"""
create a merged context for passing into jinja template
Args:
base(dict): basic layout information, fretboard info, diag size etc
Kwargs:
name: un-translated name of chord, special symbols and all
"""
context = {}
# calculate context settings.
# need to get fret and string spacing and positions.
# There are only 3 gaps for 4 strings (etc)
sgap = ( base['fboard']['right'] - base['fboard']['left'] ) / float( base['fboard']['strings'] - 1 )
# there will be x+1 fret lines, though, so we need to recognise that
fgap = ( base['fboard']['bottom'] - base['fboard']['top'] ) / float(base['fboard']['frets'])
# we only need x positions for strings.
strings = [ base['fboard']['left'] + (idx * sgap)
for idx in range(base['fboard']['strings']) ]
# frets require an extra addition for the 'nut', or 'fret 0'
frets = [ base['fboard']['top'] + ( idx * fgap )
for idx in range(base['fboard']['frets']) ]
# add the baseline
frets.append(base['fboard']['bottom'])
context.update(base)
context['frets'] = frets
context['strings'] = strings
# radius for rounding rectangles and sizing finger positions
# makes circles half a fret gap
context['radius'] = fgap / 4
# centre for positioning halfway between frets
context['centre'] = fgap / 2
# caculate mid-fret positions to simplify marker stuff
bpos = kwargs.get('barre', 0)
if bpos != 0:
# barre is provided as a fret number
# we need to calculate offsets
# barres are wider than the board by radius * 2
# and offset from position by radius in both x and y axes
context['barre'] = {
'position': kwargs['barre'],
'left': base['fboard']['left'] - context['radius'],
'top': (frets[bpos] - context['centre']) - context['radius'],
'width': base['fboard']['right'] - base['fboard']['left'] +
(context['radius'] * 2),
'height': context['radius'] * 2,
}
context['fingers'] = []
for fpos in kwargs.get('fingers', []):
if fpos == 0:
context['fingers'].append(fpos)
else:
# we have to be relative to the barre if there is one
context['fingers'].append(frets[fpos + bpos] - (fgap / 2))
context['name'] = kwargs.get('name')
return context
def gen_board(spacing, strings=4, frets=5):
"""
An attempt to generate an entire chord diagram based on spacing of
strings and frets.
Predicated on
1. width: (strings +1 ) * spacing
2. height: (frets + 3) * spacing
3. origin is x: spacing, y: 2* spacing
4. margins are 1* spacing, except top: 2* spacing
This should centre the fretboard horizontally
"""
# margin calculations (used for coords)
ctx = {}
# top of fretboard
tm = 2 * spacing
# left of board
lm = spacing
# right of board ( end of frets)
rm = strings * spacing
# bottom of board (end of strings)
bm = tm + ( frets * spacing )
ctx['width'] = rm + spacing
ctx['height'] = bm + spacing
ctx['top'] = tm
ctx['bottom'] = bm
ctx['left'] = lm
ctx['right'] = rm
# calculate coords for each string (vertical lines)
# each one has x1, y1, x2, y2 elements, 2 of which are static.
ctx['strings'] = [ lm + (spacing * i) for i in range(strings) ]
# each string will have coords x1=s, x2=s, y1=top, y2=bottom
# mark the nut differently, it'll have a thicker line
# fret 0: [ y1=f, y2=f, x1=left, x2=right]
ctx['nut'] = tm
ctx['frets'] = [ tm + (spacing * i) for i in range(frets + 1) ]
return ctx
def get_alt_name(chord):
res = re.match(r'^([ABCDEFG][b#]?)(.*)', chord)
if res is not None:
root, voicing = res.groups()
altroot = alt_names.get(root)
if altroot is not None:
altname = "{}{}".format(altroot, voicing)
else:
return chord
return altname
return chord
def generate(chordlist, definitions, destdir="chords", template="external_chord.svg.j2"):
"""
Generate chord diagrams based on a definitions file
Args:
chordlist(list [str]): list of chord names to generate
definitions(dict): dictionary describing chords (fret positions etc)
Kwargs:
destdir(str): output directory for chord diagrams
"""
if not os.path.isdir(destdir):
try:
os.makedirs(destdir)
except (IOError, OSError) as E:
print("Cannot create output directory {0.filename} (0.strerror}".format(E))
destdir = os.path.realpath(os.curdir)
cfg = {}
try:
with codecs.open('fretboard.yml', mode="r", encoding="utf-8") as cfile:
cfg.update(yaml.safe_load(cfile))
except:
raise
env = Environment(loader=FileSystemLoader('templates'))
tpl = env.get_template(template)
missing = set([])
print ("progress")
pbar = Bar("{:20}".format("Rendering Chords:"), max=len(chordlist))
try:
for chordname in pbar.iter(chordlist):
if chordname in definitions:
ch = definitions.get(chordname)
else:
altname = get_alt_name(chordname)
ch = definitions.get(chordname)
if ch is None:
missing.add(chordname)
continue
if 'name' not in ch:
ch['name'] = symbolise(chordname)
# replaces characters that cause shell problems
chordfile = safe_name(chordname)
with codecs.open("{}/{}.svg".format(destdir, chordfile), mode='w', encoding="utf-8") as output:
output.write(tpl.render(merge_ctx(cfg, **ch)))
except:
print("Failed to render {}".format(chordname))
raise
return missing
if __name__ == "__main__":
# we need to load a config for our chord diagram
opts = parse_cmdline(sys.argv[1:])
# load out chord definitions to pass into the templates
if not os.path.isdir(opts.destdir):
os.makedirs(opts.destdir)
chorddefs = yaml.load(codecs.open(opts.chordlist, mode="r", encoding="utf-8"))
if not opts.chord:
opts.chord = chorddefs.keys()
print ("generating chords")
# generate diagrams for the list of provided chords
# report on any chords that were missing definitions
missing = generate(opts.chord, chorddefs, destdir=opts.destdir, template=opts.template)
if len(missing):
print("Could not find definition for chords: \n", "\n".join(missing) )