-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
71 lines (52 loc) · 1.82 KB
/
utils.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
#!/usr/bin/env python
# coding=utf-8
import collections
import importlib
import re
INSTRUMENT_CHOICES = dict(
uke='ukulele',
guitar='guitar',
guitardd='guitar (drop d)',
mando='mandolin')
FLATS_TO_SHARPS = {
'cb': 'b',
'db': 'c#',
'eb': 'd#',
'fb': 'e',
'gb': 'f#',
'ab': 'g#',
'bb': 'a#'
}
def diffs(items): # -1 for X
items = [i for i in items if i > -1]
return [j-i for i, j in zip(items[:-1], items[1:])]
def shape_to_diff_id(pattern):
return ''.join(map(str, diffs(pattern)))
def build_diff_dict(chords):
diff = collections.defaultdict(list)
for chord, pattern in chords:
diff[shape_to_diff_id(pattern)].append((chord, pattern))
return diff
def with_same_pattern(pattern, by_diff):
ret = list(filter(lambda x: x[1] != pattern, by_diff.get(shape_to_diff_id(pattern))))
return ret or []
def render(pattern, strings, padd=0):
DIM, DIM_RESET = '\033[2m', '\033[22m'
BAR = '|-%s-'
min_, max_ = min(pattern), max(pattern)+1
bars = range(min_-1 if min_ > 1 else 1, max_+1)
print(' ' * padd + ' ' * 3, ' '.join([str(i).ljust(3, ' ') for i in bars]))
for string, note in zip(reversed(strings), reversed(pattern)):
muted = note < 0
line = [BAR % 'O' if note == i else BAR % '-' for i in bars]
line = ''.join(line)
line = ('X' if muted else '|') + line[1:]
print(' ' * padd + '%s%s %s|%s' % (('', DIM)[muted], string,
''.join(line), ('', DIM_RESET)[muted]))
def get_instrument(instrument):
instrument = importlib.import_module(instrument)
return instrument.STRINGS, instrument.CHORDS
def normalize_chord(chord):
def _replace(x):
return FLATS_TO_SHARPS.get(x.group(0))
return re.sub(r'[a-g]b', _replace, chord, flags=re.IGNORECASE)