-
Notifications
You must be signed in to change notification settings - Fork 3
/
thatchord.py
227 lines (159 loc) · 7.25 KB
/
thatchord.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
###############################################################################
###############################################################################
## ##
## THATCHORD BY TOM CONTI-LESLIE thatchord.py ##
## ##
## THIS FILE COLLECTS ALL SUBPROCESSES FROM OTHER FILES AND RUNS THATCHORD ##
## You can change the "request" string in this file and that will have an ##
## effect, provided input_type is set to DIRECT in settings.py. The rest ##
## of this file should not be changed. ##
## ##
## ##
## License: CC BY-SA 4.0 ##
## ##
## Contact: tom (dot) contileslie (at) gmail (dot) com ##
## ##
###############################################################################
###############################################################################
############ ENTER YOUR CHORD REQUEST HERE ############
# --------------------------------------------------------------------------- #
request = "Gadd9"
# --------------------------------------------------------------------------- #
# DO NOT CHANGE THE FOLLOWING CODE. THIS IS WHERE THE MAGIC HAPPENS.
# change current directory
import os
import platform # To differentiate between platforms
# Load other files
import interpret
import find
import rank
import output
import custom
import settings
from errors import err
# Load settings from file. All defaults here so empty input.
tcsettings, kwgrargs, kwioargs = settings.get_settings()
# First, figure out what the request is.
if tcsettings["input_type"] == "CONSOLE":
request = input("Enter request here: ")
elif tcsettings["input_type"] == "TERMINAL":
import sys
import argparse
# Terminal input allows for command line options.
parser = argparse.ArgumentParser(prog = "thatchord.py",
usage = ("python3 thatchord.py <request> "+
"[OPTIONS]"))
parser.add_argument("request", nargs = 1, type = str,
help = ("requested chord of form WX(Y)/Z:T, " +
"where W is the root note, " +
"X is the chord quality, " +
"Y is a list of alterations, " +
"Z is the bass note, " +
"and T is the desired index in the list"))
parser.add_argument("-c", "--configuration", nargs = "?", type = str,
help = (".yml file to take settings from; its settings"+
" are overriden by the options below"))
parser.add_argument("-i", "--instrument", nargs = "?", type = str,
help = "instrument preset")
parser.add_argument("-r", "--ranking", nargs = "?", type = str,
help = "ranking preset")
parser.add_argument("-f", "--format", nargs = "?", type = str,
help = "output format: text or png")
parser.add_argument("-o", "--output", nargs = "?", type = str,
help = "output method: print, splash or none")
parser.add_argument("-s", "--save", nargs = "?", type = str,
help = "save method: single, library or none")
parser.add_argument("-d", "--directory", nargs = "?", type = str,
help = "directory to save diagrams")
args = parser.parse_args()
request = args.request[0]
# populate dict with kwargs
override = {"instrument_preset" : args.instrument,
"ranking_preset" : args.ranking,
"output_format" : args.format,
"output_method" : args.output,
"save_method" : args.save,
"save_loc" : args.directory}
if args.configuration:
override["settingsfile"] = args.configuration
tcsettings, kwgrargs, kwioargs = settings.get_settings(**override)
# Special inputs here:
if request.upper() == "SETTINGS":
# Typing SETTINGS opens the settings file.
script_directory = os.path.dirname(os.path.realpath(__file__))
settings_path = os.path.join(script_directory, "settings.yml")
if platform.system() == "Linux":
os.system("xdg-open " + settings_path)
else:
os.system("open " + settings_path)
exit()
# Check whether a specific position in the list was requested. If not, 1 is
# default (best option).
listpos = 1
if ":" in request:
colon_positions = [i for i, x in enumerate(request) if x == ":"]
if len(colon_positions) > 1:
err("colons")
# if we made it here then there must be exactly one colon
try:
listpos = int(request[colon_positions[0] + 1:])
except ValueError:
err(15)
# remove the colon bit from the request
request = request[:colon_positions[0]]
# Check whether a minimum fret height was specified (fretspec).
at = 0
if "@" in request:
at_positions = [i for i, x in enumerate(request) if x == "@"]
if len(at_positions) > 1:
err("ats")
# if we made it here then there must be exactly one @
try:
at = int(request[at_positions[0] + 1:])
except ValueError:
err(22)
# remove the colon bit from the request
request = request[:at_positions[0]]
if at > tcsettings["nfrets"]:
err(23)
if request[0:6].upper() == "CUSTOM":
# custom note by note input triggered. Code in "custom.py".
chord = custom.interpret(request[6:])
# title removes CUSTOM but adds exclamation mark to indicate custom.
title = "!" + request[6:]
filename = request
else:
# Standard input. Use normal function.
chord = interpret.interpret(request)
# Title and filename of chord (for potential output) is the request string.
title = request
filename = request
# Find the chord at the requested listpos.
solution = find.find(chord,
nmute = tcsettings["nmute"],
important = tcsettings["important"],
index = listpos,
nfrets = tcsettings["nfrets"],
tuning = tcsettings["tuning"],
order = tcsettings["order"],
ranks = tcsettings["ranks"],
stringstarts = tcsettings["stringstarts"],
fretspec = at)
# figure out what the output format is
if tcsettings["output_format"] == "TEXT":
output.text(
solution,
name = filename,
title = title,
**kwgrargs,
**kwioargs
)
# TODO no options for where to put title yet
elif tcsettings["output_format"] == "PNG":
output.img(
solution,
name = filename,
title = title,
**kwgrargs,
**kwioargs
)